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 // StringHolder class |
| 109 |
| 110 StringHolder::StringHolder(const string& s) : |
| 111 string_(&s), |
| 112 cstring_(NULL), |
| 113 len_(s.size()) |
| 114 {} |
| 115 |
| 116 StringHolder::StringHolder(const char* s) : |
| 117 string_(NULL), |
| 118 cstring_(s), |
| 119 len_(std::strlen(s)) |
| 120 {} |
| 121 |
| 122 StringHolder::StringHolder(uint64 n) : |
| 123 converted_string_(SimpleItoa(n)), |
| 124 string_(&converted_string_), |
| 125 cstring_(NULL), |
| 126 len_(converted_string_.length()) |
| 127 {} |
| 128 |
| 129 StringHolder::~StringHolder() {} |
| 130 |
| 131 // StrCat |
| 132 |
| 133 // Implements s += sh; (s: string, sh: StringHolder) |
| 134 string& operator+=(string& lhs, const StringHolder& rhs) { |
| 135 const string* const s = rhs.GetString(); |
| 136 if (s) { |
| 137 lhs += *s; |
| 138 } else { |
| 139 const char* const cs = rhs.GetCString(); |
| 140 if (cs) |
| 141 lhs.append(cs, rhs.Length()); |
| 142 } |
| 143 return lhs; |
| 144 } |
| 145 |
| 146 string StrCat(const StringHolder& s1, const StringHolder& s2) { |
| 147 string result; |
| 148 result.reserve(s1.Length() + s2.Length() + 1); |
| 149 |
| 150 result += s1; |
| 151 result += s2; |
| 152 |
| 153 return result; |
| 154 } |
| 155 |
| 156 string StrCat(const StringHolder& s1, const StringHolder& s2, |
| 157 const StringHolder& s3) { |
| 158 string result; |
| 159 result.reserve(s1.Length() + s2.Length() + s3.Length() + 1); |
| 160 |
| 161 result += s1; |
| 162 result += s2; |
| 163 result += s3; |
| 164 |
| 165 return result; |
| 166 } |
| 167 |
| 168 string StrCat(const StringHolder& s1, const StringHolder& s2, |
| 169 const StringHolder& s3, const StringHolder& s4) { |
| 170 string result; |
| 171 result.reserve(s1.Length() + s2.Length() + s3.Length() + s4.Length() + 1); |
| 172 |
| 173 result += s1; |
| 174 result += s2; |
| 175 result += s3; |
| 176 result += s4; |
| 177 |
| 178 return result; |
| 179 } |
| 180 |
| 181 string StrCat(const StringHolder& s1, const StringHolder& s2, |
| 182 const StringHolder& s3, const StringHolder& s4, |
| 183 const StringHolder& s5) { |
| 184 string result; |
| 185 result.reserve(s1.Length() + s2.Length() + s3.Length() + s4.Length() + |
| 186 s5.Length() + 1); |
| 187 result += s1; |
| 188 result += s2; |
| 189 result += s3; |
| 190 result += s4; |
| 191 result += s5; |
| 192 |
| 193 return result; |
| 194 } |
| 195 |
| 196 string StrCat(const StringHolder& s1, const StringHolder& s2, |
| 197 const StringHolder& s3, const StringHolder& s4, |
| 198 const StringHolder& s5, const StringHolder& s6) { |
| 199 string result; |
| 200 result.reserve(s1.Length() + s2.Length() + s3.Length() + s4.Length() + |
| 201 s5.Length() + s6.Length() + 1); |
| 202 result += s1; |
| 203 result += s2; |
| 204 result += s3; |
| 205 result += s4; |
| 206 result += s5; |
| 207 result += s6; |
| 208 |
| 209 return result; |
| 210 } |
| 211 |
| 212 string StrCat(const StringHolder& s1, const StringHolder& s2, |
| 213 const StringHolder& s3, const StringHolder& s4, |
| 214 const StringHolder& s5, const StringHolder& s6, |
| 215 const StringHolder& s7) { |
| 216 string result; |
| 217 result.reserve(s1.Length() + s2.Length() + s3.Length() + s4.Length() + |
| 218 s5.Length() + s6.Length() + s7.Length() + 1); |
| 219 result += s1; |
| 220 result += s2; |
| 221 result += s3; |
| 222 result += s4; |
| 223 result += s5; |
| 224 result += s6; |
| 225 result += s7; |
| 226 |
| 227 return result; |
| 228 } |
| 229 |
| 230 string StrCat(const StringHolder& s1, const StringHolder& s2, |
| 231 const StringHolder& s3, const StringHolder& s4, |
| 232 const StringHolder& s5, const StringHolder& s6, |
| 233 const StringHolder& s7, const StringHolder& s8, |
| 234 const StringHolder& s9, const StringHolder& s10, |
| 235 const StringHolder& s11) { |
| 236 string result; |
| 237 result.reserve(s1.Length() + s2.Length() + s3.Length() + s4.Length() + |
| 238 s5.Length() + s6.Length() + s7.Length() + s8.Length() + |
| 239 s9.Length() + s10.Length() + s11.Length()); |
| 240 result += s1; |
| 241 result += s2; |
| 242 result += s3; |
| 243 result += s4; |
| 244 result += s5; |
| 245 result += s6; |
| 246 result += s7; |
| 247 result += s8; |
| 248 result += s9; |
| 249 result += s10; |
| 250 result += s11; |
| 251 |
| 252 return result; |
| 253 } |
| 254 |
| 255 // StrAppend |
| 256 |
| 257 void StrAppend(string* dest, const StringHolder& s1) { |
| 258 assert(dest); |
| 259 |
| 260 dest->reserve(dest->length() + s1.Length() + 1); |
| 261 *dest += s1; |
| 262 } |
| 263 |
| 264 void StrAppend(string* dest, const StringHolder& s1, const StringHolder& s2) { |
| 265 assert(dest); |
| 266 |
| 267 dest->reserve(dest->length() + s1.Length() + s2.Length() + 1); |
| 268 *dest += s1; |
| 269 *dest += s2; |
| 270 } |
| 271 |
| 272 } // namespace phonenumbers |
| 273 } // namespace i18n |
OLD | NEW |