OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2010 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. |
| 4 |
| 5 #ifndef BASE_I18N_CHAR_ITERATOR_H_ |
| 6 #define BASE_I18N_CHAR_ITERATOR_H_ |
| 7 #pragma once |
| 8 |
| 9 #include <string> |
| 10 |
| 11 #include "base/basictypes.h" |
| 12 #include "base/string16.h" |
| 13 |
| 14 // The CharIterator classes iterate through the characters in UTF8 and |
| 15 // UTF16 strings. Example usage: |
| 16 // |
| 17 // UTF8CharIterator iter(&str); |
| 18 // while (!iter.End()) { |
| 19 // LOG(INFO) << iter.get(); |
| 20 // iter.Advance(); |
| 21 // } |
| 22 |
| 23 namespace base { |
| 24 |
| 25 class UTF8CharIterator { |
| 26 public: |
| 27 // Requires |str| to live as long as the UTF8CharIterator does. |
| 28 UTF8CharIterator(const std::string* str); |
| 29 ~UTF8CharIterator() {} |
| 30 |
| 31 // Return the starting array index of the current character within the |
| 32 // string. |
| 33 int32 array_pos() const { return array_pos_; } |
| 34 |
| 35 // Return the logical index of the current character, independent of the |
| 36 // number of bytes each character takes. |
| 37 int32 char_pos() const { return char_pos_; } |
| 38 |
| 39 // Return the current char. |
| 40 int32 get() const { return char_; } |
| 41 |
| 42 // Returns true if we're at the end of the string. |
| 43 bool end() const { return array_pos_ == len_; } |
| 44 |
| 45 // Advance to the next actual character. Returns false if we're at the |
| 46 // end of the string. |
| 47 bool Advance(); |
| 48 |
| 49 private: |
| 50 // The string we're iterating over. |
| 51 const uint8_t* str_; |
| 52 |
| 53 // The length of the encoded string. |
| 54 int32 len_; |
| 55 |
| 56 // Array index. |
| 57 int32 array_pos_; |
| 58 |
| 59 // The next array index. |
| 60 int32 next_pos_; |
| 61 |
| 62 // Character index. |
| 63 int32 char_pos_; |
| 64 |
| 65 // The current character. |
| 66 int32 char_; |
| 67 |
| 68 DISALLOW_COPY_AND_ASSIGN(UTF8CharIterator); |
| 69 }; |
| 70 |
| 71 class UTF16CharIterator { |
| 72 public: |
| 73 // Requires |str| to live as long as the UTF16CharIterator does. |
| 74 UTF16CharIterator(const string16* str); |
| 75 ~UTF16CharIterator() {} |
| 76 |
| 77 // Return the starting array index of the current character within the |
| 78 // string. |
| 79 int32 array_pos() const { return array_pos_; } |
| 80 |
| 81 // Return the logical index of the current character, independent of the |
| 82 // number of codewords each character takes. |
| 83 int32 char_pos() const { return char_pos_; } |
| 84 |
| 85 // Return the current char. |
| 86 int32 get() const { return char_; } |
| 87 |
| 88 // Returns true if we're at the end of the string. |
| 89 bool end() const { return array_pos_ == len_; } |
| 90 |
| 91 // Advance to the next actual character. Returns false if we're at the |
| 92 // end of the string. |
| 93 bool Advance(); |
| 94 |
| 95 private: |
| 96 // The string we're iterating over. |
| 97 const char16* str_; |
| 98 |
| 99 // The length of the encoded string. |
| 100 int32 len_; |
| 101 |
| 102 // Array index. |
| 103 int32 array_pos_; |
| 104 |
| 105 // The next array index. |
| 106 int32 next_pos_; |
| 107 |
| 108 // Character index. |
| 109 int32 char_pos_; |
| 110 |
| 111 // The current character. |
| 112 int32 char_; |
| 113 |
| 114 DISALLOW_COPY_AND_ASSIGN(UTF16CharIterator); |
| 115 }; |
| 116 |
| 117 } // namespace base |
| 118 |
| 119 #endif // BASE_I18N_CHAR_ITERATOR_H_ |
OLD | NEW |