Dobro_replace() question for Fred in last message

Share your advanced PureBasic knowledge/code with the community.
User avatar
dobro
Enthusiast
Enthusiast
Posts: 766
Joined: Sun Oct 31, 2004 10:54 am
Location: France
Contact:

Post by dobro »

thanks Case ;)

thanks Demivec :)

sorry for my bad english !! :lol:
Image
Windows 98/7/10 - PB 5.42
■ sites : http://michel.dobro.free.fr/
AND51
Addict
Addict
Posts: 1040
Joined: Sun Oct 15, 2006 8:56 pm
Location: Germany
Contact:

Post by AND51 »

Yeah, I recognized this already, when I tested Trond's code. But I was not sure, until you came up with a good explanation. Thanks.
PB 4.30

Code: Select all

onErrorGoto(?Fred)
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post by Trond »

I think there's a bug in dobro's code:

Code: Select all

Text$= "add() muladd()"
text2$= dobro_replace (Text$, "add" , "Leon" )
The result: "Leon() mulLeon()"
Correct result: "Leon() muladd()", right?
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post by Trond »

I did one with more word delimeters, it also produces the correct result (see above).

Code: Select all

Declare.s dobro_replace(chaine$,chaine_cherche$,chaine_remplace$)

Procedure.s dobro_replace(chaine$,chaine_cherche$,chaine_remplace$)
      ; by Dobro
    Static extrait1$,pos
     If pos= Len (chaine$)
        text2$=extrait1$
        pos=0 :extrait1$= ""
         ProcedureReturn text2$
     EndIf
    pos=pos+1
    extrait1$=extrait1$+ Mid (chaine$,pos,1)
    
    extrait2$= Mid (chaine$,pos, Len (chaine_cherche$))
    extrait3$= Mid (chaine$,pos, Len (chaine_cherche$)+1)
    
     If extrait3$=chaine_cherche$+ "(" Or extrait3$=chaine_cherche$+ " " Or extrait3$=chaine_cherche$+ "," Or extrait3$=chaine_cherche$+ "." Or extrait3$=chaine_cherche$+ Chr (13) Or extrait3$=chaine_cherche$+ ";"
        extrait1$= Left (extrait1$, Len (extrait1$)-1)
        extrait1$=extrait1$+chaine_remplace$
        pos=pos+ Len (chaine_cherche$)-1
 
     EndIf
    text2$=dobro_replace(chaine$,chaine_cherche$,chaine_remplace$)
     ProcedureReturn text2$
EndProcedure


Declare.s replace_whole_word(Haystack.s, Needle.s, Seam.s)
Procedure.s replace_whole_word_helper(Haystack.s, Needle.s, Seam.s)
  L.s = Left(Haystack, 1)
  If L
    Select L
      Case " ", ".", ",", ";", "(", #CRLF$, ""
        ProcedureReturn L + replace_whole_word(Mid(Haystack, 2), Needle, Seam)
      Default
        ProcedureReturn L + replace_whole_word_helper(Mid(Haystack, 2), Needle, Seam)
    EndSelect
  EndIf
EndProcedure
Procedure.s replace_whole_word(Haystack.s, Needle.s, Seam.s)
  If Haystack And Left(Haystack, Len(Needle)) = Needle
    A.s = Mid(Haystack, Len(Needle)+1, 1)
    Select A
      Case " ", ".", ",", ";", "(", #CRLF$, ""
        ProcedureReturn Seam + A + replace_whole_word(Right(Haystack, Len(Haystack)-Len(Needle)-1), Needle, Seam)
    EndSelect  
  EndIf
  ProcedureReturn replace_whole_word_helper(Haystack, Needle, Seam)
EndProcedure


Text$= "replace, don'treplace don'treplace() don'treplace ...replace..."


text2$= dobro_replace(Text$, "replace" , "______" )
text3$= replace_whole_word(Text$, "replace" , "______" )
text4$= ReplaceString (Text$, "replace" , "______" )


Text$ + "|" + #CRLF$
Text$ + #CRLF$
text$ + "dobro" + #TAB$ + text2$ + "|" + #CRLF$
text$ + "trond" + #TAB$ + text3$ + "|" + #CRLF$
text$ + "pb" + #TAB$ + text4$ + "|" + #CRLF$

MessageRequester("", text$)

#Tries = 5000

time = ElapsedMilliseconds()
For U = 0 To #Tries
text2$=dobro_replace(Text$, "replace" , "Leon" )
Next
MessageRequester("", Str(ElapsedMilliseconds()-time))

time = ElapsedMilliseconds()
For U = 0 To #Tries
text4$= replace_whole_word(Text$, "replace" , "Leon" )
Next
MessageRequester("", Str(ElapsedMilliseconds()-time))

User avatar
dobro
Enthusiast
Enthusiast
Posts: 766
Joined: Sun Oct 31, 2004 10:54 am
Location: France
Contact:

Post by dobro »

Trond wrote:I think there's a bug in dobro's code:

Code: Select all

Text$= "add() muladd()"
text2$= dobro_replace (Text$, "add" , "Leon" )
The result: "Leon() mulLeon()"
Correct result: "Leon() muladd()", right?
my code page 1 is fixed !

this sample work fine :)

thanks
Image
Windows 98/7/10 - PB 5.42
■ sites : http://michel.dobro.free.fr/
User avatar
dobro
Enthusiast
Enthusiast
Posts: 766
Joined: Sun Oct 31, 2004 10:54 am
Location: France
Contact:

Post by dobro »

my code page 1 with add a Iteratif mode :)
more fast code and thread safe (because no static variable) ;)

thanks Lionel_om
Image
Windows 98/7/10 - PB 5.42
■ sites : http://michel.dobro.free.fr/
User avatar
dobro
Enthusiast
Enthusiast
Posts: 766
Joined: Sun Oct 31, 2004 10:54 am
Location: France
Contact:

Post by dobro »

Fred , si tu voulais bien lire ce topic attentivement

et nous dire ce que tu en pense ??

http://www.purebasic.fr/french/viewtopi ... 7858#97858

Merci :)
Image
Windows 98/7/10 - PB 5.42
■ sites : http://michel.dobro.free.fr/
nicolaus
Enthusiast
Enthusiast
Posts: 456
Joined: Tue Aug 05, 2003 11:30 pm
Contact:

Post by nicolaus »

dobro wrote:Fred , si tu voulais bien lire ce topic attentivement

et nous dire ce que tu en pense ??

http://www.purebasic.fr/french/viewtopi ... 7858#97858

Merci :)
Sorry but french is not my base language :D , can you write engliish please?
rsts
Addict
Addict
Posts: 2736
Joined: Wed Aug 24, 2005 8:39 am
Location: Southwest OH - USA

Post by rsts »

I believe he addressed that comment to Fred, who does understand French.
:)

cheers
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Post by netmaestro »

He'd just like Fred to read the topic and give an opinion.
BERESHEIT
nicolaus
Enthusiast
Enthusiast
Posts: 456
Joined: Tue Aug 05, 2003 11:30 pm
Contact:

Post by nicolaus »

Thanks guys :wink:
User avatar
dobro
Enthusiast
Enthusiast
Posts: 766
Joined: Sun Oct 31, 2004 10:54 am
Location: France
Contact:

Post by dobro »

yes, excuse me for using the French

I asked Fred if the integration of a function
like mine, in Pb to be a good idea
on the french forum, some have optimized this procedure

secondly, a member of the french forum has created a
message request, as gfabasic with multiple choice answers, customizable

it will be good, fred that includes this kind of work in the PureBasic that motivate members to change the language!
by offering their ideas
may be the creation of a forum "proposal"
********************************************************
in french :

oui, excusez moi , pour l'utilisation du Français

je demande a fred, si l'integration d'une fonction
comme le mienne,dans Pb serai une bonne idée,
sur le forum français, certains ont optimisé cette procedure

d'autre part, un membre du forum français a crée une fonction
message requester , comme en gfabasic, avec plusieurs choix de réponses, personalisable

il serai bien, que fred inclue ce genre de travail dans le purebasic, cela motiverai les membres a faire évoluer le langage !!
en proposant leurs idées
peut etre par la création d'un forum "proposition"
Image
Windows 98/7/10 - PB 5.42
■ sites : http://michel.dobro.free.fr/
User avatar
djes
Addict
Addict
Posts: 1806
Joined: Sat Feb 19, 2005 2:46 pm
Location: Pas-de-Calais, France

Post by djes »

Deeem2031 wrote:Imho this should work too, but it doesn't :?

Code: Select all

Haystack.s = "aa Test_Procedure20(coucou) bb Test_Procedure205 (coucou) cc Test_Procedure20 (coucou) cc Test_Procedure20" + Chr (13)+ Chr (10)+ "(coucou)"  
Needle.s="Procedure20" 
Seam.s="toto"

Procedure.s Deeem2031_Replace(String.s, Find.s, Replace.s) 
  Protected regex, result.s
  regex = CreateRegularExpression(#PB_Any, "(?<![a-zA-Z0-9])"+Find+"(?![a-zA-Z0-9])")
  If regex
    result = ReplaceRegularExpression(regex, String, Replace)
    FreeRegularExpression(regex)
  EndIf
  ProcedureReturn result
EndProcedure

Debug Deeem2031_Replace(Haystack.s, Needle.s, Seam.s)
I've tried the same

Code: Select all

dobro_test.s="aa Test_Procedure20(coucou) bb Test_Procedure205 (coucou) cc Test_Procedure20 (coucou) cc Test_Procedure20 totoProcedure20" + Chr (13)+ Chr (10)+ "(coucou)"

If CreateRegularExpression(0, "(?<![[:alnum:]])Procedure20(?![[:alnum:]])")
  Result$ = ReplaceRegularExpression(0, dobro_test, "xxx")
  MessageRequester("Résultat", Result$, #PB_MessageRequester_Ok)
Else
  Debug RegularExpressionError()
EndIf
And it doesn't work. Seems a regularexpressionlib bug. You found it, you signal it ;)
Could you ask him to add the \1 and so on expression in the replacestring? It would be nice. Don't know if it has already been asked.
Post Reply