zsh-workers
 help / color / mirror / code / Atom feed
From: Peter Stephenson <p.w.stephenson@ntlworld.com>
To: Zsh hackers list <zsh-workers@zsh.org>
Subject: Re: Lots of test failures when --disable-multibyte
Date: Mon, 4 Apr 2022 17:23:43 +0100 (BST)	[thread overview]
Message-ID: <1045406953.1902371.1649089423375@mail2.virginmedia.com> (raw)
In-Reply-To: <CAH+w=7Ycqt+3L=FaoRX2Q7_OuWtfN--od6upENnOL=NAhVmYHQ@mail.gmail.com>

This tidies single-byte niceness up to bring the interface roughly into line
with the one for multibyte compilation.  (Note that, as Bart will already know,
this has no effect for "unsetopt multibyte" if multbyte was compiled in, it's
purely for systems that don't support it at all.)

I'll commit this tomorrow if nothing further turns up along this front (I'll
assume other test issues are unrelated).

pws

diff --git a/Src/Zle/compresult.c b/Src/Zle/compresult.c
index 0fed297b5..57789c0f3 100644
--- a/Src/Zle/compresult.c
+++ b/Src/Zle/compresult.c
@@ -2248,15 +2248,13 @@ iprintm(Cmgroup g, Cmatch *mp, UNUSED(int mc), UNUSED(int ml), int lastc, int wi
 #ifdef MULTIBYTE_SUPPORT
 	len = mb_niceformat(m->disp, shout, NULL, 0);
 #else
-	nicezputs(m->disp, shout);
-	len = niceztrlen(m->disp);
+	len = sb_niceformat(m->disp, shout, NULL, 0);
 #endif
     } else {
 #ifdef MULTIBYTE_SUPPORT
 	len = mb_niceformat(m->str, shout, NULL, 0);
 #else
-	nicezputs(m->str, shout);
-	len = niceztrlen(m->str);
+	len = sb_niceformat(m->str, shout, NULL, 0);
 #endif
 
 	if ((g->flags & CGF_FILES) && m->modec) {
diff --git a/Src/utils.c b/Src/utils.c
index f9127c70c..d92e00024 100644
--- a/Src/utils.c
+++ b/Src/utils.c
@@ -438,7 +438,6 @@ putshout(int c)
     return 0;
 }
 
-#ifdef MULTIBYTE_SUPPORT
 /*
  * Turn a character into a visible representation thereof.  The visible
  * string is put together in a static buffer, and this function returns
@@ -517,6 +516,8 @@ nicechar_sel(int c, int quotable)
     return buf;
 }
 
+#ifdef MULTIBYTE_SUPPORT
+
 /**/
 mod_export char *
 nicechar(int c)
@@ -5253,26 +5254,11 @@ zputs(char const *s, FILE *stream)
 mod_export char *
 nicedup(char const *s, int heap)
 {
-    int c, len = strlen(s) * 5 + 1;
-    VARARR(char, buf, len);
-    char *p = buf, *n;
+    char *retstr;
 
-    while ((c = *s++)) {
-	if (itok(c)) {
-	    if (c <= Comma)
-		c = ztokens[c - Pound];
-	    else
-		continue;
-	}
-	if (c == Meta)
-	    c = *s++ ^ 32;
-	/* The result here is metafied */
-	n = nicechar(c);
-	while(*n)
-	    *p++ = *n++;
-    }
-    *p = '\0';
-    return heap ? dupstring(buf) : ztrdup(buf);
+    (void)sb_niceformat(s, NULL, &retstr, heap ? NICEFLAG_HEAP : 0);
+
+    return retstr;
 }
 #endif
 
@@ -5291,20 +5277,7 @@ nicedupstring(char const *s)
 mod_export int
 nicezputs(char const *s, FILE *stream)
 {
-    int c;
-
-    while ((c = *s++)) {
-	if (itok(c)) {
-	    if (c <= Comma)
-		c = ztokens[c - Pound];
-	    else
-		continue;
-	}
-	if (c == Meta)
-	    c = *s++ ^ 32;
-	if(zputs(nicechar(c), stream) < 0)
-	    return EOF;
-    }
+    sb_niceformat(s, stream, NULL, 0);
     return 0;
 }
 
@@ -5793,7 +5766,7 @@ mb_charlenconv(const char *s, int slen, wint_t *wcp)
 }
 
 /**/
-#else
+#else /* MULTIBYTE_SUPPORT */
 
 /* Simple replacement for mb_metacharlenconv */
 
@@ -5833,6 +5806,121 @@ charlenconv(const char *x, int len, int *c)
     return 1;
 }
 
+/*
+ * Non-multibyte version of mb_niceformat() above.  Same basic interface.
+ */
+
+/**/
+mod_export size_t
+sb_niceformat(const char *s, FILE *stream, char **outstrp, int flags)
+{
+    size_t l = 0, newl;
+    int umlen, outalloc, outleft;
+    char *ums, *ptr, *eptr, *fmt, *outstr, *outptr;
+
+    if (outstrp) {
+	outleft = outalloc = 2 * strlen(s);
+	outptr = outstr = zalloc(outalloc);
+    } else {
+	outleft = outalloc = 0;
+	outptr = outstr = NULL;
+    }
+
+    ums = ztrdup(s);
+    /*
+     * is this necessary at this point? niceztrlen does this
+     * but it's used in lots of places.  however, one day this may
+     * be, too.
+     */
+    untokenize(ums);
+    ptr = unmetafy(ums, &umlen);
+    eptr = ptr + umlen;
+
+    while (ptr < eptr) {
+	int c = STOUC(*ptr);
+	if (c == '\'' && (flags & NICEFLAG_QUOTE)) {
+	    fmt = "\\'";
+	    newl = 2;
+	}
+	else if (c == '\\' && (flags & NICEFLAG_QUOTE)) {
+	    fmt = "\\\\";
+	    newl = 2;
+	}
+	else {
+	    fmt = nicechar_sel(c, flags & NICEFLAG_QUOTE);
+	    newl = 1;
+	}
+
+	++ptr;
+	l += newl;
+
+	if (stream)
+	    zputs(fmt, stream);
+	if (outstr) {
+	    /* Append to output string */
+	    int outlen = strlen(fmt);
+	    if (outlen >= outleft) {
+		/* Reallocate to twice the length */
+		int outoffset = outptr - outstr;
+
+		outleft += outalloc;
+		outalloc *= 2;
+		outstr = zrealloc(outstr, outalloc);
+		outptr = outstr + outoffset;
+	    }
+	    memcpy(outptr, fmt, outlen);
+	    /* Update start position */
+	    outptr += outlen;
+	    /* Update available bytes */
+	    outleft -= outlen;
+	}
+    }
+
+    free(ums);
+    if (outstrp) {
+	*outptr = '\0';
+	/* Use more efficient storage for returned string */
+	if (flags & NICEFLAG_NODUP)
+	    *outstrp = outstr;
+	else {
+	    *outstrp = (flags & NICEFLAG_HEAP) ? dupstring(outstr) :
+		ztrdup(outstr);
+	    free(outstr);
+	}
+    }
+
+    return l;
+}
+
+/*
+ * Return 1 if sb_niceformat() would reformat this string, else 0.
+ */
+
+/**/
+mod_export int
+is_sb_niceformat(const char *s)
+{
+    int umlen, ret = 0;
+    char *ums, *ptr, *eptr;
+
+    ums = ztrdup(s);
+    untokenize(ums);
+    ptr = unmetafy(ums, &umlen);
+    eptr = ptr + umlen;
+
+    while (ptr < eptr) {
+	if (is_nicechar(*ptr))  {
+	    ret = 1;
+	    break;
+	}
+	++ptr;
+    }
+
+    free(ums);
+
+    return ret;
+}
+
 /**/
 #endif /* MULTIBYTE_SUPPORT */
 
@@ -6366,6 +6454,22 @@ quotedzputs(char const *s, FILE *stream)
 	    return outstr;
 	}
     }
+#else
+    if (is_sb_niceformat(s)){
+	if (stream) {
+	    fputs("$'", stream);
+	    sb_niceformat(s, stream, NULL, NICEFLAG_QUOTE);
+	    fputc('\'', stream);
+	    return NULL;
+	} else {
+	    char *substr;
+	    sb_niceformat(s, NULL, &substr, NICEFLAG_QUOTE|NICEFLAG_NODUP);
+	    outstr = (char *)zhalloc(4 + strlen(substr));
+	    sprintf(outstr, "$'%s'", substr);
+	    free(substr);
+	    return outstr;
+	}
+    }
 #endif /* MULTIBYTE_SUPPORT */
 
     if (!hasspecial(s)) {
diff --git a/Src/zsh.h b/Src/zsh.h
index 641d9c95c..40f9ea537 100644
--- a/Src/zsh.h
+++ b/Src/zsh.h
@@ -3277,14 +3277,15 @@ enum zexit_t {
 #define AFTERTRAPHOOK  (zshhooks + 2)
 #define GETCOLORATTR   (zshhooks + 3)
 
-#ifdef MULTIBYTE_SUPPORT
-/* Final argument to mb_niceformat() */
+/* Final argument to [ms]b_niceformat() */
 enum {
     NICEFLAG_HEAP = 1,		/* Heap allocation where needed */
     NICEFLAG_QUOTE = 2,		/* Result will appear in $'...' */
     NICEFLAG_NODUP = 4,         /* Leave allocated */
 };
 
+#ifdef MULTIBYTE_SUPPORT
+
 /* Metafied input */
 #define nicezputs(str, outs)	(void)mb_niceformat((str), (outs), NULL, 0)
 #define MB_METACHARINIT()	mb_charinit()


  reply	other threads:[~2022-04-04 16:24 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-04-04  2:03 Bart Schaefer
2022-04-04 14:43 ` Peter Stephenson
2022-04-04 15:31   ` Bart Schaefer
2022-04-04 16:23     ` Peter Stephenson [this message]
2022-04-04 21:10     ` Bart Schaefer
2022-04-04 21:45       ` Bart Schaefer
2022-04-04 22:00         ` Bart Schaefer
2022-04-05 16:00           ` Bart Schaefer
2022-04-05 16:15             ` Mikael Magnusson
2022-04-05 20:29           ` Peter Stephenson
2022-04-06  3:48             ` Bart Schaefer
2022-04-06  5:32             ` ZTST_continue (was Re: Lots of test failures when --disable-multibyte) Jun T
2022-04-06 13:37               ` Peter Stephenson
2022-04-07 12:33                 ` Jun T
2022-04-07 12:34                 ` Jun T
2022-04-04 18:52 ` Lots of test failures when --disable-multibyte Bart Schaefer

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1045406953.1902371.1649089423375@mail2.virginmedia.com \
    --to=p.w.stephenson@ntlworld.com \
    --cc=zsh-workers@zsh.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).