@@ -508,36 +508,9 @@ void linenoiseAddCompletion(linenoiseCompletions *lc, const char *str) {
508
508
lc->cvec [lc->len ++] = copy.release ();
509
509
}
510
510
511
- /* =========================== Line editing ================================= */
512
-
513
- /* We define a very simple "append buffer" structure, that is an heap
514
- * allocated string where we can append to. This is useful in order to
515
- * write all the escape sequences in a buffer and flush them to the standard
516
- * output in a single call, to avoid flickering effects. */
517
- struct abuf {
518
- char *b;
519
- int len;
520
-
521
- ~abuf () { free (b); }
522
- };
523
-
524
- static void abInit (struct abuf *ab) {
525
- ab->b = NULL ;
526
- ab->len = 0 ;
527
- }
528
-
529
- static void abAppend (struct abuf *ab, const char *s, int len) {
530
- char *new_ptr = (char *) realloc (ab->b ,ab->len +len);
531
-
532
- if (new_ptr == NULL ) return ;
533
- memcpy (new_ptr+ab->len ,s,len);
534
- ab->b = new_ptr;
535
- ab->len += len;
536
- }
537
-
538
511
/* Helper of refreshSingleLine() and refreshMultiLine() to show hints
539
512
* to the right of the prompt. */
540
- static void refreshShowHints (struct abuf * ab, struct linenoiseState * l, int plen) {
513
+ static void refreshShowHints (std::string & ab, struct linenoiseState * l, int plen) {
541
514
char seq[64 ];
542
515
if (hintsCallback && plen+l->len < l->cols ) {
543
516
int color = -1 , bold = 0 ;
@@ -551,10 +524,11 @@ static void refreshShowHints(struct abuf * ab, struct linenoiseState * l, int pl
551
524
snprintf (seq,64 ," \033 [%d;%d;49m" ,bold ,color);
552
525
else
553
526
seq[0 ] = ' \0 ' ;
554
- abAppend (ab,seq, strlen (seq) );
555
- abAppend (ab, hint,hintlen);
527
+ ab. append (seq);
528
+ ab. append ( hint, hintlen);
556
529
if (color != -1 || bold != 0 )
557
- abAppend (ab," \033 [0m" ,4 );
530
+ ab.append (" \033 [0m" );
531
+
558
532
/* Call the function to free the hint returned. */
559
533
if (freeHintsCallback) freeHintsCallback (hint);
560
534
}
@@ -575,8 +549,7 @@ static void refreshSingleLine(struct linenoiseState *l, int flags) {
575
549
char *buf = l->buf ;
576
550
size_t len = l->len ;
577
551
size_t pos = l->pos ;
578
- struct abuf ab;
579
-
552
+ std::string ab;
580
553
while ((plen+pos) >= l->cols ) {
581
554
buf++;
582
555
len--;
@@ -586,34 +559,34 @@ static void refreshSingleLine(struct linenoiseState *l, int flags) {
586
559
len--;
587
560
}
588
561
589
- abInit (&ab);
590
562
/* Cursor to left edge */
591
563
snprintf (seq,sizeof (seq)," \r " );
592
- abAppend (&ab,seq, strlen (seq) );
564
+ ab. append (seq);
593
565
594
566
if (flags & REFRESH_WRITE) {
595
567
/* Write the prompt and the current buffer content */
596
- abAppend (&ab,l-> prompt , strlen (l->prompt ) );
568
+ ab. append (l->prompt );
597
569
if (maskmode == 1 ) {
598
- while (len--) abAppend (&ab," *" ,1 );
570
+ while (len--) {
571
+ ab.append (" *" );
572
+ }
599
573
} else {
600
- abAppend (&ab, buf,len);
574
+ ab. append ( buf, len);
601
575
}
602
576
/* Show hits if any. */
603
- refreshShowHints (& ab,l, plen);
577
+ refreshShowHints (ab, l, plen);
604
578
}
605
579
606
580
/* Erase to right */
607
581
snprintf (seq,sizeof (seq)," \x1b [0K" );
608
- abAppend (&ab,seq,strlen (seq));
609
-
582
+ ab.append (seq);
610
583
if (flags & REFRESH_WRITE) {
611
584
/* Move cursor to original position. */
612
585
snprintf (seq,sizeof (seq)," \r\x1b [%dC" , (int )(pos+plen));
613
- abAppend (&ab,seq, strlen (seq) );
586
+ ab. append (seq);
614
587
}
615
588
616
- (void ) !write (fd, ab.b , ab.len ); /* Can't recover from write error. */
589
+ (void ) !write (fd, ab.c_str () , ab.size () ); /* Can't recover from write error. */
617
590
}
618
591
619
592
/* Multi line low level line refresh.
@@ -632,48 +605,46 @@ static void refreshMultiLine(struct linenoiseState *l, int flags) {
632
605
int col; /* colum position, zero-based. */
633
606
int old_rows = l->oldrows ;
634
607
int fd = l->ofd , j;
635
- struct abuf ab;
636
-
608
+ std::string ab;
637
609
l->oldrows = rows;
638
610
639
611
/* First step: clear all the lines used before. To do so start by
640
612
* going to the last row. */
641
- abInit (&ab);
642
-
643
613
if (flags & REFRESH_CLEAN) {
644
614
if (old_rows-rpos > 0 ) {
645
615
lndebug (" go down %d" , old_rows-rpos);
646
616
snprintf (seq,64 ," \x1b [%dB" , old_rows-rpos);
647
- abAppend (&ab,seq, strlen (seq) );
617
+ ab. append (seq);
648
618
}
649
619
650
620
/* Now for every row clear it, go up. */
651
621
for (j = 0 ; j < old_rows-1 ; j++) {
652
622
lndebug (" clear+up" );
653
623
snprintf (seq,64 ," \r\x1b [0K\x1b [1A" );
654
- abAppend (&ab,seq, strlen (seq) );
624
+ ab. append (seq);
655
625
}
656
626
}
657
627
658
628
if (flags & REFRESH_ALL) {
659
629
/* Clean the top line. */
660
630
lndebug (" clear" );
661
631
snprintf (seq,64 ," \r\x1b [0K" );
662
- abAppend (&ab,seq, strlen (seq) );
632
+ ab. append (seq);
663
633
}
664
634
665
635
if (flags & REFRESH_WRITE) {
666
636
/* Write the prompt and the current buffer content */
667
- abAppend (&ab,l-> prompt , strlen (l->prompt ) );
637
+ ab. append (l->prompt );
668
638
if (maskmode == 1 ) {
669
- unsigned int i;
670
- for (i = 0 ; i < l->len ; i++) abAppend (&ab," *" ,1 );
639
+ for (unsigned int i = 0 ; i < l->len ; ++i) {
640
+ ab.append (" *" );
641
+ }
671
642
} else {
672
- abAppend (&ab, l->buf ,l->len );
643
+ ab. append ( l->buf , l->len );
673
644
}
674
645
675
646
/* Show hits if any. */
676
- refreshShowHints (& ab,l, plen);
647
+ refreshShowHints (ab, l, plen);
677
648
678
649
/* If we are at the very end of the screen with our prompt, we need to
679
650
* emit a newline and move the prompt to the first column. */
@@ -682,9 +653,9 @@ static void refreshMultiLine(struct linenoiseState *l, int flags) {
682
653
(l->pos +plen) % l->cols == 0 )
683
654
{
684
655
lndebug (" <newline>" );
685
- abAppend (&ab, " \n " , 1 );
656
+ ab. append ( " \n " );
686
657
snprintf (seq,64 ," \r " );
687
- abAppend (&ab,seq, strlen (seq) );
658
+ ab. append (seq);
688
659
rows++;
689
660
if (rows > (int )l->oldrows ) l->oldrows = rows;
690
661
}
@@ -697,7 +668,7 @@ static void refreshMultiLine(struct linenoiseState *l, int flags) {
697
668
if (rows-rpos2 > 0 ) {
698
669
lndebug (" go-up %d" , rows-rpos2);
699
670
snprintf (seq,64 ," \x1b [%dA" , rows-rpos2);
700
- abAppend (&ab,seq, strlen (seq) );
671
+ ab. append (seq);
701
672
}
702
673
703
674
/* Set column. */
@@ -707,13 +678,12 @@ static void refreshMultiLine(struct linenoiseState *l, int flags) {
707
678
snprintf (seq,64 ," \r\x1b [%dC" , col);
708
679
else
709
680
snprintf (seq,64 ," \r " );
710
- abAppend (&ab,seq, strlen (seq) );
681
+ ab. append (seq);
711
682
}
712
683
713
684
lndebug (" \n " );
714
685
l->oldpos = l->pos ;
715
-
716
- (void ) !write (fd, ab.b , ab.len ); /* Can't recover from write error. */
686
+ (void ) !write (fd, ab.c_str (), ab.size ()); /* Can't recover from write error. */
717
687
}
718
688
719
689
/* Calls the two low level functions refreshSingleLine() or
0 commit comments