FifeGUI 0.2.0
A C++ GUI library designed for games.
utf8stringeditor.cpp
1// SPDX-License-Identifier: LGPL-2.1-or-later OR BSD-3-Clause
2// SPDX-FileCopyrightText: 2004 - 2008 Olof Naessén and Per Larsson
3// SPDX-FileCopyrightText: 2013 - 2026 Fifengine contributors
4
5#include <fifechan/utf8stringeditor.hpp>
6#include <fifechan/util/utf8/utf8.hpp>
7
8#include <string>
9
10namespace fcn
11{
12
13 int UTF8StringEditor::nextChar(std::string const & text, int byteOffset)
14 {
15 std::string::const_iterator c;
16 std::string::const_iterator e;
17
18 c = text.begin() + byteOffset;
19 e = text.end();
20
21 utf8::next(c, e);
22 return std::string(text.begin(), c).size();
23 }
24
25 int UTF8StringEditor::prevChar(std::string const & text, int byteOffset)
26 {
27 std::string::const_iterator c;
28 std::string::const_iterator b;
29
30 c = text.begin() + byteOffset;
31 b = text.begin();
32
33 utf8::prior(c, b);
34 return std::string(b, c).size();
35 }
36
37 int UTF8StringEditor::eraseChar(std::string& text, int byteOffset)
38 {
39 std::string::iterator begin;
40 std::string::iterator cur;
41 begin = text.begin() + byteOffset;
42 cur = begin;
43 utf8::next(cur, text.end());
44
45 text = std::string(text.begin(), begin) + std::string(cur, text.end());
46 return byteOffset; // this shouldn't change!
47 }
48
49 int UTF8StringEditor::insertChar(std::string& text, int byteOffset, int ch)
50 {
51 std::string newText;
52 std::string::iterator cut;
53 int newOffset = 0;
54
55 // make a temp string from left part of the caret (+6 extra chars)
56 newText = text.substr(0, byteOffset) + " ";
57 // append character
58 utf8::append(ch, newText.begin() + byteOffset);
59 // calculate newText real length
60 cut = newText.begin() + byteOffset;
61 utf8::next(cut, newText.end());
62 // cut the string to real length
63 newText = std::string(newText.begin(), cut);
64 newOffset = newText.size();
65 // make new text
66 text = newText + text.substr(byteOffset);
67
68 return newOffset;
69 }
70
71 int UTF8StringEditor::countChars(std::string const & text, int byteOffset)
72 {
73 return utf8::distance(text.begin(), text.begin() + byteOffset);
74 }
75
76 int UTF8StringEditor::getOffset(std::string const & text, int charIndex)
77 {
78 std::string::const_iterator cur;
79 std::string::const_iterator end;
80 int i = 0;
81
82 if (charIndex < 0) {
83 return 0;
84 }
85
86 cur = text.begin();
87 end = text.end();
88
89 for (i = 0; i < charIndex && cur != end; i++) {
90 utf8::next(cur, end);
91 }
92
93 return std::string(text.begin(), cur).size();
94 }
95}; // namespace fcn
static int insertChar(std::string &text, int byteOffset, int ch)
Insert a character at specified byte offset.
static int countChars(std::string const &text, int byteOffset)
Counts characters up to byteOffset.
static int eraseChar(std::string &text, int byteOffset)
Erase character at specified byte offset.
static int getOffset(std::string const &text, int charIndex)
Gets byte offset for character index.
static int nextChar(std::string const &text, int byteOffset)
Returns byte offset of the next character.
static int prevChar(std::string const &text, int byteOffset)
Returns byte offset of the previous character.