Fast Split() function

Share your advanced PureBasic knowledge/code with the community.
User avatar
pdwyer
Addict
Addict
Posts: 2813
Joined: Tue May 08, 2007 1:27 pm
Location: Chiba, Japan

Fast Split() function

Post 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

Last edited by pdwyer on Sat Feb 09, 2008 10:42 am, edited 1 time in total.
Paul Dwyer

“In nature, it’s not the strongest nor the most intelligent who survives. It’s the most adaptable to change” - Charles Darwin
“If you can't explain it to a six-year old you really don't understand it yourself.” - Albert Einstein
User avatar
Kiffi
Addict
Addict
Posts: 1504
Joined: Tue Mar 02, 2004 1:20 pm
Location: Amphibios 9

Re: Fast Split() function

Post 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
Hygge
User avatar
pdwyer
Addict
Addict
Posts: 2813
Joined: Tue May 08, 2007 1:27 pm
Location: Chiba, Japan

Post 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!
Paul Dwyer

“In nature, it’s not the strongest nor the most intelligent who survives. It’s the most adaptable to change” - Charles Darwin
“If you can't explain it to a six-year old you really don't understand it yourself.” - Albert Einstein
User avatar
Kiffi
Addict
Addict
Posts: 1504
Joined: Tue Mar 02, 2004 1:20 pm
Location: Amphibios 9

Post by Kiffi »

pdwyer wrote:Code above is fixed now
Thank you :-)

(image removed, sorry for the lapse!)

Greetings ... Kiffi
Last edited by Kiffi on Mon Feb 11, 2008 8:43 am, edited 1 time in total.
Hygge
User avatar
pdwyer
Addict
Addict
Posts: 2813
Joined: Tue May 08, 2007 1:27 pm
Location: Chiba, Japan

Post by pdwyer »

I'm bilingual too, so what? :roll:
Paul Dwyer

“In nature, it’s not the strongest nor the most intelligent who survives. It’s the most adaptable to change” - Charles Darwin
“If you can't explain it to a six-year old you really don't understand it yourself.” - Albert Einstein
Dare
Addict
Addict
Posts: 1965
Joined: Mon May 29, 2006 1:01 am
Location: Outback

Post 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. :)
Dare2 cut down to size
User avatar
Kiffi
Addict
Addict
Posts: 1504
Joined: Tue Mar 02, 2004 1:20 pm
Location: Amphibios 9

Post 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
Hygge
User avatar
pdwyer
Addict
Addict
Posts: 2813
Joined: Tue May 08, 2007 1:27 pm
Location: Chiba, Japan

Post 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
Paul Dwyer

“In nature, it’s not the strongest nor the most intelligent who survives. It’s the most adaptable to change” - Charles Darwin
“If you can't explain it to a six-year old you really don't understand it yourself.” - Albert Einstein
Dare
Addict
Addict
Posts: 1965
Joined: Mon May 29, 2006 1:01 am
Location: Outback

Post 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. :)
Dare2 cut down to size
Pupil
Enthusiast
Enthusiast
Posts: 715
Joined: Fri Apr 25, 2003 3:56 pm

Post 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
User avatar
pdwyer
Addict
Addict
Posts: 2813
Joined: Tue May 08, 2007 1:27 pm
Location: Chiba, Japan

Post 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>
Last edited by pdwyer on Sat Feb 09, 2008 1:24 pm, edited 2 times in total.
Paul Dwyer

“In nature, it’s not the strongest nor the most intelligent who survives. It’s the most adaptable to change” - Charles Darwin
“If you can't explain it to a six-year old you really don't understand it yourself.” - Albert Einstein
DarkDragon
Addict
Addict
Posts: 2345
Joined: Mon Jun 02, 2003 9:16 am
Location: Germany
Contact:

Post 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.
bye,
Daniel
Dare
Addict
Addict
Posts: 1965
Joined: Mon May 29, 2006 1:01 am
Location: Outback

Post by Dare »

lol
Dare2 cut down to size
User avatar
pdwyer
Addict
Addict
Posts: 2813
Joined: Tue May 08, 2007 1:27 pm
Location: Chiba, Japan

Post by pdwyer »

Dare wrote:lol
:lol:

you've noticed it too then? :wink:
Paul Dwyer

“In nature, it’s not the strongest nor the most intelligent who survives. It’s the most adaptable to change” - Charles Darwin
“If you can't explain it to a six-year old you really don't understand it yourself.” - Albert Einstein
LuCiFeR[SD]
666
666
Posts: 1033
Joined: Mon Sep 01, 2003 2:33 pm

Post 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
Post Reply