summaryrefslogtreecommitdiff
path: root/src/include/string_view.hpp
diff options
context:
space:
mode:
authorJohn Hodge <tpg@ucc.asn.au>2018-06-03 14:57:05 +0800
committerJohn Hodge <tpg@ucc.asn.au>2018-06-03 14:57:05 +0800
commitbf8f8b4b4a9fe273451be59f68acafbe61968b83 (patch)
tree82993550cb3c88de0edbd55d79e4ea8e8cefffac /src/include/string_view.hpp
parent39b3cf53798683e496804f8322da2254b10850f4 (diff)
parenta7fb27789a2b34543851d207120e2c0001ee9c27 (diff)
downloadmrust-bf8f8b4b4a9fe273451be59f68acafbe61968b83.tar.gz
Merge branch 'master' of https://github.com/thepowersgang/mrustc
Diffstat (limited to 'src/include/string_view.hpp')
-rw-r--r--src/include/string_view.hpp89
1 files changed, 89 insertions, 0 deletions
diff --git a/src/include/string_view.hpp b/src/include/string_view.hpp
new file mode 100644
index 00000000..7d049e9e
--- /dev/null
+++ b/src/include/string_view.hpp
@@ -0,0 +1,89 @@
+/*
+ * MRustC - Rust Compiler
+ * - By John Hodge (Mutabah/thePowersGang)
+ *
+ * include/string_view.hpp
+ * - Clone of the `string_view` class (introduced in C++17)
+ */
+#pragma once
+#include <string>
+#include <cstddef> // size_t
+#include <iostream> // ostream
+
+namespace std {
+
+class string_view
+{
+ const char* m_start;
+ const char* m_end;
+
+public:
+ string_view():
+ m_start(nullptr), m_end(nullptr)
+ {
+ }
+ string_view(const char* s, const char* e):
+ m_start(s), m_end(e)
+ {
+ if(!(s <= e))
+ throw ::std::invalid_argument("start must be before end for string_view");
+ }
+ string_view(const char* s):
+ m_start(s), m_end(s)
+ {
+ while(*m_end)
+ m_end ++;
+ }
+ string_view(const string& s):
+ m_start(s.data()), m_end(m_start + s.size())
+ {
+ }
+
+ size_t size() const {
+ return m_end - m_start;
+ }
+
+ bool operator==(const string_view& x) const { return cmp(x) == 0; }
+ bool operator!=(const string_view& x) const { return cmp(x) != 0; }
+ bool operator< (const string_view& x) const { return cmp(x) < 0; }
+ bool operator> (const string_view& x) const { return cmp(x) > 0; }
+ bool operator<=(const string_view& x) const { return cmp(x) <= 0; }
+ bool operator>=(const string_view& x) const { return cmp(x) >= 0; }
+ bool operator==(const char* x) const { return cmp(string_view(x)) == 0; }
+ bool operator!=(const char* x) const { return cmp(string_view(x)) != 0; }
+ bool operator< (const char* x) const { return cmp(string_view(x)) < 0; }
+ bool operator> (const char* x) const { return cmp(string_view(x)) > 0; }
+ bool operator<=(const char* x) const { return cmp(string_view(x)) <= 0; }
+ bool operator>=(const char* x) const { return cmp(string_view(x)) >= 0; }
+ bool operator==(const string& x) const { return cmp(string_view(x)) == 0; }
+ bool operator!=(const string& x) const { return cmp(string_view(x)) != 0; }
+ bool operator< (const string& x) const { return cmp(string_view(x)) < 0; }
+ bool operator> (const string& x) const { return cmp(string_view(x)) > 0; }
+ bool operator<=(const string& x) const { return cmp(string_view(x)) <= 0; }
+ bool operator>=(const string& x) const { return cmp(string_view(x)) >= 0; }
+
+ friend ::std::ostream& operator<<(::std::ostream& os, const string_view& x) {
+ for(const char* s = x.m_start; s != x.m_end; s++)
+ os << *s;
+ return os;
+ }
+
+private:
+ int cmp(const string_view& x) const {
+ const char *a, *b;
+ for( a = m_start, b = x.m_start; a != m_end && b != x.m_end; a++, b++)
+ {
+ if( *a != *b ) {
+ return *a < *b ? -1 : 1;
+ }
+ }
+ if( a == m_end && b == m_end )
+ return 0;
+ if( a == m_end )
+ return -1;
+ else
+ return 1;
+ }
+};
+
+}