| OLD | NEW |
| 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_I18N_CHAR_ITERATOR_H_ | 5 #ifndef BASE_I18N_CHAR_ITERATOR_H_ |
| 6 #define BASE_I18N_CHAR_ITERATOR_H_ | 6 #define BASE_I18N_CHAR_ITERATOR_H_ |
| 7 | 7 |
| 8 #include <string> | 8 #include <string> |
| 9 | 9 |
| 10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
| (...skipping 15 matching lines...) Expand all Loading... |
| 26 | 26 |
| 27 namespace base { | 27 namespace base { |
| 28 namespace i18n { | 28 namespace i18n { |
| 29 | 29 |
| 30 class BASE_I18N_EXPORT UTF8CharIterator { | 30 class BASE_I18N_EXPORT UTF8CharIterator { |
| 31 public: | 31 public: |
| 32 // Requires |str| to live as long as the UTF8CharIterator does. | 32 // Requires |str| to live as long as the UTF8CharIterator does. |
| 33 explicit UTF8CharIterator(const std::string* str); | 33 explicit UTF8CharIterator(const std::string* str); |
| 34 ~UTF8CharIterator(); | 34 ~UTF8CharIterator(); |
| 35 | 35 |
| 36 // Return the starting array index of the current character within the | 36 // Returns the starting array index of the current character within the |
| 37 // string. | 37 // string. |
| 38 int32 array_pos() const { return array_pos_; } | 38 int32 array_pos() const { return array_pos_; } |
| 39 | 39 |
| 40 // Return the logical index of the current character, independent of the | 40 // Returns the logical index of the current character, independent of the |
| 41 // number of bytes each character takes. | 41 // number of bytes each character takes. |
| 42 int32 char_pos() const { return char_pos_; } | 42 int32 char_pos() const { return char_pos_; } |
| 43 | 43 |
| 44 // Return the current char. | 44 // Returns the current char. |
| 45 int32 get() const { return char_; } | 45 int32 get() const { return char_; } |
| 46 | 46 |
| 47 // Returns true if we're at the end of the string. | 47 // Returns true if we're at the end of the string. |
| 48 bool end() const { return array_pos_ == len_; } | 48 bool end() const { return array_pos_ == len_; } |
| 49 | 49 |
| 50 // Advance to the next actual character. Returns false if we're at the | 50 // Advance to the next actual character. Returns false if we're at the |
| 51 // end of the string. | 51 // end of the string. |
| 52 bool Advance(); | 52 bool Advance(); |
| 53 | 53 |
| 54 // Set the iterator to the specified |position|. If |position| is not a code |
| 55 // point boundary, the position is set to the beginning of the code point. |
| 56 // Calling this function invalidates all future |char_pos()| results. |
| 57 void SetPosition(int32 position); |
| 58 |
| 54 private: | 59 private: |
| 55 // The string we're iterating over. | 60 // The string we're iterating over. |
| 56 const uint8_t* str_; | 61 const uint8_t* str_; |
| 57 | 62 |
| 58 // The length of the encoded string. | 63 // The length of the encoded string. |
| 59 int32 len_; | 64 int32 len_; |
| 60 | 65 |
| 61 // Array index. | 66 // Array index. |
| 62 int32 array_pos_; | 67 int32 array_pos_; |
| 63 | 68 |
| 64 // The next array index. | 69 // The next array index. |
| 65 int32 next_pos_; | 70 int32 next_pos_; |
| 66 | 71 |
| 67 // Character index. | 72 // Character index. |
| 68 int32 char_pos_; | 73 int32 char_pos_; |
| 69 | 74 |
| 70 // The current character. | 75 // The current character. |
| 71 int32 char_; | 76 int32 char_; |
| 72 | 77 |
| 73 DISALLOW_COPY_AND_ASSIGN(UTF8CharIterator); | 78 DISALLOW_COPY_AND_ASSIGN(UTF8CharIterator); |
| 74 }; | 79 }; |
| 75 | 80 |
| 76 class BASE_I18N_EXPORT UTF16CharIterator { | 81 class BASE_I18N_EXPORT UTF16CharIterator { |
| 77 public: | 82 public: |
| 78 // Requires |str| to live as long as the UTF16CharIterator does. | 83 // Requires |str| to live as long as the UTF16CharIterator does. |
| 79 explicit UTF16CharIterator(const string16* str); | 84 explicit UTF16CharIterator(const string16* str); |
| 80 UTF16CharIterator(const char16* str, size_t str_len); | 85 UTF16CharIterator(const char16* str, size_t str_len); |
| 81 ~UTF16CharIterator(); | 86 ~UTF16CharIterator(); |
| 82 | 87 |
| 83 // Return the starting array index of the current character within the | 88 // Returns the starting array index of the current character within the |
| 84 // string. | 89 // string. |
| 85 int32 array_pos() const { return array_pos_; } | 90 int32 array_pos() const { return array_pos_; } |
| 86 | 91 |
| 87 // Return the logical index of the current character, independent of the | 92 // Returns the logical index of the current character, independent of the |
| 88 // number of codewords each character takes. | 93 // number of codewords each character takes. |
| 89 int32 char_pos() const { return char_pos_; } | 94 int32 char_pos() const { return char_pos_; } |
| 90 | 95 |
| 91 // Return the current char. | 96 // Returns the current char. |
| 92 int32 get() const { return char_; } | 97 int32 get() const { return char_; } |
| 93 | 98 |
| 94 // Returns true if we're at the end of the string. | 99 // Returns true if we're at the end of the string. |
| 95 bool end() const { return array_pos_ == len_; } | 100 bool end() const { return array_pos_ == len_; } |
| 96 | 101 |
| 97 // Advance to the next actual character. Returns false if we're at the | 102 // Advance to the next actual character. Returns false if we're at the |
| 98 // end of the string. | 103 // end of the string. |
| 99 bool Advance(); | 104 bool Advance(); |
| 100 | 105 |
| 106 // Set the iterator to the specified |position|. If |position| is not a code |
| 107 // point boundary, the position is set to the beginning of the code point. |
| 108 // Calling this function invalidates all future |char_pos()| results. |
| 109 void SetPosition(int32 position); |
| 110 |
| 101 private: | 111 private: |
| 102 // Fills in the current character we found and advances to the next | 112 // Fills in the current character we found and advances to the next |
| 103 // character, updating all flags as necessary. | 113 // character, updating all flags as necessary. |
| 104 void ReadChar(); | 114 void ReadChar(); |
| 105 | 115 |
| 106 // The string we're iterating over. | 116 // The string we're iterating over. |
| 107 const char16* str_; | 117 const char16* str_; |
| 108 | 118 |
| 109 // The length of the encoded string. | 119 // The length of the encoded string. |
| 110 int32 len_; | 120 int32 len_; |
| (...skipping 10 matching lines...) Expand all Loading... |
| 121 // The current character. | 131 // The current character. |
| 122 int32 char_; | 132 int32 char_; |
| 123 | 133 |
| 124 DISALLOW_COPY_AND_ASSIGN(UTF16CharIterator); | 134 DISALLOW_COPY_AND_ASSIGN(UTF16CharIterator); |
| 125 }; | 135 }; |
| 126 | 136 |
| 127 } // namespace i18n | 137 } // namespace i18n |
| 128 } // namespace base | 138 } // namespace base |
| 129 | 139 |
| 130 #endif // BASE_I18N_CHAR_ITERATOR_H_ | 140 #endif // BASE_I18N_CHAR_ITERATOR_H_ |
| OLD | NEW |