summaryrefslogtreecommitdiffstats
path: root/source/core/slang-string.cpp
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-string.cpp
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-string.cpp')
-rw-r--r--source/core/slang-string.cpp48
1 files changed, 26 insertions, 22 deletions
diff --git a/source/core/slang-string.cpp b/source/core/slang-string.cpp
index 0a5b7d260..e21333809 100644
--- a/source/core/slang-string.cpp
+++ b/source/core/slang-string.cpp
@@ -11,15 +11,6 @@ namespace Slang
// for anything that uses core
static const auto s_charUtilLink = CharUtil::_ensureLink();
- // TODO: this belongs in a different file:
-
- SLANG_RETURN_NEVER void signalUnexpectedError(char const* message)
- {
- // Can be useful to uncomment during debug when problem is on CI
- // printf("Unexpected: %s\n", message);
- throw InternalError(message);
- }
-
// OSString
OSString::OSString()
@@ -120,6 +111,14 @@ namespace Slang
return UnownedStringSlice(start, end);
}
+ UnownedStringSlice UnownedStringSlice::trimStart() const
+ {
+ const char* start = m_begin;
+
+ while (start < m_end && CharUtil::isHorizontalWhitespace(*start)) start++;
+ return UnownedStringSlice(start, m_end);
+ }
+
UnownedStringSlice UnownedStringSlice::trim(char c) const
{
const char* start = m_begin;
@@ -217,37 +216,42 @@ namespace Slang
String String::fromWString(const wchar_t * wstr)
{
+ List<char> buf;
#ifdef _WIN32
- return Slang::Encoding::UTF16->ToString((const char*)wstr, (int)(wcslen(wstr) * sizeof(wchar_t)));
+ Slang::CharEncoding::UTF16->decode((const Byte*)wstr, (int)(wcslen(wstr) * sizeof(wchar_t)), buf);
#else
- return Slang::Encoding::UTF32->ToString((const char*)wstr, (int)(wcslen(wstr) * sizeof(wchar_t)));
+ Slang::CharEncoding::UTF32->decode((const Byte*)wstr, (int)(wcslen(wstr) * sizeof(wchar_t)), buf);
#endif
+ return String(buf.begin(), buf.end());
}
String String::fromWString(const wchar_t * wstr, const wchar_t * wend)
{
+ List<char> buf;
#ifdef _WIN32
- return Slang::Encoding::UTF16->ToString((const char*)wstr, (int)((wend - wstr) * sizeof(wchar_t)));
+ Slang::CharEncoding::UTF16->decode((const Byte*)wstr, (int)((wend - wstr) * sizeof(wchar_t)), buf);
#else
- return Slang::Encoding::UTF32->ToString((const char*)wstr, (int)((wend - wstr) * sizeof(wchar_t)));
+ Slang::CharEncoding::UTF32->decode((const Byte*)wstr, (int)((wend - wstr) * sizeof(wchar_t)), buf);
#endif
+ return String(buf.begin(), buf.end());
}
String String::fromWChar(const wchar_t ch)
{
+ List<char> buf;
#ifdef _WIN32
- return Slang::Encoding::UTF16->ToString((const char*)&ch, (int)(sizeof(wchar_t)));
+ Slang::CharEncoding::UTF16->decode((const Byte*)&ch, (int)(sizeof(wchar_t)), buf);
#else
- return Slang::Encoding::UTF32->ToString((const char*)&ch, (int)(sizeof(wchar_t)));
+ Slang::CharEncoding::UTF32->decode((const Byte*)&ch, (int)(sizeof(wchar_t)), buf);
#endif
+ return String(buf.begin(), buf.end());
}
- String String::fromUnicodePoint(unsigned int codePoint)
+ /* static */String String::fromUnicodePoint(Char32 codePoint)
{
char buf[6];
- int len = Slang::EncodeUnicodePointToUTF8(buf, (int)codePoint);
- buf[len] = 0;
- return String(buf);
+ int len = Slang::encodeUnicodePointToUTF8(codePoint, buf);
+ return String(buf, buf + len);
}
OSString String::toWString(Index* outLength) const
@@ -258,15 +262,15 @@ namespace Slang
}
else
{
- List<char> buf;
+ List<Byte> buf;
switch(sizeof(wchar_t))
{
case 2:
- Slang::Encoding::UTF16->GetBytes(buf, *this);
+ Slang::CharEncoding::UTF16->encode(getUnownedSlice(), buf);
break;
case 4:
- Slang::Encoding::UTF32->GetBytes(buf, *this);
+ Slang::CharEncoding::UTF32->encode(getUnownedSlice(), buf);
break;
default: