case_conversion.cc 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. // Copyright (c) 2011 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "base/i18n/case_conversion.h"
  5. #include <stdint.h>
  6. #include <string>
  7. #include "base/numerics/safe_conversions.h"
  8. #include "base/strings/string_util.h"
  9. #include "third_party/icu/source/common/unicode/uchar.h"
  10. #include "third_party/icu/source/common/unicode/unistr.h"
  11. #include "third_party/icu/source/common/unicode/ustring.h"
  12. namespace base {
  13. namespace i18n {
  14. namespace {
  15. // Provides a uniform interface for upper/lower/folding which take take
  16. // slightly varying parameters.
  17. typedef int32_t (*CaseMapperFunction)(UChar* dest, int32_t dest_capacity,
  18. const UChar* src, int32_t src_length,
  19. UErrorCode* error);
  20. int32_t ToUpperMapper(UChar* dest, int32_t dest_capacity,
  21. const UChar* src, int32_t src_length,
  22. UErrorCode* error) {
  23. // Use default locale.
  24. return u_strToUpper(dest, dest_capacity, src, src_length, nullptr, error);
  25. }
  26. int32_t ToLowerMapper(UChar* dest, int32_t dest_capacity,
  27. const UChar* src, int32_t src_length,
  28. UErrorCode* error) {
  29. // Use default locale.
  30. return u_strToLower(dest, dest_capacity, src, src_length, nullptr, error);
  31. }
  32. int32_t FoldCaseMapper(UChar* dest, int32_t dest_capacity,
  33. const UChar* src, int32_t src_length,
  34. UErrorCode* error) {
  35. return u_strFoldCase(dest, dest_capacity, src, src_length,
  36. U_FOLD_CASE_DEFAULT, error);
  37. }
  38. // Provides similar functionality as UnicodeString::caseMap but on
  39. // std::u16string.
  40. std::u16string CaseMap(StringPiece16 string, CaseMapperFunction case_mapper) {
  41. std::u16string dest;
  42. if (string.empty())
  43. return dest;
  44. // Provide an initial guess that the string length won't change. The typical
  45. // strings we use will very rarely change length in this process, so don't
  46. // optimize for that case.
  47. dest.resize(string.size());
  48. UErrorCode error;
  49. do {
  50. error = U_ZERO_ERROR;
  51. // ICU won't terminate the string if there's not enough room for the null
  52. // terminator, but will otherwise. So we don't need to save room for that.
  53. // Don't use WriteInto, which assumes null terminators.
  54. int32_t new_length = case_mapper(
  55. &dest[0], saturated_cast<int32_t>(dest.size()), string.data(),
  56. saturated_cast<int32_t>(string.size()), &error);
  57. dest.resize(new_length);
  58. } while (error == U_BUFFER_OVERFLOW_ERROR);
  59. return dest;
  60. }
  61. } // namespace
  62. std::u16string ToLower(StringPiece16 string) {
  63. return CaseMap(string, &ToLowerMapper);
  64. }
  65. std::u16string ToUpper(StringPiece16 string) {
  66. return CaseMap(string, &ToUpperMapper);
  67. }
  68. std::u16string FoldCase(StringPiece16 string) {
  69. return CaseMap(string, &FoldCaseMapper);
  70. }
  71. } // namespace i18n
  72. } // namespace base