From: Peter Stephenson <p.stephenson@samsung.com>
To: Zsh Hackers' List <zsh-workers@zsh.org>
Subject: 0b numeric prefix
Date: Tue, 25 Nov 2014 16:07:18 +0000 [thread overview]
Message-ID: <20141125160718.01444ac8@pwslap01u.europe.root.pri> (raw)
This adds the fairly common (but certainly far from universal) 0b prefix
to denote binary on input. I think numeric parsing is designed so this
can't conflict with anything existing that's valid.
I'm not sure whether to output this form in C_BASES mode. On the one
hand, it's more likely to be understood on input in utilities other than
shells than 2#blah; on the other, it won't work with older versions of
zsh or other shells (not that C_BASES is designed with other shells in
mind). Because of the latter I'm not intending to do this. It would
also be a perversion of the name of the option.
I'll add some tests later.
pws
diff --git a/Doc/Zsh/arith.yo b/Doc/Zsh/arith.yo
index 96dc2dc..a620b73 100644
--- a/Doc/Zsh/arith.yo
+++ b/Doc/Zsh/arith.yo
@@ -39,7 +39,8 @@ zero status.
cindex(arithmetic base)
cindex(bases, in arithmetic)
Integers can be in bases other than 10.
-A leading `tt(0x)' or `tt(0X)' denotes hexadecimal.
+A leading `tt(0x)' or `tt(0X)' denotes hexadecimal and a leading
+`tt(0b)' or `tt(0B) binary.
Integers may also be of the form `var(base)tt(#)var(n)',
where var(base) is a decimal number between two and thirty-six
representing the arithmetic base and var(n)
diff --git a/Src/math.c b/Src/math.c
index 2665698..438a170 100644
--- a/Src/math.c
+++ b/Src/math.c
@@ -449,12 +449,14 @@ lexconstant(void)
nptr++;
if (*nptr == '0') {
+ int lowchar;
nptr++;
- if (*nptr == 'x' || *nptr == 'X') {
+ lowchar = tolower(*nptr);
+ if (lowchar == 'x' || lowchar == 'b') {
/* Let zstrtol parse number with base */
yyval.u.l = zstrtol_underscore(ptr, &ptr, 0, 1);
/* Should we set lastbase here? */
- lastbase = 16;
+ lastbase = (lowchar == 'b') ? 2 : 16;
if (isset(FORCEFLOAT))
{
yyval.type = MN_FLOAT;
diff --git a/Src/utils.c b/Src/utils.c
index c6e7aed..5f0c106 100644
--- a/Src/utils.c
+++ b/Src/utils.c
@@ -2082,6 +2082,8 @@ zstrtol_underscore(const char *s, char **t, int base, int underscore)
base = 10;
else if (*++s == 'x' || *s == 'X')
base = 16, s++;
+ else if (*s == 'b' || *s == 'B')
+ base = 2, s++;
else
base = 8;
}
pws
next reply other threads:[~2014-11-25 16:07 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2014-11-25 16:07 Peter Stephenson [this message]
2014-11-25 16:47 ` Christian Neukirchen
2014-11-25 16:53 ` Peter Stephenson
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=20141125160718.01444ac8@pwslap01u.europe.root.pri \
--to=p.stephenson@samsung.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).