Coverage Report

Created: 2026-10-28 06:49

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 <cstddef>
56
#include <string>
57
#include <utility>
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
void AsciiStrToLower(char* absl_nonnull dst, const char* absl_nullable src,
78
                     size_t n);
79
80
void AsciiStrToUpper(char* absl_nonnull dst, const char* absl_nullable src,
81
                     size_t n);
82
83
}  // namespace ascii_internal
84
85
// ascii_isalpha()
86
//
87
// Determines whether the given character is an alphabetic character.
88
0
inline bool ascii_isalpha(unsigned char c) {
89
0
  return (ascii_internal::kPropertyBits[c] & 0x01) != 0;
90
0
}
91
92
// ascii_isalnum()
93
//
94
// Determines whether the given character is an alphanumeric character.
95
0
inline bool ascii_isalnum(unsigned char c) {
96
0
  return (ascii_internal::kPropertyBits[c] & 0x04) != 0;
97
0
}
98
99
// ascii_isspace()
100
//
101
// Determines whether the given character is a whitespace character (space,
102
// tab, vertical tab, formfeed, linefeed, or carriage return).
103
0
inline bool ascii_isspace(unsigned char c) {
104
0
  return (ascii_internal::kPropertyBits[c] & 0x08) != 0;
105
0
}
106
107
// ascii_ispunct()
108
//
109
// Determines whether the given character is a punctuation character.
110
0
inline bool ascii_ispunct(unsigned char c) {
111
0
  return (ascii_internal::kPropertyBits[c] & 0x10) != 0;
112
0
}
113
114
// ascii_isblank()
115
//
116
// Determines whether the given character is a blank character (tab or space).
117
0
inline bool ascii_isblank(unsigned char c) {
118
0
  return (ascii_internal::kPropertyBits[c] & 0x20) != 0;
119
0
}
120
121
// ascii_iscntrl()
122
//
123
// Determines whether the given character is a control character.
124
0
inline bool ascii_iscntrl(unsigned char c) {
125
0
  return (ascii_internal::kPropertyBits[c] & 0x40) != 0;
126
0
}
127
128
// ascii_isxdigit()
129
//
130
// Determines whether the given character can be represented as a hexadecimal
131
// digit character (i.e. {0-9} or {A-F} or {a-f}).
132
0
inline bool ascii_isxdigit(unsigned char c) {
133
0
  return (ascii_internal::kPropertyBits[c] & 0x80) != 0;
134
0
}
135
136
// ascii_isdigit()
137
//
138
// Determines whether the given character can be represented as a decimal
139
// digit character (i.e. {0-9}).
140
0
inline constexpr bool ascii_isdigit(unsigned char c) {
141
0
  return c >= '0' && c <= '9';
142
0
}
143
144
// ascii_isprint()
145
//
146
// Determines whether the given character is printable, including spaces.
147
0
inline constexpr bool ascii_isprint(unsigned char c) {
148
0
  return c >= 32 && c < 127;
149
0
}
150
151
// ascii_isgraph()
152
//
153
// Determines whether the given character has a graphical representation.
154
0
inline constexpr bool ascii_isgraph(unsigned char c) {
155
0
  return c > 32 && c < 127;
156
0
}
157
158
// ascii_isupper()
159
//
160
// Determines whether the given character is uppercase.
161
0
inline constexpr bool ascii_isupper(unsigned char c) {
162
0
  return c >= 'A' && c <= 'Z';
163
0
}
164
165
// ascii_islower()
166
//
167
// Determines whether the given character is lowercase.
168
0
inline constexpr bool ascii_islower(unsigned char c) {
169
0
  return c >= 'a' && c <= 'z';
170
0
}
171
172
// ascii_isascii()
173
//
174
// Determines whether the given character is ASCII.
175
0
inline constexpr bool ascii_isascii(unsigned char c) { return c < 128; }
176
177
// ascii_tolower()
178
//
179
// Returns an ASCII character, converting to lowercase if uppercase is
180
// passed. Note that character values > 127 are simply returned.
181
0
inline char ascii_tolower(unsigned char c) {
182
0
  return ascii_internal::kToLower[c];
183
0
}
184
185
// Converts the characters in `s` to lowercase, changing the contents of `s`.
186
void AsciiStrToLower(std::string* absl_nonnull s);
187
188
// Creates a lowercase string from a given absl::string_view.
189
[[nodiscard]] std::string AsciiStrToLower(absl::string_view s);
190
191
// Creates a lowercase string from a given std::string&&.
192
//
193
// (Template is used to lower priority of this overload.)
194
template <int&... DoNotSpecify>
195
[[nodiscard]] inline std::string AsciiStrToLower(std::string&& s) {
196
  std::string result = std::move(s);
197
  absl::AsciiStrToLower(&result);
198
  return result;
199
}
200
201
// ascii_toupper()
202
//
203
// Returns the ASCII character, converting to upper-case if lower-case is
204
// passed. Note that characters values > 127 are simply returned.
205
0
inline char ascii_toupper(unsigned char c) {
206
0
  return ascii_internal::kToUpper[c];
207
0
}
208
209
// Converts the characters in `s` to uppercase, changing the contents of `s`.
210
void AsciiStrToUpper(std::string* absl_nonnull s);
211
212
// Creates an uppercase string from a given absl::string_view.
213
[[nodiscard]] std::string AsciiStrToUpper(absl::string_view s);
214
215
// Creates an uppercase string from a given std::string&&.
216
//
217
// (Template is used to lower priority of this overload.)
218
template <int&... DoNotSpecify>
219
[[nodiscard]] inline std::string AsciiStrToUpper(std::string&& s) {
220
  std::string result = std::move(s);
221
  absl::AsciiStrToUpper(&result);
222
  return result;
223
}
224
225
// Returns absl::string_view with whitespace stripped from the beginning of the
226
// given string_view.
227
[[nodiscard]] absl::string_view StripLeadingAsciiWhitespace(
228
    absl::string_view str ABSL_ATTRIBUTE_LIFETIME_BOUND);
229
230
// Strips in place whitespace from the beginning of the given string.
231
void StripLeadingAsciiWhitespace(std::string* absl_nonnull str);
232
233
// Returns absl::string_view with whitespace stripped from the end of the given
234
// string_view.
235
[[nodiscard]] absl::string_view StripTrailingAsciiWhitespace(
236
    absl::string_view str ABSL_ATTRIBUTE_LIFETIME_BOUND);
237
238
// Strips in place whitespace from the end of the given string
239
void StripTrailingAsciiWhitespace(std::string* absl_nonnull str);
240
241
// Returns absl::string_view with whitespace stripped from both ends of the
242
// given string_view.
243
[[nodiscard]] absl::string_view StripAsciiWhitespace(
244
    absl::string_view str ABSL_ATTRIBUTE_LIFETIME_BOUND);
245
246
// Strips in place whitespace from both ends of the given string
247
void StripAsciiWhitespace(std::string* absl_nonnull str);
248
249
// Removes leading, trailing, and consecutive internal whitespace.
250
void RemoveExtraAsciiWhitespace(std::string* absl_nonnull str);
251
252
ABSL_NAMESPACE_END
253
}  // namespace absl
254
255
#endif  // ABSL_STRINGS_ASCII_H_