I am dealing with large and often misformed .csv files and StringUtils.LoadCSV is not flexible enough to cope with all these, so wrote a parsing function in
B4A, having the same output (list holding 1D string arrays). This seems to work all fine, but it is about 3-4x slower that StringUtils.LoadCSV. I suppose that this
is to be expected as I take it LoadCSV works directly with Java. Below the B4A code I have and 2 questions about this:
1. Are there any ways anybody can see to speed this up?
2. Would it be possible to translate this code with inline Java?
RBS
B4A, having the same output (list holding 1D string arrays). This seems to work all fine, but it is about 3-4x slower that StringUtils.LoadCSV. I suppose that this
is to be expected as I take it LoadCSV works directly with Java. Below the B4A code I have and 2 questions about this:
1. Are there any ways anybody can see to speed this up?
2. Would it be possible to translate this code with inline Java?
B4X:
Sub CSV2List(strFolder As String, _
strFile As String, _
btSeparatorByte As Byte, _
btEncloserByte As Byte, _
btEndOfLineByte As Byte, _
lRows As Long) As ResumableSub
Dim i As Long
Dim c As Long
Dim arrBytes() As Byte
Dim lBytes As Long
Dim lColumns As Long
Dim lRow As Long
Dim lUB As Long
Dim lSeparatorPos As Long
Dim lEncloserPos As Long
Dim lChr10Pos As Long
Dim arrFields() As Object
Dim arrData() As Object
Dim lstRows As List
Dim lFirstDataByte As Long
Dim bExit As Boolean
Dim bCSVHasEncloserBytes As Boolean
'this will be a zero-based UTF8 byte array
'-----------------------------------------
RAF.Initialize(strFolder, strFile, True)
lBytes = RAF.Size
Dim arrBytes(lBytes) As Byte
RAF.ReadBytes(arrBytes, 0, lBytes, 0)
lUB = arrBytes.Length - 1
bCSVHasEncloserBytes = CSVHasEncloserBytes(arrBytes, btEncloserByte)
arrFields = GetCSVHeaders(arrBytes, 44, 34, 10)
lColumns = arrFields.Length
Dim arrData(lColumns) As Object
lstRows.Initialize
lstRows.Add(arrFields) 'adding the column names
lRow = lRow + 1
'as we are skipping the the column names
'---------------------------------------
lFirstDataByte = GetCSVFirstDataByte(arrBytes, 10)
lEncloserPos = -1
lSeparatorPos = -1
lChr10Pos = lFirstDataByte - 1 '<<<<<<!!
'deal with the first byte, taking this out speeds up the next loop a bit
'-----------------------------------------------------------------------
If bCSVHasEncloserBytes Then '<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
If lColumns > 1 Then '<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
''''''''''''''''''''''''''
'SEPARATORS AND ENCLOSERS'
''''''''''''''''''''''''''
Select Case arrBytes(lFirstDataByte)
Case btSeparatorByte
arrData(c) = Null
Case btEncloserByte
lEncloserPos = lFirstDataByte
End Select
For i = lFirstDataByte + 1 To lUB - 1
Select Case arrBytes(i)
Case btSeparatorByte
If c < lColumns Then
If lEncloserPos = -1 Then
If lSeparatorPos = -1 Then
If arrBytes(i - 1) <> btEncloserByte Then
arrData(c) = BytesToString(arrBytes, lChr10Pos + 1, (i - 1) - lChr10Pos, "UTF-8")
c = c + 1
lChr10Pos = -1
End If 'If arrBytes(i - 1) <> btEncloserByte
Else 'If lSeparatorPos = -1
If (i - 1) - (lSeparatorPos + 1) > 1 Then
arrData(c) = BytesToString(arrBytes, lSeparatorPos + 1, (i - 1) - lSeparatorPos, "UTF-8")
Else 'If (i - 1) - (lSeparatorPos + 1) > 1
arrData(c) = Null
End If 'If (i - 1) - (lSeparatorPos + 1) > 1
c = c + 1
lChr10Pos = -1
End If 'If lSeparatorPos = -1
End If 'If lEncloserPos = -1
End If 'If c < lColumns
lSeparatorPos = i
Case btEncloserByte
If lEncloserPos = -1 Then
lEncloserPos = i
Else
If c < lColumns Then
If i - lEncloserPos = 1 Then
arrData(c) = Null
c = c + 1
Else 'If i - lEncloserPos = 1
arrData(c) = BytesToString(arrBytes, lEncloserPos + 1, (i - 1) - lEncloserPos, "UTF-8")
c = c + 1
End If 'If i - lEncloserPos = 1
End If 'If c < lColumns
lEncloserPos = -1
End If
lSeparatorPos = -1
lChr10Pos = -1
Case btEndOfLineByte
If lEncloserPos = -1 Then
If c < lColumns Then
If arrBytes(i - 1) = 13 Then
If (i - 2) - lSeparatorPos > 0 Then
arrData(c) = BytesToString(arrBytes, lSeparatorPos + 1, (i - 2) - lSeparatorPos, "UTF-8")
Else
arrData(c) = Null
End If
c = c + 1
Else 'If arrBytes(i - 1) = 13
If (i - 1) - lSeparatorPos > 0 Then
arrData(c) = BytesToString(arrBytes, lSeparatorPos + 1, (i - 1) - lSeparatorPos, "UTF-8")
Else
arrData(c) = Null
End If
c = c + 1
End If 'If arrBytes(i - 1) = 13
End If
lstRows.Add(arrData)
lRow = lRow + 1
lEncloserPos = -1
lSeparatorPos = -1
lChr10Pos = i
c = 0
Dim arrData(lColumns) As Object
If lRows > -1 Then
If lRow > lRows Then
bExit = True
End If
End If
End If 'If lEncloserPos = -1
End Select
If bExit Then Exit
Next
'deal with the last byte, this is needed for if there is no final linebreak
'--------------------------------------------------------------------------
If lRows = -1 Then
Select Case arrBytes(lUB)
Case btSeparatorByte
If c < lColumns Then
If lEncloserPos = -1 Then
If lSeparatorPos = -1 Then
If lUB - lChr10Pos = 1 Then
arrData(c) = Null
Else 'If lUB - lChr10Pos = 1
If arrBytes(lUB - 1) <> btEncloserByte Then
arrData(c) = BytesToString(arrBytes, lChr10Pos + 1, (lUB - 1) - lChr10Pos, "UTF-8")
End If
End If 'If lUB - lChr10Pos = 1
End If 'If lSeparatorPos = -1
Else 'If lEncloserPos = -1
If lUB - lSeparatorPos = 1 Then
arrData(c) = Null
End If
End If 'If lEncloserPos = -1
End If 'If collStrings.Count < lColumns
Case btEncloserByte
If lEncloserPos > -1 Then
If c < lColumns Then
If lUB - lEncloserPos = 1 Then
arrData(c) = Null
Else 'If lUB - lEncloserPos = 1
arrData(c) = BytesToString(arrBytes, lEncloserPos + 1, (lUB - 1) - lEncloserPos, "UTF-8")
End If 'If lUB - lEncloserPos = 1
End If 'If c < lColumns
End If 'If lEncloserPos > -1
Case btEndOfLineByte
If lEncloserPos = -1 Then
If c < lColumns Then
If lSeparatorPos > -1 Then
If arrBytes(lUB - 1) = 13 Then
If lUB - lSeparatorPos > 2 Then
arrData(c) = BytesToString(arrBytes, lSeparatorPos + 1, (lUB - 1) - lSeparatorPos, "UTF-8")
Else
arrData(c) = Null
End If
Else 'If arrBytes(lUB - 1) = 13
If lUB - lSeparatorPos > 1 Then
arrData(c) = BytesToString(arrBytes, lSeparatorPos + 1, (lUB - 2) - lSeparatorPos, "UTF-8")
Else
arrData(c) = Null
End If
End If 'If arrBytes(lUB - 1) = 13
Else 'If lSeparatorPos > -1
If lChr10Pos > -1 Then
If arrBytes(lUB - 1) = 13 Then
arrData(c) = BytesToString(arrBytes, lChr10Pos + 1, (lUB - 2) - lChr10Pos, "UTF-8")
Else
arrData(c) = BytesToString(arrBytes, lChr10Pos + 1, (lUB - 1) - lChr10Pos, "UTF-8")
End If
Else 'If lChr10Pos > -1
If arrBytes(lUB - 2) <> btEncloserByte Then
If arrBytes(lUB - 1) = 13 Then
arrData(c) = BytesToString(arrBytes, 0, lUB - 2, "UTF-8")
Else
arrData(c) = BytesToString(arrBytes, 0, lUB - 1, "UTF-8")
End If
End If 'If arrBytes(lUB - 2) <> btEncloserByte
End If 'If lChr10Pos > -1
End If 'If lSeparatorPos > -1
End If 'If c < lColumns
End If 'If lEncloserPos = -1
Case Else
If lEncloserPos = -1 Then
If c < lColumns Then
If lSeparatorPos > -1 Then
If lUB - lSeparatorPos > 1 Then
arrData(c) = BytesToString(arrBytes, lSeparatorPos + 1, lUB - lSeparatorPos, "UTF-8")
Else
arrData(c) = Null
End If
Else 'If lSeparatorPos > -1
If lChr10Pos > -1 Then
arrData(c) = BytesToString(arrBytes, lChr10Pos + 1, lUB - lChr10Pos, "UTF-8")
Else
arrData(c) = BytesToString(arrBytes, lFirstDataByte, (lUB - lFirstDataByte) + 1, "UTF-8")
End If
End If 'If lSeparatorPos > -1
End If 'If c < lColumns
End If 'If lEncloserPos = -1
End Select
End If
Else 'If lColumns > 1 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
'''''''''''''''''''''''''''''
'NO SEPARATORS BUT ENCLOSERS'
'''''''''''''''''''''''''''''
Select Case arrBytes(lFirstDataByte)
Case btEncloserByte
lEncloserPos = lFirstDataByte
End Select
For i = lFirstDataByte + 1 To lUB - 1
Select Case arrBytes(i)
Case btEncloserByte
If lEncloserPos = -1 Then
lEncloserPos = i
Else
If c < lColumns Then
If i - lEncloserPos = 1 Then
arrData(c) = Null
c = c + 1
Else 'If i - lEncloserPos = 1
arrData(c) = BytesToString(arrBytes, lEncloserPos + 1, (i - 1) - lEncloserPos, "UTF-8")
c = c + 1
End If 'If i - lEncloserPos = 1
End If 'If c < lColumns
lEncloserPos = -1
End If
lChr10Pos = -1
Case btEndOfLineByte
If lEncloserPos = -1 Then
If c < lColumns Then
If arrBytes(i - 1) = 13 Then
If (i - lChr10Pos) -1 > 1 Then
arrData(c) = BytesToString(arrBytes, lChr10Pos + 1, (i - lChr10Pos) -1 , "UTF-8")
Else
arrData(c) = Null
End If
c = c + 1
Else 'If arrBytes(i - 1) = 13
If (i - lChr10Pos) > 1 Then
arrData(c) = BytesToString(arrBytes, lChr10Pos + 1, (i - lChr10Pos) , "UTF-8")
Else
arrData(c) = Null
End If
c = c + 1
End If 'If i - lSeparatorPos > 1
End If 'If c < lColumns
lstRows.Add(arrData)
lRow = lRow + 1
lEncloserPos = -1
lChr10Pos = i
c = 0
Dim arrData(lColumns) As Object
If lRows > -1 Then
If lRow > lRows Then
bExit = True
End If
End If
End If 'If lEncloserPos = -1
End Select
If bExit Then Exit
Next
'deal with the last byte, this is needed for if there is no final linebreak
'--------------------------------------------------------------------------
If lRows = -1 Then
Select Case arrBytes(lUB)
Case btEncloserByte
If lEncloserPos > -1 Then
If c < lColumns Then
If lUB - lEncloserPos = 1 Then
arrData(c) = Null
Else 'If lUB - lEncloserPos = 1
arrData(c) = BytesToString(arrBytes, lEncloserPos + 1, (lUB - 1) - lEncloserPos, "UTF-8")
End If 'If lUB - lEncloserPos = 1
End If 'If c < lColumns
End If 'If lEncloserPos > -1
Case btEndOfLineByte
If lEncloserPos = -1 Then
If c < lColumns Then
If lChr10Pos > -1 Then
If arrBytes(lUB - 1) = 13 Then
If (lUB - 2) - lChr10Pos > 0 Then
arrData(c) = BytesToString(arrBytes, lChr10Pos + 1, (lUB - 2) - lChr10Pos, "UTF-8")
Else
arrData(c) = Null
End If
Else
If (lUB - 1) - lChr10Pos > 0 Then
arrData(c) = BytesToString(arrBytes, lChr10Pos + 1, (lUB - 1) - lChr10Pos, "UTF-8")
Else
arrData(c) = Null
End If
End If
Else 'If lChr10Pos > -1
If arrBytes(lUB - 2) <> btEncloserByte Then
If arrBytes(lUB - 1) = 13 Then
arrData(c) = BytesToString(arrBytes, 0, lUB - 2, "UTF-8")
Else
arrData(c) = BytesToString(arrBytes, 0, lUB - 1, "UTF-8")
End If
End If 'If arrBytes(lUB - 2) <> btEncloserByte
End If 'If lChr10Pos > -1
End If 'If c < lColumns
End If 'If lEncloserPos = -1
Case Else
If lEncloserPos = -1 Then
If c < lColumns Then
If lChr10Pos > -1 Then
arrData(c) = BytesToString(arrBytes, lChr10Pos + 1, lUB - lChr10Pos, "UTF-8")
Else
arrData(c) = BytesToString(arrBytes, lFirstDataByte, (lUB - lFirstDataByte) + 1, "UTF-8")
End If
End If 'If c < lColumns
End If 'If lEncloserPos = -1
End Select
End If
End If 'If lColumns > 1 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
Else 'If bCSVHasEncloserBytes<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
'''''''''''''''''''''''''''''
'SEPARATORS BUT NO ENCLOSERS'
'''''''''''''''''''''''''''''
If lColumns > 1 Then '<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
Select Case arrBytes(lFirstDataByte)
Case btSeparatorByte
arrData(c) = Null
End Select
For i = lFirstDataByte + 1 To lUB - 1
Select Case arrBytes(i)
Case btSeparatorByte
If c < lColumns Then
If lSeparatorPos = -1 Then
arrData(c) = BytesToString(arrBytes, lChr10Pos + 1, (i - 1) - lChr10Pos, "UTF-8")
c = c + 1
lChr10Pos = -1
Else 'If lSeparatorPos = -1
If (i - 1) - (lSeparatorPos + 1) > 1 Then
arrData(c) = BytesToString(arrBytes, lSeparatorPos + 1, (i - 1) - lSeparatorPos, "UTF-8")
Else 'If (i - 1) - (lSeparatorPos + 1) > 1
arrData(c) = Null
End If 'If (i - 1) - (lSeparatorPos + 1) > 1
c = c + 1
lChr10Pos = -1
End If 'If lSeparatorPos = -1
End If 'If c < lColumns
lSeparatorPos = i
Case btEndOfLineByte
If c < lColumns Then
If lSeparatorPos > -1 Then
If arrBytes(i - 1) = 13 Then
If (i - 2) - lSeparatorPos > 0 Then
arrData(c) = BytesToString(arrBytes, lSeparatorPos + 1, (i - 2) - lSeparatorPos, "UTF-8")
Else
arrData(c) = Null
End If
c = c + 1
Else 'If arrBytes(i - 1) = 13
If (i - 1) - lSeparatorPos > 0 Then
arrData(c) = BytesToString(arrBytes, lSeparatorPos + 1, (i - 1) - lSeparatorPos, "UTF-8")
Else
arrData(c) = Null
End If
c = c + 1
End If 'If arrBytes(i - 1) = 13
Else 'If lSeparatorPos > -1
If arrBytes(i - 1) = 13 Then
If (i - lChr10Pos) -1 > 1 Then
arrData(c) = BytesToString(arrBytes, lChr10Pos + 1, (i - lChr10Pos) -1 , "UTF-8")
Else
arrData(c) = Null
End If
c = c + 1
Else 'If arrBytes(i - 1) = 13
If (i - lChr10Pos) > 1 Then
arrData(c) = BytesToString(arrBytes, lChr10Pos + 1, (i - lChr10Pos) , "UTF-8")
Else
arrData(c) = Null
End If
c = c + 1
End If 'If i - lSeparatorPos > 1
End If 'If lSeparatorPos > -1
lstRows.Add(arrData)
lRow = lRow + 1
lSeparatorPos = -1
lChr10Pos = i
c = 0
Dim arrData(lColumns) As Object
If lRows > -1 Then
If lRow > lRows Then
bExit = True
End If
End If
End If 'If lEncloserPos = -1
End Select
If bExit Then Exit
Next
'deal with the last byte, this is needed for if there is no final linebreak
'--------------------------------------------------------------------------
If lRows = -1 Then
Select Case arrBytes(lUB)
Case btSeparatorByte
If c < lColumns Then
If lSeparatorPos = -1 Then
If lUB - lChr10Pos = 1 Then
arrData(c) = Null
Else 'If lUB - lChr10Pos = 1
'If arrBytes(lUB - 1) <> btEncloserByte Then
arrData(c) = BytesToString(arrBytes, lChr10Pos + 1, (lUB - 1) - lChr10Pos, "UTF-8")
End If 'If lUB - lChr10Pos = 1
End If 'If lSeparatorPos = -1
End If 'If collStrings.Count < lColumns
Case btEndOfLineByte
If lEncloserPos = -1 Then
If c < lColumns Then
If lSeparatorPos > -1 Then
If arrBytes(lUB - 1) = 13 Then
If lUB - lSeparatorPos > 2 Then
arrData(c) = BytesToString(arrBytes, lSeparatorPos + 1, (lUB - 1) - lSeparatorPos, "UTF-8")
Else
arrData(c) = Null
End If
Else 'If arrBytes(lUB - 1) = 13
If lUB - lSeparatorPos > 1 Then
arrData(c) = BytesToString(arrBytes, lSeparatorPos + 1, (lUB - 2) - lSeparatorPos, "UTF-8")
Else
arrData(c) = Null
End If
End If 'If arrBytes(lUB - 1) = 13
Else 'If lSeparatorPos > -1
If lChr10Pos > -1 Then
If arrBytes(lUB - 1) = 13 Then
If (lUB - 2) - lChr10Pos > 0 Then
arrData(c) = BytesToString(arrBytes, lChr10Pos + 1, (lUB - 2) - lChr10Pos, "UTF-8")
Else
arrData(c) = Null
End If
Else
If (lUB - 1) - lChr10Pos > 0 Then
arrData(c) = BytesToString(arrBytes, lChr10Pos + 1, (lUB - 1) - lChr10Pos, "UTF-8")
Else
arrData(c) = Null
End If
End If
Else 'If lChr10Pos > -1
If arrBytes(lUB - 1) = 13 Then
arrData(c) = BytesToString(arrBytes, 0, lUB - 2, "UTF-8")
Else
arrData(c) = BytesToString(arrBytes, 0, lUB - 1, "UTF-8")
End If
End If 'If lChr10Pos > -1
End If 'If lSeparatorPos > -1
End If 'If c < lColumns
End If 'If lEncloserPos = -1
Case Else
If c < lColumns Then
If lSeparatorPos > -1 Then
If lUB - lSeparatorPos > 1 Then
arrData(c) = BytesToString(arrBytes, lSeparatorPos + 1, lUB - lSeparatorPos, "UTF-8")
Else
arrData(c) = Null
End If
Else 'If lSeparatorPos > -1
If lChr10Pos > -1 Then
arrData(c) = BytesToString(arrBytes, lChr10Pos + 1, lUB - lChr10Pos, "UTF-8")
Else
arrData(c) = BytesToString(arrBytes, lFirstDataByte, (lUB - lFirstDataByte) + 1, "UTF-8")
End If
End If 'If lSeparatorPos > -1
End If 'If c < lColumns
End Select
End If
Else 'If lColumns > 1 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
''''''''''''''''''''''''''''''''
'NO SEPARATORS AND NO ENCLOSERS'
''''''''''''''''''''''''''''''''
For i = lFirstDataByte + 1 To lUB - 1
Select Case arrBytes(i)
Case btEndOfLineByte
If c < lColumns Then
If arrBytes(i - 1) = 13 Then
If (i - lChr10Pos) -1 > 0 Then
arrData(c) = BytesToString(arrBytes, lChr10Pos + 1, (i - lChr10Pos) -1 , "UTF-8")
Else
arrData(c) = Null
End If
c = c + 1
Else 'If arrBytes(i - 1) = 13
If (i - lChr10Pos) > 0 Then
arrData(c) = BytesToString(arrBytes, lChr10Pos + 1, (i - lChr10Pos) , "UTF-8")
Else
arrData(c) = Null
End If
c = c + 1
End If 'If i - lSeparatorPos > 1
End If 'If c < lColumns
lstRows.Add(arrData)
lRow = lRow + 1
lChr10Pos = i
c = 0
Dim arrData(lColumns) As Object
If lRows > -1 Then
If lRow > lRows Then
bExit = True
End If
End If
End Select
If bExit Then Exit
Next
'deal with the last byte, this is needed for if there is no final linebreak
'--------------------------------------------------------------------------
If lRows = -1 Then
Select Case arrBytes(lUB)
Case btEndOfLineByte
If c < lColumns Then
If lChr10Pos > -1 Then
If arrBytes(lUB - 1) = 13 Then
arrData(c) = BytesToString(arrBytes, lChr10Pos + 1, (lUB - 2) - lChr10Pos, "UTF-8")
Else
arrData(c) = BytesToString(arrBytes, lChr10Pos + 1, (lUB - 1) - lChr10Pos, "UTF-8")
End If
Else 'If lChr10Pos > -1
If arrBytes(lUB - 1) = 13 Then
arrData(c) = BytesToString(arrBytes, 0, lUB - 2, "UTF-8")
Else
arrData(c) = BytesToString(arrBytes, 0, lUB - 1, "UTF-8")
End If
End If 'If lChr10Pos > -1
End If 'If c < lColumns
Case Else
If c < lColumns Then
If lChr10Pos > -1 Then
arrData(c) = BytesToString(arrBytes, lChr10Pos + 1, lUB - lChr10Pos, "UTF-8")
Else
arrData(c) = BytesToString(arrBytes, lFirstDataByte, (lUB - lFirstDataByte) + 1, "UTF-8")
End If
End If 'If c < lColumns
End Select
End If
End If 'If lColumns > 1 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
End If 'If bCSVHasEncloserBytes<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
'add final data array
lstRows.Add(arrData)
Return lstRows
End Sub
Sub GetCSVHeaders(arrBytes() As Byte, _
btSeparatorByte As Byte, _
btEncloserByte As Byte, _
btEndOfLineByte As Byte) As String()
Dim i As Long
Dim c As Long
Dim lUB As Long
Dim lSeparatorPos As Long
Dim lEncloserPos As Long
Dim lChr10Pos As Long
Dim lstFields As List
Dim bExitLoop As Boolean
lUB = arrBytes.Length - 1
lEncloserPos = -1
lSeparatorPos = -1
lChr10Pos = -1
lstFields.Initialize
For i = 0 To lUB
Select Case arrBytes(i)
Case btSeparatorByte
If lEncloserPos = -1 Then
If lSeparatorPos = -1 Then
If i - lChr10Pos = 1 Then
bExitLoop = True
Else
If arrBytes(i - 1) <> btEncloserByte Then
lstFields.Add(BytesToString(arrBytes, lChr10Pos + 1, (i - 1) - lChr10Pos, "UTF-8"))
c = c + 1
lChr10Pos = -1
End If
End If
Else
If (i - 1) - (lSeparatorPos + 1) > 1 Then
lstFields.Add(BytesToString(arrBytes, lSeparatorPos + 1, (i - 1) - lSeparatorPos, "UTF-8"))
Else
bExitLoop = True
End If
c = c + 1
lChr10Pos = -1
End If
Else
If i - lSeparatorPos = 1 Then
bExitLoop = True
End If
End If
lSeparatorPos = i
Case btEncloserByte
If lEncloserPos = -1 Then
lEncloserPos = i
Else
If i - lEncloserPos = 1 Then
bExitLoop = True
Else
lstFields.Add(BytesToString(arrBytes, lEncloserPos + 1, (i - 1) - lEncloserPos, "UTF-8"))
c = c + 1
End If
lEncloserPos = -1
End If
lSeparatorPos = -1
lChr10Pos = -1
Case btEndOfLineByte
If lEncloserPos = -1 Then
If lSeparatorPos > -1 Then
If arrBytes(i - 1) = 13 Then
If i - lSeparatorPos > 2 Then
lstFields.Add(BytesToString(arrBytes, lSeparatorPos + 1, (i - 2) - lSeparatorPos, "UTF-8"))
c = c + 1
Else
bExitLoop = True
End If
Else
If i - lSeparatorPos > 1 Then
lstFields.Add(BytesToString(arrBytes, lSeparatorPos + 1, (i - 1) - lSeparatorPos, "UTF-8"))
c = c + 1
Else
bExitLoop = True
End If
End If
Else 'If lSeparatorPos > -1
If lChr10Pos > -1 Then
If arrBytes(i - 1) = 13 Then
lstFields.Add(BytesToString(arrBytes, lChr10Pos + 1, (i - 2) - lChr10Pos, "UTF-8"))
Else
lstFields.Add(BytesToString(arrBytes, lChr10Pos + 1, (i - 1) - lChr10Pos, "UTF-8"))
End If
c = c + 1
Else
If arrBytes(i - 2) <> btEncloserByte Then
If arrBytes(i - 1) = 13 Then
lstFields.Add(BytesToString(arrBytes, 0, i - 1, "UTF-8"))
Else
lstFields.Add(BytesToString(arrBytes, 0, i, "UTF-8"))
End If
c = c + 1
End If
End If
End If 'If lSeparatorPos > -1
bExitLoop = True
End If 'If lEncloserPos = -1
End Select
If bExitLoop Then Exit
Next
Dim arrFields(lstFields.Size) As String
For i = 0 To lstFields.Size - 1
arrFields(i) = lstFields.Get(i)
Next
Return arrFields
End Sub
Sub GetCSVFirstDataByte(arrBytes() As Byte, btEndOfLineByte As Byte) As Long
Dim i As Long
For i = 0 To arrBytes.Length - 1
If arrBytes(i) = btEndOfLineByte Then
Return i + 1
End If
Next
Return -1
End Sub
Sub CSVHasEncloserBytes(arrBytes() As Byte, btEncloserByte As Byte) As Boolean
Dim i As Long
Dim lUB As Long
lUB = arrBytes.Length - 1
For i = 0 To lUB
If arrBytes(i) = btEncloserByte Then
Return True
End If
Next
Return False
End Sub
RBS