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 // This file contains functions that remove a defined part from the string,
16 // i.e., strip the string.
17
18 #include "absl/strings/strip.h"
19
20 #include <cassert>
21 #include <cstdio>
22 #include <cstring>
23
24 #include "gmock/gmock.h"
25 #include "gtest/gtest.h"
26 #include "absl/strings/string_view.h"
27
28 namespace {
29
TEST(Strip,ConsumePrefixOneChar)30 TEST(Strip, ConsumePrefixOneChar) {
31 absl::string_view input("abc");
32 EXPECT_TRUE(absl::ConsumePrefix(&input, "a"));
33 EXPECT_EQ(input, "bc");
34
35 EXPECT_FALSE(absl::ConsumePrefix(&input, "x"));
36 EXPECT_EQ(input, "bc");
37
38 EXPECT_TRUE(absl::ConsumePrefix(&input, "b"));
39 EXPECT_EQ(input, "c");
40
41 EXPECT_TRUE(absl::ConsumePrefix(&input, "c"));
42 EXPECT_EQ(input, "");
43
44 EXPECT_FALSE(absl::ConsumePrefix(&input, "a"));
45 EXPECT_EQ(input, "");
46 }
47
TEST(Strip,ConsumePrefix)48 TEST(Strip, ConsumePrefix) {
49 absl::string_view input("abcdef");
50 EXPECT_FALSE(absl::ConsumePrefix(&input, "abcdefg"));
51 EXPECT_EQ(input, "abcdef");
52
53 EXPECT_FALSE(absl::ConsumePrefix(&input, "abce"));
54 EXPECT_EQ(input, "abcdef");
55
56 EXPECT_TRUE(absl::ConsumePrefix(&input, ""));
57 EXPECT_EQ(input, "abcdef");
58
59 EXPECT_FALSE(absl::ConsumePrefix(&input, "abcdeg"));
60 EXPECT_EQ(input, "abcdef");
61
62 EXPECT_TRUE(absl::ConsumePrefix(&input, "abcdef"));
63 EXPECT_EQ(input, "");
64
65 input = "abcdef";
66 EXPECT_TRUE(absl::ConsumePrefix(&input, "abcde"));
67 EXPECT_EQ(input, "f");
68 }
69
TEST(Strip,ConsumeSuffix)70 TEST(Strip, ConsumeSuffix) {
71 absl::string_view input("abcdef");
72 EXPECT_FALSE(absl::ConsumeSuffix(&input, "abcdefg"));
73 EXPECT_EQ(input, "abcdef");
74
75 EXPECT_TRUE(absl::ConsumeSuffix(&input, ""));
76 EXPECT_EQ(input, "abcdef");
77
78 EXPECT_TRUE(absl::ConsumeSuffix(&input, "def"));
79 EXPECT_EQ(input, "abc");
80
81 input = "abcdef";
82 EXPECT_FALSE(absl::ConsumeSuffix(&input, "abcdeg"));
83 EXPECT_EQ(input, "abcdef");
84
85 EXPECT_TRUE(absl::ConsumeSuffix(&input, "f"));
86 EXPECT_EQ(input, "abcde");
87
88 EXPECT_TRUE(absl::ConsumeSuffix(&input, "abcde"));
89 EXPECT_EQ(input, "");
90 }
91
TEST(Strip,StripPrefix)92 TEST(Strip, StripPrefix) {
93 const absl::string_view null_str;
94
95 EXPECT_EQ(absl::StripPrefix("foobar", "foo"), "bar");
96 EXPECT_EQ(absl::StripPrefix("foobar", ""), "foobar");
97 EXPECT_EQ(absl::StripPrefix("foobar", null_str), "foobar");
98 EXPECT_EQ(absl::StripPrefix("foobar", "foobar"), "");
99 EXPECT_EQ(absl::StripPrefix("foobar", "bar"), "foobar");
100 EXPECT_EQ(absl::StripPrefix("foobar", "foobarr"), "foobar");
101 EXPECT_EQ(absl::StripPrefix("", ""), "");
102 }
103
TEST(Strip,StripSuffix)104 TEST(Strip, StripSuffix) {
105 const absl::string_view null_str;
106
107 EXPECT_EQ(absl::StripSuffix("foobar", "bar"), "foo");
108 EXPECT_EQ(absl::StripSuffix("foobar", ""), "foobar");
109 EXPECT_EQ(absl::StripSuffix("foobar", null_str), "foobar");
110 EXPECT_EQ(absl::StripSuffix("foobar", "foobar"), "");
111 EXPECT_EQ(absl::StripSuffix("foobar", "foo"), "foobar");
112 EXPECT_EQ(absl::StripSuffix("foobar", "ffoobar"), "foobar");
113 EXPECT_EQ(absl::StripSuffix("", ""), "");
114 }
115
TEST(Strip,RemoveExtraAsciiWhitespace)116 TEST(Strip, RemoveExtraAsciiWhitespace) {
117 const char* inputs[] = {
118 "No extra space",
119 " Leading whitespace",
120 "Trailing whitespace ",
121 " Leading and trailing ",
122 " Whitespace \t in\v middle ",
123 "'Eeeeep! \n Newlines!\n",
124 "nospaces",
125 };
126 const char* outputs[] = {
127 "No extra space",
128 "Leading whitespace",
129 "Trailing whitespace",
130 "Leading and trailing",
131 "Whitespace in middle",
132 "'Eeeeep! Newlines!",
133 "nospaces",
134 };
135 int NUM_TESTS = 7;
136
137 for (int i = 0; i < NUM_TESTS; i++) {
138 std::string s(inputs[i]);
139 absl::RemoveExtraAsciiWhitespace(&s);
140 EXPECT_STREQ(outputs[i], s.c_str());
141 }
142
143 // Test that absl::RemoveExtraAsciiWhitespace returns immediately for empty
144 // strings (It was adding the \0 character to the C++ std::string, which broke
145 // tests involving empty())
146 std::string zero_string = "";
147 assert(zero_string.empty());
148 absl::RemoveExtraAsciiWhitespace(&zero_string);
149 EXPECT_EQ(zero_string.size(), 0);
150 EXPECT_TRUE(zero_string.empty());
151 }
152
TEST(Strip,StripTrailingAsciiWhitespace)153 TEST(Strip, StripTrailingAsciiWhitespace) {
154 std::string test = "foo ";
155 absl::StripTrailingAsciiWhitespace(&test);
156 EXPECT_EQ(test, "foo");
157
158 test = " ";
159 absl::StripTrailingAsciiWhitespace(&test);
160 EXPECT_EQ(test, "");
161
162 test = "";
163 absl::StripTrailingAsciiWhitespace(&test);
164 EXPECT_EQ(test, "");
165
166 test = " abc\t";
167 absl::StripTrailingAsciiWhitespace(&test);
168 EXPECT_EQ(test, " abc");
169 }
170
TEST(String,StripLeadingAsciiWhitespace)171 TEST(String, StripLeadingAsciiWhitespace) {
172 absl::string_view orig = "\t \n\f\r\n\vfoo";
173 EXPECT_EQ("foo", absl::StripLeadingAsciiWhitespace(orig));
174 orig = "\t \n\f\r\v\n\t \n\f\r\v\n";
175 EXPECT_EQ(absl::string_view(), absl::StripLeadingAsciiWhitespace(orig));
176 }
177
TEST(Strip,StripAsciiWhitespace)178 TEST(Strip, StripAsciiWhitespace) {
179 std::string test2 = "\t \f\r\n\vfoo \t\f\r\v\n";
180 absl::StripAsciiWhitespace(&test2);
181 EXPECT_EQ(test2, "foo");
182 std::string test3 = "bar";
183 absl::StripAsciiWhitespace(&test3);
184 EXPECT_EQ(test3, "bar");
185 std::string test4 = "\t \f\r\n\vfoo";
186 absl::StripAsciiWhitespace(&test4);
187 EXPECT_EQ(test4, "foo");
188 std::string test5 = "foo \t\f\r\v\n";
189 absl::StripAsciiWhitespace(&test5);
190 EXPECT_EQ(test5, "foo");
191 absl::string_view test6("\t \f\r\n\vfoo \t\f\r\v\n");
192 test6 = absl::StripAsciiWhitespace(test6);
193 EXPECT_EQ(test6, "foo");
194 test6 = absl::StripAsciiWhitespace(test6);
195 EXPECT_EQ(test6, "foo"); // already stripped
196 }
197
198 } // namespace
199