From: Andy Chambers
Subject: literal backslash
Date: 
Message-ID: <bec94de2-76bb-4109-9ae2-338492f8e4df@s13g2000prd.googlegroups.com>
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

From: Thomas A. Russ
Subject: Re: literal backslash
Date: 
Message-ID: <ymilk3ur2el.fsf@blackcat.isi.edu>
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
From: Ken Tilton
Subject: Re: literal backslash
Date: 
Message-ID: <47F559C6.205@optonline.net>
(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
From: dkixk
Subject: Re: literal backslash
Date: 
Message-ID: <c8e2374f-78de-4e9c-bc7f-27629f7480a1@z38g2000hsc.googlegroups.com>
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>
From: Pascal Bourguignon
Subject: Re: literal backslash
Date: 
Message-ID: <87ej9mkwfx.fsf@thalassa.informatimago.com>
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/
From: Ken Tilton
Subject: Re: literal backslash
Date: 
Message-ID: <47F5A7AB.7050701@optonline.net>
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