Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

PATCH: printf ... \'X



  printf "%d\n" \'X

is yet another way of getting the character index of character X.  I'm
guessing it's more useful for it to handle wide characters rather than
outputting the value of the first byte in a multibyte character.

Goodness knows whether string width in printf should take account of
wide character widths for %s and %b, but my guess is it should.  I
haven't done that.  In fact, I see I haven't fixed up character widths
for dopadding() yet, either.

By the way, I think the handling of "curarg" in the printf code doesn't
generally take account of the fact that curarg is unmetafied with length
curlen, right?  (This fact isn't particularly convenient here but is
pretty much inevitable given the use of getkeystring().)  I've made it
do so in this case but haven't altered the other cases.  (In the first
hunk it already does, that's just a whitespace change.)


Index: Src/builtin.c
===================================================================
RCS file: /cvsroot/zsh/zsh/Src/builtin.c,v
retrieving revision 1.161
diff -u -r1.161 builtin.c
--- Src/builtin.c	10 Sep 2006 18:10:49 -0000	1.161
+++ Src/builtin.c	10 Sep 2006 19:00:48 -0000
@@ -3938,7 +3938,8 @@
 		    char *b;
 		    int l;
 		    if (*c == 'b') {
-			b = getkeystring(metafy(curarg, curlen, META_USEHEAP), &l,
+			b = getkeystring(metafy(curarg, curlen, META_USEHEAP),
+					 &l,
 					 OPT_ISSET(ops,'b') ? GETKEYS_BINDKEY :
 					 GETKEYS_PRINTF, &nnl);
 		    } else {
@@ -4004,11 +4005,25 @@
 
 	    if (type > 0) {
 		if (curarg && (*curarg == '\'' || *curarg == '"' )) {
+		    convchar_t cc;
+#ifdef MULTIBYTE_SUPPORT
+		    if (isset(MULTIBYTE)) {
+			mb_metacharinit();
+			(void)mb_metacharlenconv(metafy(curarg+1, curlen-1,
+							META_USEHEAP), &cc);
+		    }
+		    else
+			cc = WEOF;
+		    if (cc == WEOF)
+			cc = (curlen > 1) ? STOUC(curarg[1]) : 0;
+#else
+		    cc = (curlen > 1) ? STOUC(curarg[1]) : 0;
+#endif
 		    if (type == 2) {
-			doubleval = STOUC(curarg[1]);
+			doubleval = cc;
 			print_val(doubleval);
 		    } else {
-			intval = STOUC(curarg[1]);
+			intval = cc;
 			print_val(intval);
 		    }
 		} else {


-- 
Peter Stephenson <p.w.stephenson@xxxxxxxxxxxx>
Web page now at http://homepage.ntlworld.com/p.w.stephenson/



Messages sorted by: Reverse Date, Date, Thread, Author