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