/src/abseil-cpp/absl/strings/ascii.h
Line | Count | Source (jump to first uncovered line) |
1 | | // |
2 | | // Copyright 2017 The Abseil Authors. |
3 | | // |
4 | | // Licensed under the Apache License, Version 2.0 (the "License"); |
5 | | // you may not use this file except in compliance with the License. |
6 | | // You may obtain a copy of the License at |
7 | | // |
8 | | // https://www.apache.org/licenses/LICENSE-2.0 |
9 | | // |
10 | | // Unless required by applicable law or agreed to in writing, software |
11 | | // distributed under the License is distributed on an "AS IS" BASIS, |
12 | | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
13 | | // See the License for the specific language governing permissions and |
14 | | // limitations under the License. |
15 | | // |
16 | | // ----------------------------------------------------------------------------- |
17 | | // File: ascii.h |
18 | | // ----------------------------------------------------------------------------- |
19 | | // |
20 | | // This package contains functions operating on characters and strings |
21 | | // restricted to standard ASCII. These include character classification |
22 | | // functions analogous to those found in the ANSI C Standard Library <ctype.h> |
23 | | // header file. |
24 | | // |
25 | | // C++ implementations provide <ctype.h> functionality based on their |
26 | | // C environment locale. In general, reliance on such a locale is not ideal, as |
27 | | // the locale standard is problematic (and may not return invariant information |
28 | | // for the same character set, for example). These `ascii_*()` functions are |
29 | | // hard-wired for standard ASCII, much faster, and guaranteed to behave |
30 | | // consistently. They will never be overloaded, nor will their function |
31 | | // signature change. |
32 | | // |
33 | | // `ascii_isalnum()`, `ascii_isalpha()`, `ascii_isascii()`, `ascii_isblank()`, |
34 | | // `ascii_iscntrl()`, `ascii_isdigit()`, `ascii_isgraph()`, `ascii_islower()`, |
35 | | // `ascii_isprint()`, `ascii_ispunct()`, `ascii_isspace()`, `ascii_isupper()`, |
36 | | // `ascii_isxdigit()` |
37 | | // Analogous to the <ctype.h> functions with similar names, these |
38 | | // functions take an unsigned char and return a bool, based on whether the |
39 | | // character matches the condition specified. |
40 | | // |
41 | | // If the input character has a numerical value greater than 127, these |
42 | | // functions return `false`. |
43 | | // |
44 | | // `ascii_tolower()`, `ascii_toupper()` |
45 | | // Analogous to the <ctype.h> functions with similar names, these functions |
46 | | // take an unsigned char and return a char. |
47 | | // |
48 | | // If the input character is not an ASCII {lower,upper}-case letter (including |
49 | | // numerical values greater than 127) then the functions return the same value |
50 | | // as the input character. |
51 | | |
52 | | #ifndef ABSL_STRINGS_ASCII_H_ |
53 | | #define ABSL_STRINGS_ASCII_H_ |
54 | | |
55 | | #include <algorithm> |
56 | | #include <cstddef> |
57 | | #include <string> |
58 | | |
59 | | #include "absl/base/attributes.h" |
60 | | #include "absl/base/config.h" |
61 | | #include "absl/base/nullability.h" |
62 | | #include "absl/strings/string_view.h" |
63 | | |
64 | | namespace absl { |
65 | | ABSL_NAMESPACE_BEGIN |
66 | | namespace ascii_internal { |
67 | | |
68 | | // Declaration for an array of bitfields holding character information. |
69 | | ABSL_DLL extern const unsigned char kPropertyBits[256]; |
70 | | |
71 | | // Declaration for the array of characters to upper-case characters. |
72 | | ABSL_DLL extern const char kToUpper[256]; |
73 | | |
74 | | // Declaration for the array of characters to lower-case characters. |
75 | | ABSL_DLL extern const char kToLower[256]; |
76 | | |
77 | | } // namespace ascii_internal |
78 | | |
79 | | // ascii_isalpha() |
80 | | // |
81 | | // Determines whether the given character is an alphabetic character. |
82 | 0 | inline bool ascii_isalpha(unsigned char c) { |
83 | 0 | return (ascii_internal::kPropertyBits[c] & 0x01) != 0; |
84 | 0 | } |
85 | | |
86 | | // ascii_isalnum() |
87 | | // |
88 | | // Determines whether the given character is an alphanumeric character. |
89 | 0 | inline bool ascii_isalnum(unsigned char c) { |
90 | 0 | return (ascii_internal::kPropertyBits[c] & 0x04) != 0; |
91 | 0 | } |
92 | | |
93 | | // ascii_isspace() |
94 | | // |
95 | | // Determines whether the given character is a whitespace character (space, |
96 | | // tab, vertical tab, formfeed, linefeed, or carriage return). |
97 | 66.9M | inline bool ascii_isspace(unsigned char c) { |
98 | 66.9M | return (ascii_internal::kPropertyBits[c] & 0x08) != 0; |
99 | 66.9M | } |
100 | | |
101 | | // ascii_ispunct() |
102 | | // |
103 | | // Determines whether the given character is a punctuation character. |
104 | 0 | inline bool ascii_ispunct(unsigned char c) { |
105 | 0 | return (ascii_internal::kPropertyBits[c] & 0x10) != 0; |
106 | 0 | } |
107 | | |
108 | | // ascii_isblank() |
109 | | // |
110 | | // Determines whether the given character is a blank character (tab or space). |
111 | 0 | inline bool ascii_isblank(unsigned char c) { |
112 | 0 | return (ascii_internal::kPropertyBits[c] & 0x20) != 0; |
113 | 0 | } |
114 | | |
115 | | // ascii_iscntrl() |
116 | | // |
117 | | // Determines whether the given character is a control character. |
118 | 0 | inline bool ascii_iscntrl(unsigned char c) { |
119 | 0 | return (ascii_internal::kPropertyBits[c] & 0x40) != 0; |
120 | 0 | } |
121 | | |
122 | | // ascii_isxdigit() |
123 | | // |
124 | | // Determines whether the given character can be represented as a hexadecimal |
125 | | // digit character (i.e. {0-9} or {A-F}). |
126 | 0 | inline bool ascii_isxdigit(unsigned char c) { |
127 | 0 | return (ascii_internal::kPropertyBits[c] & 0x80) != 0; |
128 | 0 | } |
129 | | |
130 | | // ascii_isdigit() |
131 | | // |
132 | | // Determines whether the given character can be represented as a decimal |
133 | | // digit character (i.e. {0-9}). |
134 | 0 | inline bool ascii_isdigit(unsigned char c) { return c >= '0' && c <= '9'; } |
135 | | |
136 | | // ascii_isprint() |
137 | | // |
138 | | // Determines whether the given character is printable, including spaces. |
139 | 0 | inline bool ascii_isprint(unsigned char c) { return c >= 32 && c < 127; } |
140 | | |
141 | | // ascii_isgraph() |
142 | | // |
143 | | // Determines whether the given character has a graphical representation. |
144 | 0 | inline bool ascii_isgraph(unsigned char c) { return c > 32 && c < 127; } |
145 | | |
146 | | // ascii_isupper() |
147 | | // |
148 | | // Determines whether the given character is uppercase. |
149 | 0 | inline bool ascii_isupper(unsigned char c) { return c >= 'A' && c <= 'Z'; } |
150 | | |
151 | | // ascii_islower() |
152 | | // |
153 | | // Determines whether the given character is lowercase. |
154 | 0 | inline bool ascii_islower(unsigned char c) { return c >= 'a' && c <= 'z'; } |
155 | | |
156 | | // ascii_isascii() |
157 | | // |
158 | | // Determines whether the given character is ASCII. |
159 | 0 | inline bool ascii_isascii(unsigned char c) { return c < 128; } |
160 | | |
161 | | // ascii_tolower() |
162 | | // |
163 | | // Returns an ASCII character, converting to lowercase if uppercase is |
164 | | // passed. Note that character values > 127 are simply returned. |
165 | 0 | inline char ascii_tolower(unsigned char c) { |
166 | 0 | return ascii_internal::kToLower[c]; |
167 | 0 | } |
168 | | |
169 | | // Converts the characters in `s` to lowercase, changing the contents of `s`. |
170 | | void AsciiStrToLower(absl::Nonnull<std::string*> s); |
171 | | |
172 | | // Creates a lowercase string from a given absl::string_view. |
173 | 0 | ABSL_MUST_USE_RESULT inline std::string AsciiStrToLower(absl::string_view s) { |
174 | 0 | std::string result(s); |
175 | 0 | absl::AsciiStrToLower(&result); |
176 | 0 | return result; |
177 | 0 | } |
178 | | |
179 | | // ascii_toupper() |
180 | | // |
181 | | // Returns the ASCII character, converting to upper-case if lower-case is |
182 | | // passed. Note that characters values > 127 are simply returned. |
183 | 0 | inline char ascii_toupper(unsigned char c) { |
184 | 0 | return ascii_internal::kToUpper[c]; |
185 | 0 | } |
186 | | |
187 | | // Converts the characters in `s` to uppercase, changing the contents of `s`. |
188 | | void AsciiStrToUpper(absl::Nonnull<std::string*> s); |
189 | | |
190 | | // Creates an uppercase string from a given absl::string_view. |
191 | 0 | ABSL_MUST_USE_RESULT inline std::string AsciiStrToUpper(absl::string_view s) { |
192 | 0 | std::string result(s); |
193 | 0 | absl::AsciiStrToUpper(&result); |
194 | 0 | return result; |
195 | 0 | } |
196 | | |
197 | | // Returns absl::string_view with whitespace stripped from the beginning of the |
198 | | // given string_view. |
199 | | ABSL_MUST_USE_RESULT inline absl::string_view StripLeadingAsciiWhitespace( |
200 | 33.5M | absl::string_view str) { |
201 | 33.5M | auto it = std::find_if_not(str.begin(), str.end(), absl::ascii_isspace); |
202 | 33.5M | return str.substr(static_cast<size_t>(it - str.begin())); |
203 | 33.5M | } |
204 | | |
205 | | // Strips in place whitespace from the beginning of the given string. |
206 | 0 | inline void StripLeadingAsciiWhitespace(absl::Nonnull<std::string*> str) { |
207 | 0 | auto it = std::find_if_not(str->begin(), str->end(), absl::ascii_isspace); |
208 | 0 | str->erase(str->begin(), it); |
209 | 0 | } |
210 | | |
211 | | // Returns absl::string_view with whitespace stripped from the end of the given |
212 | | // string_view. |
213 | | ABSL_MUST_USE_RESULT inline absl::string_view StripTrailingAsciiWhitespace( |
214 | 33.5M | absl::string_view str) { |
215 | 33.5M | auto it = std::find_if_not(str.rbegin(), str.rend(), absl::ascii_isspace); |
216 | 33.5M | return str.substr(0, static_cast<size_t>(str.rend() - it)); |
217 | 33.5M | } |
218 | | |
219 | | // Strips in place whitespace from the end of the given string |
220 | 0 | inline void StripTrailingAsciiWhitespace(absl::Nonnull<std::string*> str) { |
221 | 0 | auto it = std::find_if_not(str->rbegin(), str->rend(), absl::ascii_isspace); |
222 | 0 | str->erase(static_cast<size_t>(str->rend() - it)); |
223 | 0 | } |
224 | | |
225 | | // Returns absl::string_view with whitespace stripped from both ends of the |
226 | | // given string_view. |
227 | | ABSL_MUST_USE_RESULT inline absl::string_view StripAsciiWhitespace( |
228 | 33.5M | absl::string_view str) { |
229 | 33.5M | return StripTrailingAsciiWhitespace(StripLeadingAsciiWhitespace(str)); |
230 | 33.5M | } |
231 | | |
232 | | // Strips in place whitespace from both ends of the given string |
233 | 0 | inline void StripAsciiWhitespace(absl::Nonnull<std::string*> str) { |
234 | 0 | StripTrailingAsciiWhitespace(str); |
235 | 0 | StripLeadingAsciiWhitespace(str); |
236 | 0 | } |
237 | | |
238 | | // Removes leading, trailing, and consecutive internal whitespace. |
239 | | void RemoveExtraAsciiWhitespace(absl::Nonnull<std::string*> str); |
240 | | |
241 | | ABSL_NAMESPACE_END |
242 | | } // namespace absl |
243 | | |
244 | | #endif // ABSL_STRINGS_ASCII_H_ |