Special chars in strings

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
User avatar
djes
Addict
Addict
Posts: 1806
Joined: Sat Feb 19, 2005 2:46 pm
Location: Pas-de-Calais, France

Special chars in strings

Post 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 !
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: Special chars in strings

Post 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
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
User avatar
djes
Addict
Addict
Posts: 1806
Joined: Sat Feb 19, 2005 2:46 pm
Location: Pas-de-Calais, France

Re: Special chars in strings

Post 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 '\'...
infratec
Always Here
Always Here
Posts: 7588
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Special chars in strings

Post by infratec »

@djes

I think you can also use / in windows path.
User avatar
djes
Addict
Addict
Posts: 1806
Joined: Sat Feb 19, 2005 2:46 pm
Location: Pas-de-Calais, France

Re: Special chars in strings

Post 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 :/
User avatar
Tenaja
Addict
Addict
Posts: 1959
Joined: Tue Nov 09, 2010 10:15 pm

Re: Escape characters in strings

Post 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
User avatar
djes
Addict
Addict
Posts: 1806
Joined: Sat Feb 19, 2005 2:46 pm
Location: Pas-de-Calais, France

Re: Escape characters in strings

Post 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.
User avatar
Tenaja
Addict
Addict
Posts: 1959
Joined: Tue Nov 09, 2010 10:15 pm

Re: Escape characters in strings

Post 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.
User avatar
djes
Addict
Addict
Posts: 1806
Joined: Sat Feb 19, 2005 2:46 pm
Location: Pas-de-Calais, France

Re: Escape characters in strings

Post 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:
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3942
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: Special chars in strings

Post 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.
Windows (x64)
Raspberry Pi OS (Arm64)
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: Special chars in strings

Post by IdeasVacuum »

Good suggestion Wilbert
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
User avatar
djes
Addict
Addict
Posts: 1806
Joined: Sat Feb 19, 2005 2:46 pm
Location: Pas-de-Calais, France

Re: Special chars in strings

Post 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.
User avatar
STARGÅTE
Addict
Addict
Posts: 2227
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

Re: Special chars in strings

Post by STARGÅTE »

The single quote in PB is reserved for Characters:

Code: Select all

Debug 'A' ; gives 65
Debug 'φ' ; gives 966
PB 6.01 ― Win 10, 21H2 ― Ryzen 9 3900X, 32 GB ― NVIDIA GeForce RTX 3080 ― Vivaldi 6.0 ― www.unionbytes.de
Lizard - Script language for symbolic calculations and moreTypeface - Sprite-based font include/module
User avatar
djes
Addict
Addict
Posts: 1806
Joined: Sat Feb 19, 2005 2:46 pm
Location: Pas-de-Calais, France

Re: Special chars in strings

Post 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.
User avatar
Danilo
Addict
Addict
Posts: 3036
Joined: Sat Apr 26, 2003 8:26 am
Location: Planet Earth

Re: Special chars in strings

Post 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")
Post Reply