Page 2 of 2

Posted: Sat May 16, 2009 12:52 am
by dobro
thanks Case ;)

thanks Demivec :)

sorry for my bad english !! :lol:

Posted: Sat May 16, 2009 2:05 am
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.

Posted: Sat May 16, 2009 11:15 am
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?

Posted: Sat May 16, 2009 11:37 am
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))


Posted: Sun May 17, 2009 10:39 am
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

Posted: Tue May 19, 2009 12:27 pm
by dobro
my code page 1 with add a Iteratif mode :)
more fast code and thread safe (because no static variable) ;)

thanks Lionel_om

Posted: Tue May 19, 2009 6:23 pm
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 :)

Posted: Tue May 19, 2009 11:08 pm
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?

Posted: Tue May 19, 2009 11:22 pm
by rsts
I believe he addressed that comment to Fred, who does understand French.
:)

cheers

Posted: Wed May 20, 2009 12:45 am
by netmaestro
He'd just like Fred to read the topic and give an opinion.

Posted: Wed May 20, 2009 10:25 am
by nicolaus
Thanks guys :wink:

Posted: Wed May 20, 2009 10:27 am
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"

Posted: Wed May 20, 2009 3:34 pm
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.