DotNetDupe 4.0.6
C++17/20 Implementation of the .NET Base Class Library (BCL)
Loading...
Searching...
No Matches
StringReader.cpp
Go to the documentation of this file.
1#include "pch.h"
3#include <algorithm>
4
5namespace DotNetDupe {
6 namespace System {
7 namespace IO {
9 : m_sSource(sSource), m_iPos(0), m_nLength(sSource.GetLength()) {
10 }
11
13 m_iPos = m_nLength;
14 }
15
17 m_iPos = m_nLength;
18 }
19
22 if (m_iPos >= m_nLength) return -1;
23 return (int)(unsigned char)m_sSource[m_iPos];
24 }
25
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
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
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(const String &sSource)
Initializes a new instance of the StringReader class that reads from the specified string.
void Close() override
Closes the StringReader.
String ReadLine() override
Reads a line of characters from the current string and returns the data as a string.
int Peek() override
Returns the next available character but does not consume it.
String ReadToEnd() override
Reads all characters from the current position to the end of the string and returns them as a single ...
void Dispose() override
Releases all resources used by the StringReader.
int Read() override
Reads the next character from the input string and advances the character position by one character.
Represents text as a sequence of UTF-8 code units with culture-invariant operations.
Definition String.h:74
String Substring(int iStartIndex) const
Definition String.cpp:659
String()
Initializes a new instance of the String class to an empty string.
Definition String.cpp:64
static int FindNewlineIndex(const String &sSource, int startPos, int length)
Find index of newline character in source string.