aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--include/libbb.h49
1 files changed, 35 insertions, 14 deletions
diff --git a/include/libbb.h b/include/libbb.h
index 805846391..5d7426a66 100644
--- a/include/libbb.h
+++ b/include/libbb.h
@@ -1566,42 +1566,63 @@ extern const char bb_default_login_shell[];
1566#define RB_POWER_OFF 0x4321fedc 1566#define RB_POWER_OFF 0x4321fedc
1567#endif 1567#endif
1568 1568
1569/* Make sure we call functions instead of these macros */ 1569/* We redefine ctype macros. Unicode-correct handling of char types
1570 * can't be done with such byte-oriented operations anyway,
1571 * we don't lose anything.
1572 */
1570#undef isalnum 1573#undef isalnum
1571#undef ispunct
1572#undef isxdigit
1573/* and these we'll redefine */
1574#undef isalpha 1574#undef isalpha
1575#undef isascii 1575#undef isascii
1576#undef isblank 1576#undef isblank
1577#undef iscntrl 1577#undef iscntrl
1578#undef isdigit
1578#undef isgraph 1579#undef isgraph
1579#undef islower 1580#undef islower
1580#undef isprint 1581#undef isprint
1581#undef isupper 1582#undef ispunct
1582#undef isdigit
1583#undef isspace 1583#undef isspace
1584#undef isupper
1585#undef isxdigit
1586#undef toupper
1587#undef tolower
1584 1588
1585/* This one is more efficient - we save ~500 bytes. 1589/* We save ~500 bytes on isdigit alone.
1586 * BTW, x86 likes (unsigned char) cast more than (unsigned). */ 1590 * BTW, x86 likes (unsigned char) cast more than (unsigned). */
1587#define isdigit(a) ((unsigned char)((a) - '0') <= 9) 1591#define isdigit(a) ((unsigned char)((a) - '0') <= 9)
1588
1589#define isascii(a) ((unsigned char)(a) <= 0x7f) 1592#define isascii(a) ((unsigned char)(a) <= 0x7f)
1590#define isgraph(a) ((unsigned char)(a) > ' ') 1593#define isgraph(a) ((unsigned char)(a) > ' ')
1591#define isprint(a) ((unsigned char)(a) >= ' ') 1594#define isprint(a) ((unsigned char)(a) >= ' ')
1592#define isupper(a) ((unsigned char)((a) - 'A') <= ('Z' - 'A')) 1595#define isupper(a) ((unsigned char)((a) - 'A') <= ('Z' - 'A'))
1593#define islower(a) ((unsigned char)((a) - 'a') <= ('z' - 'a')) 1596#define islower(a) ((unsigned char)((a) - 'a') <= ('z' - 'a'))
1594#define isalpha(a) ((unsigned char)(((a) | 0x20) - 'a') <= ('z' - 'a')) 1597#define isalpha(a) ((unsigned char)(((a)|0x20) - 'a') <= ('z' - 'a'))
1595#define isblank(a) ({ unsigned char bb__isblank = (a); bb__isblank == ' ' || bb__isblank == '\t'; }) 1598#define isblank(a) ({ unsigned char bb__isblank = (a); bb__isblank == ' ' || bb__isblank == '\t'; })
1596#define iscntrl(a) ({ unsigned char bb__iscntrl = (a); bb__iscntrl < ' ' || bb__iscntrl == 0x7f; }) 1599#define iscntrl(a) ({ unsigned char bb__iscntrl = (a); bb__iscntrl < ' ' || bb__iscntrl == 0x7f; })
1597 1600/* In POSIX/C locale isspace is only these chars: "\t\n\v\f\r" and space.
1598/* In POSIX/C locale (the only locale we care about: do we REALLY want
1599 * to allow Unicode whitespace in, say, .conf files? nuts!)
1600 * isspace is only these chars: "\t\n\v\f\r" and space.
1601 * "\t\n\v\f\r" happen to have ASCII codes 9,10,11,12,13. 1601 * "\t\n\v\f\r" happen to have ASCII codes 9,10,11,12,13.
1602 * Use that.
1603 */ 1602 */
1604#define isspace(a) ({ unsigned char bb__isspace = (a) - 9; bb__isspace == (' ' - 9) || bb__isspace <= (13 - 9); }) 1603#define isspace(a) ({ unsigned char bb__isspace = (a) - 9; bb__isspace == (' ' - 9) || bb__isspace <= (13 - 9); })
1604#define isalnum(a) ({ unsigned char bb__isalnum = ((a)|0x20) - '0'; bb__isalnum <= 9 || (bb__isalnum - ('a' - '0')) <= 25; })
1605#define isxdigit(a) ({ unsigned char bb__isxdigit = ((a)|0x20) - '0'; bb__isxdigit <= 9 || (bb__isxdigit - ('a' - '0')) <= 5; })
1606// Unsafe wrt NUL!
1607//#define ispunct(a) (strchr("!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~", (a)) != NULL)
1608#define ispunct(a) (strchrnul("!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~", (a))[0])
1609
1610#define toupper(a) bb_ascii_toupper(a)
1611static ALWAYS_INLINE unsigned char bb_ascii_toupper(unsigned char a)
1612{
1613 unsigned char b = a - 'a';
1614 if (b <= ('z' - 'a'))
1615 a -= 'a' - 'A';
1616 return a;
1617}
1618#define tolower(a) bb_ascii_tolower(a)
1619static ALWAYS_INLINE unsigned char bb_ascii_tolower(unsigned char a)
1620{
1621 unsigned char b = a - 'A';
1622 if (b <= ('Z' - 'A'))
1623 a += 'a' - 'A';
1624 return a;
1625}
1605 1626
1606 1627
1607#define ARRAY_SIZE(x) ((unsigned)(sizeof(x) / sizeof((x)[0]))) 1628#define ARRAY_SIZE(x) ((unsigned)(sizeof(x) / sizeof((x)[0])))