Coverage Report

Created: 2026-04-01 06:29

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/abseil-cpp/absl/strings/ascii.h
Line
Count
Source
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
#include <utility>
59
60
#include "absl/base/attributes.h"
61
#include "absl/base/config.h"
62
#include "absl/base/nullability.h"
63
#include "absl/strings/resize_and_overwrite.h"
64
#include "absl/strings/string_view.h"
65
66
namespace absl {
67
ABSL_NAMESPACE_BEGIN
68
namespace ascii_internal {
69
70
// Declaration for an array of bitfields holding character information.
71
ABSL_DLL extern const unsigned char kPropertyBits[256];
72
73
// Declaration for the array of characters to upper-case characters.
74
ABSL_DLL extern const char kToUpper[256];
75
76
// Declaration for the array of characters to lower-case characters.
77
ABSL_DLL extern const char kToLower[256];
78
79
void AsciiStrToLower(char* absl_nonnull dst, const char* absl_nullable src,
80
                     size_t n);
81
82
void AsciiStrToUpper(char* absl_nonnull dst, const char* absl_nullable src,
83
                     size_t n);
84
85
}  // namespace ascii_internal
86
87
// ascii_isalpha()
88
//
89
// Determines whether the given character is an alphabetic character.
90
0
inline bool ascii_isalpha(unsigned char c) {
91
0
  return (ascii_internal::kPropertyBits[c] & 0x01) != 0;
92
0
}
93
94
// ascii_isalnum()
95
//
96
// Determines whether the given character is an alphanumeric character.
97
0
inline bool ascii_isalnum(unsigned char c) {
98
0
  return (ascii_internal::kPropertyBits[c] & 0x04) != 0;
99
0
}
100
101
// ascii_isspace()
102
//
103
// Determines whether the given character is a whitespace character (space,
104
// tab, vertical tab, formfeed, linefeed, or carriage return).
105
0
inline bool ascii_isspace(unsigned char c) {
106
0
  return (ascii_internal::kPropertyBits[c] & 0x08) != 0;
107
0
}
108
109
// ascii_ispunct()
110
//
111
// Determines whether the given character is a punctuation character.
112
0
inline bool ascii_ispunct(unsigned char c) {
113
0
  return (ascii_internal::kPropertyBits[c] & 0x10) != 0;
114
0
}
115
116
// ascii_isblank()
117
//
118
// Determines whether the given character is a blank character (tab or space).
119
0
inline bool ascii_isblank(unsigned char c) {
120
0
  return (ascii_internal::kPropertyBits[c] & 0x20) != 0;
121
0
}
122
123
// ascii_iscntrl()
124
//
125
// Determines whether the given character is a control character.
126
0
inline bool ascii_iscntrl(unsigned char c) {
127
0
  return (ascii_internal::kPropertyBits[c] & 0x40) != 0;
128
0
}
129
130
// ascii_isxdigit()
131
//
132
// Determines whether the given character can be represented as a hexadecimal
133
// digit character (i.e. {0-9} or {A-F} or {a-f}).
134
0
inline bool ascii_isxdigit(unsigned char c) {
135
0
  return (ascii_internal::kPropertyBits[c] & 0x80) != 0;
136
0
}
137
138
// ascii_isdigit()
139
//
140
// Determines whether the given character can be represented as a decimal
141
// digit character (i.e. {0-9}).
142
0
inline constexpr bool ascii_isdigit(unsigned char c) {
143
0
  return c >= '0' && c <= '9';
144
0
}
145
146
// ascii_isprint()
147
//
148
// Determines whether the given character is printable, including spaces.
149
0
inline constexpr bool ascii_isprint(unsigned char c) {
150
0
  return c >= 32 && c < 127;
151
0
}
152
153
// ascii_isgraph()
154
//
155
// Determines whether the given character has a graphical representation.
156
0
inline constexpr bool ascii_isgraph(unsigned char c) {
157
0
  return c > 32 && c < 127;
158
0
}
159
160
// ascii_isupper()
161
//
162
// Determines whether the given character is uppercase.
163
0
inline constexpr bool ascii_isupper(unsigned char c) {
164
0
  return c >= 'A' && c <= 'Z';
165
0
}
166
167
// ascii_islower()
168
//
169
// Determines whether the given character is lowercase.
170
0
inline constexpr bool ascii_islower(unsigned char c) {
171
0
  return c >= 'a' && c <= 'z';
172
0
}
173
174
// ascii_isascii()
175
//
176
// Determines whether the given character is ASCII.
177
0
inline constexpr bool ascii_isascii(unsigned char c) { return c < 128; }
178
179
// ascii_tolower()
180
//
181
// Returns an ASCII character, converting to lowercase if uppercase is
182
// passed. Note that character values > 127 are simply returned.
183
0
inline char ascii_tolower(unsigned char c) {
184
0
  return ascii_internal::kToLower[c];
185
0
}
186
187
// Converts the characters in `s` to lowercase, changing the contents of `s`.
188
void AsciiStrToLower(std::string* absl_nonnull s);
189
190
// Creates a lowercase string from a given absl::string_view.
191
0
[[nodiscard]] inline std::string AsciiStrToLower(absl::string_view s) {
192
0
  std::string result;
193
0
  StringResizeAndOverwrite(result, s.size(), [s](char* buf, size_t buf_size) {
194
0
    ascii_internal::AsciiStrToLower(buf, s.data(), s.size());
195
0
    return buf_size;
196
0
  });
197
0
  return result;
198
0
}
199
200
// Creates a lowercase string from a given std::string&&.
201
//
202
// (Template is used to lower priority of this overload.)
203
template <int&... DoNotSpecify>
204
[[nodiscard]] inline std::string AsciiStrToLower(std::string&& s) {
205
  std::string result = std::move(s);
206
  absl::AsciiStrToLower(&result);
207
  return result;
208
}
209
210
// ascii_toupper()
211
//
212
// Returns the ASCII character, converting to upper-case if lower-case is
213
// passed. Note that characters values > 127 are simply returned.
214
0
inline char ascii_toupper(unsigned char c) {
215
0
  return ascii_internal::kToUpper[c];
216
0
}
217
218
// Converts the characters in `s` to uppercase, changing the contents of `s`.
219
void AsciiStrToUpper(std::string* absl_nonnull s);
220
221
// Creates an uppercase string from a given absl::string_view.
222
0
[[nodiscard]] inline std::string AsciiStrToUpper(absl::string_view s) {
223
0
  std::string result;
224
0
  StringResizeAndOverwrite(result, s.size(), [s](char* buf, size_t buf_size) {
225
0
    ascii_internal::AsciiStrToUpper(buf, s.data(), s.size());
226
0
    return buf_size;
227
0
  });
228
0
  return result;
229
0
}
230
231
// Creates an uppercase string from a given std::string&&.
232
//
233
// (Template is used to lower priority of this overload.)
234
template <int&... DoNotSpecify>
235
[[nodiscard]] inline std::string AsciiStrToUpper(std::string&& s) {
236
  std::string result = std::move(s);
237
  absl::AsciiStrToUpper(&result);
238
  return result;
239
}
240
241
// Returns absl::string_view with whitespace stripped from the beginning of the
242
// given string_view.
243
[[nodiscard]] inline absl::string_view StripLeadingAsciiWhitespace(
244
0
    absl::string_view str ABSL_ATTRIBUTE_LIFETIME_BOUND) {
245
0
  auto it = std::find_if_not(str.begin(), str.end(), absl::ascii_isspace);
246
0
  return str.substr(static_cast<size_t>(it - str.begin()));
247
0
}
248
249
// Strips in place whitespace from the beginning of the given string.
250
0
inline void StripLeadingAsciiWhitespace(std::string* absl_nonnull str) {
251
0
  auto it = std::find_if_not(str->begin(), str->end(), absl::ascii_isspace);
252
0
  str->erase(str->begin(), it);
253
0
}
254
255
// Returns absl::string_view with whitespace stripped from the end of the given
256
// string_view.
257
[[nodiscard]] inline absl::string_view StripTrailingAsciiWhitespace(
258
0
    absl::string_view str ABSL_ATTRIBUTE_LIFETIME_BOUND) {
259
0
  auto it = std::find_if_not(str.rbegin(), str.rend(), absl::ascii_isspace);
260
0
  return str.substr(0, static_cast<size_t>(str.rend() - it));
261
0
}
262
263
// Strips in place whitespace from the end of the given string
264
0
inline void StripTrailingAsciiWhitespace(std::string* absl_nonnull str) {
265
0
  auto it = std::find_if_not(str->rbegin(), str->rend(), absl::ascii_isspace);
266
0
  str->erase(static_cast<size_t>(str->rend() - it));
267
0
}
268
269
// Returns absl::string_view with whitespace stripped from both ends of the
270
// given string_view.
271
[[nodiscard]] inline absl::string_view StripAsciiWhitespace(
272
0
    absl::string_view str ABSL_ATTRIBUTE_LIFETIME_BOUND) {
273
0
  return StripTrailingAsciiWhitespace(StripLeadingAsciiWhitespace(str));
274
0
}
275
276
// Strips in place whitespace from both ends of the given string
277
0
inline void StripAsciiWhitespace(std::string* absl_nonnull str) {
278
0
  StripTrailingAsciiWhitespace(str);
279
0
  StripLeadingAsciiWhitespace(str);
280
0
}
281
282
// Removes leading, trailing, and consecutive internal whitespace.
283
void RemoveExtraAsciiWhitespace(std::string* absl_nonnull str);
284
285
ABSL_NAMESPACE_END
286
}  // namespace absl
287
288
#endif  // ABSL_STRINGS_ASCII_H_