simplify scroll_wline (now span_wline) and format_line generalize scroll_wline to start at a specified offset so that we can reuse it to simplify format_line. While here, simplify scroll_wline (now called span_wline) too. ok and improvements by stsp@
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
diff --git a/tog/tog.c b/tog/tog.c
index 36dceb2..0a55123 100644
--- a/tog/tog.c
+++ b/tog/tog.c
@@ -1263,51 +1263,40 @@ expand_tab(char **ptr, const char *src)
return NULL;
}
-/*
- * Skip leading nscroll columns of a wide character string.
- * Returns the index to the first character of the scrolled string.
+/*
+ * Advance at most n columns from wline starting at offset off.
+ * Return the index to the first character after the span operation.
+ * Return the combined column width of all spanned wide character in
+ * *rcol.
*/
-static const struct got_error *
-scroll_wline(int *scrollx , wchar_t *wline, int nscroll,
- int col_tab_align)
+static int
+span_wline(int *rcol, int off, wchar_t *wline, int n, int col_tab_align)
{
- int cols = 0;
- size_t wlen = wcslen(wline);
- int i = 0;
+ int width, i, cols = 0;
- *scrollx = 0;
+ if (n == 0) {
+ *rcol = cols;
+ return off;
+ }
- while (i < wlen && cols < nscroll) {
- int width = wcwidth(wline[i]);
+ for (i = off; wline[i] != L'\0'; ++i) {
+ if (wline[i] == L'\t')
+ width = TABSIZE - ((cols + col_tab_align) % TABSIZE);
+ else
+ width = wcwidth(wline[i]);
- if (width == 0) {
- i++;
- continue;
+ if (width == -1) {
+ width = 1;
+ wline[i] = L'.';
}
- if (width == 1 || width == 2) {
- if (cols + width > nscroll)
- break;
- cols += width;
- i++;
- } else if (width == -1) {
- if (wline[i] == L'\t') {
- width = TABSIZE -
- ((cols + col_tab_align) % TABSIZE);
- } else {
- width = 1;
- wline[i] = L'.';
- }
- if (cols + width > nscroll)
- break;
- cols += width;
- i++;
- } else
- return got_error_from_errno("wcwidth");
+ if (cols + width > n)
+ break;
+ cols += width;
}
- *scrollx = i;
- return NULL;
+ *rcol = cols;
+ return i;
}
/*
@@ -1320,11 +1309,11 @@ format_line(wchar_t **wlinep, int *widthp, int *scrollxp,
const char *line, int nscroll, int wlimit, int col_tab_align, int expand)
{
const struct got_error *err = NULL;
- int cols = 0;
+ int cols;
wchar_t *wline = NULL;
char *exstr = NULL;
size_t wlen;
- int i, scrollx = 0;
+ int i, scrollx;
*wlinep = NULL;
*widthp = 0;
@@ -1340,9 +1329,7 @@ format_line(wchar_t **wlinep, int *widthp, int *scrollxp,
if (err)
return err;
- err = scroll_wline(&scrollx, wline, nscroll, col_tab_align);
- if (err)
- goto done;
+ scrollx = span_wline(&cols, 0, wline, nscroll, col_tab_align);
if (wlen > 0 && wline[wlen - 1] == L'\n') {
wline[wlen - 1] = L'\0';
@@ -1353,43 +1340,13 @@ format_line(wchar_t **wlinep, int *widthp, int *scrollxp,
wlen--;
}
- i = scrollx;
- while (i < wlen) {
- int width = wcwidth(wline[i]);
-
- if (width == 0) {
- i++;
- continue;
- }
-
- if (width == 1 || width == 2) {
- if (cols + width > wlimit)
- break;
- cols += width;
- i++;
- } else if (width == -1) {
- if (wline[i] == L'\t') {
- width = TABSIZE -
- ((cols + col_tab_align) % TABSIZE);
- } else {
- width = 1;
- wline[i] = L'.';
- }
- if (cols + width > wlimit)
- break;
- cols += width;
- i++;
- } else {
- err = got_error_from_errno("wcwidth");
- goto done;
- }
- }
+ i = span_wline(&cols, scrollx, wline, wlimit, col_tab_align);
wline[i] = L'\0';
+
if (widthp)
*widthp = cols;
if (scrollxp)
*scrollxp = scrollx;
-done:
if (err)
free(wline);
else