• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_UNICODESTRING_H_
18 #define I18N_PHONENUMBERS_UNICODESTRING_H_
19 
20 #include "phonenumbers/utf/unicodetext.h"
21 
22 #include <cstring>
23 #include <limits>
24 
25 namespace i18n {
26 namespace phonenumbers {
27 
28 // This class supports the minimal subset of icu::UnicodeString needed by
29 // AsYouTypeFormatter in order to let the libphonenumber not depend on ICU
30 // which is not available by default on some systems, such as iOS.
31 class UnicodeString {
32  public:
UnicodeString()33   UnicodeString() : cached_index_(-1) {}
34 
35   // Constructs a new unicode string copying the provided C string.
UnicodeString(const char * utf8)36   explicit UnicodeString(const char* utf8)
37       : text_(UTF8ToUnicodeText(utf8, std::strlen(utf8))),
38         cached_index_(-1) {}
39 
40   // Constructs a new unicode string containing the provided codepoint.
UnicodeString(char32 codepoint)41   explicit UnicodeString(char32 codepoint) : cached_index_(-1) {
42     append(codepoint);
43   }
44 
UnicodeString(const UnicodeString & src)45   UnicodeString(const UnicodeString& src)
46       : text_(src.text_), cached_index_(-1) {}
47 
48   UnicodeString& operator=(const UnicodeString& src);
49 
50   bool operator==(const UnicodeString& rhs) const;
51 
52   void append(const UnicodeString& unicode_string);
53 
append(char32 codepoint)54   inline void append(char32 codepoint) {
55     invalidateCachedIndex();
56     text_.push_back(codepoint);
57   }
58 
59   typedef UnicodeText::const_iterator const_iterator;
60 
begin()61   inline const_iterator begin() const {
62     return text_.begin();
63   }
64 
end()65   inline const_iterator end() const {
66     return text_.end();
67   }
68 
69   // Returns the index of the provided codepoint or -1 if not found.
70   int indexOf(char32 codepoint) const;
71 
72   // Returns the number of codepoints contained in the unicode string.
length()73   inline int length() const {
74     return text_.size();
75   }
76 
77   // Clears the unicode string.
remove()78   inline void remove() {
79     invalidateCachedIndex();
80     text_.clear();
81   }
82 
83   // Replaces the substring located at [ start, start + length - 1 ] with the
84   // provided unicode string.
85   void replace(int start, int length, const UnicodeString& src);
86 
87   void setCharAt(int pos, char32 c);
88 
89   // Copies the provided C string.
setTo(const char * s,size_t len)90   inline void setTo(const char* s, size_t len) {
91     invalidateCachedIndex();
92     text_.CopyUTF8(s, len);
93   }
94 
95   // Returns the substring located at [ start, start + length - 1 ] without
96   // copying the underlying C string. If one of the provided parameters is out
97   // of range, the function returns an empty unicode string.
98   UnicodeString tempSubString(
99       int start,
100       int length = std::numeric_limits<int>::max()) const;
101 
toUTF8String(string & out)102   inline void toUTF8String(string& out) const {
103     out = UnicodeTextToUTF8(text_);
104   }
105 
106   char32 operator[](int index) const;
107 
108  private:
109   UnicodeText text_;
110 
111   // As UnicodeText doesn't provide random access, an operator[] implementation
112   // would naively iterate from the beginning of the string to the supplied
113   // index which would be inefficient.
114   // As operator[] is very likely to be called in a loop with consecutive
115   // indexes, we save the corresponding iterator so we can reuse it the next
116   // time it is called.
117 
118   // The following function which invalidates the cached index corresponding to
119   // the iterator position must be called every time the unicode string is
120   // modified (i.e. in all the non-const methods).
invalidateCachedIndex()121   inline void invalidateCachedIndex() {
122     cached_index_ = -1;
123   }
124 
125   // Iterator corresponding to the cached index below, used by operator[].
126   mutable UnicodeText::const_iterator cached_it_;
127   mutable int cached_index_;
128 };
129 
130 }  // namespace phonenumbers
131 }  // namespace i18n
132 
133 #endif  // I18N_PHONENUMBERS_UNICODESTRING_H_
134