[All Platforms] - String Tokeniser (Procedural Paradigm)
Posted: Sun Dec 04, 2011 7:36 pm
Hey again,
If you haven't already seen, I have provided a string tokeniser in this section with an OOP paradigm (using PB Interface)... So those who do not wish to use an OOP paradigm don't get left out, I've also written a String Tokeniser using a Procedural (Functional) paradigm.
Usage
StrTok(String$, Delimiters$, List OutputList.StrTok_Token()) - Returns the number of tokens found.
Change Log
Alternatively, you can find the OOP implementation Here.
All comments, constructive criticisms and suggestions welcome
Thanks
If you haven't already seen, I have provided a string tokeniser in this section with an OOP paradigm (using PB Interface)... So those who do not wish to use an OOP paradigm don't get left out, I've also written a String Tokeniser using a Procedural (Functional) paradigm.
Usage
StrTok(String$, Delimiters$, List OutputList.StrTok_Token()) - Returns the number of tokens found.
Change Log
- Revision 2 - StrTok() will now clear the output list as standard.
Code: Select all
; ----------------------------------------------------------------------------------------------------
; Title: String Tokeniser (Procedural)
; Description: Procedure that provides a String Tokeniser.
; Author(s): Michael R. King (mrking2910@gmail.com)
; Revision: 2
; Support: Cross-Platform
;
; Notes: Refer to Usage Example.
;
; ----------------------------------------------------------------------------------------------------
EnableExplicit
CompilerIf Defined(_PBI_STRTOK_, #PB_Constant) = #False
#_PBI_STRTOK_ = #True
Structure StrTok_Token
String.s
Position.l
EndStructure
Procedure StrTok(String$, Delimiters$, List OutputList.StrTok_Token())
Protected cCnt.l, cIx.l, c.s, tLoc.l, t.s, tCnt.l
cCnt = Len(String$)
tLoc = -1
ClearList(OutputList())
For cIx = 1 To cCnt + 1
c = Mid(String$, cIx, 1)
If FindString(Delimiters$, c) Or cIx = (cCnt + 1)
If Len(t) > 0
AddElement(OutputList())
OutputList()\Position = tLoc
OutputList()\String = t
tCnt + 1
EndIf
t = ""
tLoc = -1
Else
If tLoc = -1
tLoc = cIx
EndIf
t = t + c
EndIf
Next
ProcedureReturn tCnt
EndProcedure
CompilerEndIf ;_PBI_STRTOK_
; -------------------------------------------------------------------------------------
; - DEMONSTRATION CODE - DEMONSTRATION CODE - DEMONSTRATION CODE - DEMONSTRATION CODE -
; -------------------------------------------------------------------------------------
; - Define Test String -
Define.s TestString = " Hello World. This is a test string!"
; - Create Output List -
NewList Tokens.StrTok_Token()
; - Tokenise String -
StrTok(TestString, " ", Tokens())
; - Output Results -
ForEach Tokens()
Debug "Token: '" + Tokens()\String + "' found at: [" + Str(Tokens()\Position) + "]."
Next
; - Free List -
FreeList(Tokens())
All comments, constructive criticisms and suggestions welcome

Thanks
