3 #ifndef INCLUDE_NN_NLIB_TEXTREADER_H_
4 #define INCLUDE_NN_NLIB_TEXTREADER_H_
26 if (m_Cur == m_BufEnd) {
28 if (m_Cur == m_BufEnd)
return -1;
30 int rval = *
reinterpret_cast<unsigned char*
>(m_Cur);
34 m_BegLinePos = m_BasePos + (m_Cur - &m_Buf[0]);
39 if (!m_Utf32Cache) ConstructUtf32Cache();
41 rval =
static_cast<int>(m_Utf32Cache);
46 if (m_Cur == m_BufEnd) {
48 if (m_Cur == m_BufEnd)
return -1;
50 int rval = *
reinterpret_cast<unsigned char*
>(m_Cur);
51 if (!(rval & 0x80))
return rval;
52 if (!m_Utf32Cache) ConstructUtf32Cache();
53 return static_cast<int>(m_Utf32Cache);
57 if (m_Cur + 1 < m_BufEnd) {
58 unsigned char c = *
reinterpret_cast<unsigned char*
>(m_Cur);
59 if (c >
' ')
return 0;
62 c = *
reinterpret_cast<unsigned char*
>(m_Cur);
67 return this->SkipWs_(1);
70 return this->SkipWs_(0);
75 return this->ReadUntil(len, &buf[0], N, delim);
78 bool ReadUntil(
size_t* len,
char* buf,
size_t n, T pred)
NLIB_NOEXCEPT;
79 template <
class T,
size_t N>
80 bool ReadUntil(
size_t* len,
char (&buf)[N], T pred) NLIB_NOEXCEPT {
81 return this->ReadUntil(len, buf, N, pred);
83 size_t ReadDecimalString(
char* buf,
size_t n) NLIB_NOEXCEPT
NLIB_NONNULL;
86 return this->ReadDecimalString(buf, N);
88 bool Proceed(
const char* str,
size_t n) NLIB_NOEXCEPT
NLIB_NONNULL;
90 NLIB_ASSERT(!(c & 0x80) && c != 0x0A);
91 if (m_Cur == m_BufEnd) {
93 if (m_Cur == m_BufEnd)
return false;
95 if (*m_Cur != c)
return false;
101 int ReadAsUtf8(
char* b0,
char* b1,
char* b2,
char* b3) NLIB_NOEXCEPT {
102 if (m_Cur == m_BufEnd) {
104 if (m_Cur == m_BufEnd)
return 0;
112 m_BegLinePos = m_BasePos + (m_Cur - &m_Buf[0]);
138 int utf32 = this->
Read();
142 int utf32 = this->Peek();
145 bool Close() NLIB_NOEXCEPT;
146 void SetError(
errno_t e) const NLIB_NOEXCEPT {
147 if (m_ErrorValue == 0) m_ErrorValue = e;
151 int GetLine() const NLIB_NOEXCEPT {
return m_Line; }
153 return static_cast<int>((m_Cur - &m_Buf[0]) + 1 + m_BasePos - m_BegLinePos);
158 char* GetCur() NLIB_NOEXCEPT {
return m_Cur; }
159 char* GetBufEnd() NLIB_NOEXCEPT {
return m_BufEnd; }
160 void SetBufEnd(
char* p) NLIB_NOEXCEPT { m_BufEnd = p; }
163 virtual void FillBuffer_() NLIB_NOEXCEPT;
166 void FillBuffer() NLIB_NOEXCEPT { this->FillBuffer_(); }
168 void ConstructUtf32Cache() NLIB_NOEXCEPT;
169 int SkipWs_(
int base) NLIB_NOEXCEPT;
172 char m_Buf[512 + 3 + 1];
177 InputStream* m_Stream;
187 bool TextReader::ReadUntil(
size_t* len,
char* buf,
size_t n, T pred) NLIB_NOEXCEPT {
195 char* pend = buf + n;
197 if (m_Cur == m_BufEnd) {
199 if (m_Cur == m_BufEnd) {
205 while (p != pend && m_Cur != m_BufEnd) {
206 if ((*reinterpret_cast<unsigned char*>(m_Cur) & 0xC0) != 0x80 &&
207 pred((
const char*)m_Cur)) {
216 if (m_Cur != m_BufEnd && (*reinterpret_cast<unsigned char*>(m_Cur) & 0xC0) == 0x80) {
220 }
while ((*reinterpret_cast<unsigned char*>(m_Cur) & 0xC0) == 0x80);
230 #endif // INCLUDE_NN_NLIB_TEXTREADER_H_
#define NLIB_NOEXCEPT
環境に合わせてnoexcept 又は同等の定義がされます。
bool ReadUntil(size_t *len, char(&buf)[N], char delim) noexcept
ReadUntil(len, buf, N, delim) を呼び出します。
int PeekAsUtf16(nlib_utf16_t *upper, nlib_utf16_t *lower) noexcept
ストリームの先頭1コードポイントを、upper, lower にUTF-16で格納します。
errno_t GetErrorValue() const noexcept
読み込みが失敗した際に、エラーの原因を取得できます。
#define NLIB_DISALLOW_COPY_AND_ASSIGN(TypeName)
TypeName で指定されたクラスのコピーコンストラクタと代入演算子を禁止します。
#define NLIB_SAFE_BOOL(class_name, exp)
クラス内に安全なoperator bool()を定義します。 可能であればC++11のexplicit boolを利用します。 ...
int Read() noexcept
ストリームから1文字を読み込み、UTF-32で返します。
InputStream * GetStream() noexcept
テキストリーダーが書き込みを行うストリームを取得します。
size_t ReadDecimalString(char(&buf)[N]) noexcept
ReadDecimalString(buf, N) を呼び出します。
bool Proceed(char c) noexcept
文字c の分だけストリームを進めます。
int ReadAsUtf16(nlib_utf16_t *upper, nlib_utf16_t *lower) noexcept
ストリームから1コードポイントを読み込み、upper, lower にUTF-16で格納します。
int GetLine() const noexcept
現在の行番号を取得します。
int ReadAsUtf8(char *b0, char *b1, char *b2, char *b3) noexcept
ストリームから1コードポイントを読み込み、b0, b1, b2, b3 にUTF-8で格納します。
bool ReadUntil(size_t *len, char(&buf)[N], T pred) noexcept
ReadUntil(len, buf, N, pred) を呼び出します。
int GetColumn() const noexcept
現在の桁を取得します。
int SkipWs() noexcept
ストリーム内の空白(スペース,改行,タブ,復帰)文字を読み飛ばして、読み飛ばした空白の数を返します。 ...
bool Read(BinaryReader *r, T *x)
この関数テンプレートを特殊化することで、ユーザー定義クラスに読み込むことができます。 ...
int Peek() noexcept
ストリームの先頭の1文字を、UTF-32で返します。