21 FCITX_INLINE_DEFINE_DEFAULT_DTOR_AND_COPY(
TextPrivate)
23 std::vector<std::tuple<std::string, TextFormatFlags>> texts_;
27 Text::Text() : d_ptr(std::make_unique<TextPrivate>()) {}
30 append(std::move(text), flag);
33 FCITX_DEFINE_DPTR_COPY_AND_DEFAULT_DTOR_AND_MOVE(
Text)
54 throw std::invalid_argument(
"Invalid utf8 string");
56 d->texts_.emplace_back(std::move(str), flag);
59 void Text::append(
Text text) {
61 std::copy(std::make_move_iterator(text.d_ptr->texts_.begin()),
62 std::make_move_iterator(text.d_ptr->texts_.end()),
63 std::back_inserter(d->texts_));
66 const std::string &Text::stringAt(
int idx)
const {
68 return std::get<std::string>(d->texts_[idx]);
73 return std::get<TextFormatFlags>(d->texts_[idx]);
76 size_t Text::size()
const {
78 return d->texts_.size();
81 bool Text::empty()
const {
83 return d->texts_.empty();
86 std::string Text::toString()
const {
89 for (
const auto &p : d->texts_) {
90 result += std::get<std::string>(p);
96 size_t Text::textLength()
const {
99 for (
const auto &p : d->texts_) {
100 length += std::get<std::string>(p).size();
106 std::string Text::toStringForCommit()
const {
109 for (
const auto &p : d->texts_) {
110 if (!(std::get<TextFormatFlags>(p) & TextFormatFlag::DontCommit)) {
111 result += std::get<std::string>(p);
118 std::ostream &operator<<(std::ostream &os,
const Text &text) {
120 for (
size_t i = 0; i < text.size(); i++) {
121 os <<
"<" << text.stringAt(i) <<
", flag=" << text.formatAt(i) <<
">";
122 if (i + 1 != text.size()) {
126 os <<
", cursor=" << text.
cursor() <<
")";
132 std::vector<Text> texts;
134 texts.emplace_back();
135 for (
const auto &p : d->texts_) {
136 if (std::get<std::string>(p).empty()) {
140 stringutils::SplitBehavior::KeepEmpty);
141 auto flag = std::get<TextFormatFlags>(p);
142 texts.back().append(lines[0], flag);
143 for (
size_t i = 1; i < lines.size(); i++) {
144 texts.emplace_back();
145 texts.back().append(lines[i], flag);
158 for (
const auto &[str, format] : d->texts_) {
162 if (curFormat == format) {
165 if (!curStr.empty()) {
166 normalized.append(std::move(curStr), curFormat);
172 if (!curStr.empty()) {
173 normalized.append(std::move(curStr), curFormat);
std::vector< Text > splitByLine() const
Split Text object into lines.
Formatted string commonly used in user interface.
size_t length(Iter start, Iter end)
Return the number UTF-8 characters in the string iterator range.
bool validate(Iter start, Iter end)
Check if the string iterator range is valid utf8 string.
C++ Utility functions for handling utf8 strings.
std::vector< std::string > split(std::string_view str, std::string_view delim, SplitBehavior behavior)
Split the string by delim.
A class represents a formatted string.
Text normalize() const
Remove empty string piece and merge the string with same format.
void setCursor(int pos=-1)
Set cursor by byte.
Class provides bit flag support for Enum.
int cursor() const
Get cursor by byte.