Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
String Tokenizer
#9
I completely forgot to come back and show the tokenize function I used to use that took advantage of strtok:

Code: (Select All)

Option _Explicit

Dim As _Offset tokenized
Dim As String toTokenize: toTokenize = "36" + Chr$(9) + "Hungry Coyote Import Store" + Chr$(9) + "Yoshi Latimer" + Chr$(9) + "City Center Plaza 516 Main St" + Chr$(9) + "Elgin" + Chr$(9) + "97827" + Chr$(9) + "USA" + Chr$(10) + "37" + Chr$(9) + "Hungry Owl All-Night Grocers" + Chr$(9) + "Patricia McKenna" + Chr$(9) + "8 Johnstown Road" + Chr$(9) + "Cork" + Chr$(9) + "" + Chr$(9) + "Ireland" + Chr$(10) + "38" + Chr$(9) + "Island Trading" + Chr$(9) + "Helen Bennett" + Chr$(9) + "Garden House Crowther Way" + Chr$(9) + "Cowes" + Chr$(9) + "PO31 7PJ" + Chr$(9) + "UK" + Chr$(10) '+ Chr$(0)
Dim As String delimiter: delimiter = Chr$(9) + Chr$(10)

Dim As Long i
ReDim As String tokenized(0 To 0)
tokenize toTokenize, Chr$(9) + Chr$(10), tokenized()

For i = LBound(tokenized) To UBound(tokenized)
    Print tokenized(i)
Next

Function pointerToString$ (pointer As _Offset)
    Declare CustomType Library
        Function strlen%& (ByVal ptr As _Unsigned _Offset)
    End Declare
    Dim As _Offset length: length = strlen(pointer)
    If length Then
        Dim As _MEM pString: pString = _Mem(pointer, length)
        Dim As String ret: ret = Space$(length)
        _MemGet pString, pString.OFFSET, ret
        _MemFree pString
    End If
    pointerToString = ret
End Function

Sub tokenize (toTokenize As String, delimiters As String, StorageArray() As String)
    Declare CustomType Library
        Function strtok%& (ByVal str As _Offset, delimiters As String)
    End Declare
    Dim As _Offset tokenized
    Dim As String tokCopy: If Right$(toTokenize, 1) <> Chr$(0) Then tokCopy = toTokenize + Chr$(0) Else tokCopy = toTokenize
    Dim As String delCopy: If Right$(delimiters, 1) <> Chr$(0) Then delCopy = delimiters + Chr$(0) Else delCopy = delimiters
    Dim As _Unsigned Long lowerbound: lowerbound = LBound(StorageArray)
    Dim As _Unsigned Long i: i = lowerbound
    tokenized = strtok(_Offset(tokCopy), delCopy)
    While tokenized <> 0
        ReDim _Preserve StorageArray(lowerbound To UBound(StorageArray) + 1)
        StorageArray(i) = pointerToString(tokenized)
        tokenized = strtok(0, delCopy)
        i = i + 1
    Wend
    ReDim _Preserve StorageArray(UBound(StorageArray) - 1)
End Sub
Schuwatch!
Yes, it's me. Now shut up.
Reply


Messages In This Thread
String Tokenizer - by a740g - 05-25-2023, 03:33 PM
RE: String Tokenizer - by RhoSigma - 05-25-2023, 09:33 PM
RE: String Tokenizer - by a740g - 05-25-2023, 10:45 PM
RE: String Tokenizer - by Kernelpanic - 05-25-2023, 11:04 PM
RE: String Tokenizer - by a740g - 05-26-2023, 12:21 AM
RE: String Tokenizer - by Ultraman - 05-26-2023, 03:56 PM
RE: String Tokenizer - by Kernelpanic - 05-26-2023, 08:44 PM
RE: String Tokenizer - by Kernelpanic - 05-30-2023, 09:15 PM
RE: String Tokenizer - by Ultraman - 06-29-2023, 11:42 AM



Users browsing this thread: 1 Guest(s)