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

PATCH: multibyte conversion in math expressions



This makes $(( ##X )) and $(( #var )) work in multibyte mode.

Index: Doc/Zsh/arith.yo
===================================================================
RCS file: /cvsroot/zsh/zsh/Doc/Zsh/arith.yo,v
retrieving revision 1.9
diff -u -r1.9 arith.yo
--- Doc/Zsh/arith.yo	17 May 2005 10:49:33 -0000	1.9
+++ Doc/Zsh/arith.yo	29 Jun 2006 11:24:27 -0000
@@ -129,13 +129,14 @@
 functions.
 
 An expression of the form `tt(##)var(x)' where var(x) is any character
-sequence such as `tt(a)', `tt(^A)', or `tt(\M-\C-x)' gives the ASCII
-value of this character and an expression of the form `tt(#)var(foo)'
-gives the ASCII value of the first character of the value of the
-parameter var(foo).  Note that this is different from the expression
-`tt($#)var(foo)', a standard parameter substitution which gives the
-length of the parameter var(foo).  `tt(#\)' is accepted instead of
-`tt(##)', but its use is deprecated.
+sequence such as `tt(a)', `tt(^A)', or `tt(\M-\C-x)' gives the value of
+this character and an expression of the form `tt(#)var(foo)' gives the
+value of the first character of the contents of the parameter var(foo).
+Character values are according to the character set used in the current
+locale; for multibyte character handling the option tt(MULTIBYTE) must be
+set.  Note that this form is different from `tt($#)var(foo)', a standard
+parameter substitution which gives the length of the parameter var(foo).
+`tt(#\)' is accepted instead of `tt(##)', but its use is deprecated.
 
 Named parameters and subscripted arrays can be referenced by name within an
 arithmetic expression without using the parameter expansion syntax.  For
Index: Src/math.c
===================================================================
RCS file: /cvsroot/zsh/zsh/Src/math.c,v
retrieving revision 1.24
diff -u -r1.24 math.c
--- Src/math.c	30 May 2006 22:35:03 -0000	1.24
+++ Src/math.c	29 Jun 2006 11:24:28 -0000
@@ -549,8 +549,20 @@
     queue_signals();
     if (!(t = getsparam(s)))
 	mn.u.l = 0;
-    else
-        mn.u.l = STOUC(*t == Meta ? t[1] ^ 32 : *t);
+    else {
+#ifdef MULTIBYTE_SUPPORT
+	if (isset(MULTIBYTE)) {
+	    wint_t wc;
+	    (void)mb_metacharlenconv(t, &wc);
+	    if (wc != WEOF) {
+		mn.u.l = (zlong)wc;
+		unqueue_signals();
+		return mn;
+	    }
+	}
+#endif
+	mn.u.l = STOUC(*t == Meta ? t[1] ^ 32 : *t);
+    }
     unqueue_signals();
     return mn;
 }
Index: Src/utils.c
===================================================================
RCS file: /cvsroot/zsh/zsh/Src/utils.c,v
retrieving revision 1.125
diff -u -r1.125 utils.c
--- Src/utils.c	28 Jun 2006 13:12:55 -0000	1.125
+++ Src/utils.c	29 Jun 2006 11:24:29 -0000
@@ -4410,6 +4410,17 @@
 		   (fromwhere == 2 || fromwhere == 5 || fromwhere == 6)) {
 	    control = 1;
 	    continue;
+#ifdef MULTIBYTE_SUPPORT
+	} else if (fromwhere == 6 && isset(MULTIBYTE) && STOUC(*s) > 127) {
+	    wint_t wc;
+	    int len;
+	    len = mb_metacharlenconv(s, &wc);
+	    if (wc != WEOF) {
+		*misc = (int)wc;
+		return s + len;
+	    }
+#endif
+
 	} else if (*s == Meta)
 	    *t++ = *++s ^ 32;
 	else
Index: Test/D07multibyte.ztst
===================================================================
RCS file: /cvsroot/zsh/zsh/Test/D07multibyte.ztst,v
retrieving revision 1.3
diff -u -r1.3 D07multibyte.ztst
--- Test/D07multibyte.ztst	28 Jun 2006 13:12:56 -0000	1.3
+++ Test/D07multibyte.ztst	29 Jun 2006 11:24:29 -0000
@@ -155,3 +155,13 @@
 >ølaf ødd øpened án encyclopædia
 >Ølaf Ødd Øpened Án Encyclopædia
 >Ølaf Ødd Øpened Án Encyclopædia
+
+  print $(( ##¥ ))
+  pound=£
+  print $(( #pound ))
+  alpha=α
+  print $(( ##α )) $(( #alpha ))
+0:Conversion to Unicode in mathematical expressions
+>165
+>163
+>945 945


-- 
Peter Stephenson <pws@xxxxxxx>                  Software Engineer
CSR PLC, Churchill House, Cambridge Business Park, Cowley Road
Cambridge, CB4 0WZ, UK                          Tel: +44 (0)1223 692070


To access the latest news from CSR copy this link into a web browser:  http://www.csr.com/email_sig.php



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