Page 1 of 1

[All Platforms] - String Tokeniser (Procedural Paradigm)

Posted: Sun Dec 04, 2011 7:36 pm
by Env
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
  • Revision 2 - StrTok() will now clear the output list as standard.
Code (Contains usage example)

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())
Alternatively, you can find the OOP implementation Here.

All comments, constructive criticisms and suggestions welcome :)

Thanks :D

Re: [All Platforms] - String Tokeniser (Procedural Paradigm)

Posted: Tue Dec 06, 2011 12:34 am
by IdeasVacuum
Very nice work, easier to use than C's strtok :mrgreen:

Re: [All Platforms] - String Tokeniser (Procedural Paradigm)

Posted: Wed Dec 07, 2011 9:37 pm
by Env
Diolch yn fawr, IdeasVacuum,

I did write some C code a long time ago providing a function which ideally calls the strtok function, and outputs all the results into a std::vector<std::string> type, so this is just a conversion I suppose... Hope it's found to be useful :P


Thanks again :)