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