Home | History | Annotate | Download | only in phonenumbers
      1 // Copyright (C) 2011 The Libphonenumber Authors
      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 #include <vector>
     23 
     24 #include "phonenumbers/base/basictypes.h"
     25 
     26 namespace i18n {
     27 namespace phonenumbers {
     28 
     29 using std::string;
     30 using std::vector;
     31 
     32 // Supports string("hello") + 10.
     33 string operator+(const string& s, int n);  // NOLINT(runtime/string)
     34 
     35 // Converts integer to string.
     36 string SimpleItoa(uint64 n);
     37 string SimpleItoa(int64 n);
     38 string SimpleItoa(int n);
     39 
     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 
     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'.
     58 bool TryStripPrefixString(const string& in, const string& prefix, string* out);
     59 
     60 // Returns true if 's' ends with 'suffix'.
     61 bool HasSuffixString(const string& s, const string& suffix);
     62 
     63 // Converts string to int32.
     64 void safe_strto32(const string& s, int32 *n);
     65 
     66 // Converts string to uint64.
     67 void safe_strtou64(const string& s, uint64 *n);
     68 
     69 // Converts string to int64.
     70 void safe_strto64(const string& s, int64* n);
     71 
     72 // Remove all occurrences of a given set of characters from a string.
     73 void strrmm(string* s, const string& chars);
     74 
     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 
     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.
     82 class StringHolder {
     83  public:
     84   // Don't make the constructors explicit to make the StrCat usage convenient.
     85   StringHolder(const string& s);  // NOLINT(runtime/explicit)
     86   StringHolder(const char* s);    // NOLINT(runtime/explicit)
     87   StringHolder(uint64 n);         // NOLINT(runtime/explicit)
     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 
    102  private:
    103   const string converted_string_;  // Stores the string converted from integer.
    104   const string* const string_;
    105   const char* const cstring_;
    106   const size_t len_;
    107 };
    108 
    109 string& operator+=(string& lhs, const StringHolder& rhs);
    110 
    111 // Efficient string concatenation.
    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,
    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,
    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,
    142               const StringHolder& s7, const StringHolder& s8,
    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,
    149               const StringHolder& s9, const StringHolder& s10,
    150               const StringHolder& s11);
    151 
    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 
    159 void StrAppend(string* dest, const StringHolder& s1);
    160 
    161 void StrAppend(string* dest, const StringHolder& s1, const StringHolder& s2);
    162 
    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 
    173 }  // namespace phonenumbers
    174 }  // namespace i18n
    175 
    176 #endif  // I18N_PHONENUMBERS_STRINGUTIL_H_
    177