Philippe Liard | b59d908 | 2011-08-18 11:41:24 +0000 | [diff] [blame] | 1 | // Copyright (C) 2011 The Libphonenumber Authors |
Philip Liard | dbb360c | 2011-02-25 09:02:23 +0000 | [diff] [blame] | 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> |
Philippe Liard | 7b30af6 | 2011-09-21 17:43:54 +0000 | [diff] [blame] | 22 | #include <vector> |
Philip Liard | dbb360c | 2011-02-25 09:02:23 +0000 | [diff] [blame] | 23 | |
Philippe Liard | 84ffc1d | 2013-04-30 11:35:55 +0000 | [diff] [blame] | 24 | #include "phonenumbers/base/basictypes.h" |
Philip Liard | d4e1c73 | 2011-02-25 15:51:21 +0000 | [diff] [blame] | 25 | |
Philip Liard | dbb360c | 2011-02-25 09:02:23 +0000 | [diff] [blame] | 26 | namespace i18n { |
| 27 | namespace phonenumbers { |
| 28 | |
| 29 | using std::string; |
Philippe Liard | 7b30af6 | 2011-09-21 17:43:54 +0000 | [diff] [blame] | 30 | using std::vector; |
Philip Liard | dbb360c | 2011-02-25 09:02:23 +0000 | [diff] [blame] | 31 | |
Philip Liard | 6f1ba2d | 2011-04-07 07:27:47 +0000 | [diff] [blame] | 32 | // Supports string("hello") + 10. |
Philippe Liard | 1b09e6d | 2012-05-23 09:20:57 +0000 | [diff] [blame] | 33 | string operator+(const string& s, int n); // NOLINT(runtime/string) |
Philip Liard | dbb360c | 2011-02-25 09:02:23 +0000 | [diff] [blame] | 34 | |
Philip Liard | 6f1ba2d | 2011-04-07 07:27:47 +0000 | [diff] [blame] | 35 | // Converts integer to string. |
Philip Liard | d4e1c73 | 2011-02-25 15:51:21 +0000 | [diff] [blame] | 36 | string SimpleItoa(uint64 n); |
Philippe Liard | 31b1598 | 2012-06-01 15:33:18 +0000 | [diff] [blame] | 37 | string SimpleItoa(int64 n); |
Philip Liard | dbb360c | 2011-02-25 09:02:23 +0000 | [diff] [blame] | 38 | string SimpleItoa(int n); |
| 39 | |
Philippe Liard | 7b30af6 | 2011-09-21 17:43:54 +0000 | [diff] [blame] | 40 | // Returns whether the provided string starts with the supplied prefix. |
| 41 | bool 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. |
| 45 | size_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. |
| 49 | void SplitStringUsing(const string& s, const string& delimiter, |
| 50 | vector<string>* result); |
| 51 | |
Philip Liard | 6f1ba2d | 2011-04-07 07:27:47 +0000 | [diff] [blame] | 52 | // Replaces any occurrence of the character 'remove' (or the characters |
| 53 | // in 'remove') with the character 'replacewith'. |
| 54 | void 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'. |
Philip Liard | dbb360c | 2011-02-25 09:02:23 +0000 | [diff] [blame] | 58 | bool TryStripPrefixString(const string& in, const string& prefix, string* out); |
| 59 | |
Philip Liard | 6f1ba2d | 2011-04-07 07:27:47 +0000 | [diff] [blame] | 60 | // Returns true if 's' ends with 'suffix'. |
Philip Liard | dbb360c | 2011-02-25 09:02:23 +0000 | [diff] [blame] | 61 | bool HasSuffixString(const string& s, const string& suffix); |
| 62 | |
Philip Liard | 6f1ba2d | 2011-04-07 07:27:47 +0000 | [diff] [blame] | 63 | // Converts string to int32. |
| 64 | void safe_strto32(const string& s, int32 *n); |
Philip Liard | dbb360c | 2011-02-25 09:02:23 +0000 | [diff] [blame] | 65 | |
Philip Liard | 6f1ba2d | 2011-04-07 07:27:47 +0000 | [diff] [blame] | 66 | // Converts string to uint64. |
| 67 | void safe_strtou64(const string& s, uint64 *n); |
| 68 | |
Philippe Liard | 31b1598 | 2012-06-01 15:33:18 +0000 | [diff] [blame] | 69 | // Converts string to int64. |
| 70 | void safe_strto64(const string& s, int64* n); |
| 71 | |
Philip Liard | e578692 | 2011-04-26 09:04:58 +0000 | [diff] [blame] | 72 | // Remove all occurrences of a given set of characters from a string. |
| 73 | void strrmm(string* s, const string& chars); |
| 74 | |
Philip Liard | 791ec35 | 2011-05-16 16:12:25 +0000 | [diff] [blame] | 75 | // Replaces all instances of 'substring' in 's' with 'replacement'. Returns the |
| 76 | // number of instances replaced. Replacements are not subject to re-matching. |
| 77 | int GlobalReplaceSubstring(const string& substring, const string& replacement, |
| 78 | string* s); |
| 79 | |
Philip Liard | 6f1ba2d | 2011-04-07 07:27:47 +0000 | [diff] [blame] | 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. |
Philip Liard | dbb360c | 2011-02-25 09:02:23 +0000 | [diff] [blame] | 82 | class StringHolder { |
Philippe Liard | 1b09e6d | 2012-05-23 09:20:57 +0000 | [diff] [blame] | 83 | public: |
Philip Liard | dbb360c | 2011-02-25 09:02:23 +0000 | [diff] [blame] | 84 | // Don't make the constructors explicit to make the StrCat usage convenient. |
Philippe Liard | 1b09e6d | 2012-05-23 09:20:57 +0000 | [diff] [blame] | 85 | StringHolder(const string& s); // NOLINT(runtime/explicit) |
| 86 | StringHolder(const char* s); // NOLINT(runtime/explicit) |
| 87 | StringHolder(uint64 n); // NOLINT(runtime/explicit) |
Philip Liard | dbb360c | 2011-02-25 09:02:23 +0000 | [diff] [blame] | 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 | |
Philippe Liard | 1b09e6d | 2012-05-23 09:20:57 +0000 | [diff] [blame] | 102 | private: |
Philip Liard | 6f1ba2d | 2011-04-07 07:27:47 +0000 | [diff] [blame] | 103 | const string converted_string_; // Stores the string converted from integer. |
Philip Liard | dbb360c | 2011-02-25 09:02:23 +0000 | [diff] [blame] | 104 | const string* const string_; |
| 105 | const char* const cstring_; |
| 106 | const size_t len_; |
| 107 | }; |
| 108 | |
Philip Liard | dbb360c | 2011-02-25 09:02:23 +0000 | [diff] [blame] | 109 | string& operator+=(string& lhs, const StringHolder& rhs); |
| 110 | |
Philip Liard | 6f1ba2d | 2011-04-07 07:27:47 +0000 | [diff] [blame] | 111 | // Efficient string concatenation. |
Philip Liard | dbb360c | 2011-02-25 09:02:23 +0000 | [diff] [blame] | 112 | |
| 113 | string StrCat(const StringHolder& s1, const StringHolder& s2); |
| 114 | |
| 115 | string StrCat(const StringHolder& s1, const StringHolder& s2, |
| 116 | const StringHolder& s3); |
| 117 | |
| 118 | string StrCat(const StringHolder& s1, const StringHolder& s2, |
| 119 | const StringHolder& s3, const StringHolder& s4); |
| 120 | |
| 121 | string StrCat(const StringHolder& s1, const StringHolder& s2, |
| 122 | const StringHolder& s3, const StringHolder& s4, |
| 123 | const StringHolder& s5); |
| 124 | |
| 125 | string StrCat(const StringHolder& s1, const StringHolder& s2, |
| 126 | const StringHolder& s3, const StringHolder& s4, |
| 127 | const StringHolder& s5, const StringHolder& s6); |
| 128 | |
| 129 | string StrCat(const StringHolder& s1, const StringHolder& s2, |
| 130 | const StringHolder& s3, const StringHolder& s4, |
| 131 | const StringHolder& s5, const StringHolder& s6, |
Philip Liard | 6f1ba2d | 2011-04-07 07:27:47 +0000 | [diff] [blame] | 132 | const StringHolder& s7); |
| 133 | |
| 134 | string StrCat(const StringHolder& s1, const StringHolder& s2, |
| 135 | const StringHolder& s3, const StringHolder& s4, |
| 136 | const StringHolder& s5, const StringHolder& s6, |
Philippe Liard | 7b30af6 | 2011-09-21 17:43:54 +0000 | [diff] [blame] | 137 | const StringHolder& s7, const StringHolder& s8); |
| 138 | |
| 139 | string StrCat(const StringHolder& s1, const StringHolder& s2, |
| 140 | const StringHolder& s3, const StringHolder& s4, |
| 141 | const StringHolder& s5, const StringHolder& s6, |
Philip Liard | dbb360c | 2011-02-25 09:02:23 +0000 | [diff] [blame] | 142 | const StringHolder& s7, const StringHolder& s8, |
Philippe Liard | 4567abf | 2011-08-12 07:44:38 +0000 | [diff] [blame] | 143 | const StringHolder& s9); |
| 144 | |
| 145 | string 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, |
Philip Liard | dbb360c | 2011-02-25 09:02:23 +0000 | [diff] [blame] | 149 | const StringHolder& s9, const StringHolder& s10, |
| 150 | const StringHolder& s11); |
| 151 | |
Lara Scheidegger | 7ae82ad | 2012-09-06 09:48:57 +0000 | [diff] [blame] | 152 | string 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 | |
Philip Liard | 6f1ba2d | 2011-04-07 07:27:47 +0000 | [diff] [blame] | 159 | void StrAppend(string* dest, const StringHolder& s1); |
| 160 | |
| 161 | void StrAppend(string* dest, const StringHolder& s1, const StringHolder& s2); |
| 162 | |
Philippe Liard | e55d384 | 2012-03-15 17:04:03 +0000 | [diff] [blame] | 163 | void StrAppend(string* dest, const StringHolder& s1, const StringHolder& s2, |
| 164 | const StringHolder& s3); |
| 165 | |
| 166 | void StrAppend(string* dest, const StringHolder& s1, const StringHolder& s2, |
| 167 | const StringHolder& s3, const StringHolder& s4); |
| 168 | |
| 169 | void StrAppend(string* dest, const StringHolder& s1, const StringHolder& s2, |
| 170 | const StringHolder& s3, const StringHolder& s4, |
| 171 | const StringHolder& s5); |
| 172 | |
Philip Liard | dbb360c | 2011-02-25 09:02:23 +0000 | [diff] [blame] | 173 | } // namespace phonenumbers |
| 174 | } // namespace i18n |
| 175 | |
| 176 | #endif // I18N_PHONENUMBERS_STRINGUTIL_H_ |