Philip Liard | dbb360c | 2011-02-25 09:02:23 +0000 | [diff] [blame^] | 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 | namespace i18n { |
| 24 | namespace phonenumbers { |
| 25 | |
| 26 | using std::string; |
| 27 | |
| 28 | // Support string("hello") + 10 |
| 29 | string operator+(const string& s, int n); |
| 30 | |
| 31 | // Convert integer into string |
| 32 | string SimpleItoa(int n); |
| 33 | |
| 34 | // Return true if 'in' starts with 'prefix' and write into 'out' |
| 35 | // 'in' minus 'prefix' |
| 36 | bool TryStripPrefixString(const string& in, const string& prefix, string* out); |
| 37 | |
| 38 | // Return true if 's' ends with 'suffix' |
| 39 | bool HasSuffixString(const string& s, const string& suffix); |
| 40 | |
| 41 | |
| 42 | // Hold a reference to a std::string or C string. |
| 43 | class StringHolder { |
| 44 | public: |
| 45 | // Don't make the constructors explicit to make the StrCat usage convenient. |
| 46 | StringHolder(const string& s); |
| 47 | StringHolder(const char* s); |
| 48 | ~StringHolder(); |
| 49 | |
| 50 | const string* GetString() const { |
| 51 | return string_; |
| 52 | } |
| 53 | |
| 54 | const char* GetCString() const { |
| 55 | return cstring_; |
| 56 | } |
| 57 | |
| 58 | size_t Length() const { |
| 59 | return len_; |
| 60 | } |
| 61 | |
| 62 | private: |
| 63 | const string* const string_; |
| 64 | const char* const cstring_; |
| 65 | const size_t len_; |
| 66 | }; |
| 67 | |
| 68 | |
| 69 | string& operator+=(string& lhs, const StringHolder& rhs); |
| 70 | |
| 71 | |
| 72 | // Efficient string concatenation |
| 73 | |
| 74 | string StrCat(const StringHolder& s1, const StringHolder& s2); |
| 75 | |
| 76 | string StrCat(const StringHolder& s1, const StringHolder& s2, |
| 77 | const StringHolder& s3); |
| 78 | |
| 79 | string StrCat(const StringHolder& s1, const StringHolder& s2, |
| 80 | const StringHolder& s3, const StringHolder& s4); |
| 81 | |
| 82 | string StrCat(const StringHolder& s1, const StringHolder& s2, |
| 83 | const StringHolder& s3, const StringHolder& s4, |
| 84 | const StringHolder& s5); |
| 85 | |
| 86 | string StrCat(const StringHolder& s1, const StringHolder& s2, |
| 87 | const StringHolder& s3, const StringHolder& s4, |
| 88 | const StringHolder& s5, const StringHolder& s6); |
| 89 | |
| 90 | string StrCat(const StringHolder& s1, const StringHolder& s2, |
| 91 | const StringHolder& s3, const StringHolder& s4, |
| 92 | const StringHolder& s5, const StringHolder& s6, |
| 93 | const StringHolder& s7, const StringHolder& s8, |
| 94 | const StringHolder& s9, const StringHolder& s10, |
| 95 | const StringHolder& s11); |
| 96 | |
| 97 | } // namespace phonenumbers |
| 98 | } // namespace i18n |
| 99 | |
| 100 | #endif // I18N_PHONENUMBERS_STRINGUTIL_H_ |