summaryrefslogtreecommitdiffstats
path: root/source/core/slang-text-io.h
diff options
context:
space:
mode:
authorjsmall-nvidia <jsmall@nvidia.com>2021-10-04 14:15:51 -0400
committerGitHub <noreply@github.com>2021-10-04 14:15:51 -0400
commit97bb82ebcdf8f1391b9d93b5a8d7b1dfc4e88e52 (patch)
treef120ba282cbea96d23ed179737984a4610d3b520 /source/core/slang-text-io.h
parentb3dfe383c6d31ff3dbd76dcfb32de8d536382f3e (diff)
Removing exceptions from core/compiler-core (#1953)
* #include an absolute path didn't work - because paths were taken to always be relative. * Refactor Stream. Working on all tests. * Split out CharEncode. * Make method names lower camel. m_prefix in Writer/Reader * Tidy up around CharEncode interface. * Small improvements around encode/decode. * Better use of types. * Remove readLine from TextReader. * Remove exceptions from Stream/Text handling. * Fix some typos. * Fix tabbing. * Fix missing override. * Remove remaining exception throw/catch via using signal mechanism. * Remove exceptions that are not used anymore. * Document the Stream interface. * Remove index for decoding 'get byte' function. * Fix CharReader -> ByteReader.
Diffstat (limited to 'source/core/slang-text-io.h')
-rw-r--r--source/core/slang-text-io.h434
1 files changed, 152 insertions, 282 deletions
diff --git a/source/core/slang-text-io.h b/source/core/slang-text-io.h
index b5a9ad0e1..0ee381c46 100644
--- a/source/core/slang-text-io.h
+++ b/source/core/slang-text-io.h
@@ -3,314 +3,184 @@
#include "slang-secure-crt.h"
#include "slang-stream.h"
+#include "slang-char-encode.h"
namespace Slang
{
- using Slang::List;
- using Slang::_EndLine;
+using Slang::List;
+using Slang::_EndLine;
- class TextReader
- {
- protected:
- char decodedChar[5];
- int decodedCharPtr = 0, decodedCharSize = 0;
- virtual void ReadChar() = 0;
- public:
- virtual ~TextReader()
- {
- Close();
- }
- virtual void Close(){}
- virtual String ReadLine()=0;
- virtual String ReadToEnd()=0;
- virtual bool IsEnd() = 0;
- int Read(char * buffer, int count);
- char Read()
- {
- if (decodedCharPtr == decodedCharSize)
- ReadChar();
- if (decodedCharPtr < decodedCharSize)
- return decodedChar[decodedCharPtr++];
- else
- return 0;
- }
- char Peak()
- {
- if (decodedCharPtr == decodedCharSize)
- ReadChar();
- if (decodedCharPtr < decodedCharSize)
- return decodedChar[decodedCharPtr];
- else
- return 0;
- }
- };
-
- class TextWriter
- {
- public:
- virtual ~TextWriter()
- {
- Close();
- }
- virtual void Write(const String & str)=0;
- virtual void Write(const char * str)=0;
- virtual void Close(){}
- template<typename T>
- TextWriter & operator << (const T& val)
- {
- Write(val.ToString());
- return *this;
- }
- TextWriter & operator << (int value)
- {
- Write(String(value));
- return *this;
- }
- TextWriter & operator << (float value)
- {
- Write(String(value));
- return *this;
- }
- TextWriter & operator << (double value)
- {
- Write(String(value));
- return *this;
- }
- TextWriter & operator << (const char* value)
- {
- Write(value);
- return *this;
- }
- TextWriter & operator << (const String & val)
- {
- Write(val);
- return *this;
- }
- TextWriter & operator << (const _EndLine &)
- {
-#ifdef _WIN32
- Write("\r\n");
-#else
- Write("\n");
-#endif
- return *this;
- }
- };
+class TextReader
+{
+public:
+ virtual void close(){}
+ virtual SlangResult readToEnd(String& outString) = 0;
+ virtual bool isEnd() = 0;
- template <typename ReadCharFunc>
- int GetUnicodePointFromUTF8(const ReadCharFunc & get)
+ char read()
{
- int codePoint = 0;
- int leading = get(0);
- int mask = 0x80;
- int count = 0;
- while (leading & mask)
- {
- count++;
- mask >>= 1;
- }
- codePoint = (leading & (mask - 1));
- for (int i = 1; i <= count - 1; i++)
- {
- codePoint <<= 6;
- codePoint += (get(i) & 0x3F);
- }
- return codePoint;
+ if (m_decodedCharIndex == m_decodedCharSize)
+ readChar();
+ if (m_decodedCharIndex < m_decodedCharSize)
+ return m_decodedChar[m_decodedCharIndex++];
+ else
+ return 0;
}
-
- template <typename ReadCharFunc>
- int GetUnicodePointFromUTF16(const ReadCharFunc & get)
+ char peek()
{
- int byte0 = (unsigned char)get(0);
- int byte1 = (unsigned char)get(1);
- int word0 = byte0 + (byte1 << 8);
- if (word0 >= 0xD800 && word0 <= 0xDFFF)
- {
- int byte2 = (unsigned char)get(2);
- int byte3 = (unsigned char)get(3);
- int word1 = byte2 + (byte3 << 8);
- return ((word0 & 0x3FF) << 10) + (word1 & 0x3FF) + 0x10000;
- }
+ if (m_decodedCharIndex == m_decodedCharSize)
+ readChar();
+ if (m_decodedCharIndex < m_decodedCharSize)
+ return m_decodedChar[m_decodedCharIndex];
else
- return word0;
+ return 0;
}
- template <typename ReadCharFunc>
- int GetUnicodePointFromUTF16Reversed(const ReadCharFunc & get)
+ virtual ~TextReader() { close(); }
+
+protected:
+ char m_decodedChar[5];
+ Index m_decodedCharIndex = 0;
+ Index m_decodedCharSize = 0;
+
+ virtual void readChar() = 0;
+};
+
+
+class StreamReader : public TextReader
+{
+public:
+ virtual SlangResult readToEnd(String& outString) SLANG_OVERRIDE;
+ virtual bool isEnd() SLANG_OVERRIDE { return m_index == m_buffer.getCount() && m_stream->isEnd(); }
+ virtual void close() SLANG_OVERRIDE { m_stream->close(); }
+
+ void releaseStream() { m_stream.setNull(); }
+
+ StreamReader();
+
+ SlangResult init(const String& path);
+ SlangResult init(RefPtr<Stream> stream, CharEncoding* encoding = nullptr);
+
+protected:
+ virtual void readChar() SLANG_OVERRIDE
+ {
+ m_decodedCharIndex = 0;
+
+ Char32 codePoint = 0;
+ switch (m_encodingType)
+ {
+ case CharEncodeType::UTF8:
+ {
+ codePoint = getUnicodePointFromUTF8([&]() -> Byte {return readBufferChar(); });
+ break;
+ }
+ case CharEncodeType::UTF16:
+ {
+ codePoint = getUnicodePointFromUTF16([&]() -> Byte {return readBufferChar(); });
+ break;
+ }
+ case CharEncodeType::UTF16Reversed:
+ {
+ codePoint = getUnicodePointFromUTF16Reversed([&]() -> Byte {return readBufferChar(); });
+ break;
+ }
+ case CharEncodeType::UTF32:
+ {
+ codePoint = getUnicodePointFromUTF32([&]() -> Byte {return readBufferChar(); });
+ break;
+ }
+ }
+
+ m_decodedCharSize = encodeUnicodePointToUTF8(codePoint, m_decodedChar);
+ }
+
+private:
+ char readBufferChar();
+ SlangResult readBuffer();
+
+ RefPtr<Stream> m_stream;
+ List<char> m_buffer;
+
+ CharEncodeType m_encodingType = CharEncodeType::UTF8;
+ CharEncoding* m_encoding = nullptr;
+ Index m_index = 0; ///< Index into buffer
+};
+
+class TextWriter
+{
+public:
+
+ virtual SlangResult writeSlice(const UnownedStringSlice& slice) = 0;
+ virtual void close(){}
+
+ SlangResult write(const UnownedStringSlice& slice) { return writeSlice(slice); }
+ SlangResult write(const char* str) { return writeSlice(UnownedStringSlice(str)); }
+ SlangResult write(const String& str) { return writeSlice(str.getUnownedSlice()); }
+
+ virtual ~TextWriter() { close(); }
+
+ template<typename T>
+ TextWriter& operator << (const T& val)
{
- int byte0 = (unsigned char)get(0);
- int byte1 = (unsigned char)get(1);
- int word0 = (byte0 << 8) + byte1;
- if (word0 >= 0xD800 && word0 <= 0xDFFF)
- {
- int byte2 = (unsigned char)get(2);
- int byte3 = (unsigned char)get(3);
- int word1 = (byte2 << 8) + byte3;
- return ((word0 & 0x3FF) << 10) + (word1 & 0x3FF);
- }
- else
- return word0;
+ write(val.ToString());
+ return *this;
}
-
- template <typename ReadCharFunc>
- int GetUnicodePointFromUTF32(const ReadCharFunc & get)
+ TextWriter& operator << (int value)
{
- int byte0 = (unsigned char)get(0);
- int byte1 = (unsigned char)get(1);
- int byte2 = (unsigned char)get(2);
- int byte3 = (unsigned char)get(3);
- return byte0 + (byte1 << 8) + (byte2 << 16) + (byte3 << 24);
+ write(String(value));
+ return *this;
}
-
- inline int EncodeUnicodePointToUTF8(char * buffer, int codePoint)
+ TextWriter& operator << (float value)
{
- int count = 0;
- if (codePoint <= 0x7F)
- buffer[count++] = ((char)codePoint);
- else if (codePoint <= 0x7FF)
- {
- unsigned char byte = (unsigned char)(0xC0 + (codePoint >> 6));
- buffer[count++] = ((char)byte);
- byte = 0x80 + (codePoint & 0x3F);
- buffer[count++] = ((char)byte);
- }
- else if (codePoint <= 0xFFFF)
- {
- unsigned char byte = (unsigned char)(0xE0 + (codePoint >> 12));
- buffer[count++] = ((char)byte);
- byte = (unsigned char)(0x80 + ((codePoint >> 6) & (0x3F)));
- buffer[count++] = ((char)byte);
- byte = (unsigned char)(0x80 + (codePoint & 0x3F));
- buffer[count++] = ((char)byte);
- }
- else
- {
- unsigned char byte = (unsigned char)(0xF0 + (codePoint >> 18));
- buffer[count++] = ((char)byte);
- byte = (unsigned char)(0x80 + ((codePoint >> 12) & 0x3F));
- buffer[count++] = ((char)byte);
- byte = (unsigned char)(0x80 + ((codePoint >> 6) & 0x3F));
- buffer[count++] = ((char)byte);
- byte = (unsigned char)(0x80 + (codePoint & 0x3F));
- buffer[count++] = ((char)byte);
- }
- return count;
+ write(String(value));
+ return *this;
}
-
- inline int EncodeUnicodePointToUTF16(unsigned short * buffer, int codePoint)
+ TextWriter& operator << (double value)
{
- int count = 0;
- if (codePoint <= 0xD7FF || (codePoint >= 0xE000 && codePoint <= 0xFFFF))
- buffer[count++] = (unsigned short)codePoint;
- else
- {
- int sub = codePoint - 0x10000;
- int high = (sub >> 10) + 0xD800;
- int low = (sub & 0x3FF) + 0xDC00;
- buffer[count++] = (unsigned short)high;
- buffer[count++] = (unsigned short)low;
- }
- return count;
+ write(String(value));
+ return *this;
}
-
- inline unsigned short ReverseBitOrder(unsigned short val)
+ TextWriter& operator << (const char* value)
{
- int byte0 = val & 0xFF;
- int byte1 = val >> 8;
- return (unsigned short)(byte1 + (byte0 << 8));
+ writeSlice(UnownedStringSlice(value));
+ return *this;
}
-
- inline int EncodeUnicodePointToUTF16Reversed(unsigned short * buffer, int codePoint)
+ TextWriter& operator << (const String & val)
{
- int count = 0;
- if (codePoint <= 0xD7FF || (codePoint >= 0xE000 && codePoint <= 0xFFFF))
- buffer[count++] = ReverseBitOrder((unsigned short)codePoint);
- else
- {
- int sub = codePoint - 0x10000;
- int high = (sub >> 10) + 0xD800;
- int low = (sub & 0x3FF) + 0xDC00;
- buffer[count++] = ReverseBitOrder((unsigned short)high);
- buffer[count++] = ReverseBitOrder((unsigned short)low);
- }
- return count;
+ writeSlice(val.getUnownedSlice());
+ return *this;
}
-
- class Encoding
+ TextWriter& operator << (const _EndLine &)
{
- public:
- static Encoding * UTF8, * UTF16, *UTF16Reversed, * UTF32;
- virtual void GetBytes(List<char>& buffer, const String & str) = 0;
- virtual String ToString(const char * buffer, int length) = 0;
- virtual ~Encoding()
- {}
- };
+#ifdef _WIN32
+ writeSlice(UnownedStringSlice::fromLiteral("\r\n"));
+#else
+ writeSlice(UnownedStringSlice::fromLiteral("\n"));
+#endif
+ return *this;
+ }
+};
+
+class StreamWriter : public TextWriter
+{
+public:
+ // TextWriter
+ virtual SlangResult writeSlice(const UnownedStringSlice& slice) SLANG_OVERRIDE;
+ virtual void close() SLANG_OVERRIDE { m_stream->close(); }
- class StreamWriter : public TextWriter
- {
- private:
- List<char> encodingBuffer;
- RefPtr<Stream> stream;
- Encoding * encoding;
- public:
- StreamWriter(const String & path, Encoding * encoding = Encoding::UTF8);
- StreamWriter(RefPtr<Stream> stream, Encoding * encoding = Encoding::UTF8);
- virtual void Write(const String & str);
- virtual void Write(const char * str);
- virtual void Close()
- {
- stream->close();
- }
- void ReleaseStream()
- {
- stream = 0;
- }
- };
+ void releaseStream() { m_stream.setNull(); }
+
+ StreamWriter() {}
+
+ SlangResult init(const String& path, CharEncoding* encoding = CharEncoding::UTF8);
+ SlangResult init(RefPtr<Stream> stream, CharEncoding* encoding = CharEncoding::UTF8);
+
+private:
+ List<Byte> m_encodingBuffer;
+ RefPtr<Stream> m_stream;
+ CharEncoding* m_encoding = nullptr;
+};
- class StreamReader : public TextReader
- {
- private:
- RefPtr<Stream> stream;
- List<char> buffer;
- Encoding * encoding;
- Index ptr;
- char ReadBufferChar();
- void ReadBuffer();
-
- Encoding * DetermineEncoding();
- protected:
- virtual void ReadChar()
- {
- decodedCharPtr = 0;
- int codePoint = 0;
- if (encoding == Encoding::UTF8)
- codePoint = GetUnicodePointFromUTF8([&](int) {return ReadBufferChar(); });
- else if (encoding == Encoding::UTF16)
- codePoint = GetUnicodePointFromUTF16([&](int) {return ReadBufferChar(); });
- else if (encoding == Encoding::UTF16Reversed)
- codePoint = GetUnicodePointFromUTF16Reversed([&](int) {return ReadBufferChar(); });
- else if (encoding == Encoding::UTF32)
- codePoint = GetUnicodePointFromUTF32([&](int) {return ReadBufferChar(); });
- decodedCharSize = EncodeUnicodePointToUTF8(decodedChar, codePoint);
- }
- public:
- StreamReader(const String & path);
- StreamReader(RefPtr<Stream> stream, Encoding * encoding = nullptr);
- virtual String ReadLine();
- virtual String ReadToEnd();
- virtual bool IsEnd()
- {
- return ptr == buffer.getCount() && stream->isEnd();
- }
- virtual void Close()
- {
- stream->close();
- }
- void ReleaseStream()
- {
- stream = 0;
- }
- };
}
#endif