|
@@ -2,7 +2,6 @@
|
|
|
|
|
|
#include <limits.h>
|
|
|
#include <stdlib.h>
|
|
|
-#include <string.h>
|
|
|
#include <uchar.h>
|
|
|
|
|
|
#include "core/data/Array.hpp"
|
|
@@ -15,10 +14,10 @@ using Char32String = Core::Char32String;
|
|
|
using Error = Core::Error;
|
|
|
namespace ErrorCode = Core::ErrorCode;
|
|
|
|
|
|
-constexpr int stringLength(const c32* c) {
|
|
|
+constexpr size_t stringLength(const c32* c) {
|
|
|
const c32* i = c + 1;
|
|
|
while(*(c++) != '\0') {}
|
|
|
- return static_cast<int>(c - i);
|
|
|
+ return static_cast<size_t>(c - i);
|
|
|
}
|
|
|
|
|
|
static Error readUnicode(c32& u, const char*& s) {
|
|
@@ -35,15 +34,15 @@ using C32Buffer = Core::Array<char, MB_LEN_MAX + 1>;
|
|
|
|
|
|
static Error convertC32(C32Buffer& buffer, c32 c) {
|
|
|
size_t n = c32rtomb(buffer.begin(), c, nullptr);
|
|
|
- if(n >= static_cast<size_t>(buffer.getLength())) {
|
|
|
+ if(n >= buffer.getLength()) {
|
|
|
return ErrorCode::INVALID_CHAR;
|
|
|
}
|
|
|
- buffer[static_cast<i64>(n)] = '\0';
|
|
|
+ buffer[n] = '\0';
|
|
|
return ErrorCode::NONE;
|
|
|
}
|
|
|
|
|
|
-CharString::CharString(char* buffer, i32 bufferSize)
|
|
|
- : length(0), capacity(bufferSize), data(buffer) {
|
|
|
+CharString::CharString(char* buffer, size_t bufferSize)
|
|
|
+ : length(0), capacity(bufferSize <= 0 ? 0 : bufferSize - 1), data(buffer) {
|
|
|
data[0] = '\0';
|
|
|
}
|
|
|
|
|
@@ -68,20 +67,20 @@ bool CharString::operator!=(const CharString& other) const {
|
|
|
return !((*this) == other);
|
|
|
}
|
|
|
|
|
|
-char CharString::operator[](int index) const {
|
|
|
+char CharString::operator[](size_t index) const {
|
|
|
return data[index];
|
|
|
}
|
|
|
|
|
|
-int CharString::getLength() const {
|
|
|
+size_t CharString::getLength() const {
|
|
|
return length;
|
|
|
}
|
|
|
|
|
|
-int CharString::getCapacity() const {
|
|
|
- return capacity - 1;
|
|
|
+size_t CharString::getCapacity() const {
|
|
|
+ return capacity;
|
|
|
}
|
|
|
|
|
|
Error CharString::append(char c) {
|
|
|
- if(length >= capacity - 1) {
|
|
|
+ if(length >= capacity) {
|
|
|
return ErrorCode::CAPACITY_REACHED;
|
|
|
}
|
|
|
data[length++] = c;
|
|
@@ -117,7 +116,7 @@ Error CharString::append(const char* s) {
|
|
|
|
|
|
Error CharString::append(const c32* s) {
|
|
|
|
|
|
- for(int i = stringLength(s); i > 0; i--) {
|
|
|
+ for(size_t i = stringLength(s); i > 0; i--) {
|
|
|
CORE_RETURN_ERROR(append(*(s++)));
|
|
|
}
|
|
|
return ErrorCode::NONE;
|
|
@@ -136,8 +135,8 @@ Error CharString::append(bool b) {
|
|
|
}
|
|
|
|
|
|
Error CharString::toString(CharString& s) const {
|
|
|
- int l = length;
|
|
|
- for(int i = 0; i < l; i++) {
|
|
|
+ size_t l = length;
|
|
|
+ for(size_t i = 0; i < l; i++) {
|
|
|
CORE_RETURN_ERROR(s.append(data[i]));
|
|
|
}
|
|
|
return ErrorCode::NONE;
|
|
@@ -152,62 +151,53 @@ void CharString::clear() {
|
|
|
data[0] = '\0';
|
|
|
}
|
|
|
|
|
|
-Error CharString::print() const {
|
|
|
- return Core::putChars(data);
|
|
|
+void CharString::print() const {
|
|
|
+ Core::print(data);
|
|
|
}
|
|
|
|
|
|
-Error CharString::printLine() const {
|
|
|
- CORE_RETURN_ERROR(print());
|
|
|
- return Core::putChar('\n');
|
|
|
+void CharString::printLine() const {
|
|
|
+ Core::printLine(data);
|
|
|
}
|
|
|
|
|
|
-bool CharString::startsWith(const CharString& other, int from) const {
|
|
|
- return from >= 0 && length - from >= other.getLength() &&
|
|
|
- strncmp(data + from, other.data,
|
|
|
- static_cast<size_t>(other.getLength())) == 0;
|
|
|
+bool CharString::startsWith(const CharString& other, size_t from) const {
|
|
|
+ return length >= from + other.getLength() &&
|
|
|
+ strncmp(data + from, other.data, other.getLength()) == 0;
|
|
|
}
|
|
|
|
|
|
-int CharString::search(const CharString& other, int from) const {
|
|
|
- if(from < 0) {
|
|
|
- return false;
|
|
|
- }
|
|
|
+size_t CharString::search(const CharString& other, size_t from) const {
|
|
|
char* f = strstr(data + from, other.data);
|
|
|
- return f == nullptr ? -1 : static_cast<int>(f - data);
|
|
|
+ return f == nullptr ? SIZE_MAX : static_cast<size_t>(f - data);
|
|
|
}
|
|
|
|
|
|
-bool CharString::contains(const CharString& other, int from) const {
|
|
|
- return search(other, from) >= 0;
|
|
|
+bool CharString::contains(const CharString& other, size_t from) const {
|
|
|
+ return search(other, from) != SIZE_MAX;
|
|
|
}
|
|
|
|
|
|
-int CharString::search(char u, int from) const {
|
|
|
- if(from < 0) {
|
|
|
- return false;
|
|
|
- }
|
|
|
+size_t CharString::search(char u, size_t from) const {
|
|
|
char* f = strchr(data + from, u);
|
|
|
- return f == nullptr ? -1 : static_cast<int>(f - data);
|
|
|
+ return f == nullptr ? SIZE_MAX : static_cast<size_t>(f - data);
|
|
|
}
|
|
|
|
|
|
-bool CharString::contains(char u, int from) const {
|
|
|
- return search(u, from) >= 0;
|
|
|
+bool CharString::contains(char u, size_t from) const {
|
|
|
+ return search(u, from) != SIZE_MAX;
|
|
|
}
|
|
|
|
|
|
-Error CharString::substring(CharString& s, int from, int to) const {
|
|
|
+Error CharString::substring(CharString& s, size_t from, size_t to) const {
|
|
|
s.clear();
|
|
|
- from = Math::max(from, 0);
|
|
|
- to = Math::min(to, length - 1);
|
|
|
- for(int i = from; i <= to; i++) {
|
|
|
+ to = Math::min(to + 1, length);
|
|
|
+ for(size_t i = from; i < to; i++) {
|
|
|
CORE_RETURN_ERROR(s.append(data[i]));
|
|
|
}
|
|
|
return ErrorCode::NONE;
|
|
|
}
|
|
|
|
|
|
-Error CharString::substring(CharString& s, int from) const {
|
|
|
+Error CharString::substring(CharString& s, size_t from) const {
|
|
|
return substring(s, from, length - 1);
|
|
|
}
|
|
|
|
|
|
Error CharString::replace(CharString& s, const CharString& search,
|
|
|
const CharString& replace) {
|
|
|
- int i = 0;
|
|
|
+ size_t i = 0;
|
|
|
while(i < length) {
|
|
|
if(startsWith(search, i)) {
|
|
|
CORE_RETURN_ERROR(s.append(replace));
|
|
@@ -221,7 +211,7 @@ Error CharString::replace(CharString& s, const CharString& search,
|
|
|
}
|
|
|
|
|
|
void CharString::replace(char search, char replace) {
|
|
|
- for(int i = 0; i < length; i++) {
|
|
|
+ for(size_t i = 0; i < length; i++) {
|
|
|
if(data[i] == search) {
|
|
|
data[i] = replace;
|
|
|
}
|
|
@@ -232,8 +222,8 @@ CharString::operator const char*() const {
|
|
|
return data;
|
|
|
}
|
|
|
|
|
|
-Char32String::Char32String(c32* buffer, i32 bufferSize)
|
|
|
- : length(0), capacity(bufferSize), data(buffer) {
|
|
|
+Char32String::Char32String(c32* buffer, size_t bufferSize)
|
|
|
+ : length(0), capacity(bufferSize <= 0 ? 0 : bufferSize - 1), data(buffer) {
|
|
|
data[0] = '\0';
|
|
|
}
|
|
|
|
|
@@ -255,7 +245,7 @@ bool Char32String::operator==(const Char32String& other) const {
|
|
|
if(length != other.getLength()) {
|
|
|
return false;
|
|
|
}
|
|
|
- for(int i = 0; i < length; i++) {
|
|
|
+ for(size_t i = 0; i < length; i++) {
|
|
|
if(data[i] != other[i]) {
|
|
|
return false;
|
|
|
}
|
|
@@ -271,16 +261,16 @@ bool Char32String::operator!=(const Char32String& other) const {
|
|
|
return !((*this) == other);
|
|
|
}
|
|
|
|
|
|
-c32 Char32String::operator[](int index) const {
|
|
|
+c32 Char32String::operator[](size_t index) const {
|
|
|
return data[index];
|
|
|
}
|
|
|
|
|
|
-int Char32String::getLength() const {
|
|
|
+size_t Char32String::getLength() const {
|
|
|
return length;
|
|
|
}
|
|
|
|
|
|
-int Char32String::getCapacity() const {
|
|
|
- return capacity - 1;
|
|
|
+size_t Char32String::getCapacity() const {
|
|
|
+ return capacity;
|
|
|
}
|
|
|
|
|
|
Error Char32String::append(char c) {
|
|
@@ -300,7 +290,7 @@ Error Char32String::append(wchar_t c) {
|
|
|
}
|
|
|
|
|
|
Error Char32String::append(c32 c) {
|
|
|
- if(length >= capacity - 1) {
|
|
|
+ if(length >= capacity) {
|
|
|
return ErrorCode::CAPACITY_REACHED;
|
|
|
}
|
|
|
data[length++] = c;
|
|
@@ -321,7 +311,7 @@ Error Char32String::append(const char* s) {
|
|
|
|
|
|
Error Char32String::append(const c32* s) {
|
|
|
|
|
|
- for(int i = stringLength(s); i > 0; i--) {
|
|
|
+ for(size_t i = stringLength(s); i > 0; i--) {
|
|
|
CORE_RETURN_ERROR(append(*(s++)));
|
|
|
}
|
|
|
return ErrorCode::NONE;
|
|
@@ -340,16 +330,16 @@ Error Char32String::append(bool b) {
|
|
|
}
|
|
|
|
|
|
Error Char32String::toString(CharString& s) const {
|
|
|
- int l = length;
|
|
|
- for(int i = 0; i < l; i++) {
|
|
|
+ size_t l = length;
|
|
|
+ for(size_t i = 0; i < l; i++) {
|
|
|
CORE_RETURN_ERROR(s.append(data[i]));
|
|
|
}
|
|
|
return ErrorCode::NONE;
|
|
|
}
|
|
|
|
|
|
Error Char32String::toString(Char32String& s) const {
|
|
|
- int l = length;
|
|
|
- for(int i = 0; i < l; i++) {
|
|
|
+ size_t l = length;
|
|
|
+ for(size_t i = 0; i < l; i++) {
|
|
|
CORE_RETURN_ERROR(s.append(data[i]));
|
|
|
}
|
|
|
return ErrorCode::NONE;
|
|
@@ -360,26 +350,27 @@ void Char32String::clear() {
|
|
|
data[0] = '\0';
|
|
|
}
|
|
|
|
|
|
-Error Char32String::print() const {
|
|
|
- for(int i = 0; i < length; i++) {
|
|
|
+void Char32String::print() const {
|
|
|
+ for(size_t i = 0; i < length; i++) {
|
|
|
C32Buffer buffer;
|
|
|
- CORE_RETURN_ERROR(convertC32(buffer, data[i]));
|
|
|
- CORE_RETURN_ERROR(putChars(buffer.begin()));
|
|
|
+ if(convertC32(buffer, data[i]).check()) {
|
|
|
+ Core::print('?');
|
|
|
+ } else {
|
|
|
+ Core::print(buffer.begin());
|
|
|
+ }
|
|
|
}
|
|
|
- return ErrorCode::NONE;
|
|
|
}
|
|
|
|
|
|
-Error Char32String::printLine() const {
|
|
|
- CORE_RETURN_ERROR(print());
|
|
|
- CORE_RETURN_ERROR(Core::putChar('\n'));
|
|
|
- return ErrorCode::NONE;
|
|
|
+void Char32String::printLine() const {
|
|
|
+ print();
|
|
|
+ Core::print('\n');
|
|
|
}
|
|
|
|
|
|
-bool Char32String::startsWith(const Char32String& other, int from) const {
|
|
|
- if(from > length - other.getLength()) {
|
|
|
+bool Char32String::startsWith(const Char32String& other, size_t from) const {
|
|
|
+ if(from + other.getLength() > length) {
|
|
|
return false;
|
|
|
}
|
|
|
- for(int i = 0; i < other.getLength(); i++) {
|
|
|
+ for(size_t i = 0; i < other.getLength(); i++) {
|
|
|
if(data[from + i] != other[i]) {
|
|
|
return false;
|
|
|
}
|
|
@@ -387,49 +378,48 @@ bool Char32String::startsWith(const Char32String& other, int from) const {
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
-int Char32String::search(const Char32String& other, int from) const {
|
|
|
- for(int i = from; i < length; i++) {
|
|
|
+size_t Char32String::search(const Char32String& other, size_t from) const {
|
|
|
+ for(size_t i = from; i < length; i++) {
|
|
|
if(startsWith(other, i)) {
|
|
|
return i;
|
|
|
}
|
|
|
}
|
|
|
- return -1;
|
|
|
+ return SIZE_MAX;
|
|
|
}
|
|
|
|
|
|
-bool Char32String::contains(const Char32String& other, int from) const {
|
|
|
- return search(other, from) >= 0;
|
|
|
+bool Char32String::contains(const Char32String& other, size_t from) const {
|
|
|
+ return search(other, from) != SIZE_MAX;
|
|
|
}
|
|
|
|
|
|
-int Char32String::search(c32 u, int from) const {
|
|
|
- for(int i = from; i < length; i++) {
|
|
|
+size_t Char32String::search(c32 u, size_t from) const {
|
|
|
+ for(size_t i = from; i < length; i++) {
|
|
|
if(data[i] == u) {
|
|
|
return i;
|
|
|
}
|
|
|
}
|
|
|
- return -1;
|
|
|
+ return SIZE_MAX;
|
|
|
}
|
|
|
|
|
|
-bool Char32String::contains(c32 u, int from) const {
|
|
|
- return search(u, from) >= 0;
|
|
|
+bool Char32String::contains(c32 u, size_t from) const {
|
|
|
+ return search(u, from) != SIZE_MAX;
|
|
|
}
|
|
|
|
|
|
-Error Char32String::substring(Char32String& s, int from, int to) const {
|
|
|
+Error Char32String::substring(Char32String& s, size_t from, size_t to) const {
|
|
|
s.clear();
|
|
|
- from = Math::max(from, 0);
|
|
|
- to = Math::min(to, length - 1);
|
|
|
- for(int i = from; i <= to; i++) {
|
|
|
+ to = Math::min(to + 1, length);
|
|
|
+ for(size_t i = from; i < to; i++) {
|
|
|
CORE_RETURN_ERROR(s.append(data[i]));
|
|
|
}
|
|
|
return ErrorCode::NONE;
|
|
|
}
|
|
|
|
|
|
-Error Char32String::substring(Char32String& s, int from) const {
|
|
|
+Error Char32String::substring(Char32String& s, size_t from) const {
|
|
|
return substring(s, from, length - 1);
|
|
|
}
|
|
|
|
|
|
Error Char32String::replace(Char32String& s, const Char32String& search,
|
|
|
const Char32String& replace) {
|
|
|
- int i = 0;
|
|
|
+ size_t i = 0;
|
|
|
while(i < length) {
|
|
|
if(startsWith(search, i)) {
|
|
|
CORE_RETURN_ERROR(s.append(replace));
|
|
@@ -443,7 +433,7 @@ Error Char32String::replace(Char32String& s, const Char32String& search,
|
|
|
}
|
|
|
|
|
|
void Char32String::replace(c32 search, c32 replace) {
|
|
|
- for(int i = 0; i < length; i++) {
|
|
|
+ for(size_t i = 0; i < length; i++) {
|
|
|
if(data[i] == search) {
|
|
|
data[i] = replace;
|
|
|
}
|