Chromium Code Reviews| 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_UTF_OFFSET_STRING_CONVERSIONS_H_ | 5 #ifndef BASE_UTF_OFFSET_STRING_CONVERSIONS_H_ |
| 6 #define BASE_UTF_OFFSET_STRING_CONVERSIONS_H_ | 6 #define BASE_UTF_OFFSET_STRING_CONVERSIONS_H_ |
| 7 #pragma once | 7 #pragma once |
| 8 | 8 |
| 9 #include <string> | 9 #include <string> |
| 10 #include <vector> | 10 #include <vector> |
| 11 | 11 |
| 12 #include "base/base_export.h" | 12 #include "base/base_export.h" |
| 13 #include "base/string16.h" | 13 #include "base/string16.h" |
| 14 | 14 |
| 15 namespace base { | 15 namespace base { |
| 16 class StringPiece; | 16 class StringPiece; |
| 17 class StringPiece16; | |
| 17 } | 18 } |
| 18 | 19 |
| 19 // Like the conversions in utf_string_conversions.h, but also takes one or more | 20 // Like the conversions in utf_string_conversions.h, but also takes one or more |
| 20 // offsets (|offset[s]_for_adjustment|) into the source strings, each offset | 21 // offsets (|offset[s]_for_adjustment|) into the source strings, each offset |
| 21 // will be adjusted to point at the same logical place in the result strings. | 22 // will be adjusted to point at the same logical place in the result strings. |
| 22 // If this isn't possible because an offset points past the end of the source | 23 // If this isn't possible because an offset points past the end of the source |
| 23 // strings or into the middle of a multibyte sequence, the offending offset will | 24 // strings or into the middle of a multibyte sequence, the offending offset will |
| 24 // be set to string16::npos. |offset[s]_for_adjustment| may be NULL. | 25 // be set to string16::npos. |offset[s]_for_adjustment| may be NULL. |
| 25 BASE_EXPORT bool UTF8ToUTF16AndAdjustOffset(const char* src, | 26 BASE_EXPORT bool UTF8ToUTF16AndAdjustOffset(const char* src, |
| 26 size_t src_len, | 27 size_t src_len, |
| 27 string16* output, | 28 string16* output, |
| 28 size_t* offset_for_adjustment); | 29 size_t* offset_for_adjustment); |
| 29 BASE_EXPORT bool UTF8ToUTF16AndAdjustOffsets( | 30 BASE_EXPORT bool UTF8ToUTF16AndAdjustOffsets( |
| 30 const char* src, | 31 const char* src, |
| 31 size_t src_len, | 32 size_t src_len, |
| 32 string16* output, | 33 string16* output, |
| 33 std::vector<size_t>* offsets_for_adjustment); | 34 std::vector<size_t>* offsets_for_adjustment); |
| 34 | 35 |
| 35 BASE_EXPORT string16 UTF8ToUTF16AndAdjustOffset(const base::StringPiece& utf8, | 36 BASE_EXPORT string16 UTF8ToUTF16AndAdjustOffset(const base::StringPiece& utf8, |
| 36 size_t* offset_for_adjustment); | 37 size_t* offset_for_adjustment); |
| 37 BASE_EXPORT string16 UTF8ToUTF16AndAdjustOffsets( | 38 BASE_EXPORT string16 UTF8ToUTF16AndAdjustOffsets( |
| 38 const base::StringPiece& utf8, | 39 const base::StringPiece& utf8, |
| 39 std::vector<size_t>* offsets_for_adjustment); | 40 std::vector<size_t>* offsets_for_adjustment); |
| 40 | 41 |
| 42 BASE_EXPORT bool UTF16ToUTF8AndAdjustOffset(const char16* src, | |
|
brettw
2011/09/06 18:19:18
I'd not bother with these first two variants and j
kinaba
2011/09/06 23:55:36
Done.
| |
| 43 size_t src_len, | |
| 44 std::string* output, | |
| 45 size_t* offset_for_adjustment); | |
| 46 BASE_EXPORT bool UTF16ToUTF8AndAdjustOffsets( | |
| 47 const char16* src, | |
| 48 size_t src_len, | |
| 49 std::string* output, | |
| 50 std::vector<size_t>* offsets_for_adjustment); | |
| 51 | |
| 52 BASE_EXPORT std::string UTF16ToUTF8AndAdjustOffset( | |
| 53 const base::StringPiece16& utf16, | |
| 54 size_t* offset_for_adjustment); | |
| 55 BASE_EXPORT std::string UTF16ToUTF8AndAdjustOffsets( | |
| 56 const base::StringPiece16& utf16, | |
| 57 std::vector<size_t>* offsets_for_adjustment); | |
| 58 | |
| 59 | |
| 41 // Limiting function callable by std::for_each which will replace any value | 60 // Limiting function callable by std::for_each which will replace any value |
| 42 // which is equal to or greater than |limit| with npos. | 61 // which is equal to or greater than |limit| with npos. |
| 43 template <typename T> | 62 template <typename T> |
| 44 struct LimitOffset { | 63 struct LimitOffset { |
| 45 explicit LimitOffset(size_t limit) | 64 explicit LimitOffset(size_t limit) |
| 46 : limit_(limit) {} | 65 : limit_(limit) {} |
| 47 | 66 |
| 48 void operator()(size_t& offset) { | 67 void operator()(size_t& offset) { |
| 49 if (offset >= limit_) | 68 if (offset >= limit_) |
| 50 offset = T::npos; | 69 offset = T::npos; |
| (...skipping 26 matching lines...) Expand all Loading... | |
| 77 void Add(const Adjustment& adjustment); | 96 void Add(const Adjustment& adjustment); |
| 78 | 97 |
| 79 private: | 98 private: |
| 80 void AdjustOffset(std::vector<size_t>::iterator offset); | 99 void AdjustOffset(std::vector<size_t>::iterator offset); |
| 81 | 100 |
| 82 std::vector<size_t>* offsets_for_adjustment_; | 101 std::vector<size_t>* offsets_for_adjustment_; |
| 83 std::vector<Adjustment> adjustments_; | 102 std::vector<Adjustment> adjustments_; |
| 84 }; | 103 }; |
| 85 | 104 |
| 86 #endif // BASE_UTF_OFFSET_STRING_CONVERSIONS_H_ | 105 #endif // BASE_UTF_OFFSET_STRING_CONVERSIONS_H_ |
| OLD | NEW |