Perfect use for finite state machines!

Just because I cant post on the Xojo forums so here it is
https://forum.xojo.com/60481-php-serialized-array-reader

Public Function UnserializePHPStringArray(inputValue as String) as String()
  
  Dim tmp() As String
  
  // php seems to write these as
  // a - array
  // :
  // number of elements
  // :
  // {
  //     elements as
  //     i - index # ?
  //     :
  //     index value
  //     ;
  //     s - string (is this generally the "type" ?
  //     :
  //     length
  //     :
  //     quoted string value
  //     ;
  // }
  
  Dim state As Integer 
  Dim index As Integer = 1
  
  Dim accumulatedStr As String
  Dim holdIndex As Integer
  
  While True
    Dim currentChar As String
    currentChar = inputValue.Mid(index, 1)
    
    Select Case state
    Case 0
      // initial state we're looking for the leading "a"
      If StrComp(currentChar, "a", REALbasic.StrCompCaseSensitive) = 0 Then
        accumulatedStr = ""
        index = index + 1
        state = 1
      Else
        Exit // failed !
      End If
      
    Case 1
      // after the leading "a" we need a ":"
      If currentChar = ":" Then
        accumulatedStr = ""
        index = index + 1
        state = 2
      Else
        Exit // failed !
      End If
      
    Case 2
      // got "a:" now we get the # of elements
      If InStr("0123456789",currentChar) > 0 Then
        accumulatedStr = accumulatedStr + currentChar
        index = index + 1
        state = 2
      Elseif currentChar = ":" Then
        // ok got the lead in like "a:10" to say we want an array with 10 elements
        // and now have the :
        Redim tmp( Val(accumulatedStr) - 1 ) // php uses 0 based indexes as well so 10 elements is indexes 0 - 9
        accumulatedStr = ""
        index = index + 1
        state = 3
      Else
        Exit // failed !
      End If
      
    Case 3
      // ok got the lead in like "a:10:" to say we want an array with 10 elements
      // now need the {
      If currentChar = "{" Then
        accumulatedStr = ""
        index = index + 1
        state = 4
      Else
        Exit // failed !
      End If
      
    Case 4
      // this is the start of reading each element
      // need the "i" for the index for this item
      If StrComp(currentChar, "i", REALbasic.StrCompCaseSensitive) = 0 Then 
        accumulatedStr = ""
        index = index + 1
        state = 5
      Elseif currentChar = "}" Then
        Exit // but this is sucess !!!!!!!!!!!!!!!!!!
      Else
        Exit // failed !
      End If
      
    Case 5
      // we got the leading "i: for an index
      // and this should now be a ":"
      If currentChar = ":" Then
        accumulatedStr = ""
        index = index + 1
        state = 6
      Else
        Exit // failed !
      End If
      
    Case 6
      // we got the "i: for an index and now we need the digits
      If InStr("0123456789",currentChar) > 0 Then
        accumulatedStr = accumulatedStr + currentChar
        index = index + 1
        state = 6
      Elseif currentChar = ";" Then
        holdIndex = Val(accumulatedStr) 
        accumulatedStr = ""
        index = index + 1
        state = 7
      Else
        Exit // failed !
      End If
      
    Case 7
      // this should now be the "s" for the type (we're assuming string)
      If StrComp(currentChar, "s", REALbasic.StrCompCaseSensitive) = 0 Then 
        accumulatedStr = ""
        index = index + 1
        state = 8
      Else
        Exit // failed !
      End If
      
    Case 8
      // we got the leading "i:XXX:;s" for an entry
      // and this should now be a ":" to lead to the values length
      If currentChar = ":" Then
        accumulatedStr = ""
        index = index + 1
        state = 9
      Else
        Exit // failed !
      End If
      
    Case 9
      // we got the "i: for an index and now we need the digits
      If InStr("0123456789",currentChar) > 0 Then
        accumulatedStr = accumulatedStr + currentChar
        index = index + 1
        state = 9
      Elseif currentChar = ":" Then
        // holdIndex = Val(accumulatedStr) // fun part is we really dont care how long it is :)
        accumulatedStr = ""
        index = index + 1
        state = 10
      Else
        Exit // failed !
      End If
      
    Case 10
      // we got the leading "i:XXX:;s:XXX" for an entry
      // and this should now be a " 
      If currentChar = """" Then
        accumulatedStr = ""
        index = index + 1
        state = 11
      Else
        Exit // failed !
      End If
      
    Case 11
      // we got the " to lead into the entry and now we eat everything until we get to another " (how would you embed a " ?)
      If currentChar = """" Then
        tmp(holdIndex) = accumulatedStr
        accumulatedStr = ""
        index = index + 1
        state = 12
      Else
        accumulatedStr = accumulatedStr + currentChar
        index = index + 1
        state = 11
      End If
      
    Case 12
      If currentChar = ";" Then
        accumulatedStr = ""
        index = index + 1
        state = 4
      Else
        exit
      End If
      
    Else
      Break
    End Select
    
    
  Wend
  
  Return tmp
End Function

Uh - I wonder how much longer Hal is allowed to post there … :grimacing: