diff options
Diffstat (limited to 'Source/Kernel')
-rw-r--r-- | Source/Kernel/Library/BasicString.class.cpp | 175 | ||||
-rw-r--r-- | Source/Kernel/Library/BasicString.class.h | 48 | ||||
-rw-r--r-- | Source/Kernel/Library/String.class.cpp | 73 | ||||
-rw-r--r-- | Source/Kernel/Library/String.class.h | 21 | ||||
-rwxr-xr-x | Source/Kernel/Melon.ke | bin | 158901 -> 160815 bytes |
5 files changed, 234 insertions, 83 deletions
diff --git a/Source/Kernel/Library/BasicString.class.cpp b/Source/Kernel/Library/BasicString.class.cpp new file mode 100644 index 0000000..ae89fe4 --- /dev/null +++ b/Source/Kernel/Library/BasicString.class.cpp @@ -0,0 +1,175 @@ +#include <Library/Vector.class.h> + +#define FREE if (m_string != 0) delete m_string; +#define ALLOC m_string = new T[m_length]; +#define VRFY if (m_length == 0) { m_string = NULL; return; } + +using namespace CMem; + +template <typename T> +BasicString<T>::BasicString() { + m_string = NULL; + m_length = 0; +} + +template <typename T> +BasicString<T>::BasicString(const T* string, u32int length) { + m_string = NULL; + affect(string, length); +} + +template <typename T> +BasicString<T>::BasicString(const BasicString<T> &other) { + m_string = NULL; + affect(other); +} + +template <typename T> +BasicString<T>::BasicString(const T value, u32int count) { + m_string = NULL; + affect(value, count); +} + +template <typename T> +BasicString<T>::~BasicString() { + FREE; +} + +template <typename T> +void BasicString<T>::affect(const BasicString<T> &other) { + FREE; + m_length = other.m_length; + VRFY; + ALLOC; + memcpy((u8int*)m_string, (u8int*)(other.m_string), m_length * sizeof(T)); +} + +template <typename T> +void BasicString<T>::affect(const T* string, u32int length) { + FREE; + m_length = length; + VRFY; + ALLOC; + memcpy((u8int*)string, (u8int*)string, m_length * sizeof(T)); +} + +template <typename T> +void BasicString<T>::affect(const T value, u32int count) { + FREE; + m_length = count; + VRFY; + ALLOC; + for (u32int i = 0; i < count; i++) { + m_string[i] = value; + } +} + +template <typename T> +bool BasicString<T>::compare(const BasicString<T> &other) const { + if (m_length != other.m_length) return false; + for (u32int i = 0; i < m_length; i++) { + if (m_string[i] != other.m_string[i]) return false; + } + return true; +} + +template <typename T> +bool BasicString<T>::compare(const T* string, u32int length) const { + if (m_length != length) return false; + for (u32int i = 0; i < m_length; i++) { + if (m_string[i] != string[i]) return false; + } + return true; +} + +template <typename T> +BasicString<T> &BasicString<T>::append(const BasicString<T> &other) { + T* newdata = new T[m_length + other.m_length]; + for (u32int i = 0; i < m_length; i++) { + newdata[i] = m_string[i]; + } + for (u32int i = 0; i < other.m_length; i++) { + newdata[i + m_length] = other.m_string[i]; + } + FREE; + m_string = newdata; + m_length += other.m_length; + return *this; +} + +template <typename T> +BasicString<T> &BasicString<T>::append(const T* string, u32int length) { + T* newdata = new T[m_length + length]; + for (u32int i = 0; i < m_length; i++) { + newdata[i] = m_string[i]; + } + for (u32int i = 0; i < length; i++) { + newdata[i + m_length] = string[i]; + } + FREE; + m_string = newdata; + m_length += length; + return *this; +} + +template <typename T> +BasicString<T> &BasicString<T>::append(const T other) { + T* newdata = new T[m_length + 1]; + for (u32int i = 0; i < m_length; i++) { + newdata[i] = m_string[i]; + } + FREE; + m_string = newdata; + m_string[m_length] = other; + m_length++; + return *this; +} + +template <typename T> +BasicString<T> BasicString<T>::concat(const BasicString<T> &other) const { + BasicString<T> ret(*this); + return (ret.append(other)); +} + +template <typename T> +BasicString<T> BasicString<T>::concat(const T* string, u32int length) const { + BasicString<T> ret(*this); + return (ret.append(string, length)); +} + +template <typename T> +BasicString<T> BasicString<T>::concat(const T other) const { + BasicString<T> ret(*this); + return (ret.append(other)); +} + +template <typename T> +void BasicString<T>::clear() { + FREE; + m_string = 0; + m_length = 0; +} + +template <typename T> +Vector< BasicString<T> > BasicString<T>::split(T sep) const { + Vector< BasicString<T> > ret; + ret.push(BasicString<T>()); + for (u32int i = 0; i < m_length; i++) { + if (m_string[i] == sep) { + ret.push(BasicString<T>()); + } else { + ret.back().append(m_string[i]); + } + } + return ret; +} + +template <typename T> +BasicString<T> BasicString<T>::substr(s32int start, u32int size) { + if (start < 0) start = m_length - start; + BasicString<T> ret; + ret.m_string = new T[size + 1]; + ret.m_length = size; + memcpy((u8int*)ret.m_string, (u8int*)(&m_string[start]), size * sizeof(T)); + return ret; +} diff --git a/Source/Kernel/Library/BasicString.class.h b/Source/Kernel/Library/BasicString.class.h new file mode 100644 index 0000000..a4d56c7 --- /dev/null +++ b/Source/Kernel/Library/BasicString.class.h @@ -0,0 +1,48 @@ +#ifndef DEF_BASICSTRING_CLASS_H +#define DEF_BASICSTRING_CLASS_H + +#include <Core/common.wtf.h> + +template <typename T> class Vector; + +template <typename T> +class BasicString { + protected: + T *m_string; + u32int m_length; + + public: + BasicString(); + BasicString(const T* string, u32int length); + BasicString(const BasicString<T> &other); + BasicString(const T, u32int count = 1); + virtual ~BasicString(); + + void affect(const BasicString<T> &other); + void affect(const T* string, u32int length); + void affect(const T value, u32int count = 1); + + bool compare(const BasicString<T> &other) const; + bool compare(const T* string, u32int length) const; + + BasicString<T>& append(const BasicString<T> &other); + BasicString<T>& append(const T* string, u32int length); + BasicString<T>& append(const T other); + + BasicString<T> concat(const BasicString<T> &other) const; + BasicString<T> concat(const T* string, u32int length) const; + BasicString<T> concat(const T other) const; + + T& operator[] (int index) const { return m_string[index]; } + + u32int size() const { return m_length; } + void clear(); + bool empty() const { return m_length == 0; } + + Vector< BasicString<T> > split(T sep) const; + BasicString<T> substr(s32int start, u32int size); +}; + +#include "BasicString.class.cpp" + +#endif diff --git a/Source/Kernel/Library/String.class.cpp b/Source/Kernel/Library/String.class.cpp index 7c2bbc0..e43953f 100644 --- a/Source/Kernel/Library/String.class.cpp +++ b/Source/Kernel/Library/String.class.cpp @@ -49,9 +49,7 @@ String String::number(s32int number) { return ret; } -String::String() { - m_string = 0; - m_length = 0; +String::String() : BasicString<WChar> () { } String::String(const char* string, u8int encoding) { @@ -69,35 +67,10 @@ String::String(const char* string, u8int encoding) { m_string[m_length] = 0; } -String::String(const String &other) { - m_length = other.m_length; - if (m_length == 0) { - m_string = 0; - return; - } - m_string = new WChar[m_length + 1]; - for (u32int i = 0; i < m_length; i++) { - m_string[i] = other.m_string[i]; - } - m_string[m_length] = 0; +String::String(const String &other) : BasicString<WChar> (other) { } String::~String() { - if (m_string != 0) delete [] m_string; -} - -void String::affect (const String &other) { - m_length = other.m_length; - if (m_string != 0) delete [] m_string; - if (m_length == 0) { - m_string = 0; - return; - } - m_string = new WChar[m_length + 1]; - for (u32int i = 0; i < m_length; i++) { - m_string[i] = other.m_string[i]; - } - m_string[m_length] = 0; } void String::affect (const char* string, u8int encoding) { @@ -116,14 +89,6 @@ void String::affect (const char* string, u8int encoding) { m_string[m_length] = 0; } -bool String::compare (const String &other) const { - if (m_length != other.m_length) return false; - for (u32int i = 0; i < m_length; i++) { - if (m_string[i] != other.m_string[i]) return false; - } - return true; -} - bool String::compare (const char* string, u8int encoding) const { if (m_length != WChar::utfLen(string, encoding)) return false; int i = 0, l = strlen(string), c = 0; @@ -136,21 +101,6 @@ bool String::compare (const char* string, u8int encoding) const { return true; } -String& String::append (const String &other) { - WChar* newdata = new WChar[m_length + other.m_length + 1]; - for (u32int i = 0; i < m_length; i++) { - newdata[i] = m_string[i]; - } - for (u32int i = 0; i < other.m_length; i++) { - newdata[i + m_length] = other.m_string[i]; - } - if (m_string != 0) delete [] m_string; - m_string = newdata; - m_length += other.m_length; - m_string[m_length] = 0; - return *this; -} - String& String::append (const char* other, u8int encoding) { WChar* newdata = new WChar[m_length + WChar::utfLen(other, encoding) + 1]; for (u32int i = 0; i < m_length; i++) { @@ -168,19 +118,6 @@ String& String::append (const char* other, u8int encoding) { return *this; } -String& String::append (WChar other) { - WChar* newdata = new WChar[m_length + 2]; - for (u32int i = 0; i < m_length; i++) { - newdata[i] = m_string[i]; - } - if (m_string != 0) delete[] m_string; - m_string = newdata; - m_string[m_length] = other; - m_length++; - m_string[m_length].value = 0; - return *this; -} - String String::concat (const String &other) const { //Can be optimized String ret(*this); return (ret += other); @@ -269,12 +206,8 @@ Vector<String> String::split(WChar c) const { return ret; } -String String::substr(s32int start, s32int size) { +String String::substr(s32int start, u32int size) { if (start < 0) start = m_length - start; - if (size < 0) { //this fucks - start = start + size; - size = 0 - size; - } String ret; ret.m_string = new WChar[size + 1]; ret.m_length = size; diff --git a/Source/Kernel/Library/String.class.h b/Source/Kernel/Library/String.class.h index 6216415..ad1524d 100644 --- a/Source/Kernel/Library/String.class.h +++ b/Source/Kernel/Library/String.class.h @@ -3,14 +3,9 @@ #include <Core/common.wtf.h> #include <Library/WChar.class.h> +#include <Library/BasicString.class.h> -template <typename T> class Vector; - -class String { - private: - WChar *m_string; - u32int m_length; - +class String : public BasicString<WChar> { public: static String hex(u32int number); static String number(s32int number); @@ -18,23 +13,23 @@ class String { String(const char* string, u8int encoding = UE_UTF8); String(); String(const String &other); - ~String(); + virtual ~String(); - void affect(const String &other); + void affect(const String &other) { BasicString<WChar>::affect(other); } void affect(const char* string, u8int encoding = UE_UTF8); void operator= (const String &other) { affect(other); } void operator= (const char* other) { affect(other); } - bool compare(const String &other) const; + bool compare(const String &other) const { return BasicString<WChar>::compare(other); } bool compare(const char* string, u8int encoding = UE_UTF8) const; bool operator== (const String &other) const { return compare(other); } bool operator== (const char* other) const { return compare(other); } bool operator!= (const String &other) { return !compare(other); } bool operator!= (const char* other) { return !compare(other); } - String& append(const String &other); + String& append(const String &other) { BasicString<WChar>::append(other); return *this; } String& append(const char* other, u8int encoding = UE_UTF8); - String& append(WChar other); + String& append(WChar other) { BasicString<WChar>::append(other); return *this; } String &operator+= (const String &other) { return append(other); } String &operator+= (const char* other) { return append(other); } String &operator+= (WChar other) { return append(other); } @@ -57,7 +52,7 @@ class String { Vector<String> split(WChar c) const; - String substr(s32int start, s32int size); + String substr(s32int start, u32int size); }; #endif diff --git a/Source/Kernel/Melon.ke b/Source/Kernel/Melon.ke Binary files differindex 79d78db..9f88bf0 100755 --- a/Source/Kernel/Melon.ke +++ b/Source/Kernel/Melon.ke |