DotNetDupe
4.0.6
C++17/20 Implementation of the .NET Base Class Library (BCL)
Toggle main menu visibility
Loading...
Searching...
No Matches
StringReader.cpp
Go to the documentation of this file.
1
#include "
pch.h
"
2
#include "
System/IO/StringReader.h
"
3
#include <algorithm>
4
5
namespace
DotNetDupe
{
6
namespace
System
{
7
namespace
IO
{
8
StringReader::StringReader
(
const
String
& sSource)
9
: m_sSource(sSource), m_iPos(0), m_nLength(sSource.GetLength()) {
10
}
11
12
void
StringReader::Close
() {
13
m_iPos = m_nLength;
14
}
15
16
void
StringReader::Dispose
() {
17
m_iPos = m_nLength;
18
}
19
20
int
StringReader::Peek
() {
22
if
(m_iPos >= m_nLength)
return
-1;
23
return
(
int
)(
unsigned
char)m_sSource[m_iPos];
24
}
25
26
int
StringReader::Read
() {
28
if
(m_iPos >= m_nLength)
return
-1;
29
return
(
int
)(
unsigned
char)m_sSource[m_iPos++];
30
}
31
32
int
StringReader::Read
(
char
* pBuffer,
int
iIndex,
int
nCount) {
34
if
(pBuffer ==
nullptr
)
return
0;
35
37
int
nRead = (std::min)(nCount, m_nLength - m_iPos);
38
for
(
int
i = 0; i < nRead; i++) {
39
pBuffer[iIndex + i] = (char)m_sSource[m_iPos + i];
40
}
41
m_iPos += nRead;
42
return
nRead;
43
}
44
46
static
int
FindNewlineIndex
(
const
String
& sSource,
int
startPos,
int
length) {
47
for
(
int
i = startPos; i < length; ++i) {
48
char
c = (char)sSource[i];
49
if
(c ==
'\r'
|| c ==
'\n'
)
return
i;
50
}
51
return
-1;
52
}
53
54
String
StringReader::ReadLine
() {
56
if
(m_iPos >= m_nLength)
return
String
(
""
);
57
59
int
nlIdx =
FindNewlineIndex
(m_sSource, m_iPos, m_nLength);
60
if
(nlIdx != -1) {
61
String
sResult = m_sSource.
Substring
(m_iPos, nlIdx - m_iPos);
62
char
c = (char)m_sSource[nlIdx];
63
m_iPos = nlIdx + 1;
64
if
(c ==
'\r'
&& m_iPos < m_nLength && (
char
)m_sSource[m_iPos] ==
'\n'
) m_iPos++;
65
return
sResult;
66
}
67
69
String
sResult = m_sSource.
Substring
(m_iPos, m_nLength - m_iPos);
70
m_iPos = m_nLength;
71
return
sResult;
72
}
73
74
String
StringReader::ReadToEnd
() {
76
if
(m_iPos >= m_nLength)
return
String
(
""
);
77
79
String
sResult = m_sSource.
Substring
(m_iPos, m_nLength - m_iPos);
80
m_iPos = m_nLength;
81
return
sResult;
82
}
83
}
84
}
85
}
StringReader.h
DotNetDupe::System::IO::StringReader::StringReader
StringReader(const String &sSource)
Initializes a new instance of the StringReader class that reads from the specified string.
Definition
StringReader.cpp:8
DotNetDupe::System::IO::StringReader::Close
void Close() override
Closes the StringReader.
Definition
StringReader.cpp:12
DotNetDupe::System::IO::StringReader::ReadLine
String ReadLine() override
Reads a line of characters from the current string and returns the data as a string.
Definition
StringReader.cpp:54
DotNetDupe::System::IO::StringReader::Peek
int Peek() override
Returns the next available character but does not consume it.
Definition
StringReader.cpp:20
DotNetDupe::System::IO::StringReader::ReadToEnd
String ReadToEnd() override
Reads all characters from the current position to the end of the string and returns them as a single ...
Definition
StringReader.cpp:74
DotNetDupe::System::IO::StringReader::Dispose
void Dispose() override
Releases all resources used by the StringReader.
Definition
StringReader.cpp:16
DotNetDupe::System::IO::StringReader::Read
int Read() override
Reads the next character from the input string and advances the character position by one character.
Definition
StringReader.cpp:26
DotNetDupe::System::String
Represents text as a sequence of UTF-8 code units with culture-invariant operations.
Definition
String.h:74
DotNetDupe::System::String::Substring
String Substring(int iStartIndex) const
Definition
String.cpp:659
DotNetDupe::System::String::String
String()
Initializes a new instance of the String class to an empty string.
Definition
String.cpp:64
DotNetDupe::System::IO
Definition
Console.h:17
DotNetDupe::System::IO::FindNewlineIndex
static int FindNewlineIndex(const String &sSource, int startPos, int length)
Find index of newline character in source string.
Definition
StringReader.cpp:46
DotNetDupe::System
Definition
Action.h:11
DotNetDupe
Definition
IServiceCollection.h:7
pch.h
DotNetDupe
StringReader.cpp
Generated by
1.18.0