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 5 #ifndef BASE_STRINGS_UTF_OFFSET_STRING_CONVERSIONS_H_ 6 #define BASE_STRINGS_UTF_OFFSET_STRING_CONVERSIONS_H_ 7 8 #include <string> 9 #include <vector> 10 11 #include "base/base_export.h" 12 #include "base/strings/string16.h" 13 #include "base/strings/string_piece.h" 14 15 namespace base { 16 17 // Like the conversions in utf_string_conversions.h, but also takes one or more 18 // offsets (|offset[s]_for_adjustment|) into the source strings, each offset 19 // will be adjusted to point at the same logical place in the result strings. 20 // If this isn't possible because an offset points past the end of the source 21 // strings or into the middle of a multibyte sequence, the offending offset will 22 // be set to string16::npos. |offset[s]_for_adjustment| may be NULL. 23 BASE_EXPORT bool UTF8ToUTF16AndAdjustOffset(const char* src, 24 size_t src_len, 25 string16* output, 26 size_t* offset_for_adjustment); 27 BASE_EXPORT bool UTF8ToUTF16AndAdjustOffsets( 28 const char* src, 29 size_t src_len, 30 string16* output, 31 std::vector<size_t>* offsets_for_adjustment); 32 33 BASE_EXPORT string16 UTF8ToUTF16AndAdjustOffset(const base::StringPiece& utf8, 34 size_t* offset_for_adjustment); 35 BASE_EXPORT string16 UTF8ToUTF16AndAdjustOffsets( 36 const base::StringPiece& utf8, 37 std::vector<size_t>* offsets_for_adjustment); 38 39 BASE_EXPORT std::string UTF16ToUTF8AndAdjustOffset( 40 const base::StringPiece16& utf16, 41 size_t* offset_for_adjustment); 42 BASE_EXPORT std::string UTF16ToUTF8AndAdjustOffsets( 43 const base::StringPiece16& utf16, 44 std::vector<size_t>* offsets_for_adjustment); 45 46 // Limiting function callable by std::for_each which will replace any value 47 // which is equal to or greater than |limit| with npos. 48 template <typename T> 49 struct LimitOffset { 50 explicit LimitOffset(size_t limit) 51 : limit_(limit) {} 52 53 void operator()(size_t& offset) { 54 if (offset >= limit_) 55 offset = T::npos; 56 } 57 58 size_t limit_; 59 }; 60 61 // Stack object which, on destruction, will update a vector of offsets based on 62 // any supplied adjustments. To use, declare one of these, providing the 63 // address of the offset vector to adjust. Then Add() any number of Adjustments 64 // (each Adjustment gives the |original_offset| of a substring and the lengths 65 // of the substring before and after transforming). When the OffsetAdjuster 66 // goes out of scope, all the offsets in the provided vector will be updated. 67 class BASE_EXPORT OffsetAdjuster { 68 public: 69 struct BASE_EXPORT Adjustment { 70 Adjustment(size_t original_offset, 71 size_t original_length, 72 size_t output_length); 73 74 size_t original_offset; 75 size_t original_length; 76 size_t output_length; 77 }; 78 79 explicit OffsetAdjuster(std::vector<size_t>* offsets_for_adjustment); 80 ~OffsetAdjuster(); 81 82 void Add(const Adjustment& adjustment); 83 84 private: 85 void AdjustOffset(std::vector<size_t>::iterator offset); 86 87 std::vector<size_t>* offsets_for_adjustment_; 88 std::vector<Adjustment> adjustments_; 89 }; 90 91 } // namespace base 92 93 #endif // BASE_STRINGS_UTF_OFFSET_STRING_CONVERSIONS_H_ 94