diff --git a/include/ker/string.hpp b/include/ker/string.hpp index 5f775de..2edac22 100644 --- a/include/ker/string.hpp +++ b/include/ker/string.hpp @@ -1,10 +1,45 @@ #pragma once -#include #include -#if !defined(CIRCUIT_OS) - #include - #include +#if defined(CIRCUIT_OS) +#include +#error "???" +#else +#include +#include +#include + +// itoa is not ANSI C +/** + * C++ version 0.4 char* style "itoa": + * Written by Lukás Chmela + * Released under GPLv3. + */ +static inline char* itoa(int value, char* result, int base) { + // check that the base if valid + if (base < 2 || base > 36) { *result = '\0'; return result; } + + char* ptr = result, *ptr1 = result, tmp_char; + int tmp_value; + + do { + tmp_value = value; + value /= base; + *ptr++ = "zyxwvutsrqponmlkjihgfedcba9876543210123456789abcdefghijklmnopqrstuvwxyz" [35 + (tmp_value - value * base)]; + } while ( value ); + + // Apply negative sign + if (tmp_value < 0) *ptr++ = '-'; + *ptr-- = '\0'; + while(ptr1 < ptr) { + tmp_char = *ptr; + *ptr--= *ptr1; + *ptr1++ = tmp_char; + } + return result; +} + + #endif #define KER_STRING_AVAILABLE @@ -45,6 +80,12 @@ namespace ker this->copyFrom(reinterpret_cast(text), strlen(text)); } + String(const char *bytes, size_t length) : + String(reinterpret_cast(bytes), length) + { + + } + String(const uint8_t *bytes, size_t length) : mText(nullptr), mLength(length) @@ -59,6 +100,11 @@ namespace ker } } + uint8_t at(size_t index) const + { + return this->mText[index]; + } + size_t length() const { return this->mLength; diff --git a/include/ker/vector.hpp b/include/ker/vector.hpp index 58bb5d2..ce4741a 100644 --- a/include/ker/vector.hpp +++ b/include/ker/vector.hpp @@ -2,11 +2,13 @@ #include #include -#include -#include "new.hpp" -#if !defined(CIRCUIT_OS) +#if defined(CIRCUIT_OS) +#include "kstdlib.h" +#include "new.hpp" +#else #include +#include #include #endif @@ -119,11 +121,9 @@ namespace ker } T& insert(size_t index, const T& value) - { - if(this->mReserved < (this->mLength + 1)) { - this->resize(this->mLength + 1); - } - for(int32_t i = this->mLength - 1; i > static_cast(index); i--) { + { + this->resize(this->mLength + 1); + for(int32_t i = this->mLength - 2; i >= static_cast(index); i--) { // Move every item backwards this->mData[i+1] = this->mData[i]; } @@ -150,7 +150,7 @@ namespace ker if(current > size) { // "Downgrade" - for(size_t i = this->mLength - 1; i > size; i--) { + for(int32_t i = static_cast(this->mLength) - 1; i > static_cast(size); i--) { this->mData[i].~T(); } } else { @@ -196,6 +196,14 @@ namespace ker return this->mData[this->mLength - 1]; } + T &front() { + return this->mData[0]; + } + + T &back() { + return this->mData[this->mLength - 1]; + } + T* begin() { return &this->mData[0]; diff --git a/include/stdlib.h b/include/kstdlib.h similarity index 100% rename from include/stdlib.h rename to include/kstdlib.h diff --git a/include/kstring.h b/include/kstring.h new file mode 100644 index 0000000..f949f0b --- /dev/null +++ b/include/kstring.h @@ -0,0 +1,3 @@ +#pragma once + +#include "kstdlib.h" diff --git a/include/string.h b/include/string.h deleted file mode 100644 index 08e1311..0000000 --- a/include/string.h +++ /dev/null @@ -1,3 +0,0 @@ -#pragma once - -#include "stdlib.h"