[Implemented] StringField() empty string out-of-range

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
User avatar
Demivec
Addict
Addict
Posts: 4260
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

[Implemented] StringField() empty string out-of-range

Post by Demivec »

I request that StringField() return an empty string for out-of-range indexes.

As of PB 4.41 it does this if the index is too high, but not if it is too low (< 1).
If it is too low it is returning the same result as it would if the index were 1.
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Re: StringField() return empty string for out-of-range index

Post by ts-soft »

I put sometimes the false index (0) in my code (FindString) and it works. Why will you change this?
I would request: only a compilerwarning, but code should work as in all pb-versions!
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Re: StringField() return empty string for out-of-range index

Post by Kaeru Gaman »

@ts:
well, it fact it did not work, it just did not crash.

Code: Select all

a$ = "one,two,three,four"
Repeat
  b$ = StringField(a$,field,",")
  field +1
  Debug b$
Until b$ = ""
as you see, when you start with 0 the result is wrong because the first field is returned twice.

in the discussion thread I voted that this is not to be called a bug,
just take care not to request invalid indices below 1.
oh... and have a nice day.
User avatar
Demivec
Addict
Addict
Posts: 4260
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: StringField() return empty string for out-of-range index

Post by Demivec »

@ts-soft: Code should work as in previous versions of PB, since the behavior wasn't defined for out-of-range indexes
Kaeru Gaman wrote:@ts:
well, it fact it did not work, it just did not crash.

Code: Select all

a$ = "one,two,three,four"
Repeat
  b$ = StringField(a$,field,",")
  field +1
  Debug b$
Until b$ = ""
as you see, when you start with 0 the result is wrong because the first field is returned twice.

in the discussion thread I voted that this is not to be called a bug,
just take care not to request invalid indices below 1.
@Kaeru Gaman: I agree that it is not a bug. It just seems peculiar. Here is a sample work-around that does what I requested:

Code: Select all

a$ = ",one,two,three,four"
c = CountString(a$,",")
Repeat
  b$ = StringField(a$,field + 1,",")
  field +1
  Debug b$
Until  field > c + 1
I believe you spoke against changing it based on speed concerns. If the needed fix is that a check of the indexes be coded prior to calling StringField() then I think this could be done faster natively.

Perhaps a simple help file update could be made instead of a feature request. I'll leave that to the Admins.
Mistrel
Addict
Addict
Posts: 3415
Joined: Sat Jun 30, 2007 8:04 pm

Re: StringField() return empty string for out-of-range index

Post by Mistrel »

An empty string suggests nothing between two fields. By returning the whole string you can actually compare your result to assess a "done" condition without knowing the number of fields ahead of time.
User avatar
Demivec
Addict
Addict
Posts: 4260
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: StringField() return empty string for out-of-range index

Post by Demivec »

Mistrel wrote:An empty string suggests nothing between two fields. By returning the whole string you can actually compare your result to assess a "done" condition without knowing the number of fields ahead of time.
That's a good point.

Code: Select all

a$ = "too_low,one,two,three,four,too_high"
c = CountString(a$,",")
Repeat
  b$ = StringField(a$,field + 1,",")
  field +1
  Debug b$
Until  field > c + 1
Fred
Administrator
Administrator
Posts: 18162
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: StringField() return empty string for out-of-range index

Post by Fred »

While it's not strictly speaking a bug (undocumented behavior), a runtime debugger check has been added and it will return a null string if < 1 is used.
UserOfPure
Enthusiast
Enthusiast
Posts: 469
Joined: Sun Mar 16, 2008 9:18 am

Re: StringField() return empty string for out-of-range index

Post by UserOfPure »

So just to confirm: this runtime debugger check (and the extra code it uses) will NOT be present in final executables, right?
Fred
Administrator
Administrator
Posts: 18162
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: StringField() return empty string for out-of-range index

Post by Fred »

No, debugger runtime check are only added to the executable when the debugger is activated.
User avatar
Kurzer
Enthusiast
Enthusiast
Posts: 670
Joined: Sun Jun 11, 2006 12:07 am
Location: Near Hamburg

Re: StringField() return empty string for out-of-range index

Post by Kurzer »

You could return the seprator string in case of invalid index.
It's variation of Mistrels suggestion.
PB 6.02 x64, OS: Win 7 Pro x64 & Win 11 x64, Desktopscaling: 125%, CPU: I7 6500, RAM: 16 GB, GPU: Intel Graphics HD 520, User age in 2024: 56y
"Happiness is a pet." | "Never run a changing system!"
Mistrel
Addict
Addict
Posts: 3415
Joined: Sat Jun 30, 2007 8:04 pm

Re: StringField() return empty string for out-of-range index

Post by Mistrel »

Considering as how I rely on the current implementation I would rather this not be changed. It's more convenient to have it return the entire string when exceeding the index count than otherwise.
User avatar
blueznl
PureBasic Expert
PureBasic Expert
Posts: 6166
Joined: Sat May 17, 2003 11:31 am
Contact:

Re: StringField() return empty string for out-of-range index

Post by blueznl »

I dunno, Mistrel, but it just might make sense do return an empty string. I mean, there's nothing there, so why should it return anything?

Take the string "a||c|d|", it contains 5 elements:

1 "a"
2 ""
3 "c"
4 "d"
5 ""

Now there's nothing at position 6, 7, 8 and so on, so to me it makes sense to return an empty string :-) And if that applies to ound of bounds on the right side, then I would expect the same behaviour on the left side.

Oh well, it's all a matter of taste, I guess :-)
Last edited by blueznl on Wed Mar 03, 2010 7:54 pm, edited 1 time in total.
( PB6.00 LTS Win11 x64 Asrock AB350 Pro4 Ryzen 5 3600 32GB GTX1060 6GB)
( The path to enlightenment and the PureBasic Survival Guide right here... )
Foz
Addict
Addict
Posts: 1359
Joined: Tue Nov 13, 2007 12:42 pm
Location: Manchester, UK

Re: StringField() return empty string for out-of-range index

Post by Foz »

blueznl wrote:Oh well, it's all a matter of taste, I guess :-)


Somehow it does seem sensible to me if
if... what? :lol:
User avatar
Michael Vogel
Addict
Addict
Posts: 2797
Joined: Thu Feb 09, 2006 11:27 pm
Contact:

Re: StringField() return empty string for out-of-range index

Post by Michael Vogel »

Fred wrote:While it's not strictly speaking a bug (undocumented behavior), a runtime debugger check has been added and it will return a null string if < 1 is used.
I'm not sure if I will like to see differences in program execution between debugger mode and non debugging mode :evil:
This won't make debugging easier, what do you think?
Michael

:?: Or do we have already other functions which are not working identical when debugging is switched on or off -- just not recognized by me :shock:
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Re: StringField() return empty string for out-of-range index

Post by Trond »

Michael Vogel wrote:
Fred wrote:While it's not strictly speaking a bug (undocumented behavior), a runtime debugger check has been added and it will return a null string if < 1 is used.
I'm not sure if I will like to see differences in program execution between debugger mode and non debugging mode :evil:
This won't make debugging easier, what do you think?
Michael

:?: Or do we have already other functions which are not working identical when debugging is switched on or off -- just not recognized by me :shock:
What do you mean? Its legal input remains the same. Only it fails more gracefully.

In my opinion, it should be legal to use out-of-bounds fields, and these should return null strings. This way the command is more forgiving, and in some situations it may be useful.
Post Reply