ascii.cc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. // Copyright 2017 The Abseil Authors.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // https://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #include "y_absl/strings/ascii.h"
  15. #include <climits>
  16. #include <cstring>
  17. #include <util/generic/string.h>
  18. namespace y_absl {
  19. Y_ABSL_NAMESPACE_BEGIN
  20. namespace ascii_internal {
  21. // # Table generated by this Python code (bit 0x02 is currently unused):
  22. // TODO(mbar) Move Python code for generation of table to BUILD and link here.
  23. // NOTE: The kAsciiPropertyBits table used within this code was generated by
  24. // Python code of the following form. (Bit 0x02 is currently unused and
  25. // available.)
  26. //
  27. // def Hex2(n):
  28. // return '0x' + hex(n/16)[2:] + hex(n%16)[2:]
  29. // def IsPunct(ch):
  30. // return (ord(ch) >= 32 and ord(ch) < 127 and
  31. // not ch.isspace() and not ch.isalnum())
  32. // def IsBlank(ch):
  33. // return ch in ' \t'
  34. // def IsCntrl(ch):
  35. // return ord(ch) < 32 or ord(ch) == 127
  36. // def IsXDigit(ch):
  37. // return ch.isdigit() or ch.lower() in 'abcdef'
  38. // for i in range(128):
  39. // ch = chr(i)
  40. // mask = ((ch.isalpha() and 0x01 or 0) |
  41. // (ch.isalnum() and 0x04 or 0) |
  42. // (ch.isspace() and 0x08 or 0) |
  43. // (IsPunct(ch) and 0x10 or 0) |
  44. // (IsBlank(ch) and 0x20 or 0) |
  45. // (IsCntrl(ch) and 0x40 or 0) |
  46. // (IsXDigit(ch) and 0x80 or 0))
  47. // print Hex2(mask) + ',',
  48. // if i % 16 == 7:
  49. // print ' //', Hex2(i & 0x78)
  50. // elif i % 16 == 15:
  51. // print
  52. // clang-format off
  53. // Array of bitfields holding character information. Each bit value corresponds
  54. // to a particular character feature. For readability, and because the value
  55. // of these bits is tightly coupled to this implementation, the individual bits
  56. // are not named. Note that bitfields for all characters above ASCII 127 are
  57. // zero-initialized.
  58. Y_ABSL_DLL const unsigned char kPropertyBits[256] = {
  59. 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, // 0x00
  60. 0x40, 0x68, 0x48, 0x48, 0x48, 0x48, 0x40, 0x40,
  61. 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, // 0x10
  62. 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,
  63. 0x28, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, // 0x20
  64. 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10,
  65. 0x84, 0x84, 0x84, 0x84, 0x84, 0x84, 0x84, 0x84, // 0x30
  66. 0x84, 0x84, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10,
  67. 0x10, 0x85, 0x85, 0x85, 0x85, 0x85, 0x85, 0x05, // 0x40
  68. 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05,
  69. 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, // 0x50
  70. 0x05, 0x05, 0x05, 0x10, 0x10, 0x10, 0x10, 0x10,
  71. 0x10, 0x85, 0x85, 0x85, 0x85, 0x85, 0x85, 0x05, // 0x60
  72. 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05,
  73. 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, // 0x70
  74. 0x05, 0x05, 0x05, 0x10, 0x10, 0x10, 0x10, 0x40,
  75. };
  76. // Array of characters for the ascii_tolower() function. For values 'A'
  77. // through 'Z', return the lower-case character; otherwise, return the
  78. // identity of the passed character.
  79. Y_ABSL_DLL const char kToLower[256] = {
  80. '\x00', '\x01', '\x02', '\x03', '\x04', '\x05', '\x06', '\x07',
  81. '\x08', '\x09', '\x0a', '\x0b', '\x0c', '\x0d', '\x0e', '\x0f',
  82. '\x10', '\x11', '\x12', '\x13', '\x14', '\x15', '\x16', '\x17',
  83. '\x18', '\x19', '\x1a', '\x1b', '\x1c', '\x1d', '\x1e', '\x1f',
  84. '\x20', '\x21', '\x22', '\x23', '\x24', '\x25', '\x26', '\x27',
  85. '\x28', '\x29', '\x2a', '\x2b', '\x2c', '\x2d', '\x2e', '\x2f',
  86. '\x30', '\x31', '\x32', '\x33', '\x34', '\x35', '\x36', '\x37',
  87. '\x38', '\x39', '\x3a', '\x3b', '\x3c', '\x3d', '\x3e', '\x3f',
  88. '\x40', 'a', 'b', 'c', 'd', 'e', 'f', 'g',
  89. 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
  90. 'p', 'q', 'r', 's', 't', 'u', 'v', 'w',
  91. 'x', 'y', 'z', '\x5b', '\x5c', '\x5d', '\x5e', '\x5f',
  92. '\x60', '\x61', '\x62', '\x63', '\x64', '\x65', '\x66', '\x67',
  93. '\x68', '\x69', '\x6a', '\x6b', '\x6c', '\x6d', '\x6e', '\x6f',
  94. '\x70', '\x71', '\x72', '\x73', '\x74', '\x75', '\x76', '\x77',
  95. '\x78', '\x79', '\x7a', '\x7b', '\x7c', '\x7d', '\x7e', '\x7f',
  96. '\x80', '\x81', '\x82', '\x83', '\x84', '\x85', '\x86', '\x87',
  97. '\x88', '\x89', '\x8a', '\x8b', '\x8c', '\x8d', '\x8e', '\x8f',
  98. '\x90', '\x91', '\x92', '\x93', '\x94', '\x95', '\x96', '\x97',
  99. '\x98', '\x99', '\x9a', '\x9b', '\x9c', '\x9d', '\x9e', '\x9f',
  100. '\xa0', '\xa1', '\xa2', '\xa3', '\xa4', '\xa5', '\xa6', '\xa7',
  101. '\xa8', '\xa9', '\xaa', '\xab', '\xac', '\xad', '\xae', '\xaf',
  102. '\xb0', '\xb1', '\xb2', '\xb3', '\xb4', '\xb5', '\xb6', '\xb7',
  103. '\xb8', '\xb9', '\xba', '\xbb', '\xbc', '\xbd', '\xbe', '\xbf',
  104. '\xc0', '\xc1', '\xc2', '\xc3', '\xc4', '\xc5', '\xc6', '\xc7',
  105. '\xc8', '\xc9', '\xca', '\xcb', '\xcc', '\xcd', '\xce', '\xcf',
  106. '\xd0', '\xd1', '\xd2', '\xd3', '\xd4', '\xd5', '\xd6', '\xd7',
  107. '\xd8', '\xd9', '\xda', '\xdb', '\xdc', '\xdd', '\xde', '\xdf',
  108. '\xe0', '\xe1', '\xe2', '\xe3', '\xe4', '\xe5', '\xe6', '\xe7',
  109. '\xe8', '\xe9', '\xea', '\xeb', '\xec', '\xed', '\xee', '\xef',
  110. '\xf0', '\xf1', '\xf2', '\xf3', '\xf4', '\xf5', '\xf6', '\xf7',
  111. '\xf8', '\xf9', '\xfa', '\xfb', '\xfc', '\xfd', '\xfe', '\xff',
  112. };
  113. // Array of characters for the ascii_toupper() function. For values 'a'
  114. // through 'z', return the upper-case character; otherwise, return the
  115. // identity of the passed character.
  116. Y_ABSL_DLL const char kToUpper[256] = {
  117. '\x00', '\x01', '\x02', '\x03', '\x04', '\x05', '\x06', '\x07',
  118. '\x08', '\x09', '\x0a', '\x0b', '\x0c', '\x0d', '\x0e', '\x0f',
  119. '\x10', '\x11', '\x12', '\x13', '\x14', '\x15', '\x16', '\x17',
  120. '\x18', '\x19', '\x1a', '\x1b', '\x1c', '\x1d', '\x1e', '\x1f',
  121. '\x20', '\x21', '\x22', '\x23', '\x24', '\x25', '\x26', '\x27',
  122. '\x28', '\x29', '\x2a', '\x2b', '\x2c', '\x2d', '\x2e', '\x2f',
  123. '\x30', '\x31', '\x32', '\x33', '\x34', '\x35', '\x36', '\x37',
  124. '\x38', '\x39', '\x3a', '\x3b', '\x3c', '\x3d', '\x3e', '\x3f',
  125. '\x40', '\x41', '\x42', '\x43', '\x44', '\x45', '\x46', '\x47',
  126. '\x48', '\x49', '\x4a', '\x4b', '\x4c', '\x4d', '\x4e', '\x4f',
  127. '\x50', '\x51', '\x52', '\x53', '\x54', '\x55', '\x56', '\x57',
  128. '\x58', '\x59', '\x5a', '\x5b', '\x5c', '\x5d', '\x5e', '\x5f',
  129. '\x60', 'A', 'B', 'C', 'D', 'E', 'F', 'G',
  130. 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
  131. 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',
  132. 'X', 'Y', 'Z', '\x7b', '\x7c', '\x7d', '\x7e', '\x7f',
  133. '\x80', '\x81', '\x82', '\x83', '\x84', '\x85', '\x86', '\x87',
  134. '\x88', '\x89', '\x8a', '\x8b', '\x8c', '\x8d', '\x8e', '\x8f',
  135. '\x90', '\x91', '\x92', '\x93', '\x94', '\x95', '\x96', '\x97',
  136. '\x98', '\x99', '\x9a', '\x9b', '\x9c', '\x9d', '\x9e', '\x9f',
  137. '\xa0', '\xa1', '\xa2', '\xa3', '\xa4', '\xa5', '\xa6', '\xa7',
  138. '\xa8', '\xa9', '\xaa', '\xab', '\xac', '\xad', '\xae', '\xaf',
  139. '\xb0', '\xb1', '\xb2', '\xb3', '\xb4', '\xb5', '\xb6', '\xb7',
  140. '\xb8', '\xb9', '\xba', '\xbb', '\xbc', '\xbd', '\xbe', '\xbf',
  141. '\xc0', '\xc1', '\xc2', '\xc3', '\xc4', '\xc5', '\xc6', '\xc7',
  142. '\xc8', '\xc9', '\xca', '\xcb', '\xcc', '\xcd', '\xce', '\xcf',
  143. '\xd0', '\xd1', '\xd2', '\xd3', '\xd4', '\xd5', '\xd6', '\xd7',
  144. '\xd8', '\xd9', '\xda', '\xdb', '\xdc', '\xdd', '\xde', '\xdf',
  145. '\xe0', '\xe1', '\xe2', '\xe3', '\xe4', '\xe5', '\xe6', '\xe7',
  146. '\xe8', '\xe9', '\xea', '\xeb', '\xec', '\xed', '\xee', '\xef',
  147. '\xf0', '\xf1', '\xf2', '\xf3', '\xf4', '\xf5', '\xf6', '\xf7',
  148. '\xf8', '\xf9', '\xfa', '\xfb', '\xfc', '\xfd', '\xfe', '\xff',
  149. };
  150. // clang-format on
  151. template <bool ToUpper>
  152. constexpr void AsciiStrCaseFold(char* p, char* end) {
  153. // The upper- and lowercase versions of ASCII characters differ by only 1 bit.
  154. // When we need to flip the case, we can xor with this bit to achieve the
  155. // desired result. Note that the choice of 'a' and 'A' here is arbitrary. We
  156. // could have chosen 'z' and 'Z', or any other pair of characters as they all
  157. // have the same single bit difference.
  158. constexpr unsigned char kAsciiCaseBitFlip = 'a' ^ 'A';
  159. constexpr char ch_a = ToUpper ? 'a' : 'A';
  160. constexpr char ch_z = ToUpper ? 'z' : 'Z';
  161. for (; p < end; ++p) {
  162. unsigned char v = static_cast<unsigned char>(*p);
  163. // We use & instead of && to ensure this always stays branchless
  164. // We use static_cast<int> to suppress -Wbitwise-instead-of-logical
  165. bool is_in_range = static_cast<bool>(static_cast<int>(ch_a <= v) &
  166. static_cast<int>(v <= ch_z));
  167. v ^= is_in_range ? kAsciiCaseBitFlip : 0;
  168. *p = static_cast<char>(v);
  169. }
  170. }
  171. static constexpr size_t ValidateAsciiCasefold() {
  172. constexpr size_t num_chars = 1 + CHAR_MAX - CHAR_MIN;
  173. size_t incorrect_index = 0;
  174. char lowered[num_chars] = {};
  175. char uppered[num_chars] = {};
  176. for (unsigned int i = 0; i < num_chars; ++i) {
  177. uppered[i] = lowered[i] = static_cast<char>(i);
  178. }
  179. AsciiStrCaseFold<false>(&lowered[0], &lowered[num_chars]);
  180. AsciiStrCaseFold<true>(&uppered[0], &uppered[num_chars]);
  181. for (size_t i = 0; i < num_chars; ++i) {
  182. const char ch = static_cast<char>(i),
  183. ch_upper = ('a' <= ch && ch <= 'z' ? 'A' + (ch - 'a') : ch),
  184. ch_lower = ('A' <= ch && ch <= 'Z' ? 'a' + (ch - 'A') : ch);
  185. if (uppered[i] != ch_upper || lowered[i] != ch_lower) {
  186. incorrect_index = i > 0 ? i : num_chars;
  187. break;
  188. }
  189. }
  190. return incorrect_index;
  191. }
  192. static_assert(ValidateAsciiCasefold() == 0, "error in case conversion");
  193. } // namespace ascii_internal
  194. void AsciiStrToLower(TString* s) {
  195. char* p = &(*s)[0]; // Guaranteed to be valid for empty strings
  196. return ascii_internal::AsciiStrCaseFold<false>(p, p + s->size());
  197. }
  198. void AsciiStrToUpper(TString* s) {
  199. char* p = &(*s)[0]; // Guaranteed to be valid for empty strings
  200. return ascii_internal::AsciiStrCaseFold<true>(p, p + s->size());
  201. }
  202. void RemoveExtraAsciiWhitespace(TString* str) {
  203. auto stripped = StripAsciiWhitespace(*str);
  204. if (stripped.empty()) {
  205. str->clear();
  206. return;
  207. }
  208. auto input_it = stripped.begin();
  209. auto input_end = stripped.end();
  210. auto output_it = &(*str)[0];
  211. bool is_ws = false;
  212. for (; input_it < input_end; ++input_it) {
  213. if (is_ws) {
  214. // Consecutive whitespace? Keep only the last.
  215. is_ws = y_absl::ascii_isspace(static_cast<unsigned char>(*input_it));
  216. if (is_ws) --output_it;
  217. } else {
  218. is_ws = y_absl::ascii_isspace(static_cast<unsigned char>(*input_it));
  219. }
  220. *output_it = *input_it;
  221. ++output_it;
  222. }
  223. str->erase(static_cast<size_t>(output_it - &(*str)[0]));
  224. }
  225. Y_ABSL_NAMESPACE_END
  226. } // namespace y_absl