abseil-cpp/absl/strings/ascii.h
Go to the documentation of this file.
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 <string>
57 
58 #include "absl/base/attributes.h"
59 #include "absl/base/config.h"
60 #include "absl/strings/string_view.h"
61 
62 namespace absl {
64 namespace ascii_internal {
65 
66 // Declaration for an array of bitfields holding character information.
67 ABSL_DLL extern const unsigned char kPropertyBits[256];
68 
69 // Declaration for the array of characters to upper-case characters.
70 ABSL_DLL extern const char kToUpper[256];
71 
72 // Declaration for the array of characters to lower-case characters.
73 ABSL_DLL extern const char kToLower[256];
74 
75 } // namespace ascii_internal
76 
77 // ascii_isalpha()
78 //
79 // Determines whether the given character is an alphabetic character.
80 inline bool ascii_isalpha(unsigned char c) {
81  return (ascii_internal::kPropertyBits[c] & 0x01) != 0;
82 }
83 
84 // ascii_isalnum()
85 //
86 // Determines whether the given character is an alphanumeric character.
87 inline bool ascii_isalnum(unsigned char c) {
88  return (ascii_internal::kPropertyBits[c] & 0x04) != 0;
89 }
90 
91 // ascii_isspace()
92 //
93 // Determines whether the given character is a whitespace character (space,
94 // tab, vertical tab, formfeed, linefeed, or carriage return).
95 inline bool ascii_isspace(unsigned char c) {
96  return (ascii_internal::kPropertyBits[c] & 0x08) != 0;
97 }
98 
99 // ascii_ispunct()
100 //
101 // Determines whether the given character is a punctuation character.
102 inline bool ascii_ispunct(unsigned char c) {
103  return (ascii_internal::kPropertyBits[c] & 0x10) != 0;
104 }
105 
106 // ascii_isblank()
107 //
108 // Determines whether the given character is a blank character (tab or space).
109 inline bool ascii_isblank(unsigned char c) {
110  return (ascii_internal::kPropertyBits[c] & 0x20) != 0;
111 }
112 
113 // ascii_iscntrl()
114 //
115 // Determines whether the given character is a control character.
116 inline bool ascii_iscntrl(unsigned char c) {
117  return (ascii_internal::kPropertyBits[c] & 0x40) != 0;
118 }
119 
120 // ascii_isxdigit()
121 //
122 // Determines whether the given character can be represented as a hexadecimal
123 // digit character (i.e. {0-9} or {A-F}).
124 inline bool ascii_isxdigit(unsigned char c) {
125  return (ascii_internal::kPropertyBits[c] & 0x80) != 0;
126 }
127 
128 // ascii_isdigit()
129 //
130 // Determines whether the given character can be represented as a decimal
131 // digit character (i.e. {0-9}).
132 inline bool ascii_isdigit(unsigned char c) { return c >= '0' && c <= '9'; }
133 
134 // ascii_isprint()
135 //
136 // Determines whether the given character is printable, including spaces.
137 inline bool ascii_isprint(unsigned char c) { return c >= 32 && c < 127; }
138 
139 // ascii_isgraph()
140 //
141 // Determines whether the given character has a graphical representation.
142 inline bool ascii_isgraph(unsigned char c) { return c > 32 && c < 127; }
143 
144 // ascii_isupper()
145 //
146 // Determines whether the given character is uppercase.
147 inline bool ascii_isupper(unsigned char c) { return c >= 'A' && c <= 'Z'; }
148 
149 // ascii_islower()
150 //
151 // Determines whether the given character is lowercase.
152 inline bool ascii_islower(unsigned char c) { return c >= 'a' && c <= 'z'; }
153 
154 // ascii_isascii()
155 //
156 // Determines whether the given character is ASCII.
157 inline bool ascii_isascii(unsigned char c) { return c < 128; }
158 
159 // ascii_tolower()
160 //
161 // Returns an ASCII character, converting to lowercase if uppercase is
162 // passed. Note that character values > 127 are simply returned.
163 inline char ascii_tolower(unsigned char c) {
164  return ascii_internal::kToLower[c];
165 }
166 
167 // Converts the characters in `s` to lowercase, changing the contents of `s`.
169 
170 // Creates a lowercase string from a given absl::string_view.
174  return result;
175 }
176 
177 // ascii_toupper()
178 //
179 // Returns the ASCII character, converting to upper-case if lower-case is
180 // passed. Note that characters values > 127 are simply returned.
181 inline char ascii_toupper(unsigned char c) {
182  return ascii_internal::kToUpper[c];
183 }
184 
185 // Converts the characters in `s` to uppercase, changing the contents of `s`.
187 
188 // Creates an uppercase string from a given absl::string_view.
192  return result;
193 }
194 
195 // Returns absl::string_view with whitespace stripped from the beginning of the
196 // given string_view.
199  auto it = std::find_if_not(str.begin(), str.end(), absl::ascii_isspace);
200  return str.substr(static_cast<size_t>(it - str.begin()));
201 }
202 
203 // Strips in place whitespace from the beginning of the given string.
205  auto it = std::find_if_not(str->begin(), str->end(), absl::ascii_isspace);
206  str->erase(str->begin(), it);
207 }
208 
209 // Returns absl::string_view with whitespace stripped from the end of the given
210 // string_view.
213  auto it = std::find_if_not(str.rbegin(), str.rend(), absl::ascii_isspace);
214  return str.substr(0, static_cast<size_t>(str.rend() - it));
215 }
216 
217 // Strips in place whitespace from the end of the given string
219  auto it = std::find_if_not(str->rbegin(), str->rend(), absl::ascii_isspace);
220  str->erase(static_cast<size_t>(str->rend() - it));
221 }
222 
223 // Returns absl::string_view with whitespace stripped from both ends of the
224 // given string_view.
228 }
229 
230 // Strips in place whitespace from both ends of the given string
234 }
235 
236 // Removes leading, trailing, and consecutive internal whitespace.
238 
240 } // namespace absl
241 
242 #endif // ABSL_STRINGS_ASCII_H_
absl::StripAsciiWhitespace
ABSL_MUST_USE_RESULT absl::string_view StripAsciiWhitespace(absl::string_view str)
Definition: abseil-cpp/absl/strings/ascii.h:225
xds_interop_client.str
str
Definition: xds_interop_client.py:487
absl::ascii_isblank
bool ascii_isblank(unsigned char c)
Definition: abseil-cpp/absl/strings/ascii.h:109
absl::ascii_tolower
char ascii_tolower(unsigned char c)
Definition: abseil-cpp/absl/strings/ascii.h:163
_gevent_test_main.result
result
Definition: _gevent_test_main.py:96
regen-readme.it
it
Definition: regen-readme.py:15
absl::RemoveExtraAsciiWhitespace
void RemoveExtraAsciiWhitespace(std::string *str)
Definition: abseil-cpp/absl/strings/ascii.cc:170
absl::string_view
Definition: abseil-cpp/absl/strings/string_view.h:167
testing::internal::string
::std::string string
Definition: bloaty/third_party/protobuf/third_party/googletest/googletest/include/gtest/internal/gtest-port.h:881
absl::StripTrailingAsciiWhitespace
ABSL_MUST_USE_RESULT absl::string_view StripTrailingAsciiWhitespace(absl::string_view str)
Definition: abseil-cpp/absl/strings/ascii.h:211
absl::ascii_iscntrl
bool ascii_iscntrl(unsigned char c)
Definition: abseil-cpp/absl/strings/ascii.h:116
absl::ascii_isgraph
bool ascii_isgraph(unsigned char c)
Definition: abseil-cpp/absl/strings/ascii.h:142
absl::FormatConversionChar::s
@ s
ABSL_NAMESPACE_END
#define ABSL_NAMESPACE_END
Definition: third_party/abseil-cpp/absl/base/config.h:171
absl::AsciiStrToLower
void AsciiStrToLower(std::string *s)
Definition: abseil-cpp/absl/strings/ascii.cc:158
absl::ascii_isspace
bool ascii_isspace(unsigned char c)
Definition: abseil-cpp/absl/strings/ascii.h:95
ABSL_MUST_USE_RESULT
#define ABSL_MUST_USE_RESULT
Definition: abseil-cpp/absl/base/attributes.h:441
absl::ascii_isalnum
bool ascii_isalnum(unsigned char c)
Definition: abseil-cpp/absl/strings/ascii.h:87
ABSL_NAMESPACE_BEGIN
#define ABSL_NAMESPACE_BEGIN
Definition: third_party/abseil-cpp/absl/base/config.h:170
absl::ascii_isascii
bool ascii_isascii(unsigned char c)
Definition: abseil-cpp/absl/strings/ascii.h:157
absl::AsciiStrToUpper
void AsciiStrToUpper(std::string *s)
Definition: abseil-cpp/absl/strings/ascii.cc:164
absl::ascii_isprint
bool ascii_isprint(unsigned char c)
Definition: abseil-cpp/absl/strings/ascii.h:137
absl::StripLeadingAsciiWhitespace
ABSL_MUST_USE_RESULT absl::string_view StripLeadingAsciiWhitespace(absl::string_view str)
Definition: abseil-cpp/absl/strings/ascii.h:197
absl::ascii_internal::kPropertyBits
const ABSL_DLL unsigned char kPropertyBits[256]
Definition: abseil-cpp/absl/strings/ascii.cc:60
absl::ascii_internal::kToUpper
const ABSL_DLL char kToUpper[256]
Definition: abseil-cpp/absl/strings/ascii.cc:120
absl::ascii_islower
bool ascii_islower(unsigned char c)
Definition: abseil-cpp/absl/strings/ascii.h:152
absl::ascii_isupper
bool ascii_isupper(unsigned char c)
Definition: abseil-cpp/absl/strings/ascii.h:147
absl::ascii_isalpha
bool ascii_isalpha(unsigned char c)
Definition: abseil-cpp/absl/strings/ascii.h:80
ABSL_DLL
#define ABSL_DLL
Definition: third_party/abseil-cpp/absl/base/config.h:746
absl::ascii_toupper
char ascii_toupper(unsigned char c)
Definition: abseil-cpp/absl/strings/ascii.h:181
absl::ascii_ispunct
bool ascii_ispunct(unsigned char c)
Definition: abseil-cpp/absl/strings/ascii.h:102
absl::ascii_internal::kToLower
const ABSL_DLL char kToLower[256]
Definition: abseil-cpp/absl/strings/ascii.cc:82
absl
Definition: abseil-cpp/absl/algorithm/algorithm.h:31
absl::ascii_isxdigit
bool ascii_isxdigit(unsigned char c)
Definition: abseil-cpp/absl/strings/ascii.h:124
absl::ascii_isdigit
bool ascii_isdigit(unsigned char c)
Definition: abseil-cpp/absl/strings/ascii.h:132


grpc
Author(s):
autogenerated on Thu Mar 13 2025 02:58:34