Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-12-16 09:41:01

0001 //
0002 // Copyright 2017 The Abseil Authors.
0003 //
0004 // Licensed under the Apache License, Version 2.0 (the "License");
0005 // you may not use this file except in compliance with the License.
0006 // You may obtain a copy of the License at
0007 //
0008 //      https://www.apache.org/licenses/LICENSE-2.0
0009 //
0010 // Unless required by applicable law or agreed to in writing, software
0011 // distributed under the License is distributed on an "AS IS" BASIS,
0012 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0013 // See the License for the specific language governing permissions and
0014 // limitations under the License.
0015 //
0016 // -----------------------------------------------------------------------------
0017 // File: ascii.h
0018 // -----------------------------------------------------------------------------
0019 //
0020 // This package contains functions operating on characters and strings
0021 // restricted to standard ASCII. These include character classification
0022 // functions analogous to those found in the ANSI C Standard Library <ctype.h>
0023 // header file.
0024 //
0025 // C++ implementations provide <ctype.h> functionality based on their
0026 // C environment locale. In general, reliance on such a locale is not ideal, as
0027 // the locale standard is problematic (and may not return invariant information
0028 // for the same character set, for example). These `ascii_*()` functions are
0029 // hard-wired for standard ASCII, much faster, and guaranteed to behave
0030 // consistently.  They will never be overloaded, nor will their function
0031 // signature change.
0032 //
0033 // `ascii_isalnum()`, `ascii_isalpha()`, `ascii_isascii()`, `ascii_isblank()`,
0034 // `ascii_iscntrl()`, `ascii_isdigit()`, `ascii_isgraph()`, `ascii_islower()`,
0035 // `ascii_isprint()`, `ascii_ispunct()`, `ascii_isspace()`, `ascii_isupper()`,
0036 // `ascii_isxdigit()`
0037 //   Analogous to the <ctype.h> functions with similar names, these
0038 //   functions take an unsigned char and return a bool, based on whether the
0039 //   character matches the condition specified.
0040 //
0041 //   If the input character has a numerical value greater than 127, these
0042 //   functions return `false`.
0043 //
0044 // `ascii_tolower()`, `ascii_toupper()`
0045 //   Analogous to the <ctype.h> functions with similar names, these functions
0046 //   take an unsigned char and return a char.
0047 //
0048 //   If the input character is not an ASCII {lower,upper}-case letter (including
0049 //   numerical values greater than 127) then the functions return the same value
0050 //   as the input character.
0051 
0052 #ifndef ABSL_STRINGS_ASCII_H_
0053 #define ABSL_STRINGS_ASCII_H_
0054 
0055 #include <algorithm>
0056 #include <cstddef>
0057 #include <string>
0058 
0059 #include "absl/base/attributes.h"
0060 #include "absl/base/config.h"
0061 #include "absl/base/nullability.h"
0062 #include "absl/strings/string_view.h"
0063 
0064 namespace absl {
0065 ABSL_NAMESPACE_BEGIN
0066 namespace ascii_internal {
0067 
0068 // Declaration for an array of bitfields holding character information.
0069 ABSL_DLL extern const unsigned char kPropertyBits[256];
0070 
0071 // Declaration for the array of characters to upper-case characters.
0072 ABSL_DLL extern const char kToUpper[256];
0073 
0074 // Declaration for the array of characters to lower-case characters.
0075 ABSL_DLL extern const char kToLower[256];
0076 
0077 }  // namespace ascii_internal
0078 
0079 // ascii_isalpha()
0080 //
0081 // Determines whether the given character is an alphabetic character.
0082 inline bool ascii_isalpha(unsigned char c) {
0083   return (ascii_internal::kPropertyBits[c] & 0x01) != 0;
0084 }
0085 
0086 // ascii_isalnum()
0087 //
0088 // Determines whether the given character is an alphanumeric character.
0089 inline bool ascii_isalnum(unsigned char c) {
0090   return (ascii_internal::kPropertyBits[c] & 0x04) != 0;
0091 }
0092 
0093 // ascii_isspace()
0094 //
0095 // Determines whether the given character is a whitespace character (space,
0096 // tab, vertical tab, formfeed, linefeed, or carriage return).
0097 inline bool ascii_isspace(unsigned char c) {
0098   return (ascii_internal::kPropertyBits[c] & 0x08) != 0;
0099 }
0100 
0101 // ascii_ispunct()
0102 //
0103 // Determines whether the given character is a punctuation character.
0104 inline bool ascii_ispunct(unsigned char c) {
0105   return (ascii_internal::kPropertyBits[c] & 0x10) != 0;
0106 }
0107 
0108 // ascii_isblank()
0109 //
0110 // Determines whether the given character is a blank character (tab or space).
0111 inline bool ascii_isblank(unsigned char c) {
0112   return (ascii_internal::kPropertyBits[c] & 0x20) != 0;
0113 }
0114 
0115 // ascii_iscntrl()
0116 //
0117 // Determines whether the given character is a control character.
0118 inline bool ascii_iscntrl(unsigned char c) {
0119   return (ascii_internal::kPropertyBits[c] & 0x40) != 0;
0120 }
0121 
0122 // ascii_isxdigit()
0123 //
0124 // Determines whether the given character can be represented as a hexadecimal
0125 // digit character (i.e. {0-9} or {A-F}).
0126 inline bool ascii_isxdigit(unsigned char c) {
0127   return (ascii_internal::kPropertyBits[c] & 0x80) != 0;
0128 }
0129 
0130 // ascii_isdigit()
0131 //
0132 // Determines whether the given character can be represented as a decimal
0133 // digit character (i.e. {0-9}).
0134 inline bool ascii_isdigit(unsigned char c) { return c >= '0' && c <= '9'; }
0135 
0136 // ascii_isprint()
0137 //
0138 // Determines whether the given character is printable, including spaces.
0139 inline bool ascii_isprint(unsigned char c) { return c >= 32 && c < 127; }
0140 
0141 // ascii_isgraph()
0142 //
0143 // Determines whether the given character has a graphical representation.
0144 inline bool ascii_isgraph(unsigned char c) { return c > 32 && c < 127; }
0145 
0146 // ascii_isupper()
0147 //
0148 // Determines whether the given character is uppercase.
0149 inline bool ascii_isupper(unsigned char c) { return c >= 'A' && c <= 'Z'; }
0150 
0151 // ascii_islower()
0152 //
0153 // Determines whether the given character is lowercase.
0154 inline bool ascii_islower(unsigned char c) { return c >= 'a' && c <= 'z'; }
0155 
0156 // ascii_isascii()
0157 //
0158 // Determines whether the given character is ASCII.
0159 inline bool ascii_isascii(unsigned char c) { return c < 128; }
0160 
0161 // ascii_tolower()
0162 //
0163 // Returns an ASCII character, converting to lowercase if uppercase is
0164 // passed. Note that character values > 127 are simply returned.
0165 inline char ascii_tolower(unsigned char c) {
0166   return ascii_internal::kToLower[c];
0167 }
0168 
0169 // Converts the characters in `s` to lowercase, changing the contents of `s`.
0170 void AsciiStrToLower(absl::Nonnull<std::string*> s);
0171 
0172 // Creates a lowercase string from a given absl::string_view.
0173 ABSL_MUST_USE_RESULT inline std::string AsciiStrToLower(absl::string_view s) {
0174   std::string result(s);
0175   absl::AsciiStrToLower(&result);
0176   return result;
0177 }
0178 
0179 // ascii_toupper()
0180 //
0181 // Returns the ASCII character, converting to upper-case if lower-case is
0182 // passed. Note that characters values > 127 are simply returned.
0183 inline char ascii_toupper(unsigned char c) {
0184   return ascii_internal::kToUpper[c];
0185 }
0186 
0187 // Converts the characters in `s` to uppercase, changing the contents of `s`.
0188 void AsciiStrToUpper(absl::Nonnull<std::string*> s);
0189 
0190 // Creates an uppercase string from a given absl::string_view.
0191 ABSL_MUST_USE_RESULT inline std::string AsciiStrToUpper(absl::string_view s) {
0192   std::string result(s);
0193   absl::AsciiStrToUpper(&result);
0194   return result;
0195 }
0196 
0197 // Returns absl::string_view with whitespace stripped from the beginning of the
0198 // given string_view.
0199 ABSL_MUST_USE_RESULT inline absl::string_view StripLeadingAsciiWhitespace(
0200     absl::string_view str) {
0201   auto it = std::find_if_not(str.begin(), str.end(), absl::ascii_isspace);
0202   return str.substr(static_cast<size_t>(it - str.begin()));
0203 }
0204 
0205 // Strips in place whitespace from the beginning of the given string.
0206 inline void StripLeadingAsciiWhitespace(absl::Nonnull<std::string*> str) {
0207   auto it = std::find_if_not(str->begin(), str->end(), absl::ascii_isspace);
0208   str->erase(str->begin(), it);
0209 }
0210 
0211 // Returns absl::string_view with whitespace stripped from the end of the given
0212 // string_view.
0213 ABSL_MUST_USE_RESULT inline absl::string_view StripTrailingAsciiWhitespace(
0214     absl::string_view str) {
0215   auto it = std::find_if_not(str.rbegin(), str.rend(), absl::ascii_isspace);
0216   return str.substr(0, static_cast<size_t>(str.rend() - it));
0217 }
0218 
0219 // Strips in place whitespace from the end of the given string
0220 inline void StripTrailingAsciiWhitespace(absl::Nonnull<std::string*> str) {
0221   auto it = std::find_if_not(str->rbegin(), str->rend(), absl::ascii_isspace);
0222   str->erase(static_cast<size_t>(str->rend() - it));
0223 }
0224 
0225 // Returns absl::string_view with whitespace stripped from both ends of the
0226 // given string_view.
0227 ABSL_MUST_USE_RESULT inline absl::string_view StripAsciiWhitespace(
0228     absl::string_view str) {
0229   return StripTrailingAsciiWhitespace(StripLeadingAsciiWhitespace(str));
0230 }
0231 
0232 // Strips in place whitespace from both ends of the given string
0233 inline void StripAsciiWhitespace(absl::Nonnull<std::string*> str) {
0234   StripTrailingAsciiWhitespace(str);
0235   StripLeadingAsciiWhitespace(str);
0236 }
0237 
0238 // Removes leading, trailing, and consecutive internal whitespace.
0239 void RemoveExtraAsciiWhitespace(absl::Nonnull<std::string*> str);
0240 
0241 ABSL_NAMESPACE_END
0242 }  // namespace absl
0243 
0244 #endif  // ABSL_STRINGS_ASCII_H_