maybe someone can use it...
i deliberately decided to use the string-pointer, the resulting limitations or weaknesses can be seen in the example calls.
Code: Select all
EnableExplicit
Procedure.s StringToken(*String.STRING, Separator$) ; STRING --
Static *s_string.CHARACTER, *s_currCh.CHARACTER
Protected result$, *sepCh.CHARACTER, *start.CHARACTER
If *String And *String <> *s_string ; new string to tokenize ..
*s_string = *String ; .. memorize the string pointer
*s_currCh = *s_string ; .. point to current character
EndIf
If *s_currCh = 0 ; end of string reached by last call or not valid
ProcedureReturn ""
EndIf
*start = *s_currCh ; memorize the pointer to the current character
While *s_currCh\c
*sepCh = @Separator$ ; point to separator string (start new)
While *sepCh\c
If *s_currCh\c = *sepCh\c ; found a separator character
If *start <> *s_currCh ; not at the start position
result$ = PeekS(*start, (*s_currCh - *start) >> 1)
*s_currCh + SizeOf(CHARACTER)
ProcedureReturn result$
EndIf
*start = *s_currCh + SizeOf(CHARACTER) ; new string start
Break
EndIf
*sepCh + SizeOf(CHARACTER)
Wend
*s_currCh + SizeOf(CHARACTER)
Wend
result$ = PeekS(*start, (*s_currCh - *start) >> 1)
*s_currCh = 0 ; reset current character
ProcedureReturn result$
EndProcedure ;()
CompilerIf #PB_Compiler_IsMainFile
Global DemoText$, nn, ret, ret$
DemoText$ = "Command = DIR " + #DOUBLEQUOTE$ + "*.*" + #DOUBLEQUOTE$ + ", /s" + " "
ret$ = StringToken(@DemoText$, "=, ") :Debug " => '" + ret$ + "'"
ret$ = StringToken(@DemoText$, "=, ") :Debug " => '" + ret$ + "'"
ret$ = StringToken(@DemoText$, "=, ") :Debug " => '" + ret$ + "'"
ret$ = StringToken(@DemoText$, "=, ") :Debug " => '" + ret$ + "'"
ret$ = StringToken(@DemoText$, "=, ") :Debug " => '" + ret$ + "'"
; New String
Debug #LF$ + "===================="
ret$ = StringToken(@"TestA TestB", " ") :Debug " => '" + ret$ + "'"
ret$ = StringToken(@"TestA TestB", " ") :Debug " => '" + ret$ + "'"
ret$ = StringToken(@"TestA TestB", " ") :Debug " => '" + ret$ + "'"
; empty string works
Debug #LF$ + "===================="
ret$ = StringToken(@"", "") :Debug " => '" + ret$ + "' (empty string and separator)"
ret$ = StringToken(@"", " ") :Debug " => '" + ret$ + "' (empty string)"
ret$ = StringToken(@"Test with ", "") :Debug " => '" + ret$ + "' (empty separator)"
; new string
Debug #LF$ + "===================="
DemoText$ = "Key1,Test1;Key2,Test2; , ; , ;Key3 ,Test3 ;"
For nn = 1 To 10
ret$ = StringToken(@DemoText$, ",; ") :Debug " => '" + ret$ + "'"
Next nn
; same string .. needs a reset
StringToken(@"", "")
; same variable has the same pointer !!!
DemoText$ = "This is a test string to see if split is working."
Debug #LF$ + "===================="
For nn = 1 To 15
ret$ = StringToken(@DemoText$, #CRLF$ + #TAB$ + #FF$ + #VT$ + " ") :Debug "["+nn+"] = '" + ret$ + "'"
Next nn
CompilerEndIf