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#include "phonenumbers/unicodestring.h" 18 19#include <algorithm> 20#include <cassert> 21#include <iterator> 22 23using std::advance; 24using std::equal; 25 26namespace i18n { 27namespace phonenumbers { 28 29UnicodeString& UnicodeString::operator=(const UnicodeString& src) { 30 if (&src != this) { 31 invalidateCachedIndex(); 32 text_ = src.text_; 33 } 34 return *this; 35} 36 37bool UnicodeString::operator==(const UnicodeString& rhs) const { 38 return equal(text_.begin(), text_.end(), rhs.text_.begin()); 39} 40 41void UnicodeString::append(const UnicodeString& unicode_string) { 42 invalidateCachedIndex(); 43 for (UnicodeString::const_iterator it = unicode_string.begin(); 44 it != unicode_string.end(); ++it) { 45 append(*it); 46 } 47} 48 49int UnicodeString::indexOf(char32 codepoint) const { 50 int pos = 0; 51 for (UnicodeText::const_iterator it = text_.begin(); it != text_.end(); 52 ++it, ++pos) { 53 if (*it == codepoint) { 54 return pos; 55 } 56 } 57 return -1; 58} 59 60void UnicodeString::replace(int start, int length, const UnicodeString& src) { 61 assert(length >= 0 && length <= this->length()); 62 invalidateCachedIndex(); 63 UnicodeText::const_iterator start_it = text_.begin(); 64 advance(start_it, start); 65 UnicodeText unicode_text; 66 unicode_text.append(text_.begin(), start_it); 67 unicode_text.append(src.text_); 68 advance(start_it, length); 69 unicode_text.append(start_it, text_.end()); 70 text_ = unicode_text; 71} 72 73void UnicodeString::setCharAt(int pos, char32 c) { 74 assert(pos < length()); 75 invalidateCachedIndex(); 76 UnicodeText::const_iterator pos_it = text_.begin(); 77 advance(pos_it, pos); 78 UnicodeText unicode_text; 79 unicode_text.append(text_.begin(), pos_it); 80 unicode_text.push_back(c); 81 ++pos_it; 82 unicode_text.append(pos_it, text_.end()); 83 text_ = unicode_text; 84} 85 86UnicodeString UnicodeString::tempSubString(int start, int length) const { 87 const int unicodestring_length = this->length(); 88 if (length == std::numeric_limits<int>::max()) { 89 length = unicodestring_length - start; 90 } 91 if (start > unicodestring_length || length > unicodestring_length) { 92 return UnicodeString(""); 93 } 94 UnicodeText::const_iterator start_it = text_.begin(); 95 advance(start_it, start); 96 UnicodeText::const_iterator end_it = start_it; 97 advance(end_it, length); 98 UnicodeString substring; 99 substring.text_.PointTo(start_it, end_it); 100 return substring; 101} 102 103char32 UnicodeString::operator[](int index) const { 104 assert(index < length()); 105 if (cached_index_ == -1 || cached_index_ > index) { 106 cached_it_ = text_.begin(); 107 cached_index_ = 0; 108 } 109 for (; cached_index_ < index; ++cached_index_, ++cached_it_) {} 110 return *cached_it_; 111} 112 113} // namespace phonenumbers 114} // namespace i18n 115