@@ -270,20 +270,20 @@ std::string ekg::text::line_not_found {'\x1B'};
270270
271271std::string &ekg::text::write (size_t index) {
272272 size_t chunks_size {this ->loaded_chunks .size ()};
273- size_t total_lines {};
274- size_t previous_total_lines {};
273+ size_t total_chars {};
274+ size_t previous_total_chars {};
275275 size_t chunk_size {};
276276
277277 this ->was_audited = true ;
278278
279279 for (size_t it {}; it < chunks_size; it++) {
280280 ekg::io::chunk_t &chunk {this ->loaded_chunks .at (it)};
281281
282- previous_total_lines = total_lines ;
283- total_lines += (chunk_size = chunk.size ());
282+ previous_total_chars = total_chars ;
283+ total_chars += (chunk_size = chunk.size ());
284284
285- if (index < total_lines && (index - previous_total_lines ) < chunk_size) {
286- return chunk.at (index - previous_total_lines );
285+ if (index < total_chars && (index - previous_total_chars ) < chunk_size) {
286+ return chunk.at (index - previous_total_chars );
287287 }
288288 }
289289
@@ -308,31 +308,31 @@ void ekg::text::insert(
308308 }
309309 return ;
310310 }
311+ this ->was_audited = true ;
311312
312313 bool ok {};
313314
314- size_t previous_total_lines {};
315+ size_t previous_total_chars {};
315316 size_t chunk_size {};
316- size_t current_total_lines {};
317+ size_t current_total_chars {};
317318
318319 ekg::io::chunk_t to_swizzle_chunk {};
319320
320- this ->total_lines = 0 ;
321321 for (size_t it {}; it < this ->loaded_chunks .size (); it++) {
322322 ekg::io::chunk_t &chunk {this ->loaded_chunks .at (it)};
323323
324- previous_total_lines = this ->total_lines ;
325- current_total_lines += (chunk_size = chunk.size ());
324+ previous_total_chars = this ->total_chars ;
325+ current_total_chars += (chunk_size = chunk.size ());
326326
327327 if (
328328 !ok
329329 &&
330- index < current_total_lines
330+ index < current_total_chars
331331 &&
332- (index - previous_total_lines ) < chunk_size
332+ (index - previous_total_chars ) < chunk_size
333333 ) {
334334 chunk.insert (
335- chunk.begin () + (index - previous_total_lines ),
335+ chunk.begin () + (index - previous_total_chars ),
336336 to_insert_chunk.begin (),
337337 to_insert_chunk.end ()
338338 );
@@ -381,8 +381,6 @@ void ekg::text::insert(
381381
382382 ok = true ;
383383 }
384-
385- this ->total_lines += this ->loaded_chunks .at (it).size ();
386384 }
387385}
388386
@@ -404,16 +402,22 @@ void ekg::text::erase(
404402 return ;
405403 }
406404
407- if (begin >= this ->total_lines || end >= this -> total_lines || end <= begin ) {
408- throw std::out_of_range (" ekg::text::erase -> begin or end out of index " );
405+ if (begin > this ->total_chars ) {
406+ throw std::out_of_range (" ekg::text::erase: " + std::to_string ( begin) + " , " + std::to_string ( this -> total_chars ) );
409407 return ;
410408 }
411409
412- this ->total_lines -= end - begin;
410+ end = ekg::min (begin + end, this ->total_chars );
411+
412+ this ->should_count = true ;
413+ this ->size ();
414+
415+ this ->total_chars -= end - begin;
416+ this ->was_audited = true ;
413417
414- size_t previous_total_lines {};
418+ size_t previous_total_chars {};
415419 size_t chunk_size {};
416- size_t total_lines {};
420+ size_t total_chars {};
417421 size_t remains_lines {};
418422
419423 bool empty_chunk {};
@@ -422,12 +426,12 @@ void ekg::text::erase(
422426 for (size_t it {}; it < this ->loaded_chunks .size (); it++) {
423427 ekg::io::chunk_t &chunk {this ->loaded_chunks .at (it)};
424428
425- previous_total_lines = total_lines ;
426- total_lines += (chunk_size = chunk.size ());
429+ previous_total_chars = total_chars ;
430+ total_chars += (chunk_size = chunk.size ());
427431
428- if (begin < total_lines ) {
432+ if (begin < total_chars ) {
429433 remains_lines = end - begin;
430- begin = begin - previous_total_lines ;
434+ begin = begin - previous_total_chars ;
431435 while (remains_lines != 0 ) {
432436 ekg::io::chunk_t &chunk {this ->loaded_chunks .at (it)};
433437
@@ -459,15 +463,19 @@ void ekg::text::erase(
459463 break ;
460464 }
461465 }
466+
467+ this ->should_count = true ;
468+ this ->size ();
462469}
463470
464471void ekg::text::push_back (const std::string &line) {
465472 if (this ->loaded_chunks .empty ()) {
466473 this ->loaded_chunks .emplace_back ().push_back (line);
467474 return ;
468475 }
469-
470- this ->total_lines ++;
476+
477+ this ->should_count = true ;
478+ this ->size ();
471479
472480 size_t chunks_size {this ->loaded_chunks .size ()};
473481 ekg::io::chunk_t &chunk {this ->loaded_chunks .at (chunks_size - (chunks_size != 0 ))};
@@ -484,7 +492,8 @@ std::string &ekg::text::emplace_back() {
484492 return this ->loaded_chunks .emplace_back ().emplace_back ();
485493 }
486494
487- this ->total_lines ++;
495+ this ->should_count = true ;
496+ this ->size ();
488497
489498 size_t chunks_size {this ->loaded_chunks .size ()};
490499 ekg::io::chunk_t &chunk {this ->loaded_chunks .at (chunks_size - (chunks_size != 0 ))};
@@ -496,17 +505,36 @@ std::string &ekg::text::emplace_back() {
496505}
497506
498507size_t ekg::text::lines () {
499- return this ->total_lines ;
508+ return 0 ;
509+ }
510+
511+ size_t ekg::text::size () {
512+ if (this ->should_count ) {
513+ this ->total_chars = 0 ;
514+ for (ekg::io::chunk_t &chunk : this ->loaded_chunks ) {
515+ for (std::string &lines : chunk) {
516+ this ->total_chars += lines.size ();
517+ }
518+ }
519+
520+ this ->should_count = false ;
521+ }
522+
523+ return this ->total_chars ;
500524}
501525
502526size_t ekg::text::chunks () {
503527 return this ->loaded_chunks .size ();
504528}
505529
506- bool ekg::text::audited () {
507- bool should {this ->was_audited };
530+ void ekg::text::unset_audited () {
508531 this ->was_audited = false ;
509- return should;
532+ }
533+
534+ bool ekg::text::audited () {
535+ this ->was_audited = this ->was_audited || (this ->prev_total_chars != this ->total_chars );
536+ this ->prev_total_chars = this ->total_chars ;
537+ return this ->was_audited ;
510538}
511539
512540std::vector<ekg::io::chunk_t > &ekg::text::data () {
0 commit comments