OLD | NEW |
| (Empty) |
1 // Copyright (C) 2011 Google Inc. | |
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 | |
23 #include "base/basictypes.h" | |
24 | |
25 namespace i18n { | |
26 namespace phonenumbers { | |
27 | |
28 using std::string; | |
29 | |
30 // Supports string("hello") + 10. | |
31 string operator+(const string& s, int n); | |
32 | |
33 // Converts integer to string. | |
34 string SimpleItoa(uint64 n); | |
35 string SimpleItoa(int n); | |
36 | |
37 // Replaces any occurrence of the character 'remove' (or the characters | |
38 // in 'remove') with the character 'replacewith'. | |
39 void StripString(string* s, const char* remove, char replacewith); | |
40 | |
41 // Returns true if 'in' starts with 'prefix' and writes 'in' minus 'prefix' into | |
42 // 'out'. | |
43 bool TryStripPrefixString(const string& in, const string& prefix, string* out); | |
44 | |
45 // Returns true if 's' ends with 'suffix'. | |
46 bool HasSuffixString(const string& s, const string& suffix); | |
47 | |
48 // Converts string to int32. | |
49 void safe_strto32(const string& s, int32 *n); | |
50 | |
51 // Converts string to uint64. | |
52 void safe_strtou64(const string& s, uint64 *n); | |
53 | |
54 // Remove all occurrences of a given set of characters from a string. | |
55 void strrmm(string* s, const string& chars); | |
56 | |
57 // Replaces all instances of 'substring' in 's' with 'replacement'. Returns the | |
58 // number of instances replaced. Replacements are not subject to re-matching. | |
59 int GlobalReplaceSubstring(const string& substring, const string& replacement, | |
60 string* s); | |
61 | |
62 // Holds a reference to a std::string or C string. It can also be constructed | |
63 // from an integer which is converted to a string. | |
64 class StringHolder { | |
65 public: | |
66 // Don't make the constructors explicit to make the StrCat usage convenient. | |
67 StringHolder(const string& s); | |
68 StringHolder(const char* s); | |
69 StringHolder(uint64 n); | |
70 ~StringHolder(); | |
71 | |
72 const string* GetString() const { | |
73 return string_; | |
74 } | |
75 | |
76 const char* GetCString() const { | |
77 return cstring_; | |
78 } | |
79 | |
80 size_t Length() const { | |
81 return len_; | |
82 } | |
83 | |
84 private: | |
85 const string converted_string_; // Stores the string converted from integer. | |
86 const string* const string_; | |
87 const char* const cstring_; | |
88 const size_t len_; | |
89 }; | |
90 | |
91 string& operator+=(string& lhs, const StringHolder& rhs); | |
92 | |
93 // Efficient string concatenation. | |
94 | |
95 string StrCat(const StringHolder& s1, const StringHolder& s2); | |
96 | |
97 string StrCat(const StringHolder& s1, const StringHolder& s2, | |
98 const StringHolder& s3); | |
99 | |
100 string StrCat(const StringHolder& s1, const StringHolder& s2, | |
101 const StringHolder& s3, const StringHolder& s4); | |
102 | |
103 string StrCat(const StringHolder& s1, const StringHolder& s2, | |
104 const StringHolder& s3, const StringHolder& s4, | |
105 const StringHolder& s5); | |
106 | |
107 string StrCat(const StringHolder& s1, const StringHolder& s2, | |
108 const StringHolder& s3, const StringHolder& s4, | |
109 const StringHolder& s5, const StringHolder& s6); | |
110 | |
111 string StrCat(const StringHolder& s1, const StringHolder& s2, | |
112 const StringHolder& s3, const StringHolder& s4, | |
113 const StringHolder& s5, const StringHolder& s6, | |
114 const StringHolder& s7); | |
115 | |
116 string StrCat(const StringHolder& s1, const StringHolder& s2, | |
117 const StringHolder& s3, const StringHolder& s4, | |
118 const StringHolder& s5, const StringHolder& s6, | |
119 const StringHolder& s7, const StringHolder& s8, | |
120 const StringHolder& s9, const StringHolder& s10, | |
121 const StringHolder& s11); | |
122 | |
123 void StrAppend(string* dest, const StringHolder& s1); | |
124 | |
125 void StrAppend(string* dest, const StringHolder& s1, const StringHolder& s2); | |
126 | |
127 } // namespace phonenumbers | |
128 } // namespace i18n | |
129 | |
130 #endif // I18N_PHONENUMBERS_STRINGUTIL_H_ | |
OLD | NEW |