diff options
| author | Yong He <yonghe@outlook.com> | 2017-11-05 16:39:38 -0500 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2017-11-05 16:39:38 -0500 |
| commit | 296e89ca4f3d6d99126bf2ee59666bc946add431 (patch) | |
| tree | bff41e36c9b6843d83a5ca5e83645310be6687f3 /source/core/text-io.cpp | |
| parent | c6fb1de9547bd24a693915b758cc35499f1d949f (diff) | |
| parent | ff7c46a11787ca6ecebf0a224772a41efef33fc0 (diff) | |
Merge pull request #243 from csyonghe/master
Adding associated types
Diffstat (limited to 'source/core/text-io.cpp')
| -rw-r--r-- | source/core/text-io.cpp | 30 |
1 files changed, 17 insertions, 13 deletions
diff --git a/source/core/text-io.cpp b/source/core/text-io.cpp index ec41b8d7d..d0d0a3cc6 100644 --- a/source/core/text-io.cpp +++ b/source/core/text-io.cpp @@ -194,6 +194,17 @@ namespace Slang this->encoding = determinedEncoding; } + bool HasNullBytes(char * str, int len) + { + bool hasSeenNull = false; + for (int i = 0; i < len - 1; i++) + if (str[i] == 0) + hasSeenNull = true; + else if (hasSeenNull) + return true; + return false; + } + Encoding * StreamReader::DetermineEncoding() { if (buffer.Count() >= 3 && (unsigned char)(buffer[0]) == 0xEF && (unsigned char)(buffer[1]) == 0xBB && (unsigned char)(buffer[2]) == 0xBF) @@ -213,19 +224,11 @@ namespace Slang } else { -#ifdef _WIN32 - int flag = IS_TEXT_UNICODE_SIGNATURE | IS_TEXT_UNICODE_REVERSE_SIGNATURE | IS_TEXT_UNICODE_STATISTICS | IS_TEXT_UNICODE_ASCII16; - int rs = IsTextUnicode(buffer.Buffer(), (int) buffer.Count(), &flag); - if (rs) - { - if (flag & (IS_TEXT_UNICODE_SIGNATURE | IS_TEXT_UNICODE_STATISTICS)) - return Encoding::UTF16; - else if (flag & (IS_TEXT_UNICODE_SIGNATURE | IS_TEXT_UNICODE_STATISTICS)) - return Encoding::UTF16Reversed; - else if (flag & IS_TEXT_UNICODE_ASCII16) - return Encoding::UTF8; - } -#endif + // find null bytes + if (HasNullBytes(buffer.Buffer(), (int)buffer.Count())) + { + return Encoding::UTF16; + } return Encoding::UTF8; } } @@ -233,6 +236,7 @@ namespace Slang void StreamReader::ReadBuffer() { buffer.SetSize(4096); + memset(buffer.Buffer(), 0, buffer.Count() * sizeof(buffer[0])); auto len = stream->Read(buffer.Buffer(), buffer.Count()); buffer.SetSize((int)len); ptr = 0; |
