1 //
2 // Copyright 2017 The Abseil Authors.
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //      http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16 // -----------------------------------------------------------------------------
17 // File: strip.h
18 // -----------------------------------------------------------------------------
19 //
20 // This file contains various functions for stripping substrings from a string.
21 #ifndef S2_THIRD_PARTY_ABSL_STRINGS_STRIP_H_
22 #define S2_THIRD_PARTY_ABSL_STRINGS_STRIP_H_
23 
24 #include <cstddef>
25 #include <string>
26 
27 #include "s2/third_party/absl/base/macros.h"
28 #include "s2/third_party/absl/strings/ascii.h"
29 #include "s2/third_party/absl/strings/ascii_ctype.h"
30 #include "s2/third_party/absl/strings/match.h"
31 #include "s2/third_party/absl/strings/string_view.h"
32 
33 namespace absl {
34 
35 // ConsumePrefix()
36 //
37 // Strips the `expected` prefix from the start of the given string, returning
38 // `true` if the strip operation succeeded or false otherwise.
39 //
40 // Example:
41 //
42 //   absl::string_view input("abc");
43 //   EXPECT_TRUE(absl::ConsumePrefix(&input, "a"));
44 //   EXPECT_EQ(input, "bc");
ConsumePrefix(absl::string_view * str,absl::string_view expected)45 inline bool ConsumePrefix(absl::string_view* str, absl::string_view expected) {
46   if (!absl::StartsWith(*str, expected)) return false;
47   str->remove_prefix(expected.size());
48   return true;
49 }
50 // ConsumeSuffix()
51 //
52 // Strips the `expected` suffix from the end of the given string, returning
53 // `true` if the strip operation succeeded or false otherwise.
54 //
55 // Example:
56 //
57 //   absl::string_view input("abcdef");
58 //   EXPECT_TRUE(absl::ConsumeSuffix(&input, "def"));
59 //   EXPECT_EQ(input, "abc");
ConsumeSuffix(absl::string_view * str,absl::string_view expected)60 inline bool ConsumeSuffix(absl::string_view* str, absl::string_view expected) {
61   if (!absl::EndsWith(*str, expected)) return false;
62   str->remove_suffix(expected.size());
63   return true;
64 }
65 
66 // StripPrefix()
67 //
68 // Returns a view into the input string 'str' with the given 'prefix' removed,
69 // but leaving the original string intact. If the prefix does not match at the
70 // start of the string, returns the original string instead.
StripPrefix(absl::string_view str,absl::string_view prefix)71 ABSL_MUST_USE_RESULT inline absl::string_view StripPrefix(
72     absl::string_view str, absl::string_view prefix) {
73   if (absl::StartsWith(str, prefix)) str.remove_prefix(prefix.size());
74   return str;
75 }
76 
77 // StripSuffix()
78 //
79 // Returns a view into the input string 'str' with the given 'suffix' removed,
80 // but leaving the original string intact. If the suffix does not match at the
81 // end of the string, returns the original string instead.
StripSuffix(absl::string_view str,absl::string_view suffix)82 ABSL_MUST_USE_RESULT inline absl::string_view StripSuffix(
83     absl::string_view str, absl::string_view suffix) {
84   if (absl::EndsWith(str, suffix)) str.remove_suffix(suffix.size());
85   return str;
86 }
87 
88 }  // namespace absl
89 
90 
91 // Replaces any of the *bytes* in 'remove' with the *byte* 'replace_with'.
92 //
93 // *Warning*: This function operates on *bytes* in the remove string.
94 // When the remove string contains multi-byte (non-ASCII) characters,
95 // then some strings will turn into garbage which will break downstream code.
96 // Use icu::UnicodeSet and its spanUTF8()/spanBackUTF8().
97 void ReplaceCharacters(char* str, size_t len, absl::string_view remove,
98                        char replace_with);
99 void ReplaceCharacters(string* s, absl::string_view remove, char replace_with);
100 
101 // Replaces the character 'remove' with the character 'replace_with'.
102 //
ReplaceCharacter(char * str,size_t len,char remove,char replace_with)103 inline void ReplaceCharacter(char* str, size_t len, char remove,
104                              char replace_with) {
105   for (char* end = str + len; str != end; ++str) {
106     if (*str == remove) *str = replace_with;
107   }
108 }
109 
110 ABSL_DEPRECATED("Use absl::StripAsciiWhitespace() instead")
StripWhitespace(string * str)111 inline void StripWhitespace(string* str) { absl::StripAsciiWhitespace(str); }
112 
113 ABSL_DEPRECATED("Use absl::StripAsciiWhitespace() instead")
StripWhitespace(absl::string_view * str)114 inline void StripWhitespace(absl::string_view* str) {
115   *str = absl::StripAsciiWhitespace(*str);
116 }
117 
118 // Returns a pointer to the first character in 'str' that is not
119 // ASCII whitespace. Never returns nullptr. 'str' must be NUL-terminated.
SkipLeadingWhitespace(const char * str)120 inline const char* SkipLeadingWhitespace(const char* str) {
121   while (absl::ascii_isspace(*str)) ++str;
122   return str;
123 }
SkipLeadingWhitespace(char * str)124 inline char* SkipLeadingWhitespace(char* str) {
125   while (absl::ascii_isspace(*str)) ++str;
126   return str;
127 }
128 
129 
130 #endif  // S2_THIRD_PARTY_ABSL_STRINGS_STRIP_H_
131