Hi,
How are you supposed to create strings with literal backslashes in
them?
I need a function that takes some string and returns another with any
single quotes backslashed.
(defun js-escape (str)
(with-output-to-string (s)
(loop for char across str do
(case char
(#\' (prin1 "\'" s))
(otherwise (princ char s))))))
I tried various combinations of prin1, princ and print with single and
double backslashes in the "replacement" string above but nothing
seemed to work.
Cheers,
Andy
Andy Chambers <··············@googlemail.com> writes:
> Hi,
>
> How are you supposed to create strings with literal backslashes in
> them?
Using doubled backslashes:
"\\"
> I need a function that takes some string and returns another with any
> single quotes backslashed.
>
> (defun js-escape (str)
> (with-output-to-string (s)
> (loop for char across str do
> (case char
> (#\' (prin1 "\'" s))
> (otherwise (princ char s))))))
>
> I tried various combinations of prin1, princ and print with single and
> double backslashes in the "replacement" string above but nothing
> seemed to work.
Well, one other method would have been:
...
(#\' (princ #\\ s) (princ #\' s))
...
I will also note that if you do this a lot, it may be worthwhile to
first check the input string for the presence of #\' before going
through the overhead of setting up the output string stream, iterating
and printing on a character-by-character basis.
That does add another pass through the string for any strings that
contain single quotes, but it saves consing up a new string and the
string stream overhead for cases where there are no such items. Of
course, if most of the strings you process will have ' in them, then you
might not save anything.
--
Thomas A. Russ, USC/Information Sciences Institute
(length "a\\\'b")
-> 4
Andy Chambers wrote:
> Hi,
>
> How are you supposed to create strings with literal backslashes in
> them?
>
> I need a function that takes some string and returns another with any
> single quotes backslashed.
>
> (defun js-escape (str)
> (with-output-to-string (s)
> (loop for char across str do
> (case char
> (#\' (prin1 "\'" s))
> (otherwise (princ char s))))))
>
> I tried various combinations of prin1, princ and print with single and
> double backslashes in the "replacement" string above but nothing
> seemed to work.
>
> Cheers,
> Andy
--
http://smuglispweeny.blogspot.com/
http://www.theoryyalgebra.com/
"In the morning, hear the Way;
in the evening, die content!"
-- Confucius
On Apr 3, 5:27 pm, Ken Tilton <···········@optonline.net> wrote:
> (length "a\\\'b")
> -> 4
Which is also this
CL-USER> (let ((s "a\\\'b"))
(loop for c across s do (princ c))
s)
a\'b
"a\\'b"
CL-USER>
Ken Tilton <···········@optonline.net> writes:
> (length "a\\\'b")
> -> 4
I assume you mean:
C/USER[169]> (princ "a\\\"b")
a\"b
"a\\\"b"
Note also these other ways:
C/USER[171]> (princ (format nil "a~C~Cb" #\\ #\"))
a\"b
"a\\\"b"
C/USER[176]> (princ (coerce #(#\a #\\ #\" #\b) 'string))
a\"b
"a\\\"b"
C/USER[179]> (princ (concatenate 'string "a" #(#\\ #\") "b"))
a\"b
"a\\\"b"
etc...
--
A: Because it messes up the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
----------> http://www.netmeister.org/news/learn2quote.html <-----------
---> http://homepage.ntlworld.com/g.mccaughan/g/remarks/uquote.html <---
__Pascal Bourguignon__ http://www.informatimago.com/
Andy Chambers wrote:
> Hi,
>
> How are you supposed to create strings with literal backslashes in
> them?
>
> I need a function that takes some string and returns another with any
> single quotes backslashed.
>
> (defun js-escape (str)
> (with-output-to-string (s)
> (loop for char across str do
> (case char
> (#\' (prin1 "\'" s))
> (otherwise (princ char s))))))
Maybe?:
(defun js-escape (str)
(with-output-to-string (s)
(loop for char across str do
(case char
(#\' (princ "\\\'" s))
(otherwise (princ char s))))))
(js-escape "hi'mom")
-> "hi\\'mom"
kt
--
http://smuglispweeny.blogspot.com/
http://www.theoryyalgebra.com/
"In the morning, hear the Way;
in the evening, die content!"
-- Confucius