11767c5feSopenharmony_ci// Copyright (C) 2011 The Libphonenumber Authors
21767c5feSopenharmony_ci//
31767c5feSopenharmony_ci// Licensed under the Apache License, Version 2.0 (the "License");
41767c5feSopenharmony_ci// you may not use this file except in compliance with the License.
51767c5feSopenharmony_ci// You may obtain a copy of the License at
61767c5feSopenharmony_ci//
71767c5feSopenharmony_ci// http://www.apache.org/licenses/LICENSE-2.0
81767c5feSopenharmony_ci//
91767c5feSopenharmony_ci// Unless required by applicable law or agreed to in writing, software
101767c5feSopenharmony_ci// distributed under the License is distributed on an "AS IS" BASIS,
111767c5feSopenharmony_ci// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
121767c5feSopenharmony_ci// See the License for the specific language governing permissions and
131767c5feSopenharmony_ci// limitations under the License.
141767c5feSopenharmony_ci
151767c5feSopenharmony_ci#ifndef I18N_PHONENUMBERS_ENCODING_UTILS_H_
161767c5feSopenharmony_ci#define I18N_PHONENUMBERS_ENCODING_UTILS_H_
171767c5feSopenharmony_ci
181767c5feSopenharmony_ci#include "phonenumbers/base/basictypes.h"
191767c5feSopenharmony_ci#include "phonenumbers/utf/unilib.h"
201767c5feSopenharmony_ci#include "phonenumbers/utf/utf.h"
211767c5feSopenharmony_ci
221767c5feSopenharmony_cinamespace i18n {
231767c5feSopenharmony_cinamespace phonenumbers {
241767c5feSopenharmony_ci
251767c5feSopenharmony_ciclass EncodingUtils {
261767c5feSopenharmony_ci public:
271767c5feSopenharmony_ci  // Decodes one Unicode code-point value from a UTF-8 array. Returns the number
281767c5feSopenharmony_ci  // of bytes read from the array. If the array does not contain valid UTF-8,
291767c5feSopenharmony_ci  // the function stores 0xFFFD in the output variable and returns 1.
301767c5feSopenharmony_ci  static inline int DecodeUTF8Char(const char* in, char32* out) {
311767c5feSopenharmony_ci    Rune r;
321767c5feSopenharmony_ci    int len = chartorune(&r, in);
331767c5feSopenharmony_ci    *out = r;
341767c5feSopenharmony_ci    return len;
351767c5feSopenharmony_ci  }
361767c5feSopenharmony_ci
371767c5feSopenharmony_ci  static const char* AdvanceOneUTF8Character(const char* buf_utf8) {
381767c5feSopenharmony_ci      return buf_utf8 + UniLib::OneCharLen(buf_utf8);
391767c5feSopenharmony_ci  }
401767c5feSopenharmony_ci
411767c5feSopenharmony_ci  static const char* BackUpOneUTF8Character(const char* start,
421767c5feSopenharmony_ci                                            const char* end) {
431767c5feSopenharmony_ci    while (start < end && UniLib::IsTrailByte(*--end)) {}
441767c5feSopenharmony_ci    return end;
451767c5feSopenharmony_ci  }
461767c5feSopenharmony_ci};
471767c5feSopenharmony_ci
481767c5feSopenharmony_ci}  // namespace phonenumbers
491767c5feSopenharmony_ci}  // namespace i18n
501767c5feSopenharmony_ci
511767c5feSopenharmony_ci#endif  // I18N_PHONENUMBERS_ENCODING_UTILS_H_
52