summaryrefslogtreecommitdiffstats
path: root/base/i18n/char_iterator.h
blob: f45b04b8b06b154dbf41e0acfa85207179533f2e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef BASE_I18N_CHAR_ITERATOR_H_
#define BASE_I18N_CHAR_ITERATOR_H_
#pragma once

#include <string>

#include "base/basictypes.h"
#include "base/string16.h"

// The CharIterator classes iterate through the characters in UTF8 and
// UTF16 strings.  Example usage:
//
//   UTF8CharIterator iter(&str);
//   while (!iter.End()) {
//     VLOG(1) << iter.get();
//     iter.Advance();
//   }

#if defined(OS_WIN)
typedef unsigned char uint8_t;
#endif

namespace base {

class UTF8CharIterator {
 public:
  // Requires |str| to live as long as the UTF8CharIterator does.
  UTF8CharIterator(const std::string* str);
  ~UTF8CharIterator() {}

  // Return the starting array index of the current character within the
  // string.
  int32 array_pos() const { return array_pos_; }

  // Return the logical index of the current character, independent of the
  // number of bytes each character takes.
  int32 char_pos() const { return char_pos_; }

  // Return the current char.
  int32 get() const { return char_; }

  // Returns true if we're at the end of the string.
  bool end() const { return array_pos_ == len_; }

  // Advance to the next actual character.  Returns false if we're at the
  // end of the string.
  bool Advance();

 private:
  // The string we're iterating over.
  const uint8_t* str_;

  // The length of the encoded string.
  int32 len_;

  // Array index.
  int32 array_pos_;

  // The next array index.
  int32 next_pos_;

  // Character index.
  int32 char_pos_;

  // The current character.
  int32 char_;

  DISALLOW_COPY_AND_ASSIGN(UTF8CharIterator);
};

class UTF16CharIterator {
 public:
  // Requires |str| to live as long as the UTF16CharIterator does.
  UTF16CharIterator(const string16* str);
  UTF16CharIterator(const char16* str, size_t str_len);
  ~UTF16CharIterator() {}

  // Return the starting array index of the current character within the
  // string.
  int32 array_pos() const { return array_pos_; }

  // Return the logical index of the current character, independent of the
  // number of codewords each character takes.
  int32 char_pos() const { return char_pos_; }

  // Return the current char.
  int32 get() const { return char_; }

  // Returns true if we're at the end of the string.
  bool end() const { return array_pos_ == len_; }

  // Advance to the next actual character.  Returns false if we're at the
  // end of the string.
  bool Advance();

 private:
  // Fills in the current character we found and advances to the next
  // character, updating all flags as necessary.
  void ReadChar();

  // The string we're iterating over.
  const char16* str_;

  // The length of the encoded string.
  int32 len_;

  // Array index.
  int32 array_pos_;

  // The next array index.
  int32 next_pos_;

  // Character index.
  int32 char_pos_;

  // The current character.
  int32 char_;

  DISALLOW_COPY_AND_ASSIGN(UTF16CharIterator);
};

}  // namespace base

#endif  // BASE_I18N_CHAR_ITERATOR_H_