Add bstring type

This commit is contained in:
2017-04-28 01:36:47 -05:00
parent 6bc6230eb6
commit 50ecefdb38
4 changed files with 87 additions and 0 deletions
+1
View File
@@ -20,6 +20,7 @@ install(
byteswap.hpp
memops.hpp
random.hpp
string.hpp
strops.hpp
std.hpp
types.hpp
+1
View File
@@ -12,4 +12,5 @@
#include "memops.hpp"
#include "random.hpp"
#include "strops.hpp"
#include "string.hpp"
#include "types.hpp"
+84
View File
@@ -0,0 +1,84 @@
/*
* Copyright 2015 - 2017 gtalent2@gmail.com
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
#pragma once
#include "types.hpp"
namespace ox {
// Bounded String
template<size_t buffLen>
class bstring {
private:
uint8_t m_buff[buffLen];
public:
bstring(const char *str);
const bstring &operator=(const char *str);
const bstring &operator=(char *str);
/**
* Returns the number of characters in this string.
*/
size_t len();
/**
* Returns the number of bytes used for this string.
*/
size_t size();
};
template<size_t size>
bstring<size>::bstring(const char *str) {
*this = str;
}
template<size_t size>
const bstring<size> &bstring<size>::operator=(const char *str) {
return *this = (const char*) str;
}
template<size_t size>
const bstring<size> &bstring<size>::operator=(char *str) {
auto strLen = ox_strlen(str) + 1;
if (size() < strLen) {
strLen = size();
}
ox_memcpy(m_buff, str, strLen);
// make sure last element is a null terminator
m_buff[size() - 1] = 0;
return *this;
}
template<size_t buffLen>
size_t bstring<buffLen>::len() {
size_t length = 0;
for (size_t i = 0; i < buffLen; i++) {
auto b = m_buff[i];
if (b) {
if ((b & (1 << 8)) == 0) { // normal ASCII character
length++;
} else if ((b & (256 << 6)) == (256 << 6)) { // start of UTF-8 character
length++;
}
} else {
break;
}
}
return length;
}
template<size_t buffLen>
size_t bstring<buffLen>::size() {
return buffLen;
}
}
+1
View File
@@ -5,6 +5,7 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
#pragma once
typedef signed char int8_t;