Renames stdlib and string. Adds fixes and improvements to Vector<> and String.

This commit is contained in:
Felix Queißner 2015-10-07 01:31:21 +02:00
parent d1140b2706
commit a9aa739757
5 changed files with 70 additions and 16 deletions

View file

@ -1,10 +1,45 @@
#pragma once #pragma once
#include <stdlib.h>
#include <inttypes.h> #include <inttypes.h>
#if !defined(CIRCUIT_OS) #if defined(CIRCUIT_OS)
#include <stdlib.h> #include <kstdlib.h>
#include <string.h> #error "???"
#else
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
// 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 #endif
#define KER_STRING_AVAILABLE #define KER_STRING_AVAILABLE
@ -45,6 +80,12 @@ namespace ker
this->copyFrom(reinterpret_cast<const uint8_t*>(text), strlen(text)); this->copyFrom(reinterpret_cast<const uint8_t*>(text), strlen(text));
} }
String(const char *bytes, size_t length) :
String(reinterpret_cast<const uint8_t *>(bytes), length)
{
}
String(const uint8_t *bytes, size_t length) : String(const uint8_t *bytes, size_t length) :
mText(nullptr), mText(nullptr),
mLength(length) mLength(length)
@ -59,6 +100,11 @@ namespace ker
} }
} }
uint8_t at(size_t index) const
{
return this->mText[index];
}
size_t length() const size_t length() const
{ {
return this->mLength; return this->mLength;

View file

@ -2,11 +2,13 @@
#include <inttypes.h> #include <inttypes.h>
#include <stddef.h> #include <stddef.h>
#include <string.h>
#include "new.hpp"
#if !defined(CIRCUIT_OS) #if defined(CIRCUIT_OS)
#include "kstdlib.h"
#include "new.hpp"
#else
#include <stdlib.h> #include <stdlib.h>
#include <string.h>
#include <new> #include <new>
#endif #endif
@ -119,11 +121,9 @@ namespace ker
} }
T& insert(size_t index, const T& value) T& insert(size_t index, const T& value)
{ {
if(this->mReserved < (this->mLength + 1)) { this->resize(this->mLength + 1);
this->resize(this->mLength + 1); for(int32_t i = this->mLength - 2; i >= static_cast<int32_t>(index); i--) {
}
for(int32_t i = this->mLength - 1; i > static_cast<int32_t>(index); i--) {
// Move every item backwards // Move every item backwards
this->mData[i+1] = this->mData[i]; this->mData[i+1] = this->mData[i];
} }
@ -150,7 +150,7 @@ namespace ker
if(current > size) { if(current > size) {
// "Downgrade" // "Downgrade"
for(size_t i = this->mLength - 1; i > size; i--) { for(int32_t i = static_cast<int32_t>(this->mLength) - 1; i > static_cast<int32_t>(size); i--) {
this->mData[i].~T(); this->mData[i].~T();
} }
} else { } else {
@ -196,6 +196,14 @@ namespace ker
return this->mData[this->mLength - 1]; return this->mData[this->mLength - 1];
} }
T &front() {
return this->mData[0];
}
T &back() {
return this->mData[this->mLength - 1];
}
T* begin() T* begin()
{ {
return &this->mData[0]; return &this->mData[0];

3
include/kstring.h Normal file
View file

@ -0,0 +1,3 @@
#pragma once
#include "kstdlib.h"

View file

@ -1,3 +0,0 @@
#pragma once
#include "stdlib.h"