| 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 #ifndef BASE_STRINGS_UTF_OFFSET_STRING_CONVERSIONS_H_ | 5 #ifndef BASE_STRINGS_UTF_OFFSET_STRING_CONVERSIONS_H_ |
| 6 #define BASE_STRINGS_UTF_OFFSET_STRING_CONVERSIONS_H_ | 6 #define BASE_STRINGS_UTF_OFFSET_STRING_CONVERSIONS_H_ |
| 7 | 7 |
| 8 #include <stddef.h> | 8 #include <stddef.h> |
| 9 | 9 |
| 10 #include <string> | 10 #include <string> |
| (...skipping 16 matching lines...) Expand all Loading... |
| 27 size_t original_length, | 27 size_t original_length, |
| 28 size_t output_length); | 28 size_t output_length); |
| 29 | 29 |
| 30 size_t original_offset; | 30 size_t original_offset; |
| 31 size_t original_length; | 31 size_t original_length; |
| 32 size_t output_length; | 32 size_t output_length; |
| 33 }; | 33 }; |
| 34 typedef std::vector<Adjustment> Adjustments; | 34 typedef std::vector<Adjustment> Adjustments; |
| 35 | 35 |
| 36 // Adjusts all offsets in |offsets_for_adjustment| to reflect the adjustments | 36 // Adjusts all offsets in |offsets_for_adjustment| to reflect the adjustments |
| 37 // recorded in |adjustments|. | 37 // recorded in |adjustments|. Adjusted offsets greater than |limit| will be |
| 38 // set to string16::npos. |
| 38 // | 39 // |
| 39 // Offsets represents insertion/selection points between characters: if |src| | 40 // Offsets represents insertion/selection points between characters: if |src| |
| 40 // is "abcd", then 0 is before 'a', 2 is between 'b' and 'c', and 4 is at the | 41 // is "abcd", then 0 is before 'a', 2 is between 'b' and 'c', and 4 is at the |
| 41 // end of the string. Valid input offsets range from 0 to |src_len|. On | 42 // end of the string. Valid input offsets range from 0 to |src_len|. On |
| 42 // exit, each offset will have been modified to point at the same logical | 43 // exit, each offset will have been modified to point at the same logical |
| 43 // position in the output string. If an offset cannot be successfully | 44 // position in the output string. If an offset cannot be successfully |
| 44 // adjusted (e.g., because it points into the middle of a multibyte sequence), | 45 // adjusted (e.g., because it points into the middle of a multibyte sequence), |
| 45 // it will be set to string16::npos. | 46 // it will be set to string16::npos. |
| 46 static void AdjustOffsets(const Adjustments& adjustments, | 47 static void AdjustOffsets(const Adjustments& adjustments, |
| 47 std::vector<size_t>* offsets_for_adjustment); | 48 std::vector<size_t>* offsets_for_adjustment, |
| 49 size_t limit = string16::npos); |
| 48 | 50 |
| 49 // Adjusts the single |offset| to reflect the adjustments recorded in | 51 // Adjusts the single |offset| to reflect the adjustments recorded in |
| 50 // |adjustments|. | 52 // |adjustments|. |
| 51 static void AdjustOffset(const Adjustments& adjustments, | 53 static void AdjustOffset(const Adjustments& adjustments, |
| 52 size_t* offset); | 54 size_t* offset, |
| 55 size_t limit = string16::npos); |
| 53 | 56 |
| 54 // Adjusts all offsets in |offsets_for_unadjustment| to reflect the reverse | 57 // Adjusts all offsets in |offsets_for_unadjustment| to reflect the reverse |
| 55 // of the adjustments recorded in |adjustments|. In other words, the offsets | 58 // of the adjustments recorded in |adjustments|. In other words, the offsets |
| 56 // provided represent offsets into an adjusted string and the caller wants | 59 // provided represent offsets into an adjusted string and the caller wants |
| 57 // to know the offsets they correspond to in the original string. If an | 60 // to know the offsets they correspond to in the original string. If an |
| 58 // offset cannot be successfully unadjusted (e.g., because it points into | 61 // offset cannot be successfully unadjusted (e.g., because it points into |
| 59 // the middle of a multibyte sequence), it will be set to string16::npos. | 62 // the middle of a multibyte sequence), it will be set to string16::npos. |
| 60 static void UnadjustOffsets(const Adjustments& adjustments, | 63 static void UnadjustOffsets(const Adjustments& adjustments, |
| 61 std::vector<size_t>* offsets_for_unadjustment); | 64 std::vector<size_t>* offsets_for_unadjustment); |
| 62 | 65 |
| (...skipping 27 matching lines...) Expand all Loading... |
| 90 // It may be NULL. | 93 // It may be NULL. |
| 91 BASE_EXPORT bool UTF8ToUTF16WithAdjustments( | 94 BASE_EXPORT bool UTF8ToUTF16WithAdjustments( |
| 92 const char* src, | 95 const char* src, |
| 93 size_t src_len, | 96 size_t src_len, |
| 94 string16* output, | 97 string16* output, |
| 95 base::OffsetAdjuster::Adjustments* adjustments); | 98 base::OffsetAdjuster::Adjustments* adjustments); |
| 96 BASE_EXPORT string16 UTF8ToUTF16WithAdjustments( | 99 BASE_EXPORT string16 UTF8ToUTF16WithAdjustments( |
| 97 const base::StringPiece& utf8, | 100 const base::StringPiece& utf8, |
| 98 base::OffsetAdjuster::Adjustments* adjustments); | 101 base::OffsetAdjuster::Adjustments* adjustments); |
| 99 // As above, but instead internally examines the adjustments and applies them | 102 // As above, but instead internally examines the adjustments and applies them |
| 100 // to |offsets_for_adjustment|. See comments by AdjustOffsets(). | 103 // to |offsets_for_adjustment|. Input offsets greater than the length of the |
| 104 // input string will be set to string16::npos. See comments by AdjustOffsets(). |
| 101 BASE_EXPORT string16 UTF8ToUTF16AndAdjustOffsets( | 105 BASE_EXPORT string16 UTF8ToUTF16AndAdjustOffsets( |
| 102 const base::StringPiece& utf8, | 106 const base::StringPiece& utf8, |
| 103 std::vector<size_t>* offsets_for_adjustment); | 107 std::vector<size_t>* offsets_for_adjustment); |
| 104 | |
| 105 BASE_EXPORT std::string UTF16ToUTF8AndAdjustOffsets( | 108 BASE_EXPORT std::string UTF16ToUTF8AndAdjustOffsets( |
| 106 const base::StringPiece16& utf16, | 109 const base::StringPiece16& utf16, |
| 107 std::vector<size_t>* offsets_for_adjustment); | 110 std::vector<size_t>* offsets_for_adjustment); |
| 108 | 111 |
| 109 // Limiting function callable by std::for_each which will replace any value | |
| 110 // which is greater than |limit| with npos. Typically this is called with a | |
| 111 // string length to clamp offsets into the string to [0, length] (as opposed to | |
| 112 // [0, length); see comments above). | |
| 113 template <typename T> | |
| 114 struct LimitOffset { | |
| 115 explicit LimitOffset(size_t limit) | |
| 116 : limit_(limit) {} | |
| 117 | |
| 118 void operator()(size_t& offset) { | |
| 119 if (offset > limit_) | |
| 120 offset = T::npos; | |
| 121 } | |
| 122 | |
| 123 size_t limit_; | |
| 124 }; | |
| 125 | |
| 126 } // namespace base | 112 } // namespace base |
| 127 | 113 |
| 128 #endif // BASE_STRINGS_UTF_OFFSET_STRING_CONVERSIONS_H_ | 114 #endif // BASE_STRINGS_UTF_OFFSET_STRING_CONVERSIONS_H_ |
| OLD | NEW |