18 #include "fcitx-utils/macros.h" 28 FCITX_INLINE_DEFINE_DEFAULT_DTOR_AND_COPY(
TextPrivate)
30 std::vector<std::tuple<std::string, TextFormatFlags>> texts_;
34 Text::Text() : d_ptr(std::make_unique<TextPrivate>()) {}
37 append(std::move(text), flag);
40 FCITX_DEFINE_DPTR_COPY_AND_DEFAULT_DTOR_AND_MOVE(
Text)
61 d->texts_.emplace_back(std::move(str), flag);
64 void Text::append(
Text text) {
66 std::copy(std::make_move_iterator(text.d_ptr->texts_.begin()),
67 std::make_move_iterator(text.d_ptr->texts_.end()),
68 std::back_inserter(d->texts_));
71 const std::string &Text::stringAt(
int idx)
const {
73 return std::get<std::string>(d->texts_[idx]);
78 return std::get<TextFormatFlags>(d->texts_[idx]);
81 size_t Text::size()
const {
83 return d->texts_.size();
86 bool Text::empty()
const {
88 return d->texts_.empty();
91 std::string Text::toString()
const {
94 for (
const auto &p : d->texts_) {
95 result += std::get<std::string>(p);
101 size_t Text::textLength()
const {
104 for (
const auto &p : d->texts_) {
105 length += std::get<std::string>(p).size();
111 std::string Text::toStringForCommit()
const {
114 for (
const auto &p : d->texts_) {
115 if (!(std::get<TextFormatFlags>(p) & TextFormatFlag::DontCommit)) {
116 result += std::get<std::string>(p);
123 std::ostream &operator<<(std::ostream &os,
const Text &text) {
125 for (
size_t i = 0; i < text.size(); i++) {
126 os <<
"<" << text.stringAt(i) <<
", flag=" << text.formatAt(i) <<
">";
127 if (i + 1 != text.size()) {
131 os <<
", cursor=" << text.
cursor() <<
")";
137 std::vector<Text> texts;
139 texts.emplace_back();
140 for (
const auto &p : d->texts_) {
141 if (std::get<std::string>(p).empty()) {
145 stringutils::SplitBehavior::KeepEmpty);
146 auto flag = std::get<TextFormatFlags>(p);
147 texts.back().append(lines[0], flag);
148 for (
size_t i = 1; i < lines.size(); i++) {
149 texts.emplace_back();
150 texts.back().append(lines[i], flag);
163 for (
const auto &[str, format] : d->texts_) {
167 if (curFormat == format) {
170 if (!curStr.empty()) {
171 normalized.append(std::move(curStr), curFormat);
177 if (!curStr.empty()) {
178 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.
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.
Enum flag for text formatting.
void setCursor(int pos=-1)
Set cursor by byte.
bool replaceInvalidInplace(std::string &str, char replacement)
Replace invalid UTF-8 sequences in-place with given byte.
Class provides bit flag support for Enum.
int cursor() const
Get cursor by byte.