Stringfield modification

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
Ludo
User
User
Posts: 40
Joined: Thu Oct 16, 2008 12:55 pm
Location: Belgium-Tiegem

Stringfield modification

Post by Ludo »

:D

Hey, that's my first post since registering PureBasic a few weeks ago. All to plan till now.

In the StringField function, I would allow for negative indexnumbers too. 1,2,3 ... would then start counting from the beginning of the string and -1, -2, -3 would start from the end of the string.

It is an easy thing to code myself, but it could be a usefull change after all.

Cheers,
Ludo
Fred
Administrator
Administrator
Posts: 18162
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Post by Fred »

Yes, you can achieve it easily with StringField(a$, ",", CountString(",")-x)
klaver
Enthusiast
Enthusiast
Posts: 147
Joined: Wed Jun 28, 2006 6:55 pm
Location: Schröttersburg

Post by klaver »

Fred wrote:Yes, you can achieve it easily with StringField(a$, ",", CountString(",")-x)
But it's much slower that way...
Fred
Administrator
Administrator
Posts: 18162
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Post by Fred »

Why that ? It's exactly what the function would do anyway.
klaver
Enthusiast
Enthusiast
Posts: 147
Joined: Wed Jun 28, 2006 6:55 pm
Location: Schröttersburg

Post by klaver »

a$ = "a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z"

CountString() will first walk through the whole string from beginning counting all "," characters, and then StringField() will again walk from the beginning to find the specified occurrence of ",".

So if I'd like to get the "y" field of the string, I'd rather to use StringField(a$, ",", -2). The function should then walk from the END of the string and return the string between frist and second occurrence of the delimiter. Without wasting time on CountString() and walking form the beginning, as it's much closer from the end, isn't it?

Wouldn't it be faster and much more optimal?
Fred
Administrator
Administrator
Posts: 18162
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Post by Fred »

Right, i overlooked that ;). Anyway, the difference won't be huge here, as walking backward isn't a good idea on nowaday processor. Here, in most of case, the whole string will be in the processor cache so the second pass will be almost free.
Post Reply