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 22struct NormalizeUTF8 { 23 // Put a UTF-8 string in ASCII digits: All decimal digits (Nd) replaced by 24 // their ASCII counterparts; all other characters are copied from input to 25 // output. 26 static string NormalizeDecimalDigits(const string& number) { 27 string normalized; 28 UnicodeText number_as_unicode; 29 number_as_unicode.PointToUTF8(number.data(), static_cast<int>(number.size())); 30 if (!number_as_unicode.UTF8WasValid()) 31 return normalized; // Return an empty result to indicate an error 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