• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (C) 2011 The Libphonenumber 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 // http://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 // Author: Philippe Liard
16 
17 #ifndef I18N_PHONENUMBERS_STRINGUTIL_H_
18 #define I18N_PHONENUMBERS_STRINGUTIL_H_
19 
20 #include <cstddef>
21 #include <string>
22 #include <vector>
23 
24 #include "phonenumbers/base/basictypes.h"
25 #include "absl/strings/string_view.h"
26 #include "absl/strings/str_cat.h"
27 
28 namespace i18n {
29 namespace phonenumbers {
30 
31 using std::string;
32 using std::vector;
33 
34 // Supports string("hello") + 10.
35 string operator+(const string& s, int n);  // NOLINT(runtime/string)
36 
37 // Converts integer to string.
38 string SimpleItoa(uint64 n);
39 string SimpleItoa(int64 n);
40 string SimpleItoa(int n);
41 
42 // Returns whether the provided string starts with the supplied prefix.
43 bool HasPrefixString(const string& s, const string& prefix);
44 
45 // Returns the index of the nth occurence of c in s or string::npos if less than
46 // n occurrences are present.
47 size_t FindNth(const string& s, char c, int n);
48 
49 // Splits a string using a character delimiter. Appends the components to the
50 // provided vector. Note that empty tokens are ignored.
51 void SplitStringUsing(const string& s, char delimiter,
52                       vector<string>* result);
53 
54 // Returns true if 'in' starts with 'prefix' and writes 'in' minus 'prefix' into
55 // 'out'.
56 bool TryStripPrefixString(const string& in, const string& prefix, string* out);
57 
58 // Returns true if 's' ends with 'suffix'.
59 bool HasSuffixString(const string& s, const string& suffix);
60 
61 // Converts string to int32.
62 void safe_strto32(const string& s, int32 *n);
63 
64 // Converts string to uint64.
65 void safe_strtou64(const string& s, uint64 *n);
66 
67 // Converts string to int64.
68 void safe_strto64(const string& s, int64* n);
69 
70 // Remove all occurrences of a given set of characters from a string.
71 void strrmm(string* s, const string& chars);
72 
73 // Replaces all instances of 'substring' in 's' with 'replacement'. Returns the
74 // number of instances replaced. Replacements are not subject to re-matching.
75 int GlobalReplaceSubstring(const string& substring, const string& replacement,
76                            string* s);
77 
78 // An abstract to absl::AlphaNum type; AlphaNum has more accomidating
79 // constructors for more types.
80 class StringHolder: public absl::AlphaNum {
81  public:
82   // Don't make the constructors explicit to make the StrCat usage convenient.
83   StringHolder(const string& s);  // NOLINT(runtime/explicit)
84   StringHolder(const char* s);    // NOLINT(runtime/explicit)
85   StringHolder(uint64 n);         // NOLINT(runtime/explicit)
86   ~StringHolder();
87 
GetString()88   const absl::string_view GetString() const {
89     return Piece();
90   }
91 
GetCString()92   const char* GetCString() const {
93     return data();
94   }
95 
Length()96   size_t Length() const {
97     return size();
98   }
99 };
100 
101 string& operator+=(string& lhs, const StringHolder& rhs);
102 
103 // Efficient string concatenation.
104 
105 string StrCat(const StringHolder& s1, const StringHolder& s2);
106 
107 string StrCat(const StringHolder& s1, const StringHolder& s2,
108               const StringHolder& s3);
109 
110 string StrCat(const StringHolder& s1, const StringHolder& s2,
111               const StringHolder& s3, const StringHolder& s4);
112 
113 string StrCat(const StringHolder& s1, const StringHolder& s2,
114               const StringHolder& s3, const StringHolder& s4,
115               const StringHolder& s5);
116 
117 string StrCat(const StringHolder& s1, const StringHolder& s2,
118               const StringHolder& s3, const StringHolder& s4,
119               const StringHolder& s5, const StringHolder& s6);
120 
121 string StrCat(const StringHolder& s1, const StringHolder& s2,
122               const StringHolder& s3, const StringHolder& s4,
123               const StringHolder& s5, const StringHolder& s6,
124               const StringHolder& s7);
125 
126 string StrCat(const StringHolder& s1, const StringHolder& s2,
127               const StringHolder& s3, const StringHolder& s4,
128               const StringHolder& s5, const StringHolder& s6,
129               const StringHolder& s7, const StringHolder& s8);
130 
131 string StrCat(const StringHolder& s1, const StringHolder& s2,
132               const StringHolder& s3, const StringHolder& s4,
133               const StringHolder& s5, const StringHolder& s6,
134               const StringHolder& s7, const StringHolder& s8,
135               const StringHolder& s9);
136 
137 string StrCat(const StringHolder& s1, const StringHolder& s2,
138               const StringHolder& s3, const StringHolder& s4,
139               const StringHolder& s5, const StringHolder& s6,
140               const StringHolder& s7, const StringHolder& s8,
141               const StringHolder& s9, const StringHolder& s10,
142               const StringHolder& s11);
143 
144 string StrCat(const StringHolder& s1, const StringHolder& s2,
145               const StringHolder& s3, const StringHolder& s4,
146               const StringHolder& s5, const StringHolder& s6,
147               const StringHolder& s7, const StringHolder& s8,
148               const StringHolder& s9, const StringHolder& s10,
149               const StringHolder& s11, const StringHolder& s12);
150 
151 string StrCat(const StringHolder& s1, const StringHolder& s2,
152               const StringHolder& s3, const StringHolder& s4,
153               const StringHolder& s5, const StringHolder& s6,
154               const StringHolder& s7, const StringHolder& s8,
155               const StringHolder& s9, const StringHolder& s10,
156               const StringHolder& s11, const StringHolder& s12,
157               const StringHolder& s13);
158 
159 string StrCat(const StringHolder& s1, const StringHolder& s2,
160               const StringHolder& s3, const StringHolder& s4,
161               const StringHolder& s5, const StringHolder& s6,
162               const StringHolder& s7, const StringHolder& s8,
163               const StringHolder& s9, const StringHolder& s10,
164               const StringHolder& s11, const StringHolder& s12,
165               const StringHolder& s13, const StringHolder& s14);
166 
167 string StrCat(const StringHolder& s1, const StringHolder& s2,
168               const StringHolder& s3, const StringHolder& s4,
169               const StringHolder& s5, const StringHolder& s6,
170               const StringHolder& s7, const StringHolder& s8,
171               const StringHolder& s9, const StringHolder& s10,
172               const StringHolder& s11, const StringHolder& s12,
173               const StringHolder& s13, const StringHolder& s14,
174               const StringHolder& s15);
175 
176 string StrCat(const StringHolder& s1, const StringHolder& s2,
177               const StringHolder& s3, const StringHolder& s4,
178               const StringHolder& s5, const StringHolder& s6,
179               const StringHolder& s7, const StringHolder& s8,
180               const StringHolder& s9, const StringHolder& s10,
181               const StringHolder& s11, const StringHolder& s12,
182               const StringHolder& s13, const StringHolder& s14,
183               const StringHolder& s15, const StringHolder& s16);
184 
185 void StrAppend(string* dest, const StringHolder& s1);
186 
187 void StrAppend(string* dest, const StringHolder& s1, const StringHolder& s2);
188 
189 void StrAppend(string* dest, const StringHolder& s1, const StringHolder& s2,
190                const StringHolder& s3);
191 
192 void StrAppend(string* dest, const StringHolder& s1, const StringHolder& s2,
193                const StringHolder& s3, const StringHolder& s4);
194 
195 void StrAppend(string* dest, const StringHolder& s1, const StringHolder& s2,
196                const StringHolder& s3, const StringHolder& s4,
197                const StringHolder& s5);
198 
199 }  // namespace phonenumbers
200 }  // namespace i18n
201 
202 #endif  // I18N_PHONENUMBERS_STRINGUTIL_H_
203