rvargas@google.com | 4891e7d | 2011-03-26 03:46:38 +0900 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
pkasting@chromium.org | 046cd5a | 2009-11-14 04:27:48 +0900 | [diff] [blame] | 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_UTF_OFFSET_STRING_CONVERSIONS_H_ |
| 6 | #define BASE_UTF_OFFSET_STRING_CONVERSIONS_H_ |
thakis@chromium.org | 01d1452 | 2010-07-27 08:08:24 +0900 | [diff] [blame] | 7 | #pragma once |
pkasting@chromium.org | 046cd5a | 2009-11-14 04:27:48 +0900 | [diff] [blame] | 8 | |
| 9 | #include <string> |
mrossetti@chromium.org | 9422b22 | 2011-04-14 03:43:05 +0900 | [diff] [blame] | 10 | #include <vector> |
pkasting@chromium.org | 046cd5a | 2009-11-14 04:27:48 +0900 | [diff] [blame] | 11 | |
rvargas@google.com | 4891e7d | 2011-03-26 03:46:38 +0900 | [diff] [blame] | 12 | #include "base/base_api.h" |
pkasting@chromium.org | 046cd5a | 2009-11-14 04:27:48 +0900 | [diff] [blame] | 13 | #include "base/string16.h" |
| 14 | |
| 15 | namespace base { |
| 16 | class StringPiece; |
| 17 | } |
| 18 | |
mrossetti@chromium.org | 9422b22 | 2011-04-14 03:43:05 +0900 | [diff] [blame] | 19 | // 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 | // 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 | // strings or into the middle of a multibyte sequence, the offending offset will |
pkasting@chromium.org | e6b5c20 | 2011-05-04 05:03:50 +0900 | [diff] [blame^] | 24 | // be set to string16::npos. |offset[s]_for_adjustment| may be NULL. |
| 25 | BASE_API bool UTF8ToUTF16AndAdjustOffset(const char* src, |
| 26 | size_t src_len, |
| 27 | string16* output, |
| 28 | size_t* offset_for_adjustment); |
| 29 | BASE_API bool UTF8ToUTF16AndAdjustOffsets( |
mrossetti@chromium.org | 9422b22 | 2011-04-14 03:43:05 +0900 | [diff] [blame] | 30 | const char* src, |
| 31 | size_t src_len, |
pkasting@chromium.org | e6b5c20 | 2011-05-04 05:03:50 +0900 | [diff] [blame^] | 32 | string16* output, |
mrossetti@chromium.org | 9422b22 | 2011-04-14 03:43:05 +0900 | [diff] [blame] | 33 | std::vector<size_t>* offsets_for_adjustment); |
| 34 | |
pkasting@chromium.org | e6b5c20 | 2011-05-04 05:03:50 +0900 | [diff] [blame^] | 35 | BASE_API string16 UTF8ToUTF16AndAdjustOffset(const base::StringPiece& utf8, |
| 36 | size_t* offset_for_adjustment); |
| 37 | BASE_API string16 UTF8ToUTF16AndAdjustOffsets( |
mrossetti@chromium.org | 9422b22 | 2011-04-14 03:43:05 +0900 | [diff] [blame] | 38 | const base::StringPiece& utf8, |
| 39 | std::vector<size_t>* offsets_for_adjustment); |
rvargas@google.com | 4891e7d | 2011-03-26 03:46:38 +0900 | [diff] [blame] | 40 | |
mrossetti@chromium.org | 9422b22 | 2011-04-14 03:43:05 +0900 | [diff] [blame] | 41 | // Limiting function callable by std::for_each which will replace any value |
| 42 | // which is equal to or greater than |limit| with npos. |
| 43 | template <typename T> |
| 44 | struct LimitOffset { |
| 45 | explicit LimitOffset(size_t limit) |
| 46 | : limit_(limit) {} |
| 47 | |
| 48 | void operator()(size_t& offset) { |
| 49 | if (offset >= limit_) |
| 50 | offset = T::npos; |
| 51 | } |
| 52 | |
| 53 | size_t limit_; |
| 54 | }; |
| 55 | |
pkasting@chromium.org | e6b5c20 | 2011-05-04 05:03:50 +0900 | [diff] [blame^] | 56 | // Stack object which, on destruction, will update a vector of offsets based on |
| 57 | // any supplied adjustments. To use, declare one of these, providing the |
| 58 | // address of the offset vector to adjust. Then Add() any number of Adjustments |
| 59 | // (each Adjustment gives the |original_offset| of a substring and the lengths |
| 60 | // of the substring before and after transforming). When the OffsetAdjuster |
| 61 | // goes out of scope, all the offsets in the provided vector will be updated. |
| 62 | class BASE_API OffsetAdjuster { |
| 63 | public: |
rvargas@google.com | 97a20ec | 2011-04-22 07:22:10 +0900 | [diff] [blame] | 64 | struct BASE_API Adjustment { |
pkasting@chromium.org | e6b5c20 | 2011-05-04 05:03:50 +0900 | [diff] [blame^] | 65 | Adjustment(size_t original_offset, |
| 66 | size_t original_length, |
| 67 | size_t output_length); |
mrossetti@chromium.org | 9422b22 | 2011-04-14 03:43:05 +0900 | [diff] [blame] | 68 | |
pkasting@chromium.org | e6b5c20 | 2011-05-04 05:03:50 +0900 | [diff] [blame^] | 69 | size_t original_offset; |
| 70 | size_t original_length; |
| 71 | size_t output_length; |
mrossetti@chromium.org | 9422b22 | 2011-04-14 03:43:05 +0900 | [diff] [blame] | 72 | }; |
| 73 | |
pkasting@chromium.org | e6b5c20 | 2011-05-04 05:03:50 +0900 | [diff] [blame^] | 74 | explicit OffsetAdjuster(std::vector<size_t>* offsets_for_adjustment); |
| 75 | ~OffsetAdjuster(); |
mrossetti@chromium.org | 9422b22 | 2011-04-14 03:43:05 +0900 | [diff] [blame] | 76 | |
pkasting@chromium.org | e6b5c20 | 2011-05-04 05:03:50 +0900 | [diff] [blame^] | 77 | void Add(const Adjustment& adjustment); |
mrossetti@chromium.org | 9422b22 | 2011-04-14 03:43:05 +0900 | [diff] [blame] | 78 | |
pkasting@chromium.org | e6b5c20 | 2011-05-04 05:03:50 +0900 | [diff] [blame^] | 79 | private: |
| 80 | void AdjustOffset(std::vector<size_t>::iterator offset); |
| 81 | |
| 82 | std::vector<size_t>* offsets_for_adjustment_; |
| 83 | std::vector<Adjustment> adjustments_; |
mrossetti@chromium.org | 9422b22 | 2011-04-14 03:43:05 +0900 | [diff] [blame] | 84 | }; |
pkasting@chromium.org | 046cd5a | 2009-11-14 04:27:48 +0900 | [diff] [blame] | 85 | |
| 86 | #endif // BASE_UTF_OFFSET_STRING_CONVERSIONS_H_ |