Page 1 of 2

Fast Split() function

Posted: Sat Feb 09, 2008 10:07 am
by pdwyer
As a follow on to: http://www.purebasic.fr/english/viewtopic.php?t=30630 I finally got around to putting the bits together to clean up the function call and memory leak. Below is the function and sample code. It accepts multi-char separators so can be used with #crlf$.

Code: Select all

Structure MemoryArray 
    Byte.c[0] 
    word.w[0]
EndStructure 

Procedure.l Split(StringArray.s(1), Text2Split.s, Delim.s) ;return count

    FindLen.l = Len(Delim)
    MainLen.l = Len(Text2Split)
    StringArray.s(0)
    StringCount.l = 0
    
    *MainByteArray.MemoryArray = @Text2Split  ;*MainMem
    *FindByteArray.MemoryArray = @Delim       ;*FindMem 

    PrevPos.l = 1

    ; Build BadChr Array
    Dim BadChar.l(255)
    
    ; set all alphabet to max shift pos (length of find string plus 1)
    For i = 0 To 255
        BadChar(i)  =  FindLen + 1
    Next
    
    ;Update chars that are in the find string to their position from the end.
    For i = 0 To findlen -1
        BadChar(*FindByteArray\byte[i]) = findlen - i    
    Next     

    MainArrayLoop.l = 1 
    EndSearchPos.l = MainLen - (FindLen -1)
    
    While MainArrayLoop <= EndSearchPos
    
        If CompareMemory(@Text2Split + MainArrayLoop, @Delim, FindLen) = 1
            FoundPos = MainArrayLoop + 1
            ReDim StringArray.s(StringCount )

            ;emulate fast mid() function inline,
            If Foundpos - PrevPos > 0 
                *RetMem = AllocateMemory(Foundpos - PrevPos)
                CopyMemory(@Text2Split + Prevpos -1, *RetMem, Foundpos - PrevPos)
                MidStr.s = PeekS(*RetMem,Foundpos - PrevPos)
                FreeMemory(*RetMem)
            Else
                MidStr.s = ""
            EndIf
            
            StringArray(StringCount) = MidStr
            StringCount = StringCount + 1
            PrevPos = foundpos + Findlen

        EndIf 
        ;Didn't find the string so shift as per the table.
        MainArrayLoop + BadChar(*MainByteArray.MemoryArray\byte[MainArrayLoop + FindLen])
    Wend
 
    ;catch end
    ReDim StringArray.s(StringCount)
    StringArray(StringCount) = Mid(Text2Split, Prevpos, MainLen - PrevPos +1)
    StringCount = StringCount + 1
    ReDim StringArray.s(StringCount)

    ProcedureReturn StringCount

EndProcedure
Simple example code, let me know if there are any bugs

Code: Select all

str.s = "1,,2,,3,,44,555,,66,7777,,8,,9"
Dim Splitstrs.s(0)

StrCount = Split(Splitstrs(),str,",,")
Debug strcount
For i = 0 To strcount -1
    Debug Splitstrs(i)
Next


Re: Fast Split() function

Posted: Sat Feb 09, 2008 10:35 am
by Kiffi
pdwyer wrote:let me know if there are any bugs
if i use a single char as Delimiter in your example,

Code: Select all

StrCount = Split(Splitstrs(),str,",")
i get an error (can't allocate a memory block size of 0) on this line:

Code: Select all

*RetMem = AllocateMemory(FoundPos - PrevPos)
Greetings ... Kiffi

Posted: Sat Feb 09, 2008 10:43 am
by pdwyer
:lol: oops!

Code above is fixed now, I made a change to my test method after merging the mid function then didn't test the orignial way :oops:

Thanks!

Posted: Sat Feb 09, 2008 11:04 am
by Kiffi
pdwyer wrote:Code above is fixed now
Thank you :-)

(image removed, sorry for the lapse!)

Greetings ... Kiffi

Posted: Sat Feb 09, 2008 11:40 am
by pdwyer
I'm bilingual too, so what? :roll:

Posted: Sat Feb 09, 2008 12:43 pm
by Dare
lol

I think the message is saying that kiffi is a "garlic sausage" and bandwidth robber".

I guess the site holding the smilie has anti-hot-linking in place. :)

Posted: Sat Feb 09, 2008 12:58 pm
by Kiffi
Dare wrote:I think the message is saying that kiffi is a "garlic sausage" and bandwidth robber".
pardon? i use the smileys from "The Smile Project" (http://www.smileyxtra.co.uk/)

is this forbidden?

all i want to do is to thank pdwyer for his fast reply :roll:

Astonished ... Kiffi

Posted: Sat Feb 09, 2008 1:04 pm
by pdwyer
:?

Aho ja ne kayo? ii koto iitai nara wakaru you ni suru no ha futsu ja nai no? ja nai to, iuu imi ha nani?

It must be past my bed time, I'm losing track of the world today

Posted: Sat Feb 09, 2008 1:06 pm
by Dare
Hi Kiffi,

Laughing with you, not at you!

I am not sure what you see in your post (image url is http://avalonsoftware.org/foro/images/smiles/mano2.gif) but what I see is a message that appears to be rude.

Sorry if you thought my post was aimed at you. It is not. :)

Posted: Sat Feb 09, 2008 1:13 pm
by Pupil
Kiffi the image link is being directed to this URL instead, so this is what all of us see :)
http://img146.imageshack.us/img146/8189/chorizogb6.gif

Posted: Sat Feb 09, 2008 1:14 pm
by pdwyer
I assumed he was making some crack at the fact that I fixed the code rather than left the bug there for people to see and post again.

...and that he usage of spanish was like that new trend we've been seeing on the forums lately that goes somethinge like:

This is a passive agressive statememt that I want to say but don't have the balls to put in a way that's easy for all to read! Aren't I a dick for using this font for my complaint!

Seems that it was just a misunderstanding though <sigh>

Posted: Sat Feb 09, 2008 1:17 pm
by DarkDragon
Dare wrote:Hi Kiffi,

Laughing with you, not at you!

I am not sure what you see in your post (image url is http://avalonsoftware.org/foro/images/smiles/mano2.gif) but what I see is a message that appears to be rude.

Sorry if you thought my post was aimed at you. It is not. :)
Direct linking is forbidden. It checks the referer. To see this image, copy the link in your browser-address line and then refresh the same page. Then you'll see a hand with the thumb up.

@Kiffi: You shouldn't use those smileys in your posts as there will always appear such a message to all other forum users if you do.

Posted: Sat Feb 09, 2008 1:19 pm
by Dare
lol

Posted: Sat Feb 09, 2008 1:21 pm
by pdwyer
Dare wrote:lol
:lol:

you've noticed it too then? :wink:

Posted: Sat Feb 09, 2008 4:02 pm
by LuCiFeR[SD]
pdwyer wrote:and that he usage of spanish was like that new trend we've been seeing on the forums lately
Trend? hehehe, I've seen no such trend of people using Spanish. I've seen my Sig says "Never trust a man with one eyebrow" :) and you'd have to be a Formula 1 fan to get that lol