initial upload

This commit is contained in:
2026-02-26 12:09:40 -06:00
parent 102beb1f6f
commit 8f208f436c
16 changed files with 1183 additions and 0 deletions
+42
View File
@@ -0,0 +1,42 @@
#pragma once
#include <ckitty/memory/primitives.hpp>
#include <chrono>
#include <string>
#include <iostream>
#include <iomanip>
#include <sstream>
namespace ckitty {
namespace chrono {
using namespace std::chrono;
inline u64 millis() {
auto now = high_resolution_clock::now().time_since_epoch();
return duration_cast<milliseconds>(now).count();
}
inline u64 micros() {
auto now = high_resolution_clock::now().time_since_epoch();
return duration_cast<microseconds>(now).count();
}
// Date Time String // Could be done better but this is neat too
inline std::string dateTimeString(const std::string& format, const std::tm time) {
std::ostringstream oss;
oss << std::put_time(&time, format.c_str());
return oss.str();
}
inline std::string dateTimeString(const std::string& format = "%d-%m-%Y %H-%M-%S") {
const std::time_t t = std::time(nullptr);
std::tm tm = *std::localtime(&t);
return dateTimeString(format, tm);
}
}
}
+68
View File
@@ -0,0 +1,68 @@
#pragma once
#include <ckitty/memory/primitives.hpp>
namespace ckitty {
namespace environment {
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__)
//define something for Windows (32-bit and 64-bit, this part is common)
// Do not bother to differentiate 32 bits from 64 bits.
inline const std::string OS_NAME = "Windows";
inline const std::string DEVICE = "Desktop";
inline const std::string CODE = "desktop/windows";
#define CKITTY_OS_WINDOWS
#define CKITTY_DEVICE_DESKTOP
#elif __APPLE__
#include <TargetConditionals.h>
// iOS, tvOS, or watchOS Simulator
// #elif TARGET_OS_MACCATALYST
// Mac's Catalyst (ports iOS API into Mac, like UIKit).
#if defined(TARGET_IPHONE_SIMULATOR) || defined(TARGET_OS_IPHONE)
// iOS, tvOS, or watchOS device
inline const std::string OS_NAME = "iOS";
inline const std::string DEVICE = "Mobile";
inline const std::string CODE = "mobile/ios";
#define CKITTY_OS_IOS
#define CKITTY_DEVICE_MOBILE
#elif TARGET_OS_MAC
// Other kinds of Apple platforms
inline const std::string OS_NAME = "MacOS";
inline const std::string DEVICE = "Desktop";
inline const std::string CODE = "desktop/macos";
#define CKITTY_OS_MACOS
#define CKITTY_DEVICE_DESKTOP
#else
# error "Unknown Apple platform"
#endif
#elif __ANDROID__
// Below __linux__ check should be enough to handle Android,
// but something may be unique to Android.
// all unices not caught above
inline const std::string OS_NAME = "Android";
inline const std::string DEVICE = "Mobile";
inline const std::string CODE = "mobile/android";
#define CKITTY_OS_ANDROID
#define CKITTY_DEVICE_MOBILE
#elif defined(__linux__) || defined(__unix__)
// linux / unix
inline const std::string OS_NAME = "Linux";
inline const std::string DEVICE = "Desktop";
inline const std::string CODE = "desktop/linux";
#define CKITTY_OS_LINUX
#define CKITTY_DEVICE_DESKTOP
#elif defined(__EMSCRIPTEN__) || defined(__wasm__) || defined(__wasm)
// WASM
inline const std::string OS_NAME = "WASM";
inline const std::string DEVICE = "Browser";
inline const std::string CODE = "browser/wasm";
#define CKITTY_OS_WASM
#define CKITTY_DEVICE_BROWSER
#else
# error "Unknown compiler/system."
#endif
}
}
+62
View File
@@ -0,0 +1,62 @@
#pragma once
#include <ckitty/memory/primitives.hpp>
#include <sstream>
#include <iomanip>
namespace ckitty {
struct error {
i32 code;
std::string message;
error(i32 code = 0, const std::string& msg = "")
: code(code), message(msg) {
}
operator bool() const {
return code;
}
operator std::string() const {
std::ostringstream oss;
oss << "[" << std::hex << std::showbase << std::uppercase << std::setw(2) << std::setfill('0') << code << "] ";
oss << (message.empty() ? "(no info)" : message);
return oss.str();
}
error& operator+=(const error& err) {
auto err1 = (*this) + err;
code = err1.code;
message = err1.message;
return *this;
}
error operator+(const error& err) const {
// If this error has no code (no error), return the other error
if (code == 0) {
return err;
}
// If the other error has no code, return this error
if (err.code == 0) {
return *this;
}
// Both errors have codes, combine them
error result = *this;
// Combine messages
if (!result.message.empty()) {
result.message += "\n ";
}
// add next error to the message
result.message += err.operator std::string();
return result;
}
};
}
+104
View File
@@ -0,0 +1,104 @@
#pragma once
#include <ckitty/memory/primitives.hpp>
#include <string>
#include <algorithm>
#include <cctype>
#include <locale>
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;
}
// ...
}
}
+86
View File
@@ -0,0 +1,86 @@
#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;
}
}
}