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/strings/utf_offset_string_conversions.h" | 5 #include "base/strings/utf_offset_string_conversions.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
(...skipping 19 matching lines...) Expand all Loading... |
30 for (std::vector<size_t>::iterator i(offsets_for_adjustment->begin()); | 30 for (std::vector<size_t>::iterator i(offsets_for_adjustment->begin()); |
31 i != offsets_for_adjustment->end(); ++i) | 31 i != offsets_for_adjustment->end(); ++i) |
32 AdjustOffset(adjustments, &(*i)); | 32 AdjustOffset(adjustments, &(*i)); |
33 } | 33 } |
34 | 34 |
35 // static | 35 // static |
36 void OffsetAdjuster::AdjustOffset(const Adjustments& adjustments, | 36 void OffsetAdjuster::AdjustOffset(const Adjustments& adjustments, |
37 size_t* offset) { | 37 size_t* offset) { |
38 if (*offset == string16::npos) | 38 if (*offset == string16::npos) |
39 return; | 39 return; |
40 size_t adjustment = 0; | 40 int adjustment = 0; |
41 for (Adjustments::const_iterator i = adjustments.begin(); | 41 for (Adjustments::const_iterator i = adjustments.begin(); |
42 i != adjustments.end(); ++i) { | 42 i != adjustments.end(); ++i) { |
43 if (*offset <= i->original_offset) | 43 if (*offset <= i->original_offset) |
44 break; | 44 break; |
45 if (*offset < (i->original_offset + i->original_length)) { | 45 if (*offset < (i->original_offset + i->original_length)) { |
46 *offset = string16::npos; | 46 *offset = string16::npos; |
47 return; | 47 return; |
48 } | 48 } |
49 adjustment += (i->original_length - i->output_length); | 49 adjustment += static_cast<int>(i->original_length - i->output_length); |
50 } | 50 } |
51 *offset -= adjustment; | 51 *offset -= adjustment; |
52 } | 52 } |
53 | 53 |
54 // static | 54 // static |
| 55 void OffsetAdjuster::UnadjustOffsets( |
| 56 const Adjustments& adjustments, |
| 57 std::vector<size_t>* offsets_for_unadjustment) { |
| 58 if (!offsets_for_unadjustment || adjustments.empty()) |
| 59 return; |
| 60 for (std::vector<size_t>::iterator i(offsets_for_unadjustment->begin()); |
| 61 i != offsets_for_unadjustment->end(); ++i) |
| 62 UnadjustOffset(adjustments, &(*i)); |
| 63 } |
| 64 |
| 65 // static |
| 66 void OffsetAdjuster::UnadjustOffset(const Adjustments& adjustments, |
| 67 size_t* offset) { |
| 68 if (*offset == string16::npos) |
| 69 return; |
| 70 int adjustment = 0; |
| 71 for (Adjustments::const_iterator i = adjustments.begin(); |
| 72 i != adjustments.end(); ++i) { |
| 73 if (*offset + adjustment <= i->original_offset) |
| 74 break; |
| 75 adjustment += static_cast<int>(i->original_length - i->output_length); |
| 76 if ((*offset + adjustment) < |
| 77 (i->original_offset + i->original_length)) { |
| 78 *offset = string16::npos; |
| 79 return; |
| 80 } |
| 81 } |
| 82 *offset += adjustment; |
| 83 } |
| 84 |
| 85 // static |
55 void OffsetAdjuster::MergeSequentialAdjustments( | 86 void OffsetAdjuster::MergeSequentialAdjustments( |
56 const Adjustments& first_adjustments, | 87 const Adjustments& first_adjustments, |
57 Adjustments* adjustments_on_adjusted_string) { | 88 Adjustments* adjustments_on_adjusted_string) { |
58 Adjustments::iterator adjusted_iter = adjustments_on_adjusted_string->begin(); | 89 Adjustments::iterator adjusted_iter = adjustments_on_adjusted_string->begin(); |
59 Adjustments::const_iterator first_iter = first_adjustments.begin(); | 90 Adjustments::const_iterator first_iter = first_adjustments.begin(); |
60 // Simultaneously iterate over all |adjustments_on_adjusted_string| and | 91 // Simultaneously iterate over all |adjustments_on_adjusted_string| and |
61 // |first_adjustments|, adding adjustments to or correcting the adjustments | 92 // |first_adjustments|, adding adjustments to or correcting the adjustments |
62 // in |adjustments_on_adjusted_string| as we go. |shift| keeps track of the | 93 // in |adjustments_on_adjusted_string| as we go. |shift| keeps track of the |
63 // current number of characters collapsed by |first_adjustments| up to this | 94 // current number of characters collapsed by |first_adjustments| up to this |
64 // point. |currently_collapsing| keeps track of the number of characters | 95 // point. |currently_collapsing| keeps track of the number of characters |
(...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
220 LimitOffset<base::StringPiece16>(utf16.length())); | 251 LimitOffset<base::StringPiece16>(utf16.length())); |
221 std::string result; | 252 std::string result; |
222 PrepareForUTF8Output(utf16.data(), utf16.length(), &result); | 253 PrepareForUTF8Output(utf16.data(), utf16.length(), &result); |
223 OffsetAdjuster::Adjustments adjustments; | 254 OffsetAdjuster::Adjustments adjustments; |
224 ConvertUnicode(utf16.data(), utf16.length(), &result, &adjustments); | 255 ConvertUnicode(utf16.data(), utf16.length(), &result, &adjustments); |
225 OffsetAdjuster::AdjustOffsets(adjustments, offsets_for_adjustment); | 256 OffsetAdjuster::AdjustOffsets(adjustments, offsets_for_adjustment); |
226 return result; | 257 return result; |
227 } | 258 } |
228 | 259 |
229 } // namespace base | 260 } // namespace base |
OLD | NEW |