Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

readable syntax for control characters in strings

66 views
Skip to first unread message

Erik Naggum

unread,
Oct 29, 1996, 3:00:00 AM10/29/96
to

would an ANSI Common Lisp implementation that allowed the single-escape
character followed by a character other than the single-escape character or
the double-quote in string literals to mean something other than that
character be conforming?

example: according to the specification: "foo\nbar" is interpreted as a
7-character string whose fourth element is #\n. would an implementation
that returned a string whose fourth element were #\Newline be conforming?

#\Erik
--
Those who do not know Lisp are doomed to reimplement it.

Barry Margolin

unread,
Oct 30, 1996, 3:00:00 AM10/30/96
to

In article <30555845...@naggum.no>, Erik Naggum <er...@naggum.no> wrote:
>would an ANSI Common Lisp implementation that allowed the single-escape
>character followed by a character other than the single-escape character or
>the double-quote in string literals to mean something other than that
>character be conforming?

No. The standard specifies precisely how "\n" should be interpreted,
leaving no room for implementation dependencies. Thus, a programmer should
feel safe including such a string in his programs without affecting their
portability among CL implementations.

--
Barry Margolin
BBN Planet, Cambridge, MA
bar...@bbnplanet.com - Phone (617) 873-3126 - Fax (617) 873-5508
(BBN customers, please call (800) 632-7638 option 1 for support)

Erik Naggum

unread,
Oct 31, 1996, 3:00:00 AM10/31/96
to

* Barry Margolin <bar...@tools.bbnplanet.com>
| No.

thanks.

given that a syntax familiar to Emacs Lisp (and C) users is unavailable,
how would one accomplish the same goal: to embed a (named) control
character in a literal string in source code that is likely to travel
across all sorts of information-losing channels?

for an arbitrary character, either of the following forms produce the
desired result, but I find them clumsy. do others do something else?

#.(format nil "foo~Cbar" #\Newline)
#.(concatenate 'string "foo" '(#\Newline) "bar")

(incidentally, my actual problem case involves lots of escape sequences for
a form printer now written in Emacs Lisp with the `cl' package. it is
trivially portable to Common Lisp except for the string literals.)

I feel I must have overlooked something or think in the wrong terms.

Rob Warnock

unread,
Nov 1, 1996, 3:00:00 AM11/1/96
to

Erik Naggum <er...@naggum.no> wrote:
+---------------
| given that a syntax familiar to Emacs Lisp (and C) users is unavailable...

| #.(format nil "foo~Cbar" #\Newline)
| #.(concatenate 'string "foo" '(#\Newline) "bar")
+---------------

You realize, I assume, that this particular example can already be
done easily in CL, like so:

#.(format nil "foo~%bar")

And (if you don't mind tabs being expanded) for "foo\tbar\nbaz\tgorp\n\f"
you can use:


#.(format nil "foo~9,8Tbar~%baz~9,8Tgorp~%~|")

So back to the general case...

+---------------


| how would one accomplish the same goal: to embed a (named) control
| character in a literal string in source code that is likely to travel

| across all sorts of information-losing channels? ...


| I feel I must have overlooked something or think in the wrong terms.

+---------------

I suspect the latter. Look, Lisp is a language for building languages,
right? So design a "language" for expressing the kind of strings you
want, and build a mechanism for translating that into Lisp.

Oh? You say you already *have* the language you want, namely, good old
familiar C strings? Fine! Then simply write a function that takes C
strings as input (in the form of "control strings") and gives you Lisp
strings as output. Something like this:

(cstr->string "First line:\\ttext\\nSecond line:\\tmore text\\007\\n")
==> "First line: text
Second line: more text(*ding!*)
" [i.e. ^G ]

(Unfortunately, if you want the use "backslash" as your escape character,
you'd have to double them everywhere [as shown] to get them into your
control string. And if you want to use C strings in "format" calls, you
probably don't want to use "~", either. How about "%" or "^" or "&"?)

Then if you wanted to use these inside "format", perhaps to print lines
with fields prefixed by the tab character and suffixed by (say) <cntl-A>,
you could do something like this (let "c2s" be an alias for "cstr->string"):

(format nil #.(c2s "\\tfield1=~A\\001\\tfield2=~A\\001~%") 123 456)
==> " field1=123 field2=456
"
a.k.a. ==> "^Ifield1=123^A^Ifield2=456^A^J"

By the way, with some work (well, probably quite a bit more!), a similar
hack would enable one to rewrite C "printf" control strings into "format"
control strings (at least, for those things that map):

(pf2fmt "foo = %8.8x, bar = %d\\n")
==> "foo = ~8,'0X, bar = ~D~%"

so you could say:

(format nil #.(pf2fmt "foo = 0x%8.8x, bar = %d\\n") 123456 789)
==> "foo = 0x0001e240, bar = 789
"

+---------------


| (incidentally, my actual problem case involves lots of escape sequences for
| a form printer now written in Emacs Lisp with the `cl' package. it is
| trivially portable to Common Lisp except for the string literals.)

+---------------

So write yourself a "c2s" function, and with a little editing to double
the backslashes and wrap #.(c2s ...) around the strings, you're home.


-Rob

-----
Rob Warnock, 7L-551 rp...@sgi.com
Silicon Graphics, Inc. http://reality.sgi.com/rpw3/
2011 N. Shoreline Blvd. Phone: 415-933-1673 FAX: 415-933-0979
Mountain View, CA 94043 PP-ASEL-IA

Erik Naggum

unread,
Nov 1, 1996, 3:00:00 AM11/1/96
to

* Erik Naggum <er...@naggum.no>
| given that a syntax familiar to Emacs Lisp (and C) users is unavailable,

| how would one accomplish the same goal: to embed a (named) control
| character in a literal string in source code that is likely to travel
| across all sorts of information-losing channels?
:

| I feel I must have overlooked something or think in the wrong terms.

I did. reader macros.

0 new messages