6#include <fifechan/utf8stringeditor.hpp>
14#include <utf8cpp/utf8.h>
21 assert(
"byteOffset is non-negative" && byteOffset >= 0);
22 assert(
"byteOffset is within text" && std::cmp_less(byteOffset, text.size()));
23 assert(
"text is valid utf8" && utf8::is_valid(text.begin(), text.end()));
25 std::string::const_iterator c;
26 std::string::const_iterator e;
28 c = text.begin() + byteOffset;
32 return std::string(text.begin(), c).size();
37 assert(
"byteOffset is positive" && byteOffset > 0);
38 assert(
"byteOffset is within text" && std::cmp_less_equal(byteOffset, text.size()));
39 assert(
"text is valid utf8" && utf8::is_valid(text.begin(), text.end()));
41 std::string::const_iterator c;
42 std::string::const_iterator b;
44 c = text.begin() + byteOffset;
48 return std::string(b, c).size();
53 assert(
"byteOffset is within text" && byteOffset >= 0 && std::cmp_less(byteOffset, text.size()));
54 assert(
"text is valid utf8" && utf8::is_valid(text.begin(), text.end()));
56 std::string::iterator begin;
57 std::string::iterator cur;
58 begin = text.begin() + byteOffset;
60 utf8::next(cur, text.end());
62 text = std::string(text.begin(), begin) + std::string(cur, text.end());
69 assert(
"byteOffset is non-negative" && byteOffset >= 0);
70 assert(
"byteOffset is within text" && std::cmp_less_equal(byteOffset, text.size()));
71 assert(
"character is valid unicode code point" && ch >= 0 && ch <= 0x10FFFF && (ch < 0xD800 || ch > 0xDFFF));
74 std::string::iterator cut;
77 newText = text.substr(0, byteOffset) +
" ";
78 utf8::append(ch, newText.begin() + byteOffset);
79 cut = newText.begin() + byteOffset;
80 utf8::next(cut, newText.end());
81 newText.erase(cut, newText.end());
82 newOffset = newText.size();
83 text = newText + text.substr(byteOffset);
85 assert(
"result is valid utf8" && utf8::is_valid(text.begin(), text.end()));
92 assert(
"byteOffset is non-negative" && byteOffset >= 0);
93 assert(
"byteOffset is within text bounds" && std::cmp_less_equal(byteOffset, text.size()));
94 assert(
"text is valid utf8" && utf8::is_valid(text.begin(), text.end()));
96 return utf8::distance(text.begin(), text.begin() + byteOffset);
101 assert(
"charIndex is non-negative" && charIndex >= 0);
102 assert(
"text is valid utf8" && utf8::is_valid(text.begin(), text.end()));
104 std::string::const_iterator cur;
105 std::string::const_iterator end;
115 for (i = 0; i < charIndex && cur != end; i++) {
116 utf8::next(cur, end);
119 return std::string(text.begin(), cur).size();
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.
Used replacement tokens by configure_file():