Attribute VB_Name = "StringLib"
'--- AWK の split 関数と同じ挙動 ---
'
Sub StringSplit(ByVal str As String, ByVal separator As String, fields As Collection)
    Set fields = New Collection
    Dim p As Long
    Dim i As Long
    Dim iend As Long
    Dim iendtmp As Long
    If (separator = " ") Then
        p = 1
        Do While (Mid(str, p, 1) = " " And p <= Len(str))
            p = p + 1
        Loop
    Else
        p = 1
    End If
    Do While (p <= Len(str))
        If (separator = "") Then
            '--- p 番目から始まる行を終える字の添字 iend を求める。
            iend = Len(str) + 1
            iendtmp = InStr(p, str, Chr(10))
            If (iendtmp > 0 And iendtmp < iend) Then iend = iendtmp
            '--- LF 直前の CR は除去
            If (Mid$(str, iend - 1, 1) = Chr(13)) Then
                fields.Add Mid(str, p, iend - p - 1)
            Else
                fields.Add Mid(str, p, iend - p)
            End If
            p = iend + 1
        Else
            ' p 番目から始まる語を終える字の添字 iend を求める。
            iend = Len(str) + 1
            For i = 1 To Len(separator)
                iendtmp = InStr(p, str, Mid(separator, i, 1))
                If (iendtmp > 0 And iendtmp < iend) Then iend = iendtmp
            Next i
            fields.Add Mid(str, p, iend - p)
            p = iend + 1
        End If
    Loop
End Sub
