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_STRINGS_UTF_OFFSET_STRING_CONVERSIONS_H_
6 #define BASE_STRINGS_UTF_OFFSET_STRING_CONVERSIONS_H_
11 #include "base/base_export.h"
12 #include "base/string16.h"
13 #include "base/string_piece.h"
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
,
26 size_t* offset_for_adjustment
);
27 BASE_EXPORT
bool UTF8ToUTF16AndAdjustOffsets(
31 std::vector
<size_t>* offsets_for_adjustment
);
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
);
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
);
46 // Limiting function callable by std::for_each which will replace any value
47 // which is equal to or greater than |limit| with npos.
50 explicit LimitOffset(size_t limit
)
53 void operator()(size_t& offset
) {
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
{
69 struct BASE_EXPORT Adjustment
{
70 Adjustment(size_t original_offset
,
71 size_t original_length
,
72 size_t output_length
);
74 size_t original_offset
;
75 size_t original_length
;
79 explicit OffsetAdjuster(std::vector
<size_t>* offsets_for_adjustment
);
82 void Add(const Adjustment
& adjustment
);
85 void AdjustOffset(std::vector
<size_t>::iterator offset
);
87 std::vector
<size_t>* offsets_for_adjustment_
;
88 std::vector
<Adjustment
> adjustments_
;
93 #endif // BASE_STRINGS_UTF_OFFSET_STRING_CONVERSIONS_H_