www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Putting quotes in wysiwyg strings

reply Bill Baxter <dnewsgroup billbaxter.com> writes:
 From the spec:  'There are no escape sequences inside r" ":'

Shouldn't there be an exception for the " character itself?
Right now the only way to put the quote into a wysiwyg string like
     foo"bar
is something like:
     r"foo" ~'"'~ r"bar";
or
     r"foo" "\"" r"bar";
or
     r"foo" `"` r"bar";

It would be a lot nicer if one could just do:

     r"foo\"bar";

And before you say I should just use ``, I'm writing an automatic 
resource wrapper that turns any text file into an embedded string, so I 
don't know ahead of time what's going to be in the string, and it may 
contain both `'s and "'s.

The goal is that the output string should look as much like the original 
text as possible.  One main use will be for embedding short script 
programs (like lua or minid) inside a D executable.

This:
     if (k & e.Shift) { ret ~= " `"` r"Shift" `"` r"; }
     if (k & e.Ctrl)  { if (ret) ret~=" `"` r"|" `"` r"; ret ~= " `"` 
r"Ctrl" `"` r"; }
     if (k & e.Alt)   { if (ret) ret~=" `"` r"|" `"` r"; ret ~= " `"` 
r"Alt" `"`
r"; }
     if (!ret) ret = " `"` r"None" `"` r";

definitely does not look very much like the original code.  :-(

But this does look reasonably close:
     if (k & e.Shift) { ret ~= \"Shift\"; }
     if (k & e.Ctrl)  { if (ret) ret~=\"|\"; ret ~= \"Ctrl\"; }
     if (k & e.Alt)   { if (ret) ret~=\"|\"; ret ~= \"Alt\"; }
     if (!ret) ret = \"None\";

--bb
Nov 22 2006
next sibling parent reply Lionello Lunesu <lio lunesu.remove.com> writes:
Bill Baxter wrote:
  From the spec:  'There are no escape sequences inside r" ":'
 
 Shouldn't there be an exception for the " character itself?
 Right now the only way to put the quote into a wysiwyg string like
     foo"bar
 is something like:
     r"foo" ~'"'~ r"bar";
 or
     r"foo" "\"" r"bar";
 or
     r"foo" `"` r"bar";
 
 It would be a lot nicer if one could just do:
 
     r"foo\"bar";

I'd prefer doubling the ", similar to program arguments on the command line: r"foo""bar" (This already compiles, but generates foobar instead of foo"bar; if you want foobar with the suggested behavior, you could add a space: r"foo" "bar") L.
Nov 22 2006
parent Bill Baxter <wbaxter gmail.com> writes:
Lionello Lunesu wrote:
 Bill Baxter wrote:
 
  From the spec:  'There are no escape sequences inside r" ":'

 Shouldn't there be an exception for the " character itself?
 [...]

I'd prefer doubling the ", similar to program arguments on the command line: r"foo""bar" (This already compiles, but generates foobar instead of foo"bar; if you want foobar with the suggested behavior, you could add a space: r"foo" "bar")

You're right. That would definitely be the cleaner solution. If you use \ to escape the quote then you need yet another rule to handle the case where you want a literal \ followed by a " in the string. --bb
Nov 22 2006
prev sibling next sibling parent reply Lionello Lunesu <lio lunesu.remove.com> writes:
Oh, by the way:

"
     if (k & e.Shift) { ret ~= ""Shift""; }
     if (k & e.Ctrl)  { if (ret) ret~=""|""; ret ~= ""Ctrl""; }
     if (k & e.Alt)   { if (ret) ret~=""|""; ret ~= ""Alt""; }
     if (!ret) ret = ""None"";
"

;)
Nov 22 2006
next sibling parent reply Bill Baxter <wbaxter gmail.com> writes:
Lionello Lunesu wrote:
 Oh, by the way:
 
 "
     if (k & e.Shift) { ret ~= ""Shift""; }
     if (k & e.Ctrl)  { if (ret) ret~=""|""; ret ~= ""Ctrl""; }
     if (k & e.Alt)   { if (ret) ret~=""|""; ret ~= ""Alt""; }
     if (!ret) ret = ""None"";
 "
 
 ;)

Yep, that looks quite reasonable (with the addition of an 'r' in front). I wish it worked. :-( --bb
Nov 22 2006
parent reply renox <renaud.hebert free.fr> writes:
Bah, I feel that putting any fixed delimitor for string is bond to create a
problem for some user whether it is simple, double quotes..

Why not reuse a good idea from the shell: <<FOO .... FOO
putting a customisable end-of-text word can avoid many problem.

Sure this makes the parser a bit more complex, but not that much..
So for D it would be something like:
r <word>" ... "<word> where word is some text without space
or r(<word>)" .... "<word>  or the same thing but with a keyword "raw" instead
of r.

Comments?

RenoX
Nov 22 2006
next sibling parent reply Wolven <ra wolven.net> writes:
== Quote from renox (renaud.hebert free.fr)'s article
 Bah, I feel that putting any fixed delimitor for string is bond to create a
 problem for some user whether it is simple, double quotes..
 Why not reuse a good idea from the shell: <<FOO .... FOO
 putting a customisable end-of-text word can avoid many problem.
 Sure this makes the parser a bit more complex, but not that much..
 So for D it would be something like:
 r <word>" ... "<word> where word is some text without space
 or r(<word>)" .... "<word>  or the same thing but with a keyword "raw" instead
of r.
 Comments?
 RenoX

Well RenoX, I, for one, support your idea. Years ago, I wondered why they didn't pick some better character than a single and\or double quotes to bracket literals with. Personally, I like the minature << and >> characters (ASCII decimal 171 and 187). You would still need some method of including those characters within a literal but at least you wouldn't have the problem with single and double quotes which are used in standard English text all the time... :)
Nov 22 2006
parent Lionello Lunesu <lio lunesu.remove.com> writes:
Wolven wrote:
    I, for one, support your idea.  Years ago, I wondered why they didn't pick
some
 better character than a single and\or double quotes to bracket literals with.
 Personally, I like the minature << and >> characters (ASCII decimal 171 and
187).

I'm sorry to nitpick, but that's not ASCII. "ALT-171", "-187" depend on the PC's code page.. I would definitely not be smart to pick characters outside the ASCII range (0-127) L.
Nov 22 2006
prev sibling next sibling parent reply Hasan Aljudy <hasan.aljudy gmail.com> writes:
renox wrote:
 Bah, I feel that putting any fixed delimitor for string is bond to create a
 problem for some user whether it is simple, double quotes..
 
 Why not reuse a good idea from the shell: <<FOO .... FOO
 putting a customisable end-of-text word can avoid many problem.
 
 Sure this makes the parser a bit more complex, but not that much..
 So for D it would be something like:
 r <word>" ... "<word> where word is some text without space
 or r(<word>)" .... "<word>  or the same thing but with a keyword "raw" instead
of r.
 
 Comments?
 
 RenoX

Walter likes to keep the lexer independent of the parser. I don't understand your suggestion very well, but it seems to me that it will make the lexer dependent on the parser, and maybe even on the semantic analyzer. Well, depending on how do you define these "words".
Nov 22 2006
parent renox <renaud.hebert free.fr> writes:
== Quote from Hasan Aljudy (hasan.aljudy gmail.com)'s article
 renox wrote:
 Bah, I feel that putting any fixed delimitor for string is bond to create a
 problem for some user whether it is simple, double quotes..

 Why not reuse a good idea from the shell: <<FOO .... FOO
 putting a customisable end-of-text word can avoid many problem.

 Sure this makes the parser a bit more complex, but not that much..
 So for D it would be something like:
 r <word>" ... "<word> where word is some text without space
 or r(<word>)" .... "<word>  or the same thing but with a keyword "raw" instead


 Comments?


 Walter likes to keep the lexer independent of the parser.
 I don't understand your suggestion very well, but it seems to me that it
 will make the lexer dependent on the parser, and maybe even on the
 semantic analyzer. Well, depending on how do you define these "words".

Do you know 'here strings' in shell/Perl? My suggestion is that D could provide the same thing (with a syntax more D-like of course) instead of trying to find the "mythical delimiter" for wysiwyg strings which would please everyone as it doesn't exist.. I'm not sure if 'here strings' makes the lexer dependant on the parser or not, I must admit that I'm not very good at grokking compilers. renoX
Nov 23 2006
prev sibling parent Bill Baxter <wbaxter gmail.com> writes:
renox wrote:
 Bah, I feel that putting any fixed delimitor for string is bond to create a
 problem for some user whether it is simple, double quotes..
 
 Why not reuse a good idea from the shell: <<FOO .... FOO
 putting a customisable end-of-text word can avoid many problem.
 
 Sure this makes the parser a bit more complex, but not that much..
 So for D it would be something like:
 r <word>" ... "<word> where word is some text without space
 or r(<word>)" .... "<word>  or the same thing but with a keyword "raw" instead
of r.
 
 Comments?

I wouldn't mind that, having gotten used to such "here-is documents" in languages like Perl and PHP. --bb
Nov 23 2006
prev sibling parent reply "John S. Skogtvedt" <jss2k2 chello.no> writes:
Lionello Lunesu skrev:
 Oh, by the way:
 
 "
     if (k & e.Shift) { ret ~= ""Shift""; }
     if (k & e.Ctrl)  { if (ret) ret~=""|""; ret ~= ""Ctrl""; }
     if (k & e.Alt)   { if (ret) ret~=""|""; ret ~= ""Alt""; }
     if (!ret) ret = ""None"";
 "
 
 ;)

r'''if (k & e.Shift) { ret ~= "Shift"; } if (k & e.Ctrl) { if (ret) ret~="|"; ret ~= "Ctrl"; } if (k & e.Alt) { if (ret) ret~="|"; ret ~= "Alt"; } if (!ret) ret = "None"''' or: r"""if (k & e.Shift) { ret ~= "Shift"; } if (k & e.Ctrl) { if (ret) ret~="|"; ret ~= "Ctrl"; } if (k & e.Alt) { if (ret) ret~="|"; ret ~= "Alt"; } if (!ret) ret = "None\""""
Nov 22 2006
parent reply Hasan Aljudy <hasan.aljudy gmail.com> writes:
John S. Skogtvedt wrote:
 Lionello Lunesu skrev:
 Oh, by the way:

 "
     if (k & e.Shift) { ret ~= ""Shift""; }
     if (k & e.Ctrl)  { if (ret) ret~=""|""; ret ~= ""Ctrl""; }
     if (k & e.Alt)   { if (ret) ret~=""|""; ret ~= ""Alt""; }
     if (!ret) ret = ""None"";
 "

 ;)

r'''if (k & e.Shift) { ret ~= "Shift"; } if (k & e.Ctrl) { if (ret) ret~="|"; ret ~= "Ctrl"; } if (k & e.Alt) { if (ret) ret~="|"; ret ~= "Alt"; } if (!ret) ret = "None"''' or: r"""if (k & e.Shift) { ret ~= "Shift"; } if (k & e.Ctrl) { if (ret) ret~="|"; ret ~= "Ctrl"; } if (k & e.Alt) { if (ret) ret~="|"; ret ~= "Alt"; } if (!ret) ret = "None\""""

but if D has ''' or """ the problem will still manifest in the same way!! counter example code: auto x = """hey!''"""; auto y = '''yo!'''; //did you think you can use ` to do this? auto z = """ oi ` oi """;
Nov 22 2006
parent "John S. Skogtvedt" <jss2k2 chello.no> writes:
Hasan Aljudy skrev:
 
 
 John S. Skogtvedt wrote:
 Lionello Lunesu skrev:
 Oh, by the way:

 "
     if (k & e.Shift) { ret ~= ""Shift""; }
     if (k & e.Ctrl)  { if (ret) ret~=""|""; ret ~= ""Ctrl""; }
     if (k & e.Alt)   { if (ret) ret~=""|""; ret ~= ""Alt""; }
     if (!ret) ret = ""None"";
 "

 ;)

r'''if (k & e.Shift) { ret ~= "Shift"; } if (k & e.Ctrl) { if (ret) ret~="|"; ret ~= "Ctrl"; } if (k & e.Alt) { if (ret) ret~="|"; ret ~= "Alt"; } if (!ret) ret = "None"''' or: r"""if (k & e.Shift) { ret ~= "Shift"; } if (k & e.Ctrl) { if (ret) ret~="|"; ret ~= "Ctrl"; } if (k & e.Alt) { if (ret) ret~="|"; ret ~= "Alt"; } if (!ret) ret = "None\""""

but if D has ''' or """ the problem will still manifest in the same way!! counter example code: auto x = """hey!''"""; auto y = '''yo!'''; //did you think you can use ` to do this? auto z = """ oi ` oi """;

auto z = " oi ` oi ";
Nov 23 2006
prev sibling parent reply Stewart Gordon <smjg_1998 yahoo.com> writes:
Bill Baxter wrote:
  From the spec:  'There are no escape sequences inside r" ":'
 
 Shouldn't there be an exception for the " character itself?

That would defeat the whole point.
 Right now the only way to put the quote into a wysiwyg string like
     foo"bar
 is something like:
     r"foo" ~'"'~ r"bar";
 or
     r"foo" "\"" r"bar";
 or
     r"foo" `"` r"bar";

You missed off r"foo" \" r"bar";
 It would be a lot nicer if one could just do:
 
     r"foo\"bar";

Then how would one put a backslash at the end of a wysiwyg string? <snip>
 The goal is that the output string should look as much like the original 
 text as possible.  One main use will be for embedding short script 
 programs (like lua or minid) inside a D executable.
 
 This:
     if (k & e.Shift) { ret ~= " `"` r"Shift" `"` r"; }
     if (k & e.Ctrl)  { if (ret) ret~=" `"` r"|" `"` r"; ret ~= " `"` 
 r"Ctrl" `"` r"; }
     if (k & e.Alt)   { if (ret) ret~=" `"` r"|" `"` r"; ret ~= " `"` 
 r"Alt" `"`
 r"; }
     if (!ret) ret = " `"` r"None" `"` r";
 
 definitely does not look very much like the original code.  :-(

I see now. Do you read the input line by line? If so, you could detect whether each line contains '"' or '`', and switch between r"..." and `...` at the beginning of the line if necessary. You'll only need such messy code as the above for lines that contain both characters. BTW I've used the search and replace technique before. See http://smjg.port5.com/wwwep/quines/d.html Stewart. -- -----BEGIN GEEK CODE BLOCK----- Version: 3.1 GCS/M d- s:- C++ a->--- UB P+ L E W++ N+++ o K- w++ O? M V? PS- PE- Y? PGP- t- 5? X? R b DI? D G e++++ h-- r-- !y ------END GEEK CODE BLOCK------ My e-mail is valid but not my primary mailbox. Please keep replies on the 'group where everyone may benefit.
Nov 23 2006
parent Bill Baxter <wbaxter gmail.com> writes:
Stewart Gordon wrote:
 Bill Baxter wrote:
 
 You missed off
 
     r"foo" \" r"bar";

Thanks. Didn't know that would work.
 It would be a lot nicer if one could just do:

     r"foo\"bar";

Then how would one put a backslash at the end of a wysiwyg string?

Yeh, using "" for " would be better, as Lionello pointed out. The only problem is that it changes the meaning of r"""" from an empty string (currently) to a string Python has r-strings too, and their rules are really wacky. \" escapes the quote charater, *but* the backslash still appears in the output. And r"\" is not possible. At least D's rule is easy to remember!
 I see now.  Do you read the input line by line?  If so, you could detect 
 whether each line contains '"' or '`', and switch between r"..." and 
 `...` at the beginning of the line if necessary.  You'll only need such 
 messy code as the above for lines that contain both characters.

I think I should just accept that raw strings are not the right tool for this job. I'll just stick with regular strings as the default and hope for Perl-ish "here documents" someday. --bb
Nov 23 2006