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#include <string>
16
17#include "phonenumbers/utf/unicodetext.h"
18
19namespace i18n {
20namespace phonenumbers {
21
22using std::string;
23
24struct NormalizeUTF8 {
25  // Put a UTF-8 string in ASCII digits: All decimal digits (Nd) replaced by
26  // their ASCII counterparts; all other characters are copied from input to
27  // output.
28  static string NormalizeDecimalDigits(const string& number) {
29    string normalized;
30    UnicodeText number_as_unicode;
31    number_as_unicode.PointToUTF8(number.data(), number.size());
32    for (UnicodeText::const_iterator it = number_as_unicode.begin();
33         it != number_as_unicode.end();
34         ++it) {
35      int32_t digitValue = u_charDigitValue(*it);
36      if (digitValue == -1) {
37        // Not a decimal digit.
38        char utf8[4];
39        int len = it.get_utf8(utf8);
40        normalized.append(utf8, len);
41      } else {
42        normalized.push_back('0' + digitValue);
43      }
44    }
45    return normalized;
46  }
47};
48
49}  // namespace phonenumbers
50}  // namespace i18n
51