diff options
Diffstat (limited to 'Source/Kernel/Library/String.class.cpp')
-rw-r--r-- | Source/Kernel/Library/String.class.cpp | 89 |
1 files changed, 47 insertions, 42 deletions
diff --git a/Source/Kernel/Library/String.class.cpp b/Source/Kernel/Library/String.class.cpp index c118800..518d8c9 100644 --- a/Source/Kernel/Library/String.class.cpp +++ b/Source/Kernel/Library/String.class.cpp @@ -6,7 +6,7 @@ using namespace CMem; //strlen and memcpy String String::hex(u32int number) { String ret; ret.m_length = 10; - ret.m_string = (char*)Mem::kalloc(11); + ret.m_string = new wchar[11]; ret.m_string[0] = '0'; ret.m_string[1] = 'x'; ret.m_string[10] = 0; @@ -35,7 +35,7 @@ String String::number(s32int number) { String ret; ret.m_length = order; - ret.m_string = (char*)Mem::kalloc(order + 1); + ret.m_string = new wchar[order + 1]; for (u32int i = order; i > 0; i--) { ret.m_string[i - 1] = numbers[number % 10]; @@ -55,14 +55,16 @@ String::String() { } String::String(char* string) { - m_length = strlen(string); + m_length = wchar::utf8len(string); if (m_length == 0) { m_string = 0; return; } - m_string = (char*)Mem::kalloc(m_length + 1); - for (u32int i = 0; i < m_length; i++) { - m_string[i] = string[i]; + m_string = new wchar[m_length + 1]; + int i = 0, l = strlen(string), c = 0; + while (i < l) { + i += m_string[c].affectUtf8(string + i); + c++; } m_string[m_length] = 0; } @@ -73,7 +75,7 @@ String::String(const String &other) { m_string = 0; return; } - m_string = (char*)Mem::kalloc(m_length + 1); + m_string = new wchar[m_length + 1]; for (u32int i = 0; i < m_length; i++) { m_string[i] = other.m_string[i]; } @@ -81,17 +83,17 @@ String::String(const String &other) { } String::~String() { - if (m_string != 0) Mem::kfree(m_string); + if (m_string != 0) delete [] m_string; } void String::operator= (const String &other) { m_length = other.m_length; - if (m_string != 0) Mem::kfree(m_string); + if (m_string != 0) delete [] m_string; if (m_length == 0) { m_string = 0; return; } - m_string = (char*)Mem::kalloc(m_length + 1); + m_string = new wchar[m_length + 1]; for (u32int i = 0; i < m_length; i++) { m_string[i] = other.m_string[i]; } @@ -99,15 +101,17 @@ void String::operator= (const String &other) { } void String::operator= (char* string) { - m_length = strlen(string); - if (m_string != 0) Mem::kfree(m_string); + m_length = wchar::utf8len(string); + if (m_string != 0) delete [] m_string; if (m_length == 0) { m_string = 0; return; } - m_string = (char*)Mem::kalloc(m_length + 1); - for (u32int i = 0; i < m_length; i++) { - m_string[i] = string[i]; + m_string = new wchar[m_length + 1]; + int i = 0, l = strlen(string), c = 0; + while (i < l) { + i += m_string[c].affectUtf8(string + i); + c++; } m_string[m_length] = 0; } @@ -121,22 +125,26 @@ bool String::operator== (String &other) { } bool String::operator== (char* string) { - if (m_length != strlen(string)) return false; - for (u32int i = 0; i < m_length; i++) { - if (m_string[i] != string[i]) return false; + if (m_length != wchar::utf8len(string)) return false; + int i = 0, l = strlen(string), c = 0; + wchar tmp; + while (i < l) { + i += tmp.affectUtf8(string + i); + if (m_string[c] != tmp) return false; + c++; } return true; } String& String::operator+= (String &other) { - char* newdata = (char*)Mem::kalloc(m_length + other.m_length + 1); + 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) Mem::kfree(m_string); + if (m_string != 0) delete [] m_string; m_string = newdata; m_length += other.m_length; m_string[m_length] = 0; @@ -144,30 +152,32 @@ String& String::operator+= (String &other) { } String& String::operator+= (char* other) { - char* newdata = (char*)Mem::kalloc(m_length + strlen(other) + 1); + wchar* newdata = new wchar[m_length + wchar::utf8len(other) + 1]; for (u32int i = 0; i < m_length; i++) { newdata[i] = m_string[i]; } - for (u32int i = 0; i < strlen(other); i++) { - newdata[i + m_length] = other[i]; + int i = 0, l = strlen(other), c = 0; + while (i < l) { + i += newdata[c + m_length].affectUtf8(other + i); + c++; } - if (m_string != 0) Mem::kfree(m_string); + if (m_string != 0) delete [] m_string; m_string = newdata; m_length += strlen(other); m_string[m_length] = 0; return *this; } -String& String::operator+= (char other) { - char* newdata = (char*)Mem::kalloc(m_length + 2); +String& String::operator+= (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) Mem::kfree(m_string); + if (m_string != 0) delete[] m_string; m_string = newdata; m_string[m_length] = other; m_length++; - m_string[m_length] = 0; + m_string[m_length].value = 0; return *this; } @@ -181,27 +191,22 @@ String& String::operator+ (char* other) { //Can be optimized return (ret += other); } -String& String::operator+ (char other) { //Can be optimized +String& String::operator+ (wchar other) { String ret(*this); return (ret += other); } -String::operator char* () { - if (m_string == 0) return ""; - return m_string; -} - s32int String::toInt() { if (m_string == 0) return 0; s32int pos = 0, number = 0; bool negative = false; - if (m_string[0] == '-') { + if (m_string[0].value == '-') { negative = true; pos = 1; } while (m_string[pos] >= '0' && m_string[pos] <= '9') { number *= 10; - number += (m_string[pos] - '0'); + number += (m_string[pos].value - '0'); pos++; } if (negative) return 0 - number; @@ -211,7 +216,7 @@ s32int String::toInt() { u32int String::toInt16() { if (m_string == 0) return 0; u32int pos = 0, number = 0; - if (m_string[0] == '0' && m_string[1] == 'x') pos = 2; + if (m_string[0].value == '0' && m_string[1].value == 'x') pos = 2; while (1) { char c = m_string[pos]; pos++; @@ -231,7 +236,7 @@ u32int String::toInt16() { return number; } -char& String::operator[] (int index) { +wchar& String::operator[] (int index) { return m_string[index]; } @@ -240,7 +245,7 @@ u32int String::size() { } void String::clear() { - Mem::kfree(m_string); + delete [] m_string; m_length = 0; m_string = 0; } @@ -249,7 +254,7 @@ bool String::empty() { return (m_length == 0); } -Vector<String> String::split(char c) { +Vector<String> String::split(wchar c) { Vector<String> ret; ret.push(String("")); for (u32int i = 0; i < m_length; i++) { @@ -269,9 +274,9 @@ String String::substr(s32int start, s32int size) { size = 0 - size; } String ret; - ret.m_string = (char*)Mem::kalloc(size + 1); + ret.m_string = new wchar[size + 1]; ret.m_length = size; - memcpy((u8int*)ret.m_string, (const u8int*)(m_string + start), size); + memcpy((u8int*)ret.m_string, (const u8int*)(m_string + start), size * sizeof(wchar)); ret.m_string[size] = 0; return ret; } |