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.
5 #ifndef BASE_UTF_OFFSET_STRING_CONVERSIONS_H_
6 #define BASE_UTF_OFFSET_STRING_CONVERSIONS_H_
11 #include "base/base_export.h"
12 #include "base/string16.h"
13 #include "base/string_piece.h"
15 // Like the conversions in utf_string_conversions.h, but also takes one or more
16 // offsets (|offset[s]_for_adjustment|) into the source strings, each offset
17 // will be adjusted to point at the same logical place in the result strings.
18 // If this isn't possible because an offset points past the end of the source
19 // strings or into the middle of a multibyte sequence, the offending offset will
20 // be set to string16::npos. |offset[s]_for_adjustment| may be NULL.
21 BASE_EXPORT
bool UTF8ToUTF16AndAdjustOffset(const char* src
,
24 size_t* offset_for_adjustment
);
25 BASE_EXPORT
bool UTF8ToUTF16AndAdjustOffsets(
29 std::vector
<size_t>* offsets_for_adjustment
);
31 BASE_EXPORT string16
UTF8ToUTF16AndAdjustOffset(const base::StringPiece
& utf8
,
32 size_t* offset_for_adjustment
);
33 BASE_EXPORT string16
UTF8ToUTF16AndAdjustOffsets(
34 const base::StringPiece
& utf8
,
35 std::vector
<size_t>* offsets_for_adjustment
);
37 BASE_EXPORT
std::string
UTF16ToUTF8AndAdjustOffset(
38 const base::StringPiece16
& utf16
,
39 size_t* offset_for_adjustment
);
40 BASE_EXPORT
std::string
UTF16ToUTF8AndAdjustOffsets(
41 const base::StringPiece16
& utf16
,
42 std::vector
<size_t>* offsets_for_adjustment
);
44 // Limiting function callable by std::for_each which will replace any value
45 // which is equal to or greater than |limit| with npos.
48 explicit LimitOffset(size_t limit
)
51 void operator()(size_t& offset
) {
59 // Stack object which, on destruction, will update a vector of offsets based on
60 // any supplied adjustments. To use, declare one of these, providing the
61 // address of the offset vector to adjust. Then Add() any number of Adjustments
62 // (each Adjustment gives the |original_offset| of a substring and the lengths
63 // of the substring before and after transforming). When the OffsetAdjuster
64 // goes out of scope, all the offsets in the provided vector will be updated.
65 class BASE_EXPORT OffsetAdjuster
{
67 struct BASE_EXPORT Adjustment
{
68 Adjustment(size_t original_offset
,
69 size_t original_length
,
70 size_t output_length
);
72 size_t original_offset
;
73 size_t original_length
;
77 explicit OffsetAdjuster(std::vector
<size_t>* offsets_for_adjustment
);
80 void Add(const Adjustment
& adjustment
);
83 void AdjustOffset(std::vector
<size_t>::iterator offset
);
85 std::vector
<size_t>* offsets_for_adjustment_
;
86 std::vector
<Adjustment
> adjustments_
;
89 #endif // BASE_UTF_OFFSET_STRING_CONVERSIONS_H_