RFindString()

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
User avatar
blueznl
PureBasic Expert
PureBasic Expert
Posts: 6166
Joined: Sat May 17, 2003 11:31 am
Contact:

RFindString()

Post by blueznl »

search from the end of the string backwards...

equivalents in gfa: instr() and rinstr()
( 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... )
User avatar
blueznl
PureBasic Expert
PureBasic Expert
Posts: 6166
Joined: Sat May 17, 2003 11:31 am
Contact:

Post by blueznl »

oh well, let's simply code it for now... :-)

Code: Select all

Procedure.l x_countchar(string.s,char.s)
  Protected n.l , p.l , l.l
  ;
  ; *** counts occurances of character in string
  ;
  n.l = 0
  p.l = 0
  l.l = Len(string)
  While p < l
    INC p
    If Mid(string,p,1) = char
      INC n
    EndIf
  Wend
  ProcedureReturn n
EndProcedure
  
Procedure.l x_rfindstring(string.s,find.s)
  Protected l.l , p.l
  ;
  ; *** find a string in another string, starting from the right
  ;
  l.l = Len(find.s)
  p.l = Len(string.s)
  If l > 0 And p > 0
    p = p+l
    Repeat
      DEC p
    Until p.l = 0 Or Mid(string,p,l) = find
  EndIf
  ProcedureReturn p
EndProcedure

( 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... )
User avatar
Psychophanta
Always Here
Always Here
Posts: 5153
Joined: Wed Jun 11, 2003 9:33 pm
Location: Anare
Contact:

Post by Psychophanta »

:idea: Thanks
Just you have bring to my mind a new idea.

I'll make a function in assembler for to perform equivalents in gfa: rinstr().

I will name it: RFindString(). LFindString() already exists, its name is FindString()

And by the way you can find CountChars() and CountStrings() functions made by me in assembler in www.purearea.net.

You know, you could take it and make a PB userlibrary with all those 3 functions, will you be able to do? :)
Fred
Administrator
Administrator
Posts: 18162
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Post by Fred »

Psychophanta: may be you could use name less 'common' for your addon functions because some of them (especially CountString()) will be in a next version and it will invalidate your lib.. Just a tough.

bluenz: 'INC p' is assembly, right ? Why not: 'p+1' ? :)
User avatar
Psychophanta
Always Here
Always Here
Posts: 5153
Joined: Wed Jun 11, 2003 9:33 pm
Location: Anare
Contact:

Post by Psychophanta »

Ooookay :wink:
Note that i use CountStrings() not CountString() :wink:
User avatar
blueznl
PureBasic Expert
PureBasic Expert
Posts: 6166
Joined: Sat May 17, 2003 11:31 am
Contact:

Post by blueznl »

fred, that's an old habit of using gfabasic, it had the following basic commands:

inc a
a += 1
add a , 1

old habits are difficult to kill... will it affect code, as compared to a = a + 1?

psycho, are you sure your countstrings asm function works correctly? i tried it here and got the wrong answer...

currently i'm just dumping my functions in a file, and add x_ to every function name, so fred can update what he wants, and it won't cause me troubles :-)

i always peek into the libraries to see what i can use, but often end up coding something myself... good excercise :-) but ehm... fred, will rfindstring also show up? or?
( 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... )
Fred
Administrator
Administrator
Posts: 18162
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Post by Fred »

bluenz: I was just pointing out than 'p+1' is like 'p += 1' in C, so it seems shorter than INC p, why not use it. But it's up to you of course :)
User avatar
blueznl
PureBasic Expert
PureBasic Expert
Posts: 6166
Joined: Sat May 17, 2003 11:31 am
Contact:

Post by blueznl »

readability, my dear man, readability... for some strange reason my mind can *see* basic code, yet perceives c(++) as something foggy, in other words: i have to look at basic to understand it, i have to puzzle to understand c... i have a similar thing with object oriented programming...

and inc c just *looks* better to me than c+=1 or c = c + 1

ah, forgot a variation, gfa also allowed c++

:-)
Last edited by blueznl on Mon Jan 16, 2006 8:39 am, 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... )
Saboteur
Enthusiast
Enthusiast
Posts: 272
Joined: Fri Apr 25, 2003 7:09 pm
Location: (Madrid) Spain
Contact:

Post by Saboteur »

Fred wanted to say that it is not necesary write

Code: Select all

p = p+1
you can write simply

Code: Select all

p+1
p+5
p+100
[:: PB Registered ::]

Win10 Intel core i5-3330 8GB RAM Nvidia GTX 1050Ti
User avatar
blueznl
PureBasic Expert
PureBasic Expert
Posts: 6166
Joined: Sat May 17, 2003 11:31 am
Contact:

Post by blueznl »

ooops :oops:
( 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... )
Fred
Administrator
Administrator
Posts: 18162
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Post by Fred »

Do I speak english so badly ? :twisted:
User avatar
Psychophanta
Always Here
Always Here
Posts: 5153
Joined: Wed Jun 11, 2003 9:33 pm
Location: Anare
Contact:

Post by Psychophanta »

Fred wrote:
Psychophanta: may be you could use name less 'common' for your addon functions because some of them (especially CountString()) will be in a next version and it will invalidate your lib.. Just a tough.
Don't worry, if i make a PB userlib, then i'll rename functions with a prefix, for example "al", but for normal Procedure names, user can name it as is wanted. :oops: :P
freedimension
Enthusiast
Enthusiast
Posts: 613
Joined: Tue May 06, 2003 2:50 pm
Location: Germany
Contact:

Post by freedimension »

Fred wrote:Do I speak english so badly ? :twisted:
Yes, but you havn't heard me speaking french yet :twisted:
User avatar
blueznl
PureBasic Expert
PureBasic Expert
Posts: 6166
Joined: Sat May 17, 2003 11:31 am
Contact:

Post by blueznl »

try dutch for a change

si tu veux, mon ami fred, c'est possible de continuer en francais, mais je te dit, ma connaisance de la langue francais... c'est... le mot propre c'est 'corrode' je pense?

damn, forgot most of my french, i've been working for one and a half year in paris, almost a decade back, and at that time it wasn't a problem at all... hell, i even started swearing at all those bloody foreigners behaving like tourists when using the peripherique :-)
( 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... )
User avatar
GeoTrail
Addict
Addict
Posts: 2794
Joined: Fri Feb 13, 2004 12:45 am
Location: Bergen, Norway
Contact:

Post by GeoTrail »

I get an error saying p is not a valid operator.
I need to search for a specific character in a word and then search backwards to find the beginning of that word by finding the space infront of it.

Hmm did that make any sence? hehehe
I Stepped On A Cornflake!!! Now I'm A Cereal Killer!
Post Reply