* Delete a part of the text. * @param from Start of the text to delete. * @param to End of the text to delete. * @param update Set to true if the internal state should be updated. */
| 235 | * @param update Set to true if the internal state should be updated. |
| 236 | */ |
| 237 | void Textbuf::DeleteText(uint16_t from, uint16_t to, bool update) |
| 238 | { |
| 239 | assert(from <= to); |
| 240 | |
| 241 | /* Strip marked characters from buffer. */ |
| 242 | this->chars -= static_cast<uint16_t>(Utf8StringLength(std::string_view(this->buf).substr(from, to - from))); |
| 243 | this->buf.erase(from, to - from); |
| 244 | |
| 245 | auto fixup = [&](uint16_t &pos) { |
| 246 | if (pos <= from) return; |
| 247 | if (pos <= to) { |
| 248 | pos = from; |
| 249 | } else { |
| 250 | pos -= to - from; |
| 251 | } |
| 252 | }; |
| 253 | |
| 254 | /* Fixup caret if needed. */ |
| 255 | fixup(this->caretpos); |
| 256 | |
| 257 | /* Fixup marked text if needed. */ |
| 258 | fixup(this->markpos); |
| 259 | fixup(this->markend); |
| 260 | |
| 261 | if (update) { |
| 262 | this->UpdateStringIter(); |
| 263 | this->UpdateCaretPosition(); |
| 264 | this->UpdateMarkedText(); |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | /** |
| 269 | * Discard any marked text. |
no test coverage detected