This codes here are only feasibility demo
A little other :
From a purely intuitive point of view, I consider my variant to be more understandable.
To make it clear to the interested reader what this is about.
The PB version of mk_soft recognises this with an index 2, as two stars occur.
My version recognises this with an index 1, because there is the first field that is enclosed with stars.
Index 2 mk_soft
"*Hello*World"
Index 1 my version
"*Hello*World"
That is all.
This code is free.
who can still improve and optimize it is welcome.
Hint :
The result of the analysis of these test codes is the
extreme fast and comprehensive StringField_Tool_BF, you can find it here :
https://www.purebasic.fr/english/viewto ... 12&t=77219
Have fun with it.
Variant 1 - Usual procedure
Code: Select all
DeclareModule StringField_BF
EnableExplicit
Declare.s StringField_BF(string$, index, separator$)
EndDeclareModule
Module StringField_BF
Global NewList index() : AddElement(index())
Procedure.s StringField_BF(string$, index, separator$)
; StringField - By Saki - Unicode - This code is free for using and enhancing
Protected i, ii, pos_1, pos_2, length_result, comp, amount_indexes, count_index
Protected len_separator=StringByteLength(separator$), *pointer.word, jump_in, result$
Protected *string=@string$, *separator=@separator$
If Not PeekW(*string) : ProcedureReturn "" : EndIf
If index<1 : ProcedureReturn "" : EndIf
ClearList(index()) : AddElement(index()); #########################################
If comp=CompareMemory(*string, *separator, len_separator)
jump_in=1 : index+1
EndIf
i=-2
Repeat
i+2
comp=CompareMemory(*string+i, *separator, len_separator)
If comp+jump_in
count_index+1
jump_in=0 : ii+1 : i+len_separator-2 : amount_indexes+1
AddElement(index()) : index()=i+2
EndIf
*pointer=*string+i+len_separator
Until count_index>index Or Not *pointer\w
If index>count_index : index=count_index : EndIf
amount_indexes=ii : i=0
If amount_indexes
Repeat
SelectElement(index(), i) : pos_1=index()
SelectElement(index(), i+1) : pos_2=index()
length_result=pos_2-pos_1-len_separator
If pos_2-pos_1>0
If length_result>0
result$=Space(length_result>>1)
CopyMemory(*string+pos_1, @result$, length_result)
Else
result$=#Null$
EndIf
If i=index : ProcedureReturn result$ : EndIf
EndIf
i+1
Until i>index Or i>amount_indexes-1
EndIf
EndProcedure
EndModule
UseModule StringField_BF
Define separator$=" "
Define index, result$, string$
string$=" Hello I am a splitted String "
Define multiplier=8
Define i
For i=1 To multiplier
string$+string$
Next i
Debug Len(string$)
Debug "================"
Define len_string=Len(string$)
Define time=ElapsedMilliseconds()
For index=1 To len_string/6+1
result$=StringField_BF(string$, index, separator$)
If result$<>"" : Debug result$ : EndIf
Next
MessageRequester("", Str(ElapsedMilliseconds()-time))