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
26namespace i18n {
27namespace phonenumbers {
28
29using std::string;
30using std::vector;
31
32// Supports string("hello") + 10.
33string operator+(const string& s, int n);  // NOLINT(runtime/string)
34
35// Converts integer to string.
36string SimpleItoa(uint64 n);
37string SimpleItoa(int64 n);
38string SimpleItoa(int n);
39
40// Returns whether the provided string starts with the supplied prefix.
41bool HasPrefixString(const string& s, const string& prefix);
42
43// Returns the index of the nth occurence of c in s or string::npos if less than
44// n occurrences are present.
45size_t FindNth(const string& s, char c, int n);
46
47// Splits a string using a character delimiter. Appends the components to the
48// provided vector. Note that empty tokens are ignored.
49void SplitStringUsing(const string& s, const string& delimiter,
50                      vector<string>* result);
51
52// Replaces any occurrence of the character 'remove' (or the characters
53// in 'remove') with the character 'replacewith'.
54void StripString(string* s, const char* remove, char replacewith);
55
56// Returns true if 'in' starts with 'prefix' and writes 'in' minus 'prefix' into
57// 'out'.
58bool TryStripPrefixString(const string& in, const string& prefix, string* out);
59
60// Returns true if 's' ends with 'suffix'.
61bool HasSuffixString(const string& s, const string& suffix);
62
63// Converts string to int32.
64void safe_strto32(const string& s, int32 *n);
65
66// Converts string to uint64.
67void safe_strtou64(const string& s, uint64 *n);
68
69// Converts string to int64.
70void safe_strto64(const string& s, int64* n);
71
72// Remove all occurrences of a given set of characters from a string.
73void strrmm(string* s, const string& chars);
74
75// Replaces all instances of 'substring' in 's' with 'replacement'. Returns the
76// number of instances replaced. Replacements are not subject to re-matching.
77int GlobalReplaceSubstring(const string& substring, const string& replacement,
78                           string* s);
79
80// Holds a reference to a std::string or C string. It can also be constructed
81// from an integer which is converted to a string.
82class StringHolder {
83 public:
84  // Don't make the constructors explicit to make the StrCat usage convenient.
85  StringHolder(const string& s);  // NOLINT(runtime/explicit)
86  StringHolder(const char* s);    // NOLINT(runtime/explicit)
87  StringHolder(uint64 n);         // NOLINT(runtime/explicit)
88  ~StringHolder();
89
90  const string* GetString() const {
91    return string_;
92  }
93
94  const char* GetCString() const {
95    return cstring_;
96  }
97
98  size_t Length() const {
99    return len_;
100  }
101
102 private:
103  const string converted_string_;  // Stores the string converted from integer.
104  const string* const string_;
105  const char* const cstring_;
106  const size_t len_;
107};
108
109string& operator+=(string& lhs, const StringHolder& rhs);
110
111// Efficient string concatenation.
112
113string StrCat(const StringHolder& s1, const StringHolder& s2);
114
115string StrCat(const StringHolder& s1, const StringHolder& s2,
116              const StringHolder& s3);
117
118string StrCat(const StringHolder& s1, const StringHolder& s2,
119              const StringHolder& s3, const StringHolder& s4);
120
121string StrCat(const StringHolder& s1, const StringHolder& s2,
122              const StringHolder& s3, const StringHolder& s4,
123              const StringHolder& s5);
124
125string StrCat(const StringHolder& s1, const StringHolder& s2,
126              const StringHolder& s3, const StringHolder& s4,
127              const StringHolder& s5, const StringHolder& s6);
128
129string StrCat(const StringHolder& s1, const StringHolder& s2,
130              const StringHolder& s3, const StringHolder& s4,
131              const StringHolder& s5, const StringHolder& s6,
132              const StringHolder& s7);
133
134string StrCat(const StringHolder& s1, const StringHolder& s2,
135              const StringHolder& s3, const StringHolder& s4,
136              const StringHolder& s5, const StringHolder& s6,
137              const StringHolder& s7, const StringHolder& s8);
138
139string StrCat(const StringHolder& s1, const StringHolder& s2,
140              const StringHolder& s3, const StringHolder& s4,
141              const StringHolder& s5, const StringHolder& s6,
142              const StringHolder& s7, const StringHolder& s8,
143              const StringHolder& s9);
144
145string StrCat(const StringHolder& s1, const StringHolder& s2,
146              const StringHolder& s3, const StringHolder& s4,
147              const StringHolder& s5, const StringHolder& s6,
148              const StringHolder& s7, const StringHolder& s8,
149              const StringHolder& s9, const StringHolder& s10,
150              const StringHolder& s11);
151
152string StrCat(const StringHolder& s1, const StringHolder& s2,
153              const StringHolder& s3, const StringHolder& s4,
154              const StringHolder& s5, const StringHolder& s6,
155              const StringHolder& s7, const StringHolder& s8,
156              const StringHolder& s9, const StringHolder& s10,
157              const StringHolder& s11, const StringHolder& s12);
158
159void StrAppend(string* dest, const StringHolder& s1);
160
161void StrAppend(string* dest, const StringHolder& s1, const StringHolder& s2);
162
163void StrAppend(string* dest, const StringHolder& s1, const StringHolder& s2,
164               const StringHolder& s3);
165
166void StrAppend(string* dest, const StringHolder& s1, const StringHolder& s2,
167               const StringHolder& s3, const StringHolder& s4);
168
169void StrAppend(string* dest, const StringHolder& s1, const StringHolder& s2,
170               const StringHolder& s3, const StringHolder& s4,
171               const StringHolder& s5);
172
173}  // namespace phonenumbers
174}  // namespace i18n
175
176#endif  // I18N_PHONENUMBERS_STRINGUTIL_H_
177