OpenTTD Source  20241121-master-g67a0fccfad
string_base.h
1 /*
2  * This file is part of OpenTTD.
3  * 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.
4  * 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.
5  * 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/>.
6  */
7 
8 #ifndef STRING_BASE_H
9 #define STRING_BASE_H
10 
11 #include "string_type.h"
12 
15 public:
17  enum IterType {
20  };
21 
23  static const size_t END = SIZE_MAX;
24 
29  static std::unique_ptr<StringIterator> Create();
30 
31  virtual ~StringIterator() = default;
32 
38  virtual void SetString(const char *s) = 0;
39 
46  virtual size_t SetCurPosition(size_t pos) = 0;
47 
52  virtual size_t Next(IterType what = ITER_CHARACTER) = 0;
53 
58  virtual size_t Prev(IterType what = ITER_CHARACTER) = 0;
59 
60 protected:
61  StringIterator() {}
62 };
63 
64 #endif /* STRING_BASE_H */
Class for iterating over different kind of parts of a string.
Definition: string_base.h:14
static const size_t END
Sentinel to indicate end-of-iteration.
Definition: string_base.h:23
virtual size_t Prev(IterType what=ITER_CHARACTER)=0
Move the cursor back by one iteration unit.
virtual size_t SetCurPosition(size_t pos)=0
Change the current string cursor.
virtual size_t Next(IterType what=ITER_CHARACTER)=0
Advance the cursor by one iteration unit.
IterType
Type of the iterator.
Definition: string_base.h:17
@ ITER_WORD
Iterate over words.
Definition: string_base.h:19
@ ITER_CHARACTER
Iterate over characters (or more exactly grapheme clusters).
Definition: string_base.h:18
static std::unique_ptr< StringIterator > Create()
Create a new iterator instance.
Definition: string.cpp:917
virtual void SetString(const char *s)=0
Set a new iteration string.
Types for strings.