From a23f9f78559a01e7b88dce465ff3d9d70239de2d Mon Sep 17 00:00:00 2001 From: Kittycannon Date: Wed, 29 Jul 2026 10:22:20 -0600 Subject: [PATCH] some changes --- src/ckitty/crypto/UUID.hpp | 8 +- src/ckitty/crypto/base64.hpp | 4 +- src/ckitty/crypto/openssl.hpp | 2 +- src/ckitty/memory/array.hpp | 14 +- src/ckitty/memory/bits.hpp | 6 +- src/ckitty/memory/buffers.hpp | 40 ++-- src/ckitty/memory/data_view.hpp | 12 +- src/ckitty/memory/primitives.hpp | 2 +- src/ckitty/strings/unicode.hpp | 0 .../{system/strings.hpp => strings/utils.hpp} | 180 ++++++++---------- src/ckitty/strings/xicu.hpp | 3 + src/ckitty/system/unicode.hpp | 86 --------- 12 files changed, 117 insertions(+), 240 deletions(-) create mode 100644 src/ckitty/strings/unicode.hpp rename src/ckitty/{system/strings.hpp => strings/utils.hpp} (67%) create mode 100644 src/ckitty/strings/xicu.hpp delete mode 100644 src/ckitty/system/unicode.hpp diff --git a/src/ckitty/crypto/UUID.hpp b/src/ckitty/crypto/UUID.hpp index 0dbac9a..685f663 100644 --- a/src/ckitty/crypto/UUID.hpp +++ b/src/ckitty/crypto/UUID.hpp @@ -127,12 +127,12 @@ namespace ckitty { if(str.length() != 36) throw std::runtime_error(""); // 00000000-0000-0000-0000-000000000000 - std::array hyphens = {8, 13, 18, 23}; + std::array hyphens = {8, 13, 18, 23}; UUID id; - size j = 0; + isize j = 0; // for loop the chars - for(size i = 0; i < 36; i += 2) { + for(isize i = 0; i < 36; i += 2) { // check hypen if(str[i] == '-') { if(!it::contains(hyphens, i)) @@ -142,7 +142,7 @@ namespace ckitty { // parse... 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); if (hex == -1) throw std::runtime_error("Error: Unexpected character \"" + strings::display(str[i + n]) + "\" position " + std::to_string(i + n) + "."); write |= hex & 0xFF; diff --git a/src/ckitty/crypto/base64.hpp b/src/ckitty/crypto/base64.hpp index 466e0e9..ac79fd5 100644 --- a/src/ckitty/crypto/base64.hpp +++ b/src/ckitty/crypto/base64.hpp @@ -8,9 +8,9 @@ namespace ckitty { 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]; - size strlen = EVP_EncodeBlock(buffer, data, length); + isize strlen = EVP_EncodeBlock(buffer, data, length); return std::string((const char*) buffer, strlen); } diff --git a/src/ckitty/crypto/openssl.hpp b/src/ckitty/crypto/openssl.hpp index 46680ba..b860640 100644 --- a/src/ckitty/crypto/openssl.hpp +++ b/src/ckitty/crypto/openssl.hpp @@ -27,7 +27,7 @@ namespace ckitty { * Use public (private = false) if the bytes are general purpose. * 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; if (usePrivate) r = RAND_priv_bytes(bytes, length); else r = RAND_bytes(bytes, length); diff --git a/src/ckitty/memory/array.hpp b/src/ckitty/memory/array.hpp index 88e947d..86e508a 100644 --- a/src/ckitty/memory/array.hpp +++ b/src/ckitty/memory/array.hpp @@ -19,14 +19,14 @@ namespace ckitty { protected: ptr _data; - size _length; + isize _length; public: /** * Inherits a pointer, and takes ownership of it. */ - static array inherit(T* data, size length) { + static array inherit(T* data, isize length) { array arr = array(); arr._data = ptr(data); arr._length = length; @@ -37,7 +37,7 @@ namespace ckitty { * @brief Borrows a modifiable part of a buffer to * this array. The array does not delete it. */ - static array borrow(T* data, size length) { + static array borrow(T* data, isize length) { array arr = array(); arr._data = ptr(data, []([[maybe_unused]] T* _ptr) {}); arr._length = length; @@ -46,7 +46,7 @@ namespace ckitty { public: - array(const size length = 0) + array(const isize length = 0) : _data(new T[length]), _length(length) {} array(const T* begin, const T* end) @@ -75,15 +75,15 @@ namespace ckitty { return *this; } - T& operator[](const size i) { + T& operator[](const isize i) { return *(_data.get() + i); } - const T& operator[](const size i) const { + const T& operator[](const isize i) const { return *(_data.get() + i); } - size length() const { + isize length() const { return _length; } diff --git a/src/ckitty/memory/bits.hpp b/src/ckitty/memory/bits.hpp index 5838970..1453271 100644 --- a/src/ckitty/memory/bits.hpp +++ b/src/ckitty/memory/bits.hpp @@ -38,18 +38,18 @@ namespace ckitty { } template - inline bool read(const T value, size bitno) { + inline bool read(const T value, isize bitno) { return (value >> bitno) & 1; } template - 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; return bitval ? (value | mask) : (value & ~mask); } template - 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); } diff --git a/src/ckitty/memory/buffers.hpp b/src/ckitty/memory/buffers.hpp index 69a2ed3..91e9b95 100644 --- a/src/ckitty/memory/buffers.hpp +++ b/src/ckitty/memory/buffers.hpp @@ -19,7 +19,7 @@ namespace ckitty { * @brief The sole purpose of this function * 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; } @@ -27,7 +27,7 @@ namespace ckitty { * @brief Returns true if some data fits in the * 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); } @@ -40,7 +40,7 @@ namespace ckitty { */ inline bool matches(data_view d1, data_view d2) { 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; } return true; @@ -51,15 +51,15 @@ namespace ckitty { */ inline bool startsWith(data_view d1, data_view pre) { 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; } 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 - for(size off = 0; off < data.length(); off++) { + for(isize off = 0; off < data.length(); off++) { // fits? if(!fits(off, data.length(), test.length())) return false; @@ -76,7 +76,7 @@ namespace ckitty { * @brief Checks if an index is in between * 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; } @@ -86,7 +86,7 @@ namespace ckitty { * previous one. */ inline buffer moveRealocate(buffer& buff) { - size len = buff.length(); + isize len = buff.length(); buffer out = std::move(buff); buff = buffer(len); return out; @@ -96,30 +96,16 @@ namespace ckitty { 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); 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); 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 = "") { // format: // 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? - double logv = std::log(std::max(1, view.length())); + double logv = std::log(std::max(1, view.length())); double spaces = std::floor((long double) (logv / std::log(16) + 1)); - size rowchars = std::max(2, spaces) - 1; + isize rowchars = std::max(2, spaces) - 1; // print header 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"; // loop through the bytes - size index = 0, row = 0; + isize index = 0, row = 0; while(index < view.length()) { // ascii buffer std::string ascii; diff --git a/src/ckitty/memory/data_view.hpp b/src/ckitty/memory/data_view.hpp index 80af3ca..be982ec 100644 --- a/src/ckitty/memory/data_view.hpp +++ b/src/ckitty/memory/data_view.hpp @@ -19,7 +19,7 @@ namespace ckitty { data_view(const u8* begin, const u8* 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) {} data_view(const std::string& str) @@ -34,19 +34,19 @@ namespace ckitty { return _end; } - u8 operator[](size i) const { + u8 operator[](isize i) const { return *(_begin + i); } - size length() const { - return _end - _begin; + isize length() const { + 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); } - 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); } diff --git a/src/ckitty/memory/primitives.hpp b/src/ckitty/memory/primitives.hpp index 6090a98..281daa2 100644 --- a/src/ckitty/memory/primitives.hpp +++ b/src/ckitty/memory/primitives.hpp @@ -21,7 +21,7 @@ namespace ckitty { typedef std::int32_t i32; typedef std::int64_t i64; - typedef std::size_t size; + typedef std::size_t isize; typedef float f32; typedef double f64; diff --git a/src/ckitty/strings/unicode.hpp b/src/ckitty/strings/unicode.hpp new file mode 100644 index 0000000..e69de29 diff --git a/src/ckitty/system/strings.hpp b/src/ckitty/strings/utils.hpp similarity index 67% rename from src/ckitty/system/strings.hpp rename to src/ckitty/strings/utils.hpp index 205c65f..8ead2c7 100644 --- a/src/ckitty/system/strings.hpp +++ b/src/ckitty/strings/utils.hpp @@ -1,104 +1,78 @@ -#pragma once - -#include -#include -#include -#include -#include - -namespace ckitty { - - namespace strings { - - // trim spaces - - inline void ltrim(std::string &s) { - s.erase(s.begin(), std::find_if(s.begin(), s.end(), - [](unsigned char ch) { - return !std::isspace(ch); - })); - } - - inline void rtrim(std::string &s) { - s.erase(std::find_if(s.rbegin(), s.rend(), - [](unsigned char ch) { - return !std::isspace(ch); - }).base(), s.end()); - } - - inline void trim(std::string& str) { - ltrim(str); - rtrim(str); - } - - inline std::string trimc(const std::string& str) { - std::string copy = str; - trim(copy); - return copy; - } - - inline std::string ltrimc(const std::string& str) { - std::string copy = str; - ltrim(copy); - return copy; - } - - inline std::string rtrimc(const std::string& str) { - std::string copy = str; - rtrim(copy); - return copy; - } - - /** - * Returns a displayable representation of the character. - */ - inline std::string display(char c) { - if(std::isprint(c)) return std::string(1, c); - - char chars[] = { - '0', '1', '2', '3', - '4', '5', '6', '7', - '8', '9', 'A', 'B', - 'C', 'D', 'E', 'F' - }; - - std::string str = "0x00"; - str[2] = chars[ (c >> 4) & 0xF ]; - str[3] = chars[ c & 0xF ]; - return str; - } - - inline int hexChar(char c) { - 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; - } - - // ... - - } - +#pragma once + +#include +#include +#include +#include +#include + +namespace ckitty { + + namespace strings { + + // trim spaces + + inline void ltrim(std::string &s) { + s.erase(s.begin(), std::find_if(s.begin(), s.end(), + [](unsigned char ch) { + return !std::isspace(ch); + })); + } + + inline void rtrim(std::string &s) { + s.erase(std::find_if(s.rbegin(), s.rend(), + [](unsigned char ch) { + return !std::isspace(ch); + }).base(), s.end()); + } + + inline void trim(std::string& str) { + ltrim(str); + rtrim(str); + } + + inline std::string trimc(const std::string& str) { + std::string copy = str; + trim(copy); + return copy; + } + + inline std::string ltrimc(const std::string& str) { + std::string copy = str; + ltrim(copy); + return copy; + } + + inline std::string rtrimc(const std::string& str) { + std::string copy = str; + rtrim(copy); + return copy; + } + + // 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; + } + + // ... + + } + } \ No newline at end of file diff --git a/src/ckitty/strings/xicu.hpp b/src/ckitty/strings/xicu.hpp new file mode 100644 index 0000000..f173801 --- /dev/null +++ b/src/ckitty/strings/xicu.hpp @@ -0,0 +1,3 @@ +#pragma once + +// Hooks into the x-icu project diff --git a/src/ckitty/system/unicode.hpp b/src/ckitty/system/unicode.hpp deleted file mode 100644 index a11961a..0000000 --- a/src/ckitty/system/unicode.hpp +++ /dev/null @@ -1,86 +0,0 @@ -#pragma once - -#include -#include - -/* - * 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; - } - - } - -} \ No newline at end of file