Skip to content

Commit e5179cb

Browse files
committed
Convert append buffer to std::string
This functionality exists in std::string Signed-off-by: Eric Curtin <[email protected]>
1 parent c663d7e commit e5179cb

File tree

1 file changed

+31
-61
lines changed

1 file changed

+31
-61
lines changed

linenoise.cpp

+31-61
Original file line numberDiff line numberDiff line change
@@ -508,36 +508,9 @@ void linenoiseAddCompletion(linenoiseCompletions *lc, const char *str) {
508508
lc->cvec[lc->len++] = copy.release();
509509
}
510510

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-
538511
/* Helper of refreshSingleLine() and refreshMultiLine() to show hints
539512
* 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) {
541514
char seq[64];
542515
if (hintsCallback && plen+l->len < l->cols) {
543516
int color = -1, bold = 0;
@@ -551,10 +524,11 @@ static void refreshShowHints(struct abuf * ab, struct linenoiseState * l, int pl
551524
snprintf(seq,64,"\033[%d;%d;49m",bold,color);
552525
else
553526
seq[0] = '\0';
554-
abAppend(ab,seq,strlen(seq));
555-
abAppend(ab,hint,hintlen);
527+
ab.append(seq);
528+
ab.append(hint, hintlen);
556529
if (color != -1 || bold != 0)
557-
abAppend(ab,"\033[0m",4);
530+
ab.append("\033[0m");
531+
558532
/* Call the function to free the hint returned. */
559533
if (freeHintsCallback) freeHintsCallback(hint);
560534
}
@@ -575,8 +549,7 @@ static void refreshSingleLine(struct linenoiseState *l, int flags) {
575549
char *buf = l->buf;
576550
size_t len = l->len;
577551
size_t pos = l->pos;
578-
struct abuf ab;
579-
552+
std::string ab;
580553
while((plen+pos) >= l->cols) {
581554
buf++;
582555
len--;
@@ -586,34 +559,34 @@ static void refreshSingleLine(struct linenoiseState *l, int flags) {
586559
len--;
587560
}
588561

589-
abInit(&ab);
590562
/* Cursor to left edge */
591563
snprintf(seq,sizeof(seq),"\r");
592-
abAppend(&ab,seq,strlen(seq));
564+
ab.append(seq);
593565

594566
if (flags & REFRESH_WRITE) {
595567
/* Write the prompt and the current buffer content */
596-
abAppend(&ab,l->prompt,strlen(l->prompt));
568+
ab.append(l->prompt);
597569
if (maskmode == 1) {
598-
while (len--) abAppend(&ab,"*",1);
570+
while (len--) {
571+
ab.append("*");
572+
}
599573
} else {
600-
abAppend(&ab,buf,len);
574+
ab.append(buf, len);
601575
}
602576
/* Show hits if any. */
603-
refreshShowHints(&ab,l,plen);
577+
refreshShowHints(ab, l, plen);
604578
}
605579

606580
/* Erase to right */
607581
snprintf(seq,sizeof(seq),"\x1b[0K");
608-
abAppend(&ab,seq,strlen(seq));
609-
582+
ab.append(seq);
610583
if (flags & REFRESH_WRITE) {
611584
/* Move cursor to original position. */
612585
snprintf(seq,sizeof(seq),"\r\x1b[%dC", (int)(pos+plen));
613-
abAppend(&ab,seq,strlen(seq));
586+
ab.append(seq);
614587
}
615588

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. */
617590
}
618591

619592
/* Multi line low level line refresh.
@@ -632,48 +605,46 @@ static void refreshMultiLine(struct linenoiseState *l, int flags) {
632605
int col; /* colum position, zero-based. */
633606
int old_rows = l->oldrows;
634607
int fd = l->ofd, j;
635-
struct abuf ab;
636-
608+
std::string ab;
637609
l->oldrows = rows;
638610

639611
/* First step: clear all the lines used before. To do so start by
640612
* going to the last row. */
641-
abInit(&ab);
642-
643613
if (flags & REFRESH_CLEAN) {
644614
if (old_rows-rpos > 0) {
645615
lndebug("go down %d", old_rows-rpos);
646616
snprintf(seq,64,"\x1b[%dB", old_rows-rpos);
647-
abAppend(&ab,seq,strlen(seq));
617+
ab.append(seq);
648618
}
649619

650620
/* Now for every row clear it, go up. */
651621
for (j = 0; j < old_rows-1; j++) {
652622
lndebug("clear+up");
653623
snprintf(seq,64,"\r\x1b[0K\x1b[1A");
654-
abAppend(&ab,seq,strlen(seq));
624+
ab.append(seq);
655625
}
656626
}
657627

658628
if (flags & REFRESH_ALL) {
659629
/* Clean the top line. */
660630
lndebug("clear");
661631
snprintf(seq,64,"\r\x1b[0K");
662-
abAppend(&ab,seq,strlen(seq));
632+
ab.append(seq);
663633
}
664634

665635
if (flags & REFRESH_WRITE) {
666636
/* Write the prompt and the current buffer content */
667-
abAppend(&ab,l->prompt,strlen(l->prompt));
637+
ab.append(l->prompt);
668638
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+
}
671642
} else {
672-
abAppend(&ab,l->buf,l->len);
643+
ab.append(l->buf, l->len);
673644
}
674645

675646
/* Show hits if any. */
676-
refreshShowHints(&ab,l,plen);
647+
refreshShowHints(ab, l, plen);
677648

678649
/* If we are at the very end of the screen with our prompt, we need to
679650
* emit a newline and move the prompt to the first column. */
@@ -682,9 +653,9 @@ static void refreshMultiLine(struct linenoiseState *l, int flags) {
682653
(l->pos+plen) % l->cols == 0)
683654
{
684655
lndebug("<newline>");
685-
abAppend(&ab,"\n",1);
656+
ab.append("\n");
686657
snprintf(seq,64,"\r");
687-
abAppend(&ab,seq,strlen(seq));
658+
ab.append(seq);
688659
rows++;
689660
if (rows > (int)l->oldrows) l->oldrows = rows;
690661
}
@@ -697,7 +668,7 @@ static void refreshMultiLine(struct linenoiseState *l, int flags) {
697668
if (rows-rpos2 > 0) {
698669
lndebug("go-up %d", rows-rpos2);
699670
snprintf(seq,64,"\x1b[%dA", rows-rpos2);
700-
abAppend(&ab,seq,strlen(seq));
671+
ab.append(seq);
701672
}
702673

703674
/* Set column. */
@@ -707,13 +678,12 @@ static void refreshMultiLine(struct linenoiseState *l, int flags) {
707678
snprintf(seq,64,"\r\x1b[%dC", col);
708679
else
709680
snprintf(seq,64,"\r");
710-
abAppend(&ab,seq,strlen(seq));
681+
ab.append(seq);
711682
}
712683

713684
lndebug("\n");
714685
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. */
717687
}
718688

719689
/* Calls the two low level functions refreshSingleLine() or

0 commit comments

Comments
 (0)