some changes

This commit is contained in:
2026-07-29 10:22:20 -06:00
parent 8f208f436c
commit a23f9f7855
12 changed files with 117 additions and 240 deletions
+4 -4
View File
@@ -127,12 +127,12 @@ namespace ckitty {
if(str.length() != 36) throw std::runtime_error(""); if(str.length() != 36) throw std::runtime_error("");
// 00000000-0000-0000-0000-000000000000 // 00000000-0000-0000-0000-000000000000
std::array<size, 4> hyphens = {8, 13, 18, 23}; std::array<isize, 4> hyphens = {8, 13, 18, 23};
UUID id; UUID id;
size j = 0; isize j = 0;
// for loop the chars // for loop the chars
for(size i = 0; i < 36; i += 2) { for(isize i = 0; i < 36; i += 2) {
// check hypen // check hypen
if(str[i] == '-') { if(str[i] == '-') {
if(!it::contains(hyphens, i)) if(!it::contains(hyphens, i))
@@ -142,7 +142,7 @@ namespace ckitty {
// parse... // parse...
u8 write = 0; u8 write = 0;
for(size n = 0; n < 2; n++) { for(isize n = 0; n < 2; n++) {
int hex = strings::hexChar(str[i + n]) << ((1 - n) * 4); int hex = strings::hexChar(str[i + n]) << ((1 - n) * 4);
if (hex == -1) throw std::runtime_error("Error: Unexpected character \"" + strings::display(str[i + n]) + "\" position " + std::to_string(i + n) + "."); if (hex == -1) throw std::runtime_error("Error: Unexpected character \"" + strings::display(str[i + n]) + "\" position " + std::to_string(i + n) + ".");
write |= hex & 0xFF; write |= hex & 0xFF;
+2 -2
View File
@@ -8,9 +8,9 @@ namespace ckitty {
namespace base64 { namespace base64 {
inline std::string encode(const u8* data, const size length) { inline std::string encode(const u8* data, const isize length) {
u8 buffer[length * 4 / 3 + 2]; u8 buffer[length * 4 / 3 + 2];
size strlen = EVP_EncodeBlock(buffer, data, length); isize strlen = EVP_EncodeBlock(buffer, data, length);
return std::string((const char*) buffer, strlen); return std::string((const char*) buffer, strlen);
} }
+1 -1
View File
@@ -27,7 +27,7 @@ namespace ckitty {
* Use public (private = false) if the bytes are general purpose. * Use public (private = false) if the bytes are general purpose.
* Use private if the bytes are meant to be secure, i.e. UUID. * Use private if the bytes are meant to be secure, i.e. UUID.
*/ */
inline void randomBytes(u8* bytes, size length, bool usePrivate = false) { inline void randomBytes(u8* bytes, isize length, bool usePrivate = false) {
int r; int r;
if (usePrivate) r = RAND_priv_bytes(bytes, length); if (usePrivate) r = RAND_priv_bytes(bytes, length);
else r = RAND_bytes(bytes, length); else r = RAND_bytes(bytes, length);
+7 -7
View File
@@ -19,14 +19,14 @@ namespace ckitty {
protected: protected:
ptr<T[]> _data; ptr<T[]> _data;
size _length; isize _length;
public: public:
/** /**
* Inherits a pointer, and takes ownership of it. * Inherits a pointer, and takes ownership of it.
*/ */
static array<T> inherit(T* data, size length) { static array<T> inherit(T* data, isize length) {
array<T> arr = array<T>(); array<T> arr = array<T>();
arr._data = ptr<T[]>(data); arr._data = ptr<T[]>(data);
arr._length = length; arr._length = length;
@@ -37,7 +37,7 @@ namespace ckitty {
* @brief Borrows a modifiable part of a buffer to * @brief Borrows a modifiable part of a buffer to
* this array. The array does not delete it. * this array. The array does not delete it.
*/ */
static array<T> borrow(T* data, size length) { static array<T> borrow(T* data, isize length) {
array<T> arr = array<T>(); array<T> arr = array<T>();
arr._data = ptr<T[]>(data, []([[maybe_unused]] T* _ptr) {}); arr._data = ptr<T[]>(data, []([[maybe_unused]] T* _ptr) {});
arr._length = length; arr._length = length;
@@ -46,7 +46,7 @@ namespace ckitty {
public: public:
array(const size length = 0) array(const isize length = 0)
: _data(new T[length]), _length(length) {} : _data(new T[length]), _length(length) {}
array(const T* begin, const T* end) array(const T* begin, const T* end)
@@ -75,15 +75,15 @@ namespace ckitty {
return *this; return *this;
} }
T& operator[](const size i) { T& operator[](const isize i) {
return *(_data.get() + i); return *(_data.get() + i);
} }
const T& operator[](const size i) const { const T& operator[](const isize i) const {
return *(_data.get() + i); return *(_data.get() + i);
} }
size length() const { isize length() const {
return _length; return _length;
} }
+3 -3
View File
@@ -38,18 +38,18 @@ namespace ckitty {
} }
template<typename T> template<typename T>
inline bool read(const T value, size bitno) { inline bool read(const T value, isize bitno) {
return (value >> bitno) & 1; return (value >> bitno) & 1;
} }
template<typename T> template<typename T>
inline T write(const T value, size bitno, bool bitval) { inline T write(const T value, isize bitno, bool bitval) {
size mask = T(1) << bitno; size mask = T(1) << bitno;
return bitval ? (value | mask) : (value & ~mask); return bitval ? (value | mask) : (value & ~mask);
} }
template<typename T> template<typename T>
inline void write_r(T& value, size bitno, bool bitval) { inline void write_r(T& value, isize bitno, bool bitval) {
value = write(value, bitno, bitval); value = write(value, bitno, bitval);
} }
+13 -27
View File
@@ -19,7 +19,7 @@ namespace ckitty {
* @brief The sole purpose of this function * @brief The sole purpose of this function
* is to stop me from confusing the signs. * is to stop me from confusing the signs.
*/ */
inline bool fits(size index, size length) { inline bool fits(isize index, isize length) {
return index < length; return index < length;
} }
@@ -27,7 +27,7 @@ namespace ckitty {
* @brief Returns true if some data fits in the * @brief Returns true if some data fits in the
* provided space. * provided space.
*/ */
inline bool fits(size offset, size length, size test) { inline bool fits(isize offset, isize length, isize test) {
return (offset < length) && ((length - offset) >= test); return (offset < length) && ((length - offset) >= test);
} }
@@ -40,7 +40,7 @@ namespace ckitty {
*/ */
inline bool matches(data_view d1, data_view d2) { inline bool matches(data_view d1, data_view d2) {
if(d1.length() != d2.length()) return false; if(d1.length() != d2.length()) return false;
for(size i = 0; i < d1.length(); i++) { for(isize i = 0; i < d1.length(); i++) {
if(d1[i] != d2[i]) return false; if(d1[i] != d2[i]) return false;
} }
return true; return true;
@@ -51,15 +51,15 @@ namespace ckitty {
*/ */
inline bool startsWith(data_view d1, data_view pre) { inline bool startsWith(data_view d1, data_view pre) {
if(d1.length() < pre.length()) return false; if(d1.length() < pre.length()) return false;
for(size i = 0; i < pre.length(); i++) { for(isize i = 0; i < pre.length(); i++) {
if(d1[i] != pre[i]) return false; if(d1[i] != pre[i]) return false;
} }
return true; return true;
} }
inline bool find(data_view data, data_view test, size& index) { inline bool find(data_view data, data_view test, isize& index) {
// loop until we find match // loop until we find match
for(size off = 0; off < data.length(); off++) { for(isize off = 0; off < data.length(); off++) {
// fits? // fits?
if(!fits(off, data.length(), test.length())) return false; if(!fits(off, data.length(), test.length())) return false;
@@ -76,7 +76,7 @@ namespace ckitty {
* @brief Checks if an index is in between * @brief Checks if an index is in between
* min and max, inclusively. * min and max, inclusively.
*/ */
inline bool in_range(size min, size index, size max) { inline bool in_range(isize min, isize index, isize max) {
return min <= index && index <= max; return min <= index && index <= max;
} }
@@ -86,7 +86,7 @@ namespace ckitty {
* previous one. * previous one.
*/ */
inline buffer moveRealocate(buffer& buff) { inline buffer moveRealocate(buffer& buff) {
size len = buff.length(); isize len = buff.length();
buffer out = std::move(buff); buffer out = std::move(buff);
buff = buffer(len); buff = buffer(len);
return out; return out;
@@ -96,30 +96,16 @@ namespace ckitty {
return data_view((const u8*) str.c_str(), str.length()); return data_view((const u8*) str.c_str(), str.length());
} }
inline void copy(buffer& buff, size& index, const std::string& str) { inline void copy(buffer& buff, isize& index, const std::string& str) {
std::copy(str.begin(), str.end(), buff.begin() + index); std::copy(str.begin(), str.end(), buff.begin() + index);
index += str.size(); index += str.size();
} }
inline void copy(buffer& buff, size& index, const buffer& data) { inline void copy(buffer& buff, isize& index, const buffer& data) {
std::copy(data.begin(), data.end(), buff.begin() + index); std::copy(data.begin(), data.end(), buff.begin() + index);
index += data.length(); index += data.length();
} }
inline void hex(data_view view, std::ostream& os) {
char chars[] = {
'0', '1', '2', '3',
'4', '5', '6', '7',
'8', '9', 'A', 'B',
'C', 'D', 'E', 'F'
};
for(u8 c : view) {
os << chars[ (c >> 4) & 0xF ];
os << chars[ c & 0xF ];
}
}
inline void hexdump(data_view view, std::ostream& os, std::string prefix = "") { inline void hexdump(data_view view, std::ostream& os, std::string prefix = "") {
// format: // format:
// FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF // FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF
@@ -132,9 +118,9 @@ namespace ckitty {
}; };
// how many rows chars to use? // how many rows chars to use?
double logv = std::log(std::max<size>(1, view.length())); double logv = std::log(std::max<isize>(1, view.length()));
double spaces = std::floor((long double) (logv / std::log(16) + 1)); double spaces = std::floor((long double) (logv / std::log(16) + 1));
size rowchars = std::max<size>(2, spaces) - 1; isize rowchars = std::max<isize>(2, spaces) - 1;
// print header // print header
os << prefix << std::string(rowchars, ' ') << " X0 X1 X2 X3 X4 X5 X6 X7 X8 X9 XA XB XC XD XE XF | -------- --------\n"; os << prefix << std::string(rowchars, ' ') << " X0 X1 X2 X3 X4 X5 X6 X7 X8 X9 XA XB XC XD XE XF | -------- --------\n";
@@ -150,7 +136,7 @@ namespace ckitty {
os << prefix << "\n"; os << prefix << "\n";
// loop through the bytes // loop through the bytes
size index = 0, row = 0; isize index = 0, row = 0;
while(index < view.length()) { while(index < view.length()) {
// ascii buffer // ascii buffer
std::string ascii; std::string ascii;
+6 -6
View File
@@ -19,7 +19,7 @@ namespace ckitty {
data_view(const u8* begin, const u8* end) data_view(const u8* begin, const u8* end)
: _begin(begin), _end(end) {} : _begin(begin), _end(end) {}
data_view(const u8* data, const size length) data_view(const u8* data, const isize length)
: _begin(data), _end(data + length) {} : _begin(data), _end(data + length) {}
data_view(const std::string& str) data_view(const std::string& str)
@@ -34,19 +34,19 @@ namespace ckitty {
return _end; return _end;
} }
u8 operator[](size i) const { u8 operator[](isize i) const {
return *(_begin + i); return *(_begin + i);
} }
size length() const { isize length() const {
return _end - _begin; return isize(_end - _begin);
} }
const data_view sub_off(size offset, size length) const { const data_view sub_off(isize offset, isize length) const {
return data_view(_begin + offset, _begin + offset + length); return data_view(_begin + offset, _begin + offset + length);
} }
const data_view sub_abs(size begin, size end) const { const data_view sub_abs(isize begin, isize end) const {
return data_view(_begin + begin, _begin + end); return data_view(_begin + begin, _begin + end);
} }
+1 -1
View File
@@ -21,7 +21,7 @@ namespace ckitty {
typedef std::int32_t i32; typedef std::int32_t i32;
typedef std::int64_t i64; typedef std::int64_t i64;
typedef std::size_t size; typedef std::size_t isize;
typedef float f32; typedef float f32;
typedef double f64; typedef double f64;
View File
@@ -1,104 +1,78 @@
#pragma once #pragma once
#include <ckitty/memory/primitives.hpp> #include <ckitty/memory/primitives.hpp>
#include <string> #include <string>
#include <algorithm> #include <algorithm>
#include <cctype> #include <cctype>
#include <locale> #include <locale>
namespace ckitty { namespace ckitty {
namespace strings { namespace strings {
// trim spaces // trim spaces
inline void ltrim(std::string &s) { inline void ltrim(std::string &s) {
s.erase(s.begin(), std::find_if(s.begin(), s.end(), s.erase(s.begin(), std::find_if(s.begin(), s.end(),
[](unsigned char ch) { [](unsigned char ch) {
return !std::isspace(ch); return !std::isspace(ch);
})); }));
} }
inline void rtrim(std::string &s) { inline void rtrim(std::string &s) {
s.erase(std::find_if(s.rbegin(), s.rend(), s.erase(std::find_if(s.rbegin(), s.rend(),
[](unsigned char ch) { [](unsigned char ch) {
return !std::isspace(ch); return !std::isspace(ch);
}).base(), s.end()); }).base(), s.end());
} }
inline void trim(std::string& str) { inline void trim(std::string& str) {
ltrim(str); ltrim(str);
rtrim(str); rtrim(str);
} }
inline std::string trimc(const std::string& str) { inline std::string trimc(const std::string& str) {
std::string copy = str; std::string copy = str;
trim(copy); trim(copy);
return copy; return copy;
} }
inline std::string ltrimc(const std::string& str) { inline std::string ltrimc(const std::string& str) {
std::string copy = str; std::string copy = str;
ltrim(copy); ltrim(copy);
return copy; return copy;
} }
inline std::string rtrimc(const std::string& str) { inline std::string rtrimc(const std::string& str) {
std::string copy = str; std::string copy = str;
rtrim(copy); rtrim(copy);
return copy; return copy;
} }
/** // ascii upper & lower
* Returns a displayable representation of the character.
*/ inline void upper(std::string& str) {
inline std::string display(char c) { std::transform(str.begin(), str.end(), str.begin(), ::toupper);
if(std::isprint(c)) return std::string(1, c); }
char chars[] = { inline void lower(std::string& str) {
'0', '1', '2', '3', std::transform(str.begin(), str.end(), str.begin(), ::tolower);
'4', '5', '6', '7', }
'8', '9', 'A', 'B',
'C', 'D', 'E', 'F' inline std::string copy_upper(const std::string& str) {
}; std::string copy = str;
upper(copy);
std::string str = "0x00"; return copy;
str[2] = chars[ (c >> 4) & 0xF ]; }
str[3] = chars[ c & 0xF ];
return str; inline std::string copy_lower(const std::string& str) {
} std::string copy = str;
lower(copy);
inline int hexChar(char c) { return copy;
if('0' <= c && c <= '9') return c - '0'; }
if('a' <= c && c <= 'f') return c - 'a' + 10;
if('A' <= c && c <= 'F') return c - 'A' + 10; // ...
return -1;
} }
// ascii upper & lower
inline void upper(std::string& str) {
std::transform(str.begin(), str.end(), str.begin(), ::toupper);
}
inline void lower(std::string& str) {
std::transform(str.begin(), str.end(), str.begin(), ::tolower);
}
inline std::string copy_upper(const std::string& str) {
std::string copy = str;
upper(copy);
return copy;
}
inline std::string copy_lower(const std::string& str) {
std::string copy = str;
lower(copy);
return copy;
}
// ...
}
} }
+3
View File
@@ -0,0 +1,3 @@
#pragma once
// Hooks into the x-icu project
-86
View File
@@ -1,86 +0,0 @@
#pragma once
#include <ckitty/memory/array.hpp>
#include <ckitty/system/strings.hpp>
/*
* NOTICE: This is the standalone unicode library for ckittylib.
* If using ICU, use the /unicode folder's headers.
*/
namespace ckitty {
namespace unicode {
using sequences = std::string;
using string = std::u32string;
/**
* Returns the amount of bytes needed to
* represent an UTF-8 character given
* the first byte of the sequence.
*
* Returns 0 for invalid sequences.
*/
inline u32 sequenceLength(const u8 head) {
return (head - 160 >> 20 - head / 16) + 2;
}
/**
* Encodes a UTF-32 character into a UTF-8 sequence.
*/
inline sequences encodeChar(const u32 cha) {}
/**
* Decodes a UTF-8 sequence (the first one) into a UTF-32 character.
* Invalid sequences return 0 and raise a flag.
*/
inline u32 decodeChar(const sequences& cha, bool* error = nullptr) {}
inline sequences encodeChar(const u32 cha) {}
struct decode_result {
// Was the result valid?
bool valid = false;
// First character error.
u64 errCharIndex = 0;
// Byte index of character.
u64 errByteIndex = 0;
// UTF32 result
string output;
};
/**
* Decodes all UTF-8 sequences into 32 bit chars.
* Invalid ones are read as one byte.
*/
inline decode_result decodeUTF8(const sequences& str) {
decode_result r;
return r;
}
/**
* Encodes UTF32 into UTF-8 byte sequences.
*/
inline sequences encodeUTF8(const string& str) {}
struct validate_result {
// Was the result valid?
bool valid = false;
// First character error.
u64 errCharIndex = 0;
// Byte index of character.
u64 errByteIndex = 0;
};
/**
* Validates that a std::string is made out of valid UTF8 sequences.
*/
inline validate_result validateUTF8(const sequences& str) {
validate_result r;
return r;
}
}
}