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
7#include <string>
8
9#include <utf8.h>
10
11namespace fcn
12{
13
14 int UTF8StringEditor::nextChar(std::string const & text, int byteOffset)
15 {
16 std::string::const_iterator c;
17 std::string::const_iterator e;
18
19 c = text.begin() + byteOffset;
20 e = text.end();
21
22 utf8::next(c, e);
23 return std::string(text.begin(), c).size();
24 }
25
26 int UTF8StringEditor::prevChar(std::string const & text, int byteOffset)
27 {
28 std::string::const_iterator c;
29 std::string::const_iterator b;
30
31 c = text.begin() + byteOffset;
32 b = text.begin();
33
34 utf8::prior(c, b);
35 return std::string(b, c).size();
36 }
37
38 int UTF8StringEditor::eraseChar(std::string& text, int byteOffset)
39 {
40 std::string::iterator begin;
41 std::string::iterator cur;
42 begin = text.begin() + byteOffset;
43 cur = begin;
44 utf8::next(cur, text.end());
45
46 text = std::string(text.begin(), begin) + std::string(cur, text.end());
47 return byteOffset; // this shouldn't change!
48 }
49
50 int UTF8StringEditor::insertChar(std::string& text, int byteOffset, int ch)
51 {
52 std::string newText;
53 std::string::iterator cut;
54 int newOffset = 0;
55
56 // make a temp string from left part of the caret (+6 extra chars)
57 newText = text.substr(0, byteOffset) + " ";
58 // append character
59 utf8::append(ch, newText.begin() + byteOffset);
60 // calculate newText real length
61 cut = newText.begin() + byteOffset;
62 utf8::next(cut, newText.end());
63 // cut the string to real length
64 newText = std::string(newText.begin(), cut);
65 newOffset = newText.size();
66 // make new text
67 text = newText + text.substr(byteOffset);
68
69 return newOffset;
70 }
71
72 int UTF8StringEditor::countChars(std::string const & text, int byteOffset)
73 {
74 return utf8::distance(text.begin(), text.begin() + byteOffset);
75 }
76
77 int UTF8StringEditor::getOffset(std::string const & text, int charIndex)
78 {
79 std::string::const_iterator cur;
80 std::string::const_iterator end;
81 int i = 0;
82
83 if (charIndex < 0) {
84 return 0;
85 }
86
87 cur = text.begin();
88 end = text.end();
89
90 for (i = 0; i < charIndex && cur != end; i++) {
91 utf8::next(cur, end);
92 }
93
94 return std::string(text.begin(), cur).size();
95 }
96}; // 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.