Renames stdlib and string. Adds fixes and improvements to Vector<> and String.
This commit is contained in:
parent
d1140b2706
commit
a9aa739757
5 changed files with 70 additions and 16 deletions
|
@ -1,10 +1,45 @@
|
|||
#pragma once
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <inttypes.h>
|
||||
#if !defined(CIRCUIT_OS)
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#if defined(CIRCUIT_OS)
|
||||
#include <kstdlib.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
|
||||
|
||||
#define KER_STRING_AVAILABLE
|
||||
|
@ -45,6 +80,12 @@ namespace ker
|
|||
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) :
|
||||
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;
|
||||
|
|
|
@ -2,11 +2,13 @@
|
|||
|
||||
#include <inttypes.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 <string.h>
|
||||
#include <new>
|
||||
#endif
|
||||
|
||||
|
@ -120,10 +122,8 @@ 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<int32_t>(index); i--) {
|
||||
for(int32_t i = this->mLength - 2; i >= static_cast<int32_t>(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<int32_t>(this->mLength) - 1; i > static_cast<int32_t>(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];
|
||||
|
|
3
include/kstring.h
Normal file
3
include/kstring.h
Normal file
|
@ -0,0 +1,3 @@
|
|||
#pragma once
|
||||
|
||||
#include "kstdlib.h"
|
|
@ -1,3 +0,0 @@
|
|||
#pragma once
|
||||
|
||||
#include "stdlib.h"
|
Loading…
Reference in a new issue