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 #include <cassert> |
| 18 #include <cstring> |
| 19 #include <sstream> |
| 20 |
| 21 #include "stringutil.h" |
| 22 |
| 23 namespace i18n { |
| 24 namespace phonenumbers { |
| 25 |
| 26 using std::stringstream; |
| 27 |
| 28 string operator+(const string& s, int n) { |
| 29 stringstream stream; |
| 30 |
| 31 stream << s << n; |
| 32 string result; |
| 33 stream >> result; |
| 34 |
| 35 return result; |
| 36 } |
| 37 |
| 38 template <typename T> |
| 39 string GenericSimpleItoa(const T& n) { |
| 40 stringstream stream; |
| 41 |
| 42 stream << n; |
| 43 string result; |
| 44 stream >> result; |
| 45 |
| 46 return result; |
| 47 } |
| 48 |
| 49 string SimpleItoa(int n) { |
| 50 return GenericSimpleItoa(n); |
| 51 } |
| 52 |
| 53 string SimpleItoa(uint64 n) { |
| 54 return GenericSimpleItoa(n); |
| 55 } |
| 56 |
| 57 void StripString(string* s, const char* remove, char replacewith) { |
| 58 const char* str_start = s->c_str(); |
| 59 const char* str = str_start; |
| 60 for (str = strpbrk(str, remove); |
| 61 str != NULL; |
| 62 str = strpbrk(str + 1, remove)) { |
| 63 (*s)[str - str_start] = replacewith; |
| 64 } |
| 65 } |
| 66 |
| 67 bool TryStripPrefixString(const string& in, const string& prefix, string* out) { |
| 68 assert(out); |
| 69 const bool has_prefix = in.compare(0, prefix.length(), prefix) == 0; |
| 70 out->assign(has_prefix ? in.substr(prefix.length()) : in); |
| 71 |
| 72 return has_prefix; |
| 73 } |
| 74 |
| 75 bool HasSuffixString(const string& s, const string& suffix) { |
| 76 if (s.length() < suffix.length()) { |
| 77 return false; |
| 78 } |
| 79 return s.compare(s.length() - suffix.length(), suffix.length(), suffix) == 0; |
| 80 } |
| 81 |
| 82 template <typename T> |
| 83 void GenericAtoi(const string& s, T* out) { |
| 84 stringstream stream; |
| 85 stream << s; |
| 86 stream >> *out; |
| 87 } |
| 88 |
| 89 void safe_strto32(const string& s, int32 *n) { |
| 90 GenericAtoi(s, n); |
| 91 } |
| 92 |
| 93 void safe_strtou64(const string& s, uint64 *n) { |
| 94 GenericAtoi(s, n); |
| 95 } |
| 96 |
| 97 void strrmm(string* s, const string& chars) { |
| 98 for (string::iterator it = s->begin(); it != s->end(); ) { |
| 99 const char current_char = *it; |
| 100 if (chars.find(current_char) != string::npos) { |
| 101 it = s->erase(it); |
| 102 } else { |
| 103 ++it; |
| 104 } |
| 105 } |
| 106 } |
| 107 |
| 108 int GlobalReplaceSubstring(const string& substring, |
| 109 const string& replacement, |
| 110 string* s) { |
| 111 assert(s != NULL); |
| 112 if (s->empty() || substring.empty()) |
| 113 return 0; |
| 114 string tmp; |
| 115 int num_replacements = 0; |
| 116 int pos = 0; |
| 117 for (size_t match_pos = s->find(substring.data(), pos, substring.length()); |
| 118 match_pos != string::npos; |
| 119 pos = match_pos + substring.length(), |
| 120 match_pos = s->find(substring.data(), pos, substring.length())) { |
| 121 ++num_replacements; |
| 122 // Append the original content before the match. |
| 123 tmp.append(*s, pos, match_pos - pos); |
| 124 // Append the replacement for the match. |
| 125 tmp.append(replacement.begin(), replacement.end()); |
| 126 } |
| 127 // Append the content after the last match. |
| 128 tmp.append(*s, pos, s->length() - pos); |
| 129 s->swap(tmp); |
| 130 return num_replacements; |
| 131 } |
| 132 |
| 133 // StringHolder class |
| 134 |
| 135 StringHolder::StringHolder(const string& s) : |
| 136 string_(&s), |
| 137 cstring_(NULL), |
| 138 len_(s.size()) |
| 139 {} |
| 140 |
| 141 StringHolder::StringHolder(const char* s) : |
| 142 string_(NULL), |
| 143 cstring_(s), |
| 144 len_(std::strlen(s)) |
| 145 {} |
| 146 |
| 147 StringHolder::StringHolder(uint64 n) : |
| 148 converted_string_(SimpleItoa(n)), |
| 149 string_(&converted_string_), |
| 150 cstring_(NULL), |
| 151 len_(converted_string_.length()) |
| 152 {} |
| 153 |
| 154 StringHolder::~StringHolder() {} |
| 155 |
| 156 // StrCat |
| 157 |
| 158 // Implements s += sh; (s: string, sh: StringHolder) |
| 159 string& operator+=(string& lhs, const StringHolder& rhs) { |
| 160 const string* const s = rhs.GetString(); |
| 161 if (s) { |
| 162 lhs += *s; |
| 163 } else { |
| 164 const char* const cs = rhs.GetCString(); |
| 165 if (cs) |
| 166 lhs.append(cs, rhs.Length()); |
| 167 } |
| 168 return lhs; |
| 169 } |
| 170 |
| 171 string StrCat(const StringHolder& s1, const StringHolder& s2) { |
| 172 string result; |
| 173 result.reserve(s1.Length() + s2.Length() + 1); |
| 174 |
| 175 result += s1; |
| 176 result += s2; |
| 177 |
| 178 return result; |
| 179 } |
| 180 |
| 181 string StrCat(const StringHolder& s1, const StringHolder& s2, |
| 182 const StringHolder& s3) { |
| 183 string result; |
| 184 result.reserve(s1.Length() + s2.Length() + s3.Length() + 1); |
| 185 |
| 186 result += s1; |
| 187 result += s2; |
| 188 result += s3; |
| 189 |
| 190 return result; |
| 191 } |
| 192 |
| 193 string StrCat(const StringHolder& s1, const StringHolder& s2, |
| 194 const StringHolder& s3, const StringHolder& s4) { |
| 195 string result; |
| 196 result.reserve(s1.Length() + s2.Length() + s3.Length() + s4.Length() + 1); |
| 197 |
| 198 result += s1; |
| 199 result += s2; |
| 200 result += s3; |
| 201 result += s4; |
| 202 |
| 203 return result; |
| 204 } |
| 205 |
| 206 string StrCat(const StringHolder& s1, const StringHolder& s2, |
| 207 const StringHolder& s3, const StringHolder& s4, |
| 208 const StringHolder& s5) { |
| 209 string result; |
| 210 result.reserve(s1.Length() + s2.Length() + s3.Length() + s4.Length() + |
| 211 s5.Length() + 1); |
| 212 result += s1; |
| 213 result += s2; |
| 214 result += s3; |
| 215 result += s4; |
| 216 result += s5; |
| 217 |
| 218 return result; |
| 219 } |
| 220 |
| 221 string StrCat(const StringHolder& s1, const StringHolder& s2, |
| 222 const StringHolder& s3, const StringHolder& s4, |
| 223 const StringHolder& s5, const StringHolder& s6) { |
| 224 string result; |
| 225 result.reserve(s1.Length() + s2.Length() + s3.Length() + s4.Length() + |
| 226 s5.Length() + s6.Length() + 1); |
| 227 result += s1; |
| 228 result += s2; |
| 229 result += s3; |
| 230 result += s4; |
| 231 result += s5; |
| 232 result += s6; |
| 233 |
| 234 return result; |
| 235 } |
| 236 |
| 237 string StrCat(const StringHolder& s1, const StringHolder& s2, |
| 238 const StringHolder& s3, const StringHolder& s4, |
| 239 const StringHolder& s5, const StringHolder& s6, |
| 240 const StringHolder& s7) { |
| 241 string result; |
| 242 result.reserve(s1.Length() + s2.Length() + s3.Length() + s4.Length() + |
| 243 s5.Length() + s6.Length() + s7.Length() + 1); |
| 244 result += s1; |
| 245 result += s2; |
| 246 result += s3; |
| 247 result += s4; |
| 248 result += s5; |
| 249 result += s6; |
| 250 result += s7; |
| 251 |
| 252 return result; |
| 253 } |
| 254 |
| 255 string StrCat(const StringHolder& s1, const StringHolder& s2, |
| 256 const StringHolder& s3, const StringHolder& s4, |
| 257 const StringHolder& s5, const StringHolder& s6, |
| 258 const StringHolder& s7, const StringHolder& s8, |
| 259 const StringHolder& s9, const StringHolder& s10, |
| 260 const StringHolder& s11) { |
| 261 string result; |
| 262 result.reserve(s1.Length() + s2.Length() + s3.Length() + s4.Length() + |
| 263 s5.Length() + s6.Length() + s7.Length() + s8.Length() + |
| 264 s9.Length() + s10.Length() + s11.Length()); |
| 265 result += s1; |
| 266 result += s2; |
| 267 result += s3; |
| 268 result += s4; |
| 269 result += s5; |
| 270 result += s6; |
| 271 result += s7; |
| 272 result += s8; |
| 273 result += s9; |
| 274 result += s10; |
| 275 result += s11; |
| 276 |
| 277 return result; |
| 278 } |
| 279 |
| 280 // StrAppend |
| 281 |
| 282 void StrAppend(string* dest, const StringHolder& s1) { |
| 283 assert(dest); |
| 284 |
| 285 dest->reserve(dest->length() + s1.Length() + 1); |
| 286 *dest += s1; |
| 287 } |
| 288 |
| 289 void StrAppend(string* dest, const StringHolder& s1, const StringHolder& s2) { |
| 290 assert(dest); |
| 291 |
| 292 dest->reserve(dest->length() + s1.Length() + s2.Length() + 1); |
| 293 *dest += s1; |
| 294 *dest += s2; |
| 295 } |
| 296 |
| 297 } // namespace phonenumbers |
| 298 } // namespace i18n |
OLD | NEW |