Sub CSV2List(strFolder As String, _
			 strFile As String, _
			 bFileHasFieldNames As Boolean, _
			 bAddFieldNamesToList As Boolean, _
             btSeparatorByte As Byte, _
             btEncloserByte As Byte, _
			 bLookForEncloserByte As Boolean, _
             btEndOfLineByte As Byte, _
             lRows As Long, _
			 bReplaceBooleanTextToInt As Boolean, _
			 btDeleteByte As Byte) As ResumableSub

	Dim i As Long
	Dim c As Long
	Dim x As Int
	Dim arrBytes() As Byte
	Dim arrBytesFields() As Byte
	Dim lBytes As Long
	Dim arrFields() As String
	Dim lstFields As List
	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 lstRows As List
	Dim lFirstDataByte As Long
	Dim bExit As Boolean
	Dim bCSVHasEncloserBytes As Boolean

	RAF.Initialize(strFolder, strFile, True)
	lBytes = RAF.Size
	
	If iMaxTextFileSize = 0 Then 'this will be the case when setting up the menu array
		iMaxTextFileSize = iMaxTextFileSizeDefault
	End If

	'this will be a zero-based UTF8 byte array
	'-----------------------------------------
	If lBytes <= iMaxTextFileSize Then
		Dim arrBytes(lBytes) As Byte '<<<<<   this will cause an OOM crash with large files!!
	Else
		Dim rs As ResumableSub = Dialog.Show(mvPageRoot, Array As Object("OK"), _
							 "text file to list", "", 0, "", _
							 strFolder & "\" & CRLF & strFile & CRLF & CRLF & "This file is too large to handle!" & CRLF & "(" & Round(lBytes / 1000000) & " Mb)", _
							 -1, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, Null, False, 1)
		Wait For (rs) Complete (strResult As String)
		RAF.Close
		Return lstRows
	End If
	
	RAF.ReadBytes(arrBytes, 0, lBytes, 0)
	
	If btDeleteByte <> 0 Then
		arrBytes = AlterByteArray(arrBytes, -1, -1, btDeleteByte, 0, 0)
	End If
	
	lUB = arrBytes.Length - 1
	
	arrBytesFields = GetBytesForFields(arrBytes, btEndOfLineByte)
	
	For i = 0 To arrBytesFields.Length - 1
		If arrBytesFields(i) = 44 Then
			btSeparatorByte = 44
			Exit
		Else
			If arrBytesFields(i) = 9 Then
				btSeparatorByte = 9
				Exit
			End If
		End If
	Next
	
	lstFields = ParseCSVLine3(arrBytesFields, btSeparatorByte)
	Dim arrFields(lstFields.Size) As String
	
	For i = 0 To arrFields.Length - 1
		arrFields(i) = lstFields.Get(i)
		'Log(arrFields(i))
	Next
	
	lColumns = arrFields.Length
	Dim arrData(lColumns) As String
	
	lstRows.Initialize
	
	If bFileHasFieldNames Then
		If bAddFieldNamesToList Then
			lstRows.Add(arrFields) 'adding the column names
			lRow = lRow + 1
		End If
		'as we are skipping the the column names
		'---------------------------------------
		lFirstDataByte = GetCSVFirstDataByte(arrBytes, btEndOfLineByte)
	End If
	
	'so we ignore enclosers in the fields row!
	'-----------------------------------------
	If bLookForEncloserByte Then
		bCSVHasEncloserBytes = CSVHasEncloserBytes(arrBytes, lFirstDataByte, 0, btEncloserByte)
	Else
		If btEncloserByte = -1 Then
			bCSVHasEncloserBytes = False
		Else
			bCSVHasEncloserBytes = True
		End If
	End If

	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) = "" '<<<<<<<<< was 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 - lSeparatorPos > 1 Then
										arrData(c) = BytesToString(arrBytes, lSeparatorPos + 1, (i - 1) - lSeparatorPos, "UTF-8")
									Else    'If (i - 1) - (lSeparatorPos + 1) > 1
										arrData(c) = "" '<<<<<<<<< was 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) = "" '<<<<<<<<< was 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) = "" '<<<<<<<<< was 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) = "" '<<<<<<<<< was Null!
									End If
									c = c + 1
								End If    'If arrBytes(i - 1) = 13
							End If
							
							If bReplaceBooleanTextToInt Then
								For x = 0 To arrData.Length - 1
									If arrData(x) = "False" Then
										arrData(x) = 0
									Else
										If arrData(x) = "True" Then
											arrData(x) = 1
										End If
									End If
								Next
							End If
					
							lstRows.Add(arrData)
							
							lRow = lRow + 1
							lEncloserPos = -1
							lSeparatorPos = -1
							lChr10Pos = i
							c = 0
							Dim arrData(lColumns) As String
						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) = "" '<<<<<<<<< was 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) = "" '<<<<<<<<< was 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) = "" '<<<<<<<<< was 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) = "" '<<<<<<<<< was 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) = "" '<<<<<<<<< was 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
										'-------------------------------------------------------------------------------------
										'the -1 was added here as otherwise a Chr(13) could be added at the end of arrData(c)!
										'-------------------------------------------------------------------------------------
										arrData(c) = BytesToString(arrBytes, lSeparatorPos + 1, (lUB - lSeparatorPos) - 1, "UTF-8")
									Else
										arrData(c) = "" '<<<<<<<<< was 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 'If lRows = -1
		
		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) = "" '<<<<<<<<< was 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) = "" '<<<<<<<<< was 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) = "" '<<<<<<<<< was Null!
									End If
									c = c + 1
								End If    'If i - lSeparatorPos > 1
							End If    'If c < lColumns
							
							If bReplaceBooleanTextToInt Then
								For x = 0 To arrData.Length - 1
									If arrData(x) = "False" Then
										arrData(x) = 0
									Else
										If arrData(x) = "True" Then
											arrData(x) = 1
										End If
									End If
								Next
							End If
					
							lstRows.Add(arrData)
							lRow = lRow + 1
							lEncloserPos = -1
							lChr10Pos = i
							c = 0
							Dim arrData(lColumns) As String

							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) = "" '<<<<<<<<< was 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) = "" '<<<<<<<<< was Null!
										End If
									Else
										If (lUB - 1) - lChr10Pos > 0 Then
											arrData(c) = BytesToString(arrBytes, lChr10Pos + 1, (lUB - 1) - lChr10Pos, "UTF-8")
										Else
											arrData(c) = "" '<<<<<<<<< was 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  'If lRows = -1
		End If 'If lColumns > 1 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
		
	Else  'If bCSVHasEncloserBytes<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
		
		'''''''''''''''''''''''''''''
		'SEPARATORS BUT NO ENCLOSERS'
		'''''''''''''''''''''''''''''
		If lColumns > 1 Then '<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
			Select Case arrBytes(lFirstDataByte)
				Case btSeparatorByte
					arrData(c) = "" '<<<<<<<<< was 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 - lSeparatorPos > 1 Then
									arrData(c) = BytesToString(arrBytes, lSeparatorPos + 1, (i - 1) - lSeparatorPos, "UTF-8")
								Else    'If (i - 1) - (lSeparatorPos + 1) > 1
									arrData(c) = "" '<<<<<<<<< was 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) = "" '<<<<<<<<< was 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) = "" '<<<<<<<<< was 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) = "" '<<<<<<<<< was 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) = "" '<<<<<<<<< was Null!
									End If
									c = c + 1
								End If    'If i - lSeparatorPos > 1
							End If    'If lSeparatorPos > -1
							
							If bReplaceBooleanTextToInt Then
								For x = 0 To arrData.Length - 1
									If arrData(x) = "False" Then
										arrData(x) = 0
									Else
										If arrData(x) = "True" Then
											arrData(x) = 1
										End If
									End If
								Next
							End If
					
							lstRows.Add(arrData)
							lRow = lRow + 1
							lSeparatorPos = -1
							lChr10Pos = i
							c = 0
							Dim arrData(lColumns) As String

							If lRows > -1 Then
								If lRow > lRows Then
									bExit = True
								End If
							End If 'If lRows > -1
						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) = "" '<<<<<<<<< was Null!
								Else    'If lUB - lChr10Pos = 1
									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) = "" '<<<<<<<<< was 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) = "" '<<<<<<<<< was 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) = "" '<<<<<<<<< was Null!
											End If
										Else
											If (lUB - 1) - lChr10Pos > 0 Then
												arrData(c) = BytesToString(arrBytes, lChr10Pos + 1, (lUB - 1) - lChr10Pos, "UTF-8")
											Else
												arrData(c) = "" '<<<<<<<<< was 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
									'-------------------------------------------------------------------------------------
									'the -1 was added here as otherwise a Chr(13) could be added at the end of arrData(c)!
									'-------------------------------------------------------------------------------------
									arrData(c) = BytesToString(arrBytes, lSeparatorPos + 1, (lUB - lSeparatorPos) - 1, "UTF-8")
								Else
									arrData(c) = "" '<<<<<<<<< was 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  'If lRows = -1
		
		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) = "" '<<<<<<<<< was 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) = "" '<<<<<<<<< was Null!
								End If
								c = c + 1
							End If    'If i - lSeparatorPos > 1
						End If    'If c < lColumns
						
						If bReplaceBooleanTextToInt Then
							For x = 0 To arrData.Length - 1
								If arrData(x) = "False" Then
									arrData(x) = 0
								Else
									If arrData(x) = "True" Then
										arrData(x) = 1
									End If
								End If
							Next
						End If
					
						lstRows.Add(arrData)
						lRow = lRow + 1
						lChr10Pos = i
						c = 0
						Dim arrData(lColumns) As String

						If lRows > -1 Then
							If lRow > lRows Then
								bExit = True
							End If
						End If  'If lRows > -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 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 'If lRows = -1
		End If 'If lColumns > 1 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
		
	End If 'If bCSVHasEncloserBytes<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
	
	'add final data array
	If bReplaceBooleanTextToInt Then
		For x = 0 To arrData.Length - 1
			If arrData(x) = "False" Then
				arrData(x) = 0
			Else
				If arrData(x) = "True" Then
					arrData(x) = 1
				End If
			End If
		Next
	End If
	
	lstRows.Add(arrData)
	
	Return lstRows
	
End Sub

Sub CSVHasEncloserBytes(arrBytes() As Byte, lFirstByte As Long, lBytesToCheck As Long, btEncloserByte As Byte) As Boolean
	
	Dim i As Long
	Dim lUB As Long
	
	If lBytesToCheck = 0 Then
		lUB = arrBytes.Length - 1
	Else
		lUB = lBytesToCheck - 1
		If lUB > arrBytes.Length -1 Then
			lUB = arrBytes.Length -1
		End If
	End If

	For i = lFirstByte To lUB
		If arrBytes(i) = btEncloserByte Then
			Return True
		End If
	Next
	
	Return False
	
End Sub

Sub AlterByteArray(arrBytes() As Byte, lStart As Long, lEnd As Long, _
				   btDelete As Byte, btAlterFrom As Byte, btAlterTo As Byte) As Byte()
				   
	Dim l As Long
	Dim n As Long
	Dim bAlterStartEnd As Boolean
	Dim lCountByte As Long
	
	If lStart = -1 Then
		lStart = 0
		bAlterStartEnd = True
	End If
	
	If lEnd = -1 Then
		lEnd = arrBytes.Length - 1
		bAlterStartEnd = True
	End If
	
	If 	bAlterStartEnd Then
		Dim arrBytes2((lEnd - lStart) + 1) As Byte
		For l = lStart To lEnd
			arrBytes2(l - lStart) = arrBytes(l)
		Next
	Else
		Dim arrBytes2() As Byte = arrBytes
	End If
	
	If btDelete <> 0 Then
		For l = 0 To arrBytes2.Length - 1
			If arrBytes2(l) = btDelete Then
				lCountByte = lCountByte + 1
			End If
		Next
		
		Dim arrBytes3(arrBytes2.Length - lCountByte) As Byte
		
		For l = 0 To arrBytes2.Length - 1
			If arrBytes2(l) <> btDelete Then
				arrBytes3(n) = arrBytes2(l)
				n = n + 1
			End If
		Next
	Else
		Dim arrBytes3() As Byte = arrBytes2
	End If
	
	If btAlterFrom <> 0 Then
		For l = 0 To arrBytes3.Length - 1
			If arrBytes3(l) = btAlterFrom Then
				arrBytes3(l) = btAlterTo
			End If
		Next
	End If
	
	Return arrBytes3
	
End Sub

Sub GetBytesForFields(arrBytes() As Byte, btEndOfLineByte As Byte) As Byte()
	
	Dim i As Int
	Dim iLastByteIndex As Int
	
	For i = 0 To arrBytes.Length - 1
		If arrBytes(i) = btEndOfLineByte Then
			If arrBytes(i - 1) = 13 Then
				iLastByteIndex = i - 2
			Else
				iLastByteIndex = i - 1
			End If
			Exit
		End If
	Next
	
	Dim arrBytes2(iLastByteIndex + 1) As Byte
	
	For i = 0 To iLastByteIndex
		arrBytes2(i) = arrBytes(i)
	Next
	
	Return arrBytes2
	
End Sub

Sub ParseCSVLine3(arrBytes() As Byte, btSeparatorByte As Byte) As List
	
	Dim lstValues As List
	Dim iStartIndex As Int
	Dim iEndIndex As Int
	Dim insideQuotes As Boolean = False
		
	lstValues.Initialize
        
	For iEndIndex = 0 To arrBytes.Length - 1
		'34 is double quote
		If arrBytes(iEndIndex) = 34 Then
			insideQuotes = insideQuotes = False
		Else
			If arrBytes(iEndIndex) = btSeparatorByte Then
				If insideQuotes = False Then
					lstValues.add(BytesToString(arrBytes, iStartIndex, iEndIndex - iStartIndex, "UTF-8"))
					iStartIndex = iEndIndex + 1
				End If
			End If
		End If
	Next
	
	lstValues.add(BytesToString(arrBytes, iStartIndex, iEndIndex - iStartIndex, "UTF-8"))
	
	Return lstValues
	
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

