/src/CMake/Source/kwsys/String.c
Line | Count | Source |
1 | | /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying |
2 | | file Copyright.txt or https://cmake.org/licensing#kwsys for details. */ |
3 | | #ifdef KWSYS_STRING_C |
4 | | /* |
5 | | All code in this source file is conditionally compiled to work-around |
6 | | template definition auto-search on VMS. Other source files in this |
7 | | directory that use the stl string cause the compiler to load this |
8 | | source to try to get the definition of the string template. This |
9 | | condition blocks the compiler from seeing the symbols defined here. |
10 | | */ |
11 | | # include "kwsysPrivate.h" |
12 | | # include KWSYS_HEADER(String.h) |
13 | | |
14 | | /* Work-around CMake dependency scanning limitation. This must |
15 | | duplicate the above list of headers. */ |
16 | | # if 0 |
17 | | # include "String.h.in" |
18 | | # endif |
19 | | |
20 | | # include <ctype.h> |
21 | | |
22 | | int kwsysString_isalnum(char c) |
23 | 0 | { |
24 | 0 | return (kwsysString_isalpha(c) | /* bitwise-or to avoid branching. */ |
25 | 0 | kwsysString_isdigit(c)); |
26 | 0 | } |
27 | | |
28 | | int kwsysString_isalpha(char c) |
29 | 0 | { |
30 | 0 | return (kwsysString_islower(c) | /* bitwise-or to avoid branching. */ |
31 | 0 | kwsysString_isupper(c)); |
32 | 0 | } |
33 | | |
34 | | int kwsysString_isascii(char c) |
35 | 0 | { |
36 | 0 | unsigned char const uc = (unsigned char)c; |
37 | 0 | return (uc & 0x80) ^ 0x80; |
38 | 0 | } |
39 | | |
40 | | int kwsysString_isblank(char c) |
41 | 0 | { |
42 | 0 | unsigned char const uc = (unsigned char)c; |
43 | 0 | return uc < 0x80 && isblank(uc); |
44 | 0 | } |
45 | | |
46 | | int kwsysString_iscntrl(char c) |
47 | 0 | { |
48 | 0 | unsigned char const uc = (unsigned char)c; |
49 | 0 | return uc < 0x80 && iscntrl(uc); |
50 | 0 | } |
51 | | |
52 | | int kwsysString_isdigit(char c) |
53 | 0 | { |
54 | 0 | unsigned char const uc = (unsigned char)c; |
55 | | /* Push '0-9' to 246-255 so integer division by 246 is 1 only for them. */ |
56 | 0 | return ((uc + 246 - '0') & 0xFF) / 246; |
57 | 0 | } |
58 | | |
59 | | int kwsysString_isgraph(char c) |
60 | 0 | { |
61 | 0 | unsigned char const uc = (unsigned char)c; |
62 | 0 | return uc < 0x80 && isgraph(uc); |
63 | 0 | } |
64 | | |
65 | | int kwsysString_islower(char c) |
66 | 0 | { |
67 | 0 | unsigned char const uc = (unsigned char)c; |
68 | | /* Push 'a-z' to 230-255 so integer division by 230 is 1 only for them. */ |
69 | 0 | return ((uc + 230 - 'a') & 0xFF) / 230; |
70 | 0 | } |
71 | | |
72 | | int kwsysString_isprint(char c) |
73 | 0 | { |
74 | 0 | unsigned char const uc = (unsigned char)c; |
75 | | /* Push 32-126 to 161-255 so integer division by 161 is 1 only for them. */ |
76 | 0 | return ((uc + 161 - ' ') & 0xFF) / 161; |
77 | 0 | } |
78 | | |
79 | | int kwsysString_ispunct(char c) |
80 | 0 | { |
81 | 0 | unsigned char const uc = (unsigned char)c; |
82 | 0 | return uc < 0x80 && ispunct(uc); |
83 | 0 | } |
84 | | |
85 | | int kwsysString_isspace(char c) |
86 | 133k | { |
87 | 133k | unsigned char const uc = (unsigned char)c; |
88 | 133k | return uc < 0x80 && isspace(uc); |
89 | 133k | } |
90 | | |
91 | | int kwsysString_isupper(char c) |
92 | 0 | { |
93 | 0 | unsigned char const uc = (unsigned char)c; |
94 | | /* Push 'A-Z' to 230-255 so integer division by 230 is 1 only for them. */ |
95 | 0 | return ((uc + 230 - 'A') & 0xFF) / 230; |
96 | 0 | } |
97 | | |
98 | | int kwsysString_isxdigit(char c) |
99 | 0 | { |
100 | 0 | unsigned char const uc = (unsigned char)c; |
101 | 0 | return ( |
102 | | /* Push '0-9' to 246-255 so integer division by 246 is 1 only for them. */ |
103 | 0 | (((uc + 246 - '0') & 0xFF) / 246) | /* bitwise-or to avoid branching. */ |
104 | | /* Push 'A-F' to 250-255 so integer division by 250 is 1 only for them. */ |
105 | 0 | (((uc + 250 - 'A') & 0xFF) / 250) | /* bitwise-or to avoid branching. */ |
106 | | /* Push 'a-f' to 250-255 so integer division by 250 is 1 only for them. */ |
107 | 0 | (((uc + 250 - 'a') & 0xFF) / 250)); |
108 | 0 | } |
109 | | |
110 | | unsigned char kwsysString_tolower(char c) |
111 | 1.94M | { |
112 | 1.94M | unsigned char const uc = (unsigned char)c; |
113 | | /* Push 'A-Z' to 230-255 so integer division by 230 is 1 only for them. */ |
114 | 1.94M | return (unsigned char)(uc ^ ((((uc + 230 - 'A') & 0xFF) / 230) << 5)); |
115 | 1.94M | } |
116 | | |
117 | | unsigned char kwsysString_toupper(char c) |
118 | 1.94M | { |
119 | 1.94M | unsigned char const uc = (unsigned char)c; |
120 | | /* Push 'a-z' to 230-255 so integer division by 230 is 1 only for them. */ |
121 | 1.94M | return (unsigned char)(uc ^ ((((uc + 230 - 'a') & 0xFF) / 230) << 5)); |
122 | 1.94M | } |
123 | | |
124 | | int kwsysString_strcasecmp(char const* lhs, char const* rhs) |
125 | 0 | { |
126 | 0 | int result; |
127 | 0 | while ((result = kwsysString_tolower(*lhs) - kwsysString_tolower(*rhs++)) == |
128 | 0 | 0 && |
129 | 0 | *lhs++) { |
130 | 0 | } |
131 | 0 | return result; |
132 | 0 | } |
133 | | |
134 | | int kwsysString_strncasecmp(char const* lhs, char const* rhs, size_t n) |
135 | 0 | { |
136 | 0 | int result = 0; |
137 | 0 | while (n && |
138 | 0 | (result = kwsysString_tolower(*lhs) - kwsysString_tolower(*rhs++)) == |
139 | 0 | 0 && |
140 | 0 | *lhs++) { |
141 | 0 | --n; |
142 | 0 | } |
143 | 0 | return result; |
144 | 0 | } |
145 | | |
146 | | #endif /* KWSYS_STRING_C */ |