Page 1 of 2

Special chars in strings

Posted: Tue Jul 01, 2014 5:46 pm
by djes
I know it has already be asked, and that it adds a bit of complexity to a beginner language, but I've had numerous times this problem. The last time was with the form designer : I wanted to have a default string with double quotes (") chr(34). The form designer accepted it, but complained when changing tooltip (!)

So, wouldn't it be possible to internally parse the strings so that we can use these special chars. Maybe with a \, or a special code, "&34", I don't know, I just say that it would be really valuable !

Thank you !

Re: Special chars in strings

Posted: Wed Jul 02, 2014 10:26 am
by IdeasVacuum
'Escaped' characters is a commonplace solution. I think the (15) escapes from C are handy..........
\a Alarm (Beep/Bell)
\b Backspace
\f Formfeed
\n Newline (Line Feed)
\r Carriage Return
\t Horizontal Tab
\v Vertical Tab
\\ Backslash
\' Single quote
\" Double quote
\? Question mark
\0 Null
\ooo Octal. There can be 1, 2 or 3 octal digits, \o, \oo and \ooo are all valid
\xhh Hex
\uhhhh Unicode character

Re: Special chars in strings

Posted: Wed Jul 02, 2014 10:48 am
by djes
IdeasVacuum wrote:'Escaped' characters is a commonplace solution. I think the (15) escapes from C are handy..........
\a Alarm (Beep/Bell)
\b Backspace
\f Formfeed
\n Newline (Line Feed)
\r Carriage Return
\t Horizontal Tab
\v Vertical Tab
\\ Backslash
\' Single quote
\" Double quote
\? Question mark
\0 Null
\ooo Octal. There can be 1, 2 or 3 octal digits, \o, \oo and \ooo are all valid
\xhh Hex
\uhhhh Unicode character
+1 totally agree.
The bad is that for storing windows path we would need to \\ a lot, and it would be really ugly. I'd rather prefer an escape character rarely used, like the old ESC character '^', but I know the balance will be in favor of '\'...

Re: Special chars in strings

Posted: Wed Jul 02, 2014 11:23 am
by infratec
@djes

I think you can also use / in windows path.

Re: Special chars in strings

Posted: Wed Jul 02, 2014 1:22 pm
by djes
infratec wrote:@djes

I think you can also use / in windows path.
Yes, for internal commands, but if we design a windows program with hardcoded paths in strings, it will be a pain to write \\ everywhere :/

Re: Escape characters in strings

Posted: Wed Jul 02, 2014 3:15 pm
by Tenaja
If I had to guess, it is unlikely Fred will implement this system-wide. If he did, every bit of code containing a string with a backslash from the beginning of PB time would require a rewrite...and it could be a difficult debug, too. Especially with strings that are rarely used.

With this routine you can replace escape chars at your leisure, rather than be forced to use it everywhere (massive rewrites), or add a flag to every string routine (every string call becomes slower).

Code: Select all

Procedure.s SwapEscapeCharacters(s.s)
	s = ReplaceString(s, "\r", #CR$)	  ; 13		; NOTE: Windows (CRLF)
	s = ReplaceString(s, "\t", #TAB$)	 ; 9
	s = ReplaceString(s, "\n", #LF$)	  ; 10
	s = ReplaceString(s, "\\", "\")		; 92
	ProcedureReturn s
EndProcedure

Re: Escape characters in strings

Posted: Wed Jul 02, 2014 3:24 pm
by djes
Tenaja wrote:If I had to guess, it is unlikely Fred will implement this system-wide. If he did, every bit of code containing a string with a backslash from the beginning of PB time would require a rewrite...and it could be a difficult debug, too. Especially with strings that are rarely used.
Yes, but it wouldn't be a step backward ;)
Tenaja wrote:With this routine you can replace escape chars at your leisure, rather than be forced to use it everywhere (massive rewrites), or add a flag to every string routine (every string call becomes slower).

Code: Select all

Procedure.s SwapEscapeCharacters(s.s)
	s = ReplaceString(s, "\r", #CR$)	  ; 13		; NOTE: Windows (CRLF)
	s = ReplaceString(s, "\t", #TAB$)	 ; 9
	s = ReplaceString(s, "\n", #LF$)	  ; 10
	s = ReplaceString(s, "\\", "\")		; 92
	ProcedureReturn s
EndProcedure
Of course, but the goal of this thread is precisely to have it natively.

Re: Escape characters in strings

Posted: Wed Jul 02, 2014 4:05 pm
by Tenaja
djes wrote: Of course, but the goal of this thread is precisely to have it natively.
Then my vote is...

-1...NOOOOooooo....

I chose PB over C because of its native string handling. I far have too much string code to support this request. For the rare times you need it, it is very simple to call a proc.

Re: Escape characters in strings

Posted: Wed Jul 02, 2014 4:27 pm
by djes
Tenaja wrote:
djes wrote: Of course, but the goal of this thread is precisely to have it natively.
Then my vote is...

-1...NOOOOooooo....

I chose PB over C because of its native string handling. I far have too much string code to support this request. For the rare times you need it, it is very simple to call a proc.
:lol:

Re: Special chars in strings

Posted: Wed Jul 02, 2014 4:31 pm
by wilbert
I would like support for escaped strings.
To not cause any problems, using a prefix could be a solution.
For example

A.s = %"A string\r\nwith escaped characters\r\ninside it"

for an escaped string and

A.s = "Normal string"

for a normal string.

Re: Special chars in strings

Posted: Wed Jul 02, 2014 4:39 pm
by IdeasVacuum
Good suggestion Wilbert

Re: Special chars in strings

Posted: Wed Jul 02, 2014 4:52 pm
by djes
Yes, good idea, or ala PHP syntax : strings with simples quotes ('\n\p\\') are parsed, and strings with double quotes ("\my damn path"), not parsed.

Re: Special chars in strings

Posted: Wed Jul 02, 2014 6:30 pm
by STARGÅTE
The single quote in PB is reserved for Characters:

Code: Select all

Debug 'A' ; gives 65
Debug 'φ' ; gives 966

Re: Special chars in strings

Posted: Wed Jul 02, 2014 10:45 pm
by djes
STARGÅTE wrote:The single quote in PB is reserved for Characters:

Code: Select all

Debug 'A' ; gives 65
Debug 'φ' ; gives 966
Oh yes, I've almost forgotten that ! Big jump in the past ! Thank you for this precision.

Re: Special chars in strings

Posted: Thu Jul 03, 2014 7:21 am
by Danilo
There is another language that uses the tilde to prevent the ugly, but on Windows common, path problems with '\':

Code: Select all

Procedure.s Esc(s.s)
   s = ReplaceString(s, "~q", Chr( 34)) ;  34 (quotation mark ")
   s = ReplaceString(s, "~n", Chr( 10)) ;  10 (newline)
   s = ReplaceString(s, "~r", Chr( 13)) ;  13 (return)
   s = ReplaceString(s, "~t", Chr(  9)) ;   9 (tab)
   s = ReplaceString(s, "~~", Chr(126)) ; 126 (tilde ~)
   ProcedureReturn s
EndProcedure

Debug Esc("some ~qText~q ~~ ~t~t~t~t and a~r~nnew line")