| 1 |
/// \file |
| 2 |
/// \brief replacements for ctype.h functions |
| 3 |
/// \ingroup cgraph_utils |
| 4 |
/// |
| 5 |
/// The behavior of the ctype.h functions is locale-dependent, while Graphviz |
| 6 |
/// code typically wants to ask about character data specifically interpreted as |
| 7 |
/// ASCII. The current locale is frequently irrelevant because Graphviz (1) |
| 8 |
/// supports input in encodings different than the user’s locale via the |
| 9 |
/// `charset` attribute and (2) is often producing output formats that are |
| 10 |
/// implicitly ASCII-only. |
| 11 |
/// |
| 12 |
/// This discrepancy leads to misbehavior when trying to use the ctype.h |
| 13 |
/// functions as-is. For example, certain Windows environments with a signed |
| 14 |
/// `char` type crash when `isdigit` is called with a `char` that is part of a |
| 15 |
/// multi-byte unicode character and has its high bit set. |
| 16 |
/// |
| 17 |
/// There are various solutions to this like using a full internationalization |
| 18 |
/// library or constructing an ASCII locale and calling the `*_l` variants. But |
| 19 |
/// for simplicity we just implement the exact discriminators we need. |
| 20 |
|
| 21 |
#pragma once |
| 22 |
|
| 23 |
#include <stdbool.h> |
| 24 |
|
| 25 |
static inline bool gv_islower(int c) { return c >= 'a' && c <= 'z'; } |
| 26 |
|
| 27 |
static inline bool gv_isupper(int c) { return c >= 'A' && c <= 'Z'; } |
| 28 |
|
| 29 |
static inline bool gv_isalpha(int c) { return gv_islower(c) || gv_isupper(c); } |
| 30 |
|
| 31 |
static inline bool gv_isblank(int c) { return c == ' ' || c == '\t'; }; |
| 32 |
|
| 33 |
static inline bool gv_iscntrl(int c) { |
| 34 |
if (c >= 0 && c <= 31) |
| 35 |
return true; |
| 36 |
if (c == 127) |
| 37 |
return true; |
| 38 |
return false; |
| 39 |
} |
| 40 |
|
| 41 |
static inline bool gv_isdigit(int c) { return c >= '0' && c <= '9'; } |
| 42 |
|
| 43 |
static inline bool gv_isalnum(int c) { return gv_isalpha(c) || gv_isdigit(c); } |
| 44 |
|
| 45 |
static inline bool gv_isgraph(int c) { return c > 32 && c < 127; } |
| 46 |
|
| 47 |
static inline bool gv_isprint(int c) { return c > 31 && c < 127; } |
| 48 |
|
| 49 |
static inline bool gv_ispunct(int c) { |
| 50 |
if (gv_isalnum(c)) |
| 51 |
return false; |
| 52 |
return c > 32 && c < 127; |
| 53 |
} |
| 54 |
|
| 55 |
static inline bool gv_isspace(int c) { |
| 56 |
if (c == '\t') |
| 57 |
return true; |
| 58 |
if (c == '\n') |
| 59 |
return true; |
| 60 |
if (c == '\v') |
| 61 |
return true; |
| 62 |
if (c == '\f') |
| 63 |
return true; |
| 64 |
if (c == '\r') |
| 65 |
return true; |
| 66 |
if (c == ' ') |
| 67 |
return true; |
| 68 |
return false; |
| 69 |
} |
| 70 |
|
| 71 |
static inline bool gv_isxdigit(int c) { |
| 72 |
if (gv_isdigit(c)) |
| 73 |
return true; |
| 74 |
if (c >= 'A' && c <= 'F') |
| 75 |
return true; |
| 76 |
if (c >= 'a' && c <= 'f') |
| 77 |
return true; |
| 78 |
return false; |
| 79 |
} |
| 80 |
|
| 81 |
static inline char gv_tolower(int c) { |
| 82 |
if (gv_isupper(c)) |
| 83 |
return (char)c - 'A' + 'a'; |
| 84 |
return (char)c; |
| 85 |
} |
| 86 |
|
| 87 |
static inline void gv_tolower_str(char *s) { |
| 88 |
for (; *s != '\0'; ++s) { |
| 89 |
*s = gv_tolower(*s); |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
static inline char gv_toupper(int c) { |
| 94 |
if (gv_islower(c)) |
| 95 |
return (char)c - 'a' + 'A'; |
| 96 |
return (char)c; |
| 97 |
} |
| 98 |
|
| 99 |
static inline void gv_toupper_str(char *s) { |
| 100 |
for (; *s != '\0'; ++s) { |
| 101 |
*s = gv_toupper(*s); |
| 102 |
} |
| 103 |
} |