Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(109)

Side by Side Diff: base/strings/utf_offset_string_conversions.h

Issue 23619016: Switch the offset conversion routines from... (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: Created 7 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #ifndef BASE_STRINGS_UTF_OFFSET_STRING_CONVERSIONS_H_ 5 #ifndef BASE_STRINGS_UTF_OFFSET_STRING_CONVERSIONS_H_
6 #define BASE_STRINGS_UTF_OFFSET_STRING_CONVERSIONS_H_ 6 #define BASE_STRINGS_UTF_OFFSET_STRING_CONVERSIONS_H_
7 7
8 #include <string> 8 #include <string>
9 #include <vector> 9 #include <vector>
10 10
11 #include "base/base_export.h" 11 #include "base/base_export.h"
12 #include "base/strings/string16.h" 12 #include "base/strings/string16.h"
13 #include "base/strings/string_piece.h" 13 #include "base/strings/string_piece.h"
14 14
15 namespace base { 15 namespace base {
16 16
17 // Like the conversions in utf_string_conversions.h, but also takes one or more 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 18 // |offset[s]_for_adjustment| representing insertion/selection points between
19 // will be adjusted to point at the same logical place in the result strings. 19 // characters: if |src| is "abcd", then 0 is before 'a', 2 is between 'b' and
20 // If this isn't possible because an offset points past the end of the source 20 // 'c', and 4 is at the end of the string. Valid input offsets range from 0 to
21 // strings or into the middle of a multibyte sequence, the offending offset will 21 // |src_len|. On exit, each offset will have been modified to point at the same
22 // be set to string16::npos. |offset[s]_for_adjustment| may be NULL. 22 // logical position in the output string. If an offset cannot be successfully
23 // adjusted (e.g. because it points into the middle of a multibyte sequence), it
24 // will be set to string16::npos.
25 //
26 // |offset[s]_for_adjustment| may be NULL.
23 BASE_EXPORT bool UTF8ToUTF16AndAdjustOffset(const char* src, 27 BASE_EXPORT bool UTF8ToUTF16AndAdjustOffset(const char* src,
24 size_t src_len, 28 size_t src_len,
25 string16* output, 29 string16* output,
26 size_t* offset_for_adjustment); 30 size_t* offset_for_adjustment);
27 BASE_EXPORT bool UTF8ToUTF16AndAdjustOffsets( 31 BASE_EXPORT bool UTF8ToUTF16AndAdjustOffsets(
28 const char* src, 32 const char* src,
29 size_t src_len, 33 size_t src_len,
30 string16* output, 34 string16* output,
31 std::vector<size_t>* offsets_for_adjustment); 35 std::vector<size_t>* offsets_for_adjustment);
32 36
33 BASE_EXPORT string16 UTF8ToUTF16AndAdjustOffset(const base::StringPiece& utf8, 37 BASE_EXPORT string16 UTF8ToUTF16AndAdjustOffset(const base::StringPiece& utf8,
34 size_t* offset_for_adjustment); 38 size_t* offset_for_adjustment);
35 BASE_EXPORT string16 UTF8ToUTF16AndAdjustOffsets( 39 BASE_EXPORT string16 UTF8ToUTF16AndAdjustOffsets(
36 const base::StringPiece& utf8, 40 const base::StringPiece& utf8,
37 std::vector<size_t>* offsets_for_adjustment); 41 std::vector<size_t>* offsets_for_adjustment);
38 42
39 BASE_EXPORT std::string UTF16ToUTF8AndAdjustOffset( 43 BASE_EXPORT std::string UTF16ToUTF8AndAdjustOffset(
40 const base::StringPiece16& utf16, 44 const base::StringPiece16& utf16,
41 size_t* offset_for_adjustment); 45 size_t* offset_for_adjustment);
42 BASE_EXPORT std::string UTF16ToUTF8AndAdjustOffsets( 46 BASE_EXPORT std::string UTF16ToUTF8AndAdjustOffsets(
43 const base::StringPiece16& utf16, 47 const base::StringPiece16& utf16,
44 std::vector<size_t>* offsets_for_adjustment); 48 std::vector<size_t>* offsets_for_adjustment);
45 49
46 // Limiting function callable by std::for_each which will replace any value 50 // Limiting function callable by std::for_each which will replace any value
47 // which is equal to or greater than |limit| with npos. 51 // which is greater than |limit| with npos. Typically this is called with a
52 // string length to clamp offsets into the string to [0, length] (as opposed to
53 // [0, length); see comments above).
48 template <typename T> 54 template <typename T>
49 struct LimitOffset { 55 struct LimitOffset {
50 explicit LimitOffset(size_t limit) 56 explicit LimitOffset(size_t limit)
51 : limit_(limit) {} 57 : limit_(limit) {}
52 58
53 void operator()(size_t& offset) { 59 void operator()(size_t& offset) {
54 if (offset >= limit_) 60 if (offset > limit_)
55 offset = T::npos; 61 offset = T::npos;
56 } 62 }
57 63
58 size_t limit_; 64 size_t limit_;
59 }; 65 };
60 66
61 // Stack object which, on destruction, will update a vector of offsets based on 67 // 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 68 // 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 69 // 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 70 // (each Adjustment gives the |original_offset| of a substring and the lengths
(...skipping 19 matching lines...) Expand all
84 private: 90 private:
85 void AdjustOffset(std::vector<size_t>::iterator offset); 91 void AdjustOffset(std::vector<size_t>::iterator offset);
86 92
87 std::vector<size_t>* offsets_for_adjustment_; 93 std::vector<size_t>* offsets_for_adjustment_;
88 std::vector<Adjustment> adjustments_; 94 std::vector<Adjustment> adjustments_;
89 }; 95 };
90 96
91 } // namespace base 97 } // namespace base
92 98
93 #endif // BASE_STRINGS_UTF_OFFSET_STRING_CONVERSIONS_H_ 99 #endif // BASE_STRINGS_UTF_OFFSET_STRING_CONVERSIONS_H_
OLDNEW
« no previous file with comments | « no previous file | base/strings/utf_offset_string_conversions.cc » ('j') | net/base/net_util_unittest.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698