1 | // |
---|
2 | // TextIterator.h |
---|
3 | // |
---|
4 | // $Id: //poco/1.3/Foundation/include/Poco/TextIterator.h#1 $ |
---|
5 | // |
---|
6 | // Library: Foundation |
---|
7 | // Package: Text |
---|
8 | // Module: TextIterator |
---|
9 | // |
---|
10 | // Definition of the TextIterator class. |
---|
11 | // |
---|
12 | // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH. |
---|
13 | // and Contributors. |
---|
14 | // |
---|
15 | // Permission is hereby granted, free of charge, to any person or organization |
---|
16 | // obtaining a copy of the software and accompanying documentation covered by |
---|
17 | // this license (the "Software") to use, reproduce, display, distribute, |
---|
18 | // execute, and transmit the Software, and to prepare derivative works of the |
---|
19 | // Software, and to permit third-parties to whom the Software is furnished to |
---|
20 | // do so, all subject to the following: |
---|
21 | // |
---|
22 | // The copyright notices in the Software and this entire statement, including |
---|
23 | // the above license grant, this restriction and the following disclaimer, |
---|
24 | // must be included in all copies of the Software, in whole or in part, and |
---|
25 | // all derivative works of the Software, unless such copies or derivative |
---|
26 | // works are solely in the form of machine-executable object code generated by |
---|
27 | // a source language processor. |
---|
28 | // |
---|
29 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
---|
30 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
---|
31 | // FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT |
---|
32 | // SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE |
---|
33 | // FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, |
---|
34 | // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
---|
35 | // DEALINGS IN THE SOFTWARE. |
---|
36 | // |
---|
37 | |
---|
38 | |
---|
39 | #ifndef Foundation_TextIterator_INCLUDED |
---|
40 | #define Foundation_TextIterator_INCLUDED |
---|
41 | |
---|
42 | |
---|
43 | #include "Poco/Foundation.h" |
---|
44 | |
---|
45 | |
---|
46 | namespace Poco { |
---|
47 | |
---|
48 | |
---|
49 | class TextEncoding; |
---|
50 | |
---|
51 | |
---|
52 | class Foundation_API TextIterator |
---|
53 | /// An unidirectional iterator for iterating over characters in a string. |
---|
54 | /// The TextIterator uses a TextEncoding object to |
---|
55 | /// work with multi-byte character encodings like UTF-8. |
---|
56 | /// Characters are reported in Unicode. |
---|
57 | /// |
---|
58 | /// Example: Count the number of UTF-8 characters in a string. |
---|
59 | /// |
---|
60 | /// UTF8Encoding utf8Encoding; |
---|
61 | /// std::string utf8String("...."); |
---|
62 | /// TextIterator it(utf8String, utf8Encoding); |
---|
63 | /// TextIterator end(utf8String); |
---|
64 | /// int n = 0; |
---|
65 | /// while (it != end) { ++n; ++it; } |
---|
66 | /// |
---|
67 | /// NOTE: When an UTF-16 encoding is used, surrogate pairs will be |
---|
68 | /// reported as two separate characters, due to restrictions of |
---|
69 | /// the TextEncoding class. |
---|
70 | { |
---|
71 | public: |
---|
72 | TextIterator(); |
---|
73 | /// Creates an uninitialized TextIterator. |
---|
74 | |
---|
75 | TextIterator(const std::string& str, const TextEncoding& encoding); |
---|
76 | /// Creates a TextIterator for the given string. |
---|
77 | /// The encoding object must not be deleted as long as the iterator |
---|
78 | /// is in use. |
---|
79 | |
---|
80 | TextIterator(const std::string::const_iterator& begin, const std::string::const_iterator& end, const TextEncoding& encoding); |
---|
81 | /// Creates a TextIterator for the given range. |
---|
82 | /// The encoding object must not be deleted as long as the iterator |
---|
83 | /// is in use. |
---|
84 | |
---|
85 | TextIterator(const std::string& str); |
---|
86 | /// Creates an end TextIterator for the given string. |
---|
87 | |
---|
88 | TextIterator(const std::string::const_iterator& end); |
---|
89 | /// Creates an end TextIterator. |
---|
90 | |
---|
91 | ~TextIterator(); |
---|
92 | /// Destroys the TextIterator. |
---|
93 | |
---|
94 | TextIterator(const TextIterator& it); |
---|
95 | /// Copy constructor. |
---|
96 | |
---|
97 | TextIterator& operator = (const TextIterator& it); |
---|
98 | /// Assignment operator. |
---|
99 | |
---|
100 | void swap(TextIterator& it); |
---|
101 | /// Swaps the iterator with another one. |
---|
102 | |
---|
103 | int operator * () const; |
---|
104 | /// Returns the Unicode value of the current character. |
---|
105 | /// If there is no valid character at the current position, |
---|
106 | /// -1 is returned. |
---|
107 | |
---|
108 | TextIterator& operator ++ (); |
---|
109 | /// Prefix increment operator. |
---|
110 | |
---|
111 | TextIterator operator ++ (int); |
---|
112 | /// Postfix increment operator. |
---|
113 | |
---|
114 | bool operator == (const TextIterator& it) const; |
---|
115 | /// Compares two iterators for equality. |
---|
116 | |
---|
117 | bool operator != (const TextIterator& it) const; |
---|
118 | /// Compares two iterators for inequality. |
---|
119 | |
---|
120 | private: |
---|
121 | const TextEncoding* _pEncoding; |
---|
122 | std::string::const_iterator _it; |
---|
123 | std::string::const_iterator _end; |
---|
124 | }; |
---|
125 | |
---|
126 | |
---|
127 | // |
---|
128 | // inlines |
---|
129 | // |
---|
130 | inline bool TextIterator::operator == (const TextIterator& it) const |
---|
131 | { |
---|
132 | return _it == it._it; |
---|
133 | } |
---|
134 | |
---|
135 | |
---|
136 | inline bool TextIterator::operator != (const TextIterator& it) const |
---|
137 | { |
---|
138 | return _it != it._it; |
---|
139 | } |
---|
140 | |
---|
141 | |
---|
142 | inline void swap(TextIterator& it1, TextIterator& it2) |
---|
143 | { |
---|
144 | it1.swap(it2); |
---|
145 | } |
---|
146 | |
---|
147 | |
---|
148 | } // namespace Poco |
---|
149 | |
---|
150 | |
---|
151 | #endif // Foundation_TextIterator_INCLUDED |
---|