aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRon Yorston <rmy@pobox.com>2021-08-09 14:58:37 +0100
committerRon Yorston <rmy@pobox.com>2021-08-09 14:58:37 +0100
commit56e1d04ae71a05586fa3414aabdef0de720d0720 (patch)
treee06c0b37c22dd811e48b7a9886a4de2bf04b129f
parentdd10dfda5834b0e67ebab0425d0a48fbfc939d7f (diff)
downloadbusybox-w32-56e1d04ae71a05586fa3414aabdef0de720d0720.tar.gz
busybox-w32-56e1d04ae71a05586fa3414aabdef0de720d0720.tar.bz2
busybox-w32-56e1d04ae71a05586fa3414aabdef0de720d0720.zip
win32: code shrink character class detection
Add a routine to detect the names of character classes. Use it in fnmatch(3) and regcomp(3), replacing local code in the former. Saves 216 bytes.
-rw-r--r--win32/Kbuild1
-rw-r--r--win32/fnmatch.c32
-rw-r--r--win32/match_class.c7
-rw-r--r--win32/match_class.h11
-rw-r--r--win32/regcomp.c50
5 files changed, 83 insertions, 18 deletions
diff --git a/win32/Kbuild b/win32/Kbuild
index cabd70849..6ab0dc077 100644
--- a/win32/Kbuild
+++ b/win32/Kbuild
@@ -13,6 +13,7 @@ lib-$(CONFIG_PLATFORM_MINGW32) += ioctl.o
13lib-$(CONFIG_FEATURE_PRNG_ISAAC) += isaac.o 13lib-$(CONFIG_FEATURE_PRNG_ISAAC) += isaac.o
14lib-$(CONFIG_PLATFORM_MINGW32) += mingw.o 14lib-$(CONFIG_PLATFORM_MINGW32) += mingw.o
15lib-$(CONFIG_PLATFORM_MINGW32) += process.o 15lib-$(CONFIG_PLATFORM_MINGW32) += process.o
16lib-$(CONFIG_PLATFORM_MINGW32) += match_class.o
16lib-$(CONFIG_PLATFORM_MINGW32) += mntent.o 17lib-$(CONFIG_PLATFORM_MINGW32) += mntent.o
17lib-$(CONFIG_PLATFORM_MINGW32) += net.o 18lib-$(CONFIG_PLATFORM_MINGW32) += net.o
18lib-$(CONFIG_PLATFORM_MINGW32) += poll.o 19lib-$(CONFIG_PLATFORM_MINGW32) += poll.o
diff --git a/win32/fnmatch.c b/win32/fnmatch.c
index 0e76a7ba1..7d8fde6a2 100644
--- a/win32/fnmatch.c
+++ b/win32/fnmatch.c
@@ -17,7 +17,7 @@
17 Boston, MA 02111-1307, USA. */ 17 Boston, MA 02111-1307, USA. */
18 18
19#include <platform.h> 19#include <platform.h>
20int index_in_strings(const char *strings, const char *key) FAST_FUNC; 20#include "match_class.h"
21 21
22#if HAVE_CONFIG_H 22#if HAVE_CONFIG_H
23# include <config.h> 23# include <config.h>
@@ -373,56 +373,52 @@ internal_fnmatch (const char *pattern, const char *string,
373 if (__iswctype (__btowc ((unsigned char) *n), wt)) 373 if (__iswctype (__btowc ((unsigned char) *n), wt))
374 goto matched; 374 goto matched;
375# else 375# else
376#define CHAR_CLASSES \ 376 switch (match_class(str)) {
377 "alnum\0alpha\0blank\0cntrl\0digit\0graph\0" \ 377 case CCLASS_ALNUM:
378 "lower\0print\0punct\0space\0upper\0xdigit\0"
379
380 switch (index_in_strings(CHAR_CLASSES, str)) {
381 case 0:
382 if (ISALNUM ((unsigned char) *n)) 378 if (ISALNUM ((unsigned char) *n))
383 goto matched; 379 goto matched;
384 break; 380 break;
385 case 1: 381 case CCLASS_ALPHA:
386 if (ISALPHA ((unsigned char) *n)) 382 if (ISALPHA ((unsigned char) *n))
387 goto matched; 383 goto matched;
388 break; 384 break;
389 case 2: 385 case CCLASS_BLANK:
390 if (ISBLANK ((unsigned char) *n)) 386 if (ISBLANK ((unsigned char) *n))
391 goto matched; 387 goto matched;
392 break; 388 break;
393 case 3: 389 case CCLASS_CNTRL:
394 if (ISCNTRL ((unsigned char) *n)) 390 if (ISCNTRL ((unsigned char) *n))
395 goto matched; 391 goto matched;
396 break; 392 break;
397 case 4: 393 case CCLASS_DIGIT:
398 if (ISDIGIT ((unsigned char) *n)) 394 if (ISDIGIT ((unsigned char) *n))
399 goto matched; 395 goto matched;
400 break; 396 break;
401 case 5: 397 case CCLASS_GRAPH:
402 if (ISGRAPH ((unsigned char) *n)) 398 if (ISGRAPH ((unsigned char) *n))
403 goto matched; 399 goto matched;
404 break; 400 break;
405 case 6: 401 case CCLASS_LOWER:
406 if (ISLOWER ((unsigned char) *n)) 402 if (ISLOWER ((unsigned char) *n))
407 goto matched; 403 goto matched;
408 break; 404 break;
409 case 7: 405 case CCLASS_PRINT:
410 if (ISPRINT ((unsigned char) *n)) 406 if (ISPRINT ((unsigned char) *n))
411 goto matched; 407 goto matched;
412 break; 408 break;
413 case 8: 409 case CCLASS_PUNCT:
414 if (ISPUNCT ((unsigned char) *n)) 410 if (ISPUNCT ((unsigned char) *n))
415 goto matched; 411 goto matched;
416 break; 412 break;
417 case 9: 413 case CCLASS_SPACE:
418 if (ISSPACE ((unsigned char) *n)) 414 if (ISSPACE ((unsigned char) *n))
419 goto matched; 415 goto matched;
420 break; 416 break;
421 case 10: 417 case CCLASS_UPPER:
422 if (ISUPPER ((unsigned char) *n)) 418 if (ISUPPER ((unsigned char) *n))
423 goto matched; 419 goto matched;
424 break; 420 break;
425 case 11: 421 case CCLASS_XDIGIT:
426 if (ISXDIGIT ((unsigned char) *n)) 422 if (ISXDIGIT ((unsigned char) *n))
427 goto matched; 423 goto matched;
428 break; 424 break;
diff --git a/win32/match_class.c b/win32/match_class.c
new file mode 100644
index 000000000..789e0df02
--- /dev/null
+++ b/win32/match_class.c
@@ -0,0 +1,7 @@
1#include "libbb.h"
2#include "match_class.h"
3
4int match_class(const char *name)
5{
6 return index_in_strings(CHAR_CLASSES, name);
7}
diff --git a/win32/match_class.h b/win32/match_class.h
new file mode 100644
index 000000000..92fd1323f
--- /dev/null
+++ b/win32/match_class.h
@@ -0,0 +1,11 @@
1#define CHAR_CLASSES \
2 "alnum\0alpha\0blank\0cntrl\0digit\0graph\0" \
3 "lower\0print\0punct\0space\0upper\0xdigit\0"
4
5enum {
6 CCLASS_ALNUM, CCLASS_ALPHA, CCLASS_BLANK, CCLASS_CNTRL,
7 CCLASS_DIGIT, CCLASS_GRAPH, CCLASS_LOWER, CCLASS_PRINT,
8 CCLASS_PUNCT, CCLASS_SPACE, CCLASS_UPPER, CCLASS_XDIGIT
9};
10
11extern int match_class(const char *name);
diff --git a/win32/regcomp.c b/win32/regcomp.c
index dca7e6ef3..e1692d341 100644
--- a/win32/regcomp.c
+++ b/win32/regcomp.c
@@ -18,6 +18,8 @@
18 Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 18 Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301 USA. */ 19 02110-1301 USA. */
20 20
21#include "match_class.h"
22
21#define UNUSED_PARAM __attribute__ ((__unused__)) 23#define UNUSED_PARAM __attribute__ ((__unused__))
22 24
23static reg_errcode_t re_compile_internal (regex_t *preg, const char * pattern, 25static reg_errcode_t re_compile_internal (regex_t *preg, const char * pattern,
@@ -3571,6 +3573,7 @@ build_charclass (RE_TRANSLATE_TYPE trans, bitset_t sbcset,
3571 } \ 3573 } \
3572 } while (0) 3574 } while (0)
3573 3575
3576#if 0
3574 if (strcmp (class_name, "alnum") == 0) 3577 if (strcmp (class_name, "alnum") == 0)
3575 BUILD_CHARCLASS_LOOP (isalnum); 3578 BUILD_CHARCLASS_LOOP (isalnum);
3576 else if (strcmp (class_name, "cntrl") == 0) 3579 else if (strcmp (class_name, "cntrl") == 0)
@@ -3602,6 +3605,53 @@ build_charclass (RE_TRANSLATE_TYPE trans, bitset_t sbcset,
3602 BUILD_CHARCLASS_LOOP (isxdigit); 3605 BUILD_CHARCLASS_LOOP (isxdigit);
3603 else 3606 else
3604 return REG_ECTYPE; 3607 return REG_ECTYPE;
3608#else
3609 switch (match_class(class_name)) {
3610 case CCLASS_ALNUM:
3611 BUILD_CHARCLASS_LOOP (isalnum);
3612 break;
3613 case CCLASS_CNTRL:
3614 BUILD_CHARCLASS_LOOP (iscntrl);
3615 break;
3616 case CCLASS_LOWER:
3617 BUILD_CHARCLASS_LOOP (islower);
3618 break;
3619 case CCLASS_SPACE:
3620 BUILD_CHARCLASS_LOOP (isspace);
3621 break;
3622 case CCLASS_ALPHA:
3623 BUILD_CHARCLASS_LOOP (isalpha);
3624 break;
3625 case CCLASS_DIGIT:
3626 BUILD_CHARCLASS_LOOP (isdigit);
3627 break;
3628 case CCLASS_PRINT:
3629 BUILD_CHARCLASS_LOOP (isprint);
3630 break;
3631 case CCLASS_UPPER:
3632 BUILD_CHARCLASS_LOOP (isupper);
3633 break;
3634 case CCLASS_BLANK:
3635#ifndef GAWK
3636 BUILD_CHARCLASS_LOOP (isblank);
3637#else
3638 /* see comments above */
3639 BUILD_CHARCLASS_LOOP (is_blank);
3640#endif
3641 break;
3642 case CCLASS_GRAPH:
3643 BUILD_CHARCLASS_LOOP (isgraph);
3644 break;
3645 case CCLASS_PUNCT:
3646 BUILD_CHARCLASS_LOOP (ispunct);
3647 break;
3648 case CCLASS_XDIGIT:
3649 BUILD_CHARCLASS_LOOP (isxdigit);
3650 break;
3651 default:
3652 return REG_ECTYPE;
3653 }
3654#endif
3605 3655
3606 return REG_NOERROR; 3656 return REG_NOERROR;
3607} 3657}