From: Thien-Thi Nguyen <ttn@gnuvola.org>
To: florian <lorian@fsavigny.de>
Cc: help-gnu-emacs@gnu.org
Subject: Re: Is X'ABCD' notation of general interest?
Date: Sun, 12 Apr 2009 13:45:53 +0200 [thread overview]
Message-ID: <8763hajdse.fsf@ambire.localdomain> (raw)
In-Reply-To: <45202d31-fd33-4b35-9bb7-fe4af6eb4012@c9g2000yqm.googlegroups.com> (florian's message of "Thu, 9 Apr 2009 03:05:03 -0700 (PDT)")
() florian <lorian@fsavigny.de>
() Thu, 9 Apr 2009 03:05:03 -0700 (PDT)
The X'ABCD' notation represents arbitrary bytes as their
two-digit hexadecimal representation, which produces strings
that have double the size and can be passed over the command
line (SQLite uses this). Does anybody think that functions to
handle that (I have round-trip tested them) would be of general
interest, so that posting them on gnu.emacs.sources would make
sense? (Just don't want to clutter public space needlessly.)
You may be interested in knowing that Emacs can `read' hex if the
number has the "#x" prefix.
(let ((standard-input "#x0a"))
(read))
=> 10
Something like the following (completely untested) code might be
faster than using `string-to-number':
(defvar small "#x......"
"Fixed-size buffer holding a small hex number to be `read'.")
(defun next-small-X-quote-hex-quote-number ()
"Return the next parsed X'HEX-NUMBER' or nil."
(let ((ok (- (length small) 2)) ;; -2 for "#x"
b e)
(when (re-search-forward "X'[0-9a-fA-F]+'" nil t)
(setq b (+ 2 (match-beginning 0))
e (1- (point)))
(if (not (< ok (- e b)))
;; "fast path" (one hopes, one wonders)
(do ((w 2 (1+ w)))
((= b e) (let ((standard-input small))
(read)))
(aset small w (char-before (incf b))))
;; fallback for big strings
(string-to-number (buffer-substring b e) 16)))))
This is the limit (performance wise) if the buffer is not to be
modified. If the buffer can be modified, you can go even faster
if you rewrite the "X'" as "x#" directly and set `standard-input'
to `(current-buffer)' (plus usual performance tweaks). Of course,
if speed matters not, then no worries, feel free to ignore...
thi
next prev parent reply other threads:[~2009-04-12 11:45 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2009-04-06 22:22 Converting strings of hexadecimal numbers to the respective bytes florian
2009-04-06 22:54 ` Juanma Barranquero
[not found] ` <mailman.4740.1239058484.31690.help-gnu-emacs@gnu.org>
2009-04-09 10:05 ` Is X'ABCD' notation of general interest? (was: Converting strings of hexadecimal numbers to the respective bytes) florian
2009-04-09 12:24 ` Juanma Barranquero
2009-04-12 11:45 ` Thien-Thi Nguyen [this message]
2009-04-12 21:25 ` Is X'ABCD' notation of general interest? Nikolaj Schumacher
2009-04-13 14:12 ` Thien-Thi Nguyen
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=8763hajdse.fsf@ambire.localdomain \
--to=ttn@gnuvola.org \
--cc=help-gnu-emacs@gnu.org \
--cc=lorian@fsavigny.de \
/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 external index
https://git.savannah.gnu.org/cgit/emacs.git
https://git.savannah.gnu.org/cgit/emacs/org-mode.git
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.