4 * This file is part of OpenTTD.
5 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
6 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
7 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
13 #include "string_type.h"
15 /** Class for iterating over different kind of parts of a string. */
16 class StringIterator
{
18 /** Type of the iterator. */
20 ITER_CHARACTER
, ///< Iterate over characters (or more exactly grapheme clusters).
21 ITER_WORD
, ///< Iterate over words.
24 /** Sentinel to indicate end-of-iteration. */
25 static const size_t END
= SIZE_MAX
;
28 * Create a new iterator instance.
29 * @return New iterator instance.
31 static StringIterator
*Create();
33 virtual ~StringIterator() {}
36 * Set a new iteration string. Must also be called if the string contents
37 * changed. The cursor is reset to the start of the string.
38 * @param s New string.
40 virtual void SetString(const char *s
) = 0;
43 * Change the current string cursor.
44 * @param p New cursor position.
45 * @return Actual new cursor position at the next valid character boundary.
46 * @pre p has to be inside the current string.
48 virtual size_t SetCurPosition(size_t pos
) = 0;
51 * Advance the cursor by one iteration unit.
52 * @return New cursor position (in bytes) or #END if the cursor is already at the end of the string.
54 virtual size_t Next(IterType what
= ITER_CHARACTER
) = 0;
57 * Move the cursor back by one iteration unit.
58 * @return New cursor position (in bytes) or #END if the cursor is already at the start of the string.
60 virtual size_t Prev(IterType what
= ITER_CHARACTER
) = 0;
66 #endif /* STRING_BASE_H */