Space in preference string

Just starting out? Need help? Post your questions and find answers here.
User avatar
le_magn
Enthusiast
Enthusiast
Posts: 289
Joined: Wed Aug 24, 2005 12:11 pm
Location: Italia

Space in preference string

Post 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?
Image
AZJIO
Addict
Addict
Posts: 2225
Joined: Sun May 14, 2017 1:48 am

Re: Space in preference string

Post by AZJIO »

Code: Select all

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

Code: Select all

[MAIN]
String = " / "
User avatar
le_magn
Enthusiast
Enthusiast
Posts: 289
Joined: Wed Aug 24, 2005 12:11 pm
Location: Italia

Re: Space in preference string

Post 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
Image
BarryG
Addict
Addict
Posts: 4219
Joined: Thu Apr 18, 2019 8:17 am

Re: Space in preference string

Post 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.
Fred
Administrator
Administrator
Posts: 18350
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: Space in preference string

Post by Fred »

Moved for investigation
AZJIO
Addict
Addict
Posts: 2225
Joined: Sun May 14, 2017 1:48 am

Re: Space in preference string

Post by AZJIO »

You can make this rule with #PB_Preference_NoSpace
User_Russian
Addict
Addict
Posts: 1588
Joined: Wed Nov 12, 2008 5:01 pm
Location: Russia

Re: Space in preference string

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

Re: Space in preference string

Post 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.
Last edited by infratec on Sat Sep 07, 2024 10:32 am, edited 1 time in total.
Little John
Addict
Addict
Posts: 4803
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: Space in preference string

Post by Little John »

I agree with you, infratec. And the most important point in this context is, that PB's behaviour is precisely documented.
BarryG
Addict
Addict
Posts: 4219
Joined: Thu Apr 18, 2019 8:17 am

Re: Space in preference string

Post by BarryG »

@infratec: It depends if your value contains #DQUOTE$ as well. Best to have a set rule and stick to it.
Little John
Addict
Addict
Posts: 4803
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: Space in preference string

Post 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.
BarryG
Addict
Addict
Posts: 4219
Joined: Thu Apr 18, 2019 8:17 am

Re: Space in preference string

Post by BarryG »

What if the value looks like this? Don't laugh; it's a possibility.

Code: Select all

Keyname =     " " """ "   "
Little John
Addict
Addict
Posts: 4803
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: Space in preference string

Post 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.
BarryG
Addict
Addict
Posts: 4219
Joined: Thu Apr 18, 2019 8:17 am

Re: Space in preference string

Post 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.
AZJIO
Addict
Addict
Posts: 2225
Joined: Sun May 14, 2017 1:48 am

Re: Space in preference string

Post 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?
Post Reply