Skip to content

Commit 6c397cc

Browse files
author
A9G-Data-Droid
committed
Move Select outside of loop.
Add comments.
1 parent ee06023 commit 6c397cc

5 files changed

Lines changed: 49 additions & 22 deletions

File tree

TestLineEndingConversion/IntegrationTesting.vb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ Imports System.IO
22
Imports Microsoft.VisualStudio.TestTools.UnitTesting
33
Imports TextFileConvert
44

5+
6+
57
Namespace TestLineEndingConversion
68
<TestClass>
79
Public Class IntegrationTesting

TextFileConvert/ConvertLineEndings.vb

Lines changed: 40 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
Imports System.IO
22
Imports System.Text
33

4+
5+
46
Public Class ConvertLineEndings
57
''' <summary>
68
''' These are the different conversions that this library can perform.
@@ -37,10 +39,10 @@ Public Class ConvertLineEndings
3739
''' <summary>
3840
''' Loads a whole text file in to memory, Performs a find\replace, and writes a new file.
3941
''' </summary>
40-
''' <param name="originalFile">The file to convert.</param>
42+
''' <param name="originalFile">The file path to convert.</param>
4143
''' <param name="newFile">The name of a new file to create.</param>
42-
''' <param name="convertMode">This is the type of conversion we are going to perform</param>
43-
''' <returns>Exit code.</returns>
44+
''' <param name="convertMode">This is the type of conversion we are going to perform.</param>
45+
''' <returns>Exit code. 0 is success. -1 is a symbolic link.</returns>
4446
Private Shared Async Function ReplaceLineEndings(originalFile As String, newFile As String,
4547
convertMode As TextConvertMode) As Task(Of Integer)
4648

@@ -65,44 +67,62 @@ Public Class ConvertLineEndings
6567
Return ex.HResult
6668
End Try
6769

68-
Return 0 ' Exit status 0 is a good thing
70+
Return 0
6971
End Function
7072

73+
''' <summary>
74+
''' This is where the actual conversion logic lives.
75+
''' </summary>
76+
''' <param name="originalFile">The file you want to convert.</param>
77+
''' <param name="fileEncoding">The encoding you want to read that file as.</param>
78+
''' <param name="convertMode">The type of conversion you want to perform.</param>
79+
''' <returns>The full text of the file with new line endings.</returns>
7180
Private Shared Async Function GetConvertedText(originalFile As FileStream, fileEncoding As Encoding, convertMode As TextConvertMode) As Task(Of String)
72-
Const CR As Char = ChrW(13)
73-
Const LF As Char = ChrW(10)
81+
Const CR As Char = ChrW(13) ' Carriage Return
82+
Const LF As Char = ChrW(10) ' Line Feed
7483

7584
Dim convertedLines As New StringBuilder
7685
Using oldFile As New StreamReader(originalFile, fileEncoding, True)
77-
Do Until oldFile.EndOfStream ' Read through the whole file
78-
Dim readBuffer(0) As Char
79-
Dim readChars As Integer = Await oldFile.ReadAsync(readBuffer, 0, 1)
80-
If readChars >= 1 Then
81-
Select Case convertMode
82-
Case TextConvertMode.Dos2Ux
86+
Select Case convertMode
87+
Case TextConvertMode.Dos2Ux
88+
Do Until oldFile.EndOfStream
89+
Dim readBuffer(0) As Char
90+
Dim readChars As Integer = Await oldFile.ReadAsync(readBuffer, 0, 1)
91+
If readChars >= 1 Then
92+
' Look for Dos line endings
8393
If readBuffer(0) = CR AndAlso oldFile.Peek() = 10 Then
8494
' Strip out CR chars if followed by LF
8595
Await oldFile.ReadAsync(readBuffer, 0, 1)
8696
End If
87-
Case TextConvertMode.Ux2Dos
97+
98+
'Yield readBuffer
99+
convertedLines.Append(readBuffer)
100+
End If
101+
Loop
102+
Case TextConvertMode.Ux2Dos
103+
Do Until oldFile.EndOfStream
104+
Dim readBuffer(0) As Char
105+
Dim readChars As Integer = Await oldFile.ReadAsync(readBuffer, 0, 1)
106+
If readChars >= 1 Then
107+
' Check for CR first to avoid doubling the CR character when LF is found
88108
If readBuffer(0) = CR AndAlso oldFile.Peek() = 10 Then
89-
ReDim Preserve readBuffer(1)
90109
' This is a DOS line ending, keep it.
110+
ReDim Preserve readBuffer(1)
91111
Dim tempBuffer(1) As Char
92112
Await oldFile.ReadAsync(tempBuffer, 0, 1)
93113
readBuffer(1) = tempBuffer(0)
94114
ElseIf readBuffer(0) = LF Then
115+
' This is a Unix line ending. Add preceeding CR.
95116
ReDim readBuffer(1)
96-
' Add preceeding CR
97117
readBuffer(0) = CR
98118
readBuffer(1) = LF
99119
End If
100-
End Select
101120

102-
'Yield readBuffer
103-
convertedLines.Append(readBuffer)
104-
End If
105-
Loop
121+
'Yield readBuffer
122+
convertedLines.Append(readBuffer)
123+
End If
124+
Loop
125+
End Select
106126
End Using
107127

108128
Return convertedLines.ToString

TextFileConvert/common.vb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,10 @@ DESCRIPTION
4848

4949
''' <summary>
5050
''' Detect when the file in the given path is a symbolic link.
51+
''' WARNING: Could have false positive for any file with a reparse point that is not a symbolic link.
5152
''' </summary>
52-
''' <param name="path"></param>
53-
''' <returns>True if </returns>
53+
''' <param name="path">Full path to file to test.</param>
54+
''' <returns>True if Reparse Point is found.</returns>
5455
Public Function IsSymbolic(path As String) As Boolean
5556
Dim pathInfo As New FileInfo(path)
5657
Return pathInfo.Attributes.HasFlag(FileAttributes.ReparsePoint)

dos2ux/dos2ux.vb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
Imports System.IO
22
Imports TextFileConvert
33

4+
5+
46
Module Dos2Ux
57

68
'Public Async Function Main(args As String()) As Task(Of Integer)

ux2dos/ux2dos.vb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
Imports System.IO
22
Imports TextFileConvert
33

4+
5+
46
Module Ux2Dos
57

68
'Public Async Function Main(args As String()) As Task(Of Integer)

0 commit comments

Comments
 (0)