"GOT", but the "O" is a cute, smiling pufferfish. Index | Thread | Search

From:
Stefan Sperling <stsp@stsp.name>
Subject:
Re: simplify scroll_wline/format_line
To:
Omar Polo <op@omarpolo.com>
Cc:
gameoftrees@openbsd.org
Date:
Fri, 17 Jun 2022 10:26:51 +0200

Download raw body.

Thread
On Thu, Jun 16, 2022 at 08:23:57PM +0200, Omar Polo wrote:
> diff 4cb9d869771dfcbb9e460e1f2924b289b34aff7c /home/op/w/got
> blob - 6db125a8a56dececb4b421750f0f92d3644f8601
> file + tog/tog.c
> --- tog/tog.c
> +++ tog/tog.c
> @@ -1263,51 +1263,38 @@ 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 max n columns from wline starting at offset off.  Return

s/at max/at most/

> + * the index to the first character after the span operation and the
> + * reached column.

I would use one sentence per return value, for clarity:

Return the index to the first wide character after the span operation.
Return the combined column width of all spanned wide characters 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) {
> +		width = wcwidth(wline[i]);
> +		if (wline[i] == L'\t')
> +			width = TABSIZE - ((cols + col_tab_align) % TABSIZE);

There is no need to call wcwidth() on \t.

 		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'.';