strip_test.cc
Go to the documentation of this file.
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"
27 
28 namespace {
29 
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 
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 
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 
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 
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 
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]);
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 
154  std::string test = "foo ";
156  EXPECT_EQ(test, "foo");
157 
158  test = " ";
160  EXPECT_EQ(test, "");
161 
162  test = "";
164  EXPECT_EQ(test, "");
165 
166  test = " abc\t";
168  EXPECT_EQ(test, " abc");
169 }
170 
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";
176 }
177 
178 TEST(Strip, StripAsciiWhitespace) {
179  std::string test2 = "\t \f\r\n\vfoo \t\f\r\v\n";
181  EXPECT_EQ(test2, "foo");
182  std::string test3 = "bar";
184  EXPECT_EQ(test3, "bar");
185  std::string test4 = "\t \f\r\n\vfoo";
187  EXPECT_EQ(test4, "foo");
188  std::string test5 = "foo \t\f\r\v\n";
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
ABSL_MUST_USE_RESULT absl::string_view StripPrefix(absl::string_view str, absl::string_view prefix)
Definition: strip.h:70
void RemoveExtraAsciiWhitespace(std::string *str)
Definition: ascii.cc:169
bool ConsumePrefix(absl::string_view *str, absl::string_view expected)
Definition: strip.h:44
ABSL_MUST_USE_RESULT absl::string_view StripAsciiWhitespace(absl::string_view str)
Definition: ascii.h:223
ABSL_MUST_USE_RESULT absl::string_view StripSuffix(absl::string_view str, absl::string_view suffix)
Definition: strip.h:81
ABSL_MUST_USE_RESULT absl::string_view StripLeadingAsciiWhitespace(absl::string_view str)
Definition: ascii.h:195
TEST(Symbolize, Unimplemented)
bool ConsumeSuffix(absl::string_view *str, absl::string_view expected)
Definition: strip.h:59
ABSL_MUST_USE_RESULT absl::string_view StripTrailingAsciiWhitespace(absl::string_view str)
Definition: ascii.h:209


abseil_cpp
Author(s):
autogenerated on Tue Jun 18 2019 19:44:37