Page 1 of 2

Space in preference string

Posted: Thu Sep 05, 2024 11:50 pm
by le_magn
Hi all, i have a question, in preference file i have a problem fo example:

Code: Select all

[MAIN]
String =  / 
the value contain 2 spaces, 1 before / and 1 after (space/space) but i can't read the first space
readpreference string trim the space before /.
Is this a bug?

Re: Space in preference string

Posted: Fri Sep 06, 2024 1:34 am
by AZJIO

Code: Select all

If OpenPreferences("C:\1.ini")
	PreferenceGroup("MAIN")
	Debug "|" + Trim(ReadPreferenceString("String", ""), ~"\"") + "|"
	ClosePreferences()
EndIf

Code: Select all

[MAIN]
String = " / "

Re: Space in preference string

Posted: Sat Sep 07, 2024 12:26 am
by le_magn
AZJIO wrote: Fri Sep 06, 2024 1:34 am

Code: Select all

If OpenPreferences("C:\1.ini")
	PreferenceGroup("MAIN")
	Debug "|" + Trim(ReadPreferenceString("String", ""), ~"\"") + "|"
	ClosePreferences()
EndIf

Code: Select all

[MAIN]
String = " / "
Thank's for answeer, but this is only a workaround, simple to do but not solve the question, why readpreference trim all spaces at left of string? i think this is not normal

Re: Space in preference string

Posted: Sat Sep 07, 2024 1:23 am
by BarryG
Yes, this is a bug. The manual says the format is:

Code: Select all

'Keyword = Value' syntax
So the keyword and value are separated by " = " only. This means values starting with spaces shouldn't be trimmed, because the extra leading spaces may be wanted.

Re: Space in preference string

Posted: Sat Sep 07, 2024 7:21 am
by Fred
Moved for investigation

Re: Space in preference string

Posted: Sat Sep 07, 2024 7:45 am
by AZJIO
You can make this rule with #PB_Preference_NoSpace

Re: Space in preference string

Posted: Sat Sep 07, 2024 7:47 am
by User_Russian
Also, don't forget about compatibility. Many programs are written with the idea that spaces are automatically trimmed.
In addition, ini files are often edited manually and can accidentally enter an extra space near a key or value (without noticing it) and this will break the program.

Re: Space in preference string

Posted: Sat Sep 07, 2024 9:03 am
by infratec
I think starting a value wth a space is in general a bad idea.
If it is not avoidable the value should be written in double qoutes like in other casses too.
Like this:

Code: Select all

WritePreferenceString("Key", #DQUOTE$ + Value$ + #DQUOTE$)
Else you can never be sure how many spaces in front or tail are wanted or not.

Re: Space in preference string

Posted: Sat Sep 07, 2024 9:17 am
by Little John
I agree with you, infratec. And the most important point in this context is, that PB's behaviour is precisely documented.

Re: Space in preference string

Posted: Sat Sep 07, 2024 12:24 pm
by BarryG
@infratec: It depends if your value contains #DQUOTE$ as well. Best to have a set rule and stick to it.

Re: Space in preference string

Posted: Sat Sep 07, 2024 12:40 pm
by Little John
BarryG wrote: It depends if your value contains #DQUOTE$ as well.
There is an old solution for this old problem: You can escape any #DQUOTE$ in your data.

Re: Space in preference string

Posted: Sat Sep 07, 2024 1:38 pm
by BarryG
What if the value looks like this? Don't laugh; it's a possibility.

Code: Select all

Keyname =     " " """ "   "

Re: Space in preference string

Posted: Sat Sep 07, 2024 1:48 pm
by Little John
There are several possibilities. It's your program and your PREFS file – you can do whatever fits your needs. :-)
For instance, you can write ' in the PREFS file, and after reading it, the program can convert it to "". Or e.g. you can use PB's built-in EscapeString()/UnescapeString().
You can e.g. save your data in a JSON file, and then look how PB's SaveJSON() function handles it. Then do the same in your program with your PREFS file.

Re: Space in preference string

Posted: Sun Sep 08, 2024 2:10 am
by BarryG
Little John wrote: Sat Sep 07, 2024 1:48 pmIt's your program and your PREFS file – you can do whatever fits your needs. :-)
So true, and indeed it's what I do. :) I dropped the Preferences() lib years ago and have used my own ever since.

Re: Space in preference string

Posted: Sun Sep 08, 2024 5:23 am
by AZJIO
BarryG wrote: Sun Sep 08, 2024 2:10 am and have used my own ever since
does it work as fast? Are you using regular expressions or character-by-character analysis?