180d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com// Copyright (C) 2011 The Libphonenumber Authors
280d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com//
380d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com// Licensed under the Apache License, Version 2.0 (the "License");
480d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com// you may not use this file except in compliance with the License.
580d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com// You may obtain a copy of the License at
680d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com//
780d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com// http://www.apache.org/licenses/LICENSE-2.0
880d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com//
980d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com// Unless required by applicable law or agreed to in writing, software
1080d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com// distributed under the License is distributed on an "AS IS" BASIS,
1180d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1280d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com// See the License for the specific language governing permissions and
1380d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com// limitations under the License.
1480d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com
1580d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com// Author: Philippe Liard
1680d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com
1780d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com#include "phonenumbers/unicodestring.h"
1880d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com
1980d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com#include <algorithm>
2080d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com#include <cassert>
2180d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com#include <iterator>
2280d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com
2380d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.comusing std::advance;
2480d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.comusing std::equal;
2580d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com
2680d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.comnamespace i18n {
2780d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.comnamespace phonenumbers {
2880d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com
2980d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.comUnicodeString& UnicodeString::operator=(const UnicodeString& src) {
3080d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com  if (&src != this) {
3180d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com    invalidateCachedIndex();
3280d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com    text_ = src.text_;
3380d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com  }
3480d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com  return *this;
3580d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com}
3680d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com
3780d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.combool UnicodeString::operator==(const UnicodeString& rhs) const {
3880d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com  return equal(text_.begin(), text_.end(), rhs.text_.begin());
3980d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com}
4080d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com
4180d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.comvoid UnicodeString::append(const UnicodeString& unicode_string) {
4280d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com  invalidateCachedIndex();
4380d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com  for (UnicodeString::const_iterator it = unicode_string.begin();
4480d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com       it != unicode_string.end(); ++it) {
4580d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com    append(*it);
4680d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com  }
4780d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com}
4880d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com
4980d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.comint UnicodeString::indexOf(char32 codepoint) const {
5080d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com  int pos = 0;
5180d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com  for (UnicodeText::const_iterator it = text_.begin(); it != text_.end();
5280d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com       ++it, ++pos) {
5380d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com    if (*it == codepoint) {
5480d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com      return pos;
5580d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com    }
5680d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com  }
5780d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com  return -1;
5880d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com}
5980d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com
6080d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.comvoid UnicodeString::replace(int start, int length, const UnicodeString& src) {
6180d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com  assert(length >= 0 && length <= this->length());
6280d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com  invalidateCachedIndex();
6380d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com  UnicodeText::const_iterator start_it = text_.begin();
6480d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com  advance(start_it, start);
6580d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com  UnicodeText unicode_text;
6680d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com  unicode_text.append(text_.begin(), start_it);
6780d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com  unicode_text.append(src.text_);
6880d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com  advance(start_it, length);
6980d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com  unicode_text.append(start_it, text_.end());
7080d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com  text_ = unicode_text;
7180d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com}
7280d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com
7380d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.comvoid UnicodeString::setCharAt(int pos, char32 c) {
7480d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com  assert(pos < length());
7580d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com  invalidateCachedIndex();
7680d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com  UnicodeText::const_iterator pos_it = text_.begin();
7780d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com  advance(pos_it, pos);
7880d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com  UnicodeText unicode_text;
7980d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com  unicode_text.append(text_.begin(), pos_it);
8080d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com  unicode_text.push_back(c);
8180d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com  ++pos_it;
8280d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com  unicode_text.append(pos_it, text_.end());
8380d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com  text_ = unicode_text;
8480d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com}
8580d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com
8680d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.comUnicodeString UnicodeString::tempSubString(int start, int length) const {
8780d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com  const int unicodestring_length = this->length();
8880d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com  if (length == std::numeric_limits<int>::max()) {
8980d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com    length = unicodestring_length - start;
9080d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com  }
9180d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com  if (start > unicodestring_length || length > unicodestring_length) {
9280d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com    return UnicodeString("");
9380d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com  }
9480d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com  UnicodeText::const_iterator start_it = text_.begin();
9580d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com  advance(start_it, start);
9680d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com  UnicodeText::const_iterator end_it = start_it;
9780d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com  advance(end_it, length);
9880d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com  UnicodeString substring;
9980d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com  substring.text_.PointTo(start_it, end_it);
10080d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com  return substring;
10180d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com}
10280d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com
10380d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.comchar32 UnicodeString::operator[](int index) const {
10480d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com  assert(index < length());
10580d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com  if (cached_index_ == -1 || cached_index_ > index) {
10680d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com    cached_it_ = text_.begin();
10780d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com    cached_index_ = 0;
10880d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com  }
10980d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com  for (; cached_index_ < index; ++cached_index_, ++cached_it_) {}
11080d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com  return *cached_it_;
11180d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com}
11280d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com
11380d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com}  // namespace phonenumbers
11480d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com}  // namespace i18n
115