• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
15 #include "absl/strings/ascii.h"
16 
17 #include <algorithm>
18 #include <cctype>
19 #include <clocale>
20 #include <cstring>
21 #include <string>
22 
23 #include "gtest/gtest.h"
24 #include "absl/base/macros.h"
25 #include "absl/strings/string_view.h"
26 
27 namespace {
28 
TEST(AsciiIsFoo,All)29 TEST(AsciiIsFoo, All) {
30   for (int i = 0; i < 256; i++) {
31     const auto c = static_cast<unsigned char>(i);
32     if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'))
33       EXPECT_TRUE(absl::ascii_isalpha(c)) << ": failed on " << c;
34     else
35       EXPECT_TRUE(!absl::ascii_isalpha(c)) << ": failed on " << c;
36   }
37   for (int i = 0; i < 256; i++) {
38     const auto c = static_cast<unsigned char>(i);
39     if ((c >= '0' && c <= '9'))
40       EXPECT_TRUE(absl::ascii_isdigit(c)) << ": failed on " << c;
41     else
42       EXPECT_TRUE(!absl::ascii_isdigit(c)) << ": failed on " << c;
43   }
44   for (int i = 0; i < 256; i++) {
45     const auto c = static_cast<unsigned char>(i);
46     if (absl::ascii_isalpha(c) || absl::ascii_isdigit(c))
47       EXPECT_TRUE(absl::ascii_isalnum(c)) << ": failed on " << c;
48     else
49       EXPECT_TRUE(!absl::ascii_isalnum(c)) << ": failed on " << c;
50   }
51   for (int i = 0; i < 256; i++) {
52     const auto c = static_cast<unsigned char>(i);
53     if (i != '\0' && strchr(" \r\n\t\v\f", i))
54       EXPECT_TRUE(absl::ascii_isspace(c)) << ": failed on " << c;
55     else
56       EXPECT_TRUE(!absl::ascii_isspace(c)) << ": failed on " << c;
57   }
58   for (int i = 0; i < 256; i++) {
59     const auto c = static_cast<unsigned char>(i);
60     if (i >= 32 && i < 127)
61       EXPECT_TRUE(absl::ascii_isprint(c)) << ": failed on " << c;
62     else
63       EXPECT_TRUE(!absl::ascii_isprint(c)) << ": failed on " << c;
64   }
65   for (int i = 0; i < 256; i++) {
66     const auto c = static_cast<unsigned char>(i);
67     if (absl::ascii_isprint(c) && !absl::ascii_isspace(c) &&
68         !absl::ascii_isalnum(c)) {
69       EXPECT_TRUE(absl::ascii_ispunct(c)) << ": failed on " << c;
70     } else {
71       EXPECT_TRUE(!absl::ascii_ispunct(c)) << ": failed on " << c;
72     }
73   }
74   for (int i = 0; i < 256; i++) {
75     const auto c = static_cast<unsigned char>(i);
76     if (i == ' ' || i == '\t')
77       EXPECT_TRUE(absl::ascii_isblank(c)) << ": failed on " << c;
78     else
79       EXPECT_TRUE(!absl::ascii_isblank(c)) << ": failed on " << c;
80   }
81   for (int i = 0; i < 256; i++) {
82     const auto c = static_cast<unsigned char>(i);
83     if (i < 32 || i == 127)
84       EXPECT_TRUE(absl::ascii_iscntrl(c)) << ": failed on " << c;
85     else
86       EXPECT_TRUE(!absl::ascii_iscntrl(c)) << ": failed on " << c;
87   }
88   for (int i = 0; i < 256; i++) {
89     const auto c = static_cast<unsigned char>(i);
90     if (absl::ascii_isdigit(c) || (i >= 'A' && i <= 'F') ||
91         (i >= 'a' && i <= 'f')) {
92       EXPECT_TRUE(absl::ascii_isxdigit(c)) << ": failed on " << c;
93     } else {
94       EXPECT_TRUE(!absl::ascii_isxdigit(c)) << ": failed on " << c;
95     }
96   }
97   for (int i = 0; i < 256; i++) {
98     const auto c = static_cast<unsigned char>(i);
99     if (i > 32 && i < 127)
100       EXPECT_TRUE(absl::ascii_isgraph(c)) << ": failed on " << c;
101     else
102       EXPECT_TRUE(!absl::ascii_isgraph(c)) << ": failed on " << c;
103   }
104   for (int i = 0; i < 256; i++) {
105     const auto c = static_cast<unsigned char>(i);
106     if (i >= 'A' && i <= 'Z')
107       EXPECT_TRUE(absl::ascii_isupper(c)) << ": failed on " << c;
108     else
109       EXPECT_TRUE(!absl::ascii_isupper(c)) << ": failed on " << c;
110   }
111   for (int i = 0; i < 256; i++) {
112     const auto c = static_cast<unsigned char>(i);
113     if (i >= 'a' && i <= 'z')
114       EXPECT_TRUE(absl::ascii_islower(c)) << ": failed on " << c;
115     else
116       EXPECT_TRUE(!absl::ascii_islower(c)) << ": failed on " << c;
117   }
118   for (unsigned char c = 0; c < 128; c++) {
119     EXPECT_TRUE(absl::ascii_isascii(c)) << ": failed on " << c;
120   }
121   for (int i = 128; i < 256; i++) {
122     const auto c = static_cast<unsigned char>(i);
123     EXPECT_TRUE(!absl::ascii_isascii(c)) << ": failed on " << c;
124   }
125 }
126 
127 // Checks that absl::ascii_isfoo returns the same value as isfoo in the C
128 // locale.
TEST(AsciiIsFoo,SameAsIsFoo)129 TEST(AsciiIsFoo, SameAsIsFoo) {
130 #ifndef __ANDROID__
131   // temporarily change locale to C. It should already be C, but just for safety
132   const char* old_locale = setlocale(LC_CTYPE, "C");
133   ASSERT_TRUE(old_locale != nullptr);
134 #endif
135 
136   for (int i = 0; i < 256; i++) {
137     const auto c = static_cast<unsigned char>(i);
138     EXPECT_EQ(isalpha(c) != 0, absl::ascii_isalpha(c)) << c;
139     EXPECT_EQ(isdigit(c) != 0, absl::ascii_isdigit(c)) << c;
140     EXPECT_EQ(isalnum(c) != 0, absl::ascii_isalnum(c)) << c;
141     EXPECT_EQ(isspace(c) != 0, absl::ascii_isspace(c)) << c;
142     EXPECT_EQ(ispunct(c) != 0, absl::ascii_ispunct(c)) << c;
143     EXPECT_EQ(isblank(c) != 0, absl::ascii_isblank(c)) << c;
144     EXPECT_EQ(iscntrl(c) != 0, absl::ascii_iscntrl(c)) << c;
145     EXPECT_EQ(isxdigit(c) != 0, absl::ascii_isxdigit(c)) << c;
146     EXPECT_EQ(isprint(c) != 0, absl::ascii_isprint(c)) << c;
147     EXPECT_EQ(isgraph(c) != 0, absl::ascii_isgraph(c)) << c;
148     EXPECT_EQ(isupper(c) != 0, absl::ascii_isupper(c)) << c;
149     EXPECT_EQ(islower(c) != 0, absl::ascii_islower(c)) << c;
150     EXPECT_EQ(isascii(c) != 0, absl::ascii_isascii(c)) << c;
151   }
152 
153 #ifndef __ANDROID__
154   // restore the old locale.
155   ASSERT_TRUE(setlocale(LC_CTYPE, old_locale));
156 #endif
157 }
158 
TEST(AsciiToFoo,All)159 TEST(AsciiToFoo, All) {
160 #ifndef __ANDROID__
161   // temporarily change locale to C. It should already be C, but just for safety
162   const char* old_locale = setlocale(LC_CTYPE, "C");
163   ASSERT_TRUE(old_locale != nullptr);
164 #endif
165 
166   for (int i = 0; i < 256; i++) {
167     const auto c = static_cast<unsigned char>(i);
168     if (absl::ascii_islower(c))
169       EXPECT_EQ(absl::ascii_toupper(c), 'A' + (i - 'a')) << c;
170     else
171       EXPECT_EQ(absl::ascii_toupper(c), static_cast<char>(i)) << c;
172 
173     if (absl::ascii_isupper(c))
174       EXPECT_EQ(absl::ascii_tolower(c), 'a' + (i - 'A')) << c;
175     else
176       EXPECT_EQ(absl::ascii_tolower(c), static_cast<char>(i)) << c;
177 
178     // These CHECKs only hold in a C locale.
179     EXPECT_EQ(static_cast<char>(tolower(i)), absl::ascii_tolower(c)) << c;
180     EXPECT_EQ(static_cast<char>(toupper(i)), absl::ascii_toupper(c)) << c;
181   }
182 #ifndef __ANDROID__
183   // restore the old locale.
184   ASSERT_TRUE(setlocale(LC_CTYPE, old_locale));
185 #endif
186 }
187 
TEST(AsciiStrTo,Lower)188 TEST(AsciiStrTo, Lower) {
189   const char buf[] = "ABCDEF";
190   const std::string str("GHIJKL");
191   const std::string str2("MNOPQR");
192   const absl::string_view sp(str2);
193   std::string mutable_str("_`?@[{AMNOPQRSTUVWXYZ");
194 
195   EXPECT_EQ("abcdef", absl::AsciiStrToLower(buf));
196   EXPECT_EQ("ghijkl", absl::AsciiStrToLower(str));
197   EXPECT_EQ("mnopqr", absl::AsciiStrToLower(sp));
198 
199   absl::AsciiStrToLower(&mutable_str);
200   EXPECT_EQ("_`?@[{amnopqrstuvwxyz", mutable_str);
201 
202   char mutable_buf[] = "Mutable";
203   std::transform(mutable_buf, mutable_buf + strlen(mutable_buf),
204                  mutable_buf, absl::ascii_tolower);
205   EXPECT_STREQ("mutable", mutable_buf);
206 }
207 
TEST(AsciiStrTo,Upper)208 TEST(AsciiStrTo, Upper) {
209   const char buf[] = "abcdef";
210   const std::string str("ghijkl");
211   const std::string str2("_`?@[{amnopqrstuvwxyz");
212   const absl::string_view sp(str2);
213 
214   EXPECT_EQ("ABCDEF", absl::AsciiStrToUpper(buf));
215   EXPECT_EQ("GHIJKL", absl::AsciiStrToUpper(str));
216   EXPECT_EQ("_`?@[{AMNOPQRSTUVWXYZ", absl::AsciiStrToUpper(sp));
217 
218   char mutable_buf[] = "Mutable";
219   std::transform(mutable_buf, mutable_buf + strlen(mutable_buf),
220                  mutable_buf, absl::ascii_toupper);
221   EXPECT_STREQ("MUTABLE", mutable_buf);
222 }
223 
TEST(StripLeadingAsciiWhitespace,FromStringView)224 TEST(StripLeadingAsciiWhitespace, FromStringView) {
225   EXPECT_EQ(absl::string_view{},
226             absl::StripLeadingAsciiWhitespace(absl::string_view{}));
227   EXPECT_EQ("foo", absl::StripLeadingAsciiWhitespace({"foo"}));
228   EXPECT_EQ("foo", absl::StripLeadingAsciiWhitespace({"\t  \n\f\r\n\vfoo"}));
229   EXPECT_EQ("foo foo\n ",
230             absl::StripLeadingAsciiWhitespace({"\t  \n\f\r\n\vfoo foo\n "}));
231   EXPECT_EQ(absl::string_view{}, absl::StripLeadingAsciiWhitespace(
232                                      {"\t  \n\f\r\v\n\t  \n\f\r\v\n"}));
233 }
234 
TEST(StripLeadingAsciiWhitespace,InPlace)235 TEST(StripLeadingAsciiWhitespace, InPlace) {
236   std::string str;
237 
238   absl::StripLeadingAsciiWhitespace(&str);
239   EXPECT_EQ("", str);
240 
241   str = "foo";
242   absl::StripLeadingAsciiWhitespace(&str);
243   EXPECT_EQ("foo", str);
244 
245   str = "\t  \n\f\r\n\vfoo";
246   absl::StripLeadingAsciiWhitespace(&str);
247   EXPECT_EQ("foo", str);
248 
249   str = "\t  \n\f\r\n\vfoo foo\n ";
250   absl::StripLeadingAsciiWhitespace(&str);
251   EXPECT_EQ("foo foo\n ", str);
252 
253   str = "\t  \n\f\r\v\n\t  \n\f\r\v\n";
254   absl::StripLeadingAsciiWhitespace(&str);
255   EXPECT_EQ(absl::string_view{}, str);
256 }
257 
TEST(StripTrailingAsciiWhitespace,FromStringView)258 TEST(StripTrailingAsciiWhitespace, FromStringView) {
259   EXPECT_EQ(absl::string_view{},
260             absl::StripTrailingAsciiWhitespace(absl::string_view{}));
261   EXPECT_EQ("foo", absl::StripTrailingAsciiWhitespace({"foo"}));
262   EXPECT_EQ("foo", absl::StripTrailingAsciiWhitespace({"foo\t  \n\f\r\n\v"}));
263   EXPECT_EQ(" \nfoo foo",
264             absl::StripTrailingAsciiWhitespace({" \nfoo foo\t  \n\f\r\n\v"}));
265   EXPECT_EQ(absl::string_view{}, absl::StripTrailingAsciiWhitespace(
266                                      {"\t  \n\f\r\v\n\t  \n\f\r\v\n"}));
267 }
268 
TEST(StripTrailingAsciiWhitespace,InPlace)269 TEST(StripTrailingAsciiWhitespace, InPlace) {
270   std::string str;
271 
272   absl::StripTrailingAsciiWhitespace(&str);
273   EXPECT_EQ("", str);
274 
275   str = "foo";
276   absl::StripTrailingAsciiWhitespace(&str);
277   EXPECT_EQ("foo", str);
278 
279   str = "foo\t  \n\f\r\n\v";
280   absl::StripTrailingAsciiWhitespace(&str);
281   EXPECT_EQ("foo", str);
282 
283   str = " \nfoo foo\t  \n\f\r\n\v";
284   absl::StripTrailingAsciiWhitespace(&str);
285   EXPECT_EQ(" \nfoo foo", str);
286 
287   str = "\t  \n\f\r\v\n\t  \n\f\r\v\n";
288   absl::StripTrailingAsciiWhitespace(&str);
289   EXPECT_EQ(absl::string_view{}, str);
290 }
291 
TEST(StripAsciiWhitespace,FromStringView)292 TEST(StripAsciiWhitespace, FromStringView) {
293   EXPECT_EQ(absl::string_view{},
294             absl::StripAsciiWhitespace(absl::string_view{}));
295   EXPECT_EQ("foo", absl::StripAsciiWhitespace({"foo"}));
296   EXPECT_EQ("foo",
297             absl::StripAsciiWhitespace({"\t  \n\f\r\n\vfoo\t  \n\f\r\n\v"}));
298   EXPECT_EQ("foo foo", absl::StripAsciiWhitespace(
299                            {"\t  \n\f\r\n\vfoo foo\t  \n\f\r\n\v"}));
300   EXPECT_EQ(absl::string_view{},
301             absl::StripAsciiWhitespace({"\t  \n\f\r\v\n\t  \n\f\r\v\n"}));
302 }
303 
TEST(StripAsciiWhitespace,InPlace)304 TEST(StripAsciiWhitespace, InPlace) {
305   std::string str;
306 
307   absl::StripAsciiWhitespace(&str);
308   EXPECT_EQ("", str);
309 
310   str = "foo";
311   absl::StripAsciiWhitespace(&str);
312   EXPECT_EQ("foo", str);
313 
314   str = "\t  \n\f\r\n\vfoo\t  \n\f\r\n\v";
315   absl::StripAsciiWhitespace(&str);
316   EXPECT_EQ("foo", str);
317 
318   str = "\t  \n\f\r\n\vfoo foo\t  \n\f\r\n\v";
319   absl::StripAsciiWhitespace(&str);
320   EXPECT_EQ("foo foo", str);
321 
322   str = "\t  \n\f\r\v\n\t  \n\f\r\v\n";
323   absl::StripAsciiWhitespace(&str);
324   EXPECT_EQ(absl::string_view{}, str);
325 }
326 
TEST(RemoveExtraAsciiWhitespace,InPlace)327 TEST(RemoveExtraAsciiWhitespace, InPlace) {
328   const char* inputs[] = {"No extra space",
329                           "  Leading whitespace",
330                           "Trailing whitespace  ",
331                           "  Leading and trailing  ",
332                           " Whitespace \t  in\v   middle  ",
333                           "'Eeeeep!  \n Newlines!\n",
334                           "nospaces",
335                           "",
336                           "\n\t a\t\n\nb \t\n"};
337 
338   const char* outputs[] = {
339       "No extra space",
340       "Leading whitespace",
341       "Trailing whitespace",
342       "Leading and trailing",
343       "Whitespace in middle",
344       "'Eeeeep! Newlines!",
345       "nospaces",
346       "",
347       "a\nb",
348   };
349   const int NUM_TESTS = ABSL_ARRAYSIZE(inputs);
350 
351   for (int i = 0; i < NUM_TESTS; i++) {
352     std::string s(inputs[i]);
353     absl::RemoveExtraAsciiWhitespace(&s);
354     EXPECT_EQ(outputs[i], s);
355   }
356 }
357 
358 }  // namespace
359