summaryrefslogtreecommitdiffstats
path: root/source/core
diff options
context:
space:
mode:
Diffstat (limited to 'source/core')
-rw-r--r--source/core/slang-io.cpp86
-rw-r--r--source/core/slang-io.h17
-rw-r--r--source/core/slang-string-util.cpp24
-rw-r--r--source/core/slang-string-util.h5
4 files changed, 131 insertions, 1 deletions
diff --git a/source/core/slang-io.cpp b/source/core/slang-io.cpp
index 162e49980..c11ba85e0 100644
--- a/source/core/slang-io.cpp
+++ b/source/core/slang-io.cpp
@@ -4,6 +4,7 @@
#include "../../slang-com-helper.h"
#include "slang-string-util.h"
+#include "slang-char-util.h"
#ifndef __STDC__
# define __STDC__ 1
@@ -918,7 +919,90 @@ namespace Slang
return SLANG_OK;
}
+
+ String URI::getPath() const
+ {
+ Index startIndex = uri.indexOf("://");
+ if (startIndex == -1)
+ return String();
+ startIndex += 3;
+ Index endIndex = uri.indexOf('?');
+ if (endIndex == -1)
+ endIndex = uri.getLength();
+ StringBuilder sb;
+#if SLANG_WINDOWS_FAMILY
+ if (uri[startIndex] == '/')
+ startIndex++;
+#endif
+ for (Index i = startIndex; i < endIndex;)
+ {
+ auto ch = uri[i];
+ if (ch == '%')
+ {
+ Int charVal = CharUtil::getHexDigitValue(uri[i + 1]) * 16 +
+ CharUtil::getHexDigitValue(uri[i + 2]);
+ sb.appendChar((char)charVal);
+ i += 3;
+ }
+ else
+ {
+ sb.appendChar(uri[i]);
+ i++;
+ }
+ }
+ return sb.ProduceString();
+ }
+ StringSlice URI::getProtocol() const
+ {
+ Index separatorIndex = uri.indexOf("://");
+ if (separatorIndex != -1)
+ return uri.subString(0, separatorIndex);
+ return StringSlice();
+ }
-}
+ bool URI::isSafeURIChar(char ch)
+ {
+ return (ch >= '0' && ch <= '9') || (ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z') ||
+ ch == '-' || ch == '_' || ch == '/' || ch == '.';
+ }
+ URI URI::fromLocalFilePath(UnownedStringSlice path)
+ {
+ URI uri;
+ StringBuilder sb;
+ sb << "file://";
+
+#if SLANG_WINDOWS_FAMILY
+ sb << "/";
+#endif
+
+ for (auto ch : path)
+ {
+ if (isSafeURIChar(ch))
+ {
+ sb.appendChar(ch);
+ }
+ else if (ch == '\\')
+ {
+ sb.appendChar('/');
+ }
+ else
+ {
+ char buffer[32];
+ int length = IntToAscii(buffer, (int)ch, 16);
+ ReverseInternalAscii(buffer, length);
+ sb << "%" << buffer;
+ }
+ }
+ return URI::fromString(sb.getUnownedSlice());
+ }
+
+ URI URI::fromString(UnownedStringSlice uriString)
+ {
+ URI uri;
+ uri.uri = uriString;
+ return uri;
+ }
+
+}
diff --git a/source/core/slang-io.h b/source/core/slang-io.h
index de761cf4b..51b1d45e5 100644
--- a/source/core/slang-io.h
+++ b/source/core/slang-io.h
@@ -157,6 +157,23 @@ namespace Slang
static SlangResult remove(const String& path);
};
+ struct URI
+ {
+ String uri;
+ bool operator==(const URI& other) const { return uri == other.uri; }
+ bool operator!=(const URI& other) const { return uri != other.uri; }
+
+ HashCode getHashCode() const { return uri.getHashCode(); }
+
+ bool isLocalFile() { return uri.startsWith("file://"); };
+ String getPath() const;
+ StringSlice getProtocol() const;
+
+ static URI fromLocalFilePath(UnownedStringSlice path);
+ static URI fromString(UnownedStringSlice uriString);
+ static bool isSafeURIChar(char ch);
+ };
+
// Helper class to clean up temporary files on dtor
class TemporaryFileSet: public RefObject
{
diff --git a/source/core/slang-string-util.cpp b/source/core/slang-string-util.cpp
index 5282f01a6..c4b654072 100644
--- a/source/core/slang-string-util.cpp
+++ b/source/core/slang-string-util.cpp
@@ -645,4 +645,28 @@ ComPtr<ISlangBlob> StringUtil::createStringBlob(const String& string)
return (cur == end) ? SLANG_OK : SLANG_FAIL;
}
+int StringUtil::parseIntAndAdvancePos(UnownedStringSlice text, Index& pos)
+{
+ int result = 0;
+ while (text[pos] == ' ' && pos < text.getLength())
+ {
+ pos++;
+ continue;
+ }
+ while (pos < text.getLength())
+ {
+ if (text[pos] >= '0' && text[pos] <= '9')
+ {
+ result *= 10;
+ result += text[pos] - '0';
+ pos++;
+ }
+ else
+ {
+ break;
+ }
+ }
+ return result;
+}
+
} // namespace Slang
diff --git a/source/core/slang-string-util.h b/source/core/slang-string-util.h
index 03f503ed2..b7576da1d 100644
--- a/source/core/slang-string-util.h
+++ b/source/core/slang-string-util.h
@@ -109,6 +109,11 @@ struct StringUtil
/// Convert into int64_t. Returns SLANG_OK on success.
static SlangResult parseInt64(const UnownedStringSlice& text, int64_t& out);
+
+ /// Parse an integer from text starting at pos until the end or the first non-digit char.
+ /// Modifies pos to the position where parsing ends.
+ /// Returns parsed integer.
+ static int parseIntAndAdvancePos(UnownedStringSlice text, Index& pos);
};
/* A helper class that allows parsing of lines from text with iteration. Uses StringUtil::extractLine for the actual underlying implementation. */