zsh-workers
 help / color / mirror / code / Atom feed
* PATCH: C_BASES option for outputting hex and octal
@ 2000-08-10 16:11 Peter Stephenson
  2000-08-11 20:16 ` Zefram
  0 siblings, 1 reply; 3+ messages in thread
From: Peter Stephenson @ 2000-08-10 16:11 UTC (permalink / raw)
  To: Zsh hackers list

Does anyone object to my adding options for trivial things like this?
Because if so they ought to have got the point and given up objecting years
ago :-/.  (I finally got fed up with 0x${$(([#16]$num))##*\#}.)

If it weren't for backward compatibility, I'd set C_BASES by default and
just unset it for ksh compatibility.  But the current format could have
worked its way into a lot of scripts.

% print $(( [#16] 0xff ))
16#FF
% setopt cbases
% print $(( [#16] 0xff ))
0xFF
% print $(( [#8] 8#77 ))
8#77
% setopt octalzeroes
% print $(( [#8] 077 ))
077

Index: Doc/Zsh/options.yo
===================================================================
RCS file: /cvsroot/zsh/zsh/Doc/Zsh/options.yo,v
retrieving revision 1.11
diff -u -r1.11 options.yo
--- Doc/Zsh/options.yo	2000/08/03 07:51:53	1.11
+++ Doc/Zsh/options.yo	2000/08/10 16:05:06
@@ -206,6 +206,19 @@
 This disables backslashed escape sequences in echo strings unless the
 tt(-e) option is specified.
 )
+pindex(C_BASES)
+cindex(bases, output in C format)
+cindex(hexadecimal, output in C format)
+cindex(octal, output in C format)
+item(tt(C_BASES))(
+Output hexadecimal numbers in the standard C format, for example `tt(0xFF)'
+instead of the usual `tt(16#FF)'.  If the option tt(OCTAL_ZEROES) is also
+set (it is not by default), octal numbers will be treated similarly and
+hence appear as `tt(077)' instead of `tt(8#77)'.  This option has no effect
+on the choice of the output base, nor on the output of bases other than
+hexadecimal and octal.  Note that these formats will be understood on input
+irrespective of the setting of tt(C_BASES).
+)
 pindex(CDABLE_VARS)
 cindex(cd, to parameter)
 item(tt(CDABLE_VARS) (tt(-T)))(
Index: Src/options.c
===================================================================
RCS file: /cvsroot/zsh/zsh/Src/options.c,v
retrieving revision 1.5
diff -u -r1.5 options.c
--- Src/options.c	2000/07/30 17:03:53	1.5
+++ Src/options.c	2000/08/10 16:05:07
@@ -90,6 +90,7 @@
 {NULL, "bgnice",	      OPT_EMULATE|OPT_NONBOURNE, BGNICE},
 {NULL, "braceccl",	      OPT_EMULATE,		 BRACECCL},
 {NULL, "bsdecho",	      OPT_EMULATE|OPT_SH,	 BSDECHO},
+{NULL, "cbases",	      0,			 CBASES},
 {NULL, "cdablevars",	      OPT_EMULATE,		 CDABLEVARS},
 {NULL, "chasedots",	      OPT_EMULATE,		 CHASEDOTS},
 {NULL, "chaselinks",	      OPT_EMULATE,		 CHASELINKS},
Index: Src/params.c
===================================================================
RCS file: /cvsroot/zsh/zsh/Src/params.c,v
retrieving revision 1.25
diff -u -r1.25 params.c
--- Src/params.c	2000/08/03 13:10:13	1.25
+++ Src/params.c	2000/08/10 16:05:07
@@ -3045,7 +3045,12 @@
 	base = 10;
 
     if (base != 10) {
-	sprintf(s, "%d#", base);
+	if (isset(CBASES) && base == 16)
+	    sprintf(s, "0x");
+	else if (isset(CBASES) && base == 8 && isset(OCTALZEROES))
+	    sprintf(s, "0");
+	else
+	    sprintf(s, "%d#", base);
 	s += strlen(s);
     }
     for (x = v; x; digs++)
Index: Src/zsh.h
===================================================================
RCS file: /cvsroot/zsh/zsh/Src/zsh.h,v
retrieving revision 1.20
diff -u -r1.20 zsh.h
--- Src/zsh.h	2000/08/08 09:13:37	1.20
+++ Src/zsh.h	2000/08/10 16:05:07
@@ -1326,6 +1326,7 @@
     BGNICE,
     BRACECCL,
     BSDECHO,
+    CBASES,
     CDABLEVARS,
     CHASEDOTS,
     CHASELINKS,

-- 
Peter Stephenson <pws@csr.com>
Cambridge Silicon Radio, Unit 300, Science Park, Milton Road,
Cambridge, CB4 0XL, UK                          Tel: +44 (0)1223 392070


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: PATCH: C_BASES option for outputting hex and octal
  2000-08-10 16:11 PATCH: C_BASES option for outputting hex and octal Peter Stephenson
@ 2000-08-11 20:16 ` Zefram
  2000-08-12 15:46   ` Peter Stephenson
  0 siblings, 1 reply; 3+ messages in thread
From: Zefram @ 2000-08-11 20:16 UTC (permalink / raw)
  To: Peter Stephenson; +Cc: Zsh hackers list

Peter Stephenson wrote:
>Does anyone object to my adding options for trivial things like this?
>Because if so they ought to have got the point and given up objecting years
>ago :-/.  (I finally got fed up with 0x${$(([#16]$num))##*\#}.)

Wouldn't it be nicer to have $(([#0x]$num)) do the 0x-prefixed output,
or some equivalent syntax?  It'd be nicer not to have to fiddle with a
global option to control an essentially local decision.

Hmm.  Perhaps a better way to do this kind of syntax would be to have a
flag to say "don't output a radix prefix", so you'd do "0x$(([#16-]$num))"
to get the 0x prefix.  This is more flexible too, there are plenty of
situations where you want non-decimal output without any standard prefix.

-zefram


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: PATCH: C_BASES option for outputting hex and octal
  2000-08-11 20:16 ` Zefram
@ 2000-08-12 15:46   ` Peter Stephenson
  0 siblings, 0 replies; 3+ messages in thread
From: Peter Stephenson @ 2000-08-12 15:46 UTC (permalink / raw)
  To: Zsh hackers list

Zefram wrote:
> Wouldn't it be nicer to have $(([#0x]$num)) do the 0x-prefixed output,
> or some equivalent syntax?  It'd be nicer not to have to fiddle with a
> global option to control an essentially local decision.

Well, there is a local fix for this, and it was being fed up with having to
use it when all I wanted was what seemed to me the standard output that
made me fix it this way.  If the usual output wasn't so non-standard I
wouldn't have thought about it.  But the [#0x] idea is a good one (and I
supposed `typeset -i 0x foo' would be a natural companion, though option
argument passing for builtins is currently horrible).

-- 
Peter Stephenson <pws@pwstephenson.fsnet.co.uk>
Work: pws@csr.com
Web: http://www.pwstephenson.fsnet.co.uk


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2000-08-12 15:47 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2000-08-10 16:11 PATCH: C_BASES option for outputting hex and octal Peter Stephenson
2000-08-11 20:16 ` Zefram
2000-08-12 15:46   ` Peter Stephenson

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).