| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/utf_offset_string_conversions.h" | 5 #include "base/utf_offset_string_conversions.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 | 8 |
| 9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
| 10 #include "base/string_piece.h" | 10 #include "base/string_piece.h" |
| 11 #include "base/utf_string_conversion_utils.h" | 11 #include "base/utf_string_conversion_utils.h" |
| 12 | 12 |
| 13 using base::PrepareForUTF16Or32Output; | 13 using base::PrepareForUTF16Or32Output; |
| 14 using base::PrepareForUTF8Output; |
| 14 using base::ReadUnicodeCharacter; | 15 using base::ReadUnicodeCharacter; |
| 15 using base::WriteUnicodeCharacter; | 16 using base::WriteUnicodeCharacter; |
| 16 | 17 |
| 17 // Converts the given source Unicode character type to the given destination | 18 // Converts the given source Unicode character type to the given destination |
| 18 // Unicode character type as a STL string. The given input buffer and size | 19 // Unicode character type as a STL string. The given input buffer and size |
| 19 // determine the source, and the given output STL string will be replaced by | 20 // determine the source, and the given output STL string will be replaced by |
| 20 // the result. | 21 // the result. |
| 21 bool ConvertUnicode(const char* src, | 22 template<typename SrcChar, typename DestStdString> |
| 23 bool ConvertUnicode(const SrcChar* src, |
| 22 size_t src_len, | 24 size_t src_len, |
| 23 string16* output, | 25 DestStdString* output, |
| 24 std::vector<size_t>* offsets_for_adjustment) { | 26 std::vector<size_t>* offsets_for_adjustment) { |
| 25 if (offsets_for_adjustment) { | 27 if (offsets_for_adjustment) { |
| 26 std::for_each(offsets_for_adjustment->begin(), | 28 std::for_each(offsets_for_adjustment->begin(), |
| 27 offsets_for_adjustment->end(), | 29 offsets_for_adjustment->end(), |
| 28 LimitOffset<string16>(src_len)); | 30 LimitOffset<DestStdString>(src_len)); |
| 29 } | 31 } |
| 30 | 32 |
| 31 // ICU requires 32-bit numbers. | 33 // ICU requires 32-bit numbers. |
| 32 bool success = true; | 34 bool success = true; |
| 33 OffsetAdjuster offset_adjuster(offsets_for_adjustment); | 35 OffsetAdjuster offset_adjuster(offsets_for_adjustment); |
| 34 int32 src_len32 = static_cast<int32>(src_len); | 36 int32 src_len32 = static_cast<int32>(src_len); |
| 35 for (int32 i = 0; i < src_len32; i++) { | 37 for (int32 i = 0; i < src_len32; i++) { |
| 36 uint32 code_point; | 38 uint32 code_point; |
| 37 size_t original_i = i; | 39 size_t original_i = i; |
| 38 size_t chars_written = 0; | 40 size_t chars_written = 0; |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 91 | 93 |
| 92 string16 UTF8ToUTF16AndAdjustOffsets( | 94 string16 UTF8ToUTF16AndAdjustOffsets( |
| 93 const base::StringPiece& utf8, | 95 const base::StringPiece& utf8, |
| 94 std::vector<size_t>* offsets_for_adjustment) { | 96 std::vector<size_t>* offsets_for_adjustment) { |
| 95 string16 result; | 97 string16 result; |
| 96 UTF8ToUTF16AndAdjustOffsets(utf8.data(), utf8.length(), &result, | 98 UTF8ToUTF16AndAdjustOffsets(utf8.data(), utf8.length(), &result, |
| 97 offsets_for_adjustment); | 99 offsets_for_adjustment); |
| 98 return result; | 100 return result; |
| 99 } | 101 } |
| 100 | 102 |
| 103 std::string UTF16ToUTF8AndAdjustOffset( |
| 104 const base::StringPiece16& utf16, |
| 105 size_t* offset_for_adjustment) { |
| 106 std::vector<size_t> offsets; |
| 107 if (offset_for_adjustment) |
| 108 offsets.push_back(*offset_for_adjustment); |
| 109 std::string result = UTF16ToUTF8AndAdjustOffsets(utf16, &offsets); |
| 110 if (offset_for_adjustment) |
| 111 *offset_for_adjustment = offsets[0]; |
| 112 return result; |
| 113 } |
| 114 |
| 115 std::string UTF16ToUTF8AndAdjustOffsets( |
| 116 const base::StringPiece16& utf16, |
| 117 std::vector<size_t>* offsets_for_adjustment) { |
| 118 std::string result; |
| 119 PrepareForUTF8Output(utf16.data(), utf16.length(), &result); |
| 120 ConvertUnicode(utf16.data(), utf16.length(), &result, offsets_for_adjustment); |
| 121 return result; |
| 122 } |
| 123 |
| 101 OffsetAdjuster::Adjustment::Adjustment(size_t original_offset, | 124 OffsetAdjuster::Adjustment::Adjustment(size_t original_offset, |
| 102 size_t original_length, | 125 size_t original_length, |
| 103 size_t output_length) | 126 size_t output_length) |
| 104 : original_offset(original_offset), | 127 : original_offset(original_offset), |
| 105 original_length(original_length), | 128 original_length(original_length), |
| 106 output_length(output_length) { | 129 output_length(output_length) { |
| 107 } | 130 } |
| 108 | 131 |
| 109 OffsetAdjuster::OffsetAdjuster(std::vector<size_t>* offsets_for_adjustment) | 132 OffsetAdjuster::OffsetAdjuster(std::vector<size_t>* offsets_for_adjustment) |
| 110 : offsets_for_adjustment_(offsets_for_adjustment) { | 133 : offsets_for_adjustment_(offsets_for_adjustment) { |
| (...skipping 24 matching lines...) Expand all Loading... |
| 135 if (*offset <= i->original_offset) | 158 if (*offset <= i->original_offset) |
| 136 break; | 159 break; |
| 137 if (*offset < (i->original_offset + i->original_length)) { | 160 if (*offset < (i->original_offset + i->original_length)) { |
| 138 *offset = string16::npos; | 161 *offset = string16::npos; |
| 139 return; | 162 return; |
| 140 } | 163 } |
| 141 adjustment += (i->original_length - i->output_length); | 164 adjustment += (i->original_length - i->output_length); |
| 142 } | 165 } |
| 143 *offset -= adjustment; | 166 *offset -= adjustment; |
| 144 } | 167 } |
| OLD | NEW |