Add formatting, printing, new stl stuff, unfuck vec, change clangd styling

This commit is contained in:
Gvidas Juknevičius 2025-05-18 18:51:05 +03:00
parent a45b7cbb21
commit 3b1b2bfb06
Signed by: MCorange
GPG Key ID: 5BE6B533CB76FE86
15 changed files with 284 additions and 54 deletions

View File

@ -177,7 +177,7 @@ RequiresClausePosition: OwnLine
RequiresExpressionIndentation: OuterScope
SeparateDefinitionBlocks: Always
ShortNamespaceLines: 1
SortIncludes: CaseSensitive
SortIncludes: Never
SortJavaStaticImport: Before
SortUsingDeclarations: LexicographicNumeric
SpaceAfterCStyleCast: false

33
.vscode/launch.json vendored Normal file
View File

@ -0,0 +1,33 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/test/test",
"args": [],
"stopAtEntry": false,
"cwd": "${fileDirname}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
},
{
"description": "Set Disassembly Flavor to Intel",
"text": "-gdb-set disassembly-flavor intel",
"ignoreFailures": true
}
]
}
]
}

View File

@ -3,13 +3,13 @@
#include "result.hpp"
#include <option.hpp>
#include <stdint.h>
#include <stddef.h>
#include <stdio.h>
template <typename T>
class Vec {
private:
T* items;
T* items = nullptr;
size_t count = 0;
size_t capacity = 0;
@ -24,14 +24,14 @@ public:
if (this->count <= n) {
return Option<T*>();
}
return Option<T*>(this->items[n]);
return Option<T*>(&this->items[n]);
}
Option<const T*> nth(const size_t n) const {
if (this->count <= n) {
return Option<const T*>();
}
return Option<const T*>(this->items[n]);
return Option<const T*>(&this->items[n]);
}
Option<T> pop() {
@ -88,6 +88,17 @@ public:
this->count = 0;
}
T* to_ptr() {
// Special case for cstr
// makes items[count] a null value so the cstr is valid
// TODO: Maybe make Vec::to_ptr only do this when T is char
if (this->count >= this->capacity) {
this->reserve(this->capacity * 2);
}
this->items[this->count] = (T)NULL;
return this->items;
}
static Vec<T> from_ptr(const T* ptr, size_t len) {
Vec<T> vec = Vec();
for (size_t i = 0; i < len; i++) {
@ -100,11 +111,13 @@ public:
return this->count;
}
// IMPL: Clone
Vec clone() {
Vec nv = Vec();
nv.items = new T(*this->items);
nv.count = this->count;
nv.capacity = this->capacity;
nv = *this;
// nv.items = new T(*this->items);
// nv.count = this->count;
// nv.capacity = this->capacity;
return nv;
}
};

View File

@ -0,0 +1,54 @@
#ifndef _H_LIBCP_FORMAT_BASE_TYPES
#define _H_LIBCP_FORMAT_BASE_TYPES
#include <string.hpp>
extern "C" {
int snprintf(char* __restrict s, size_t maxlen, const char* __restrict format, ...);
}
template <typename T>
struct Formatter;
template <>
struct Formatter<int> {
static String fmt_to_str(const int& val) {
char buf[32];
snprintf(buf, sizeof(buf), "%d", val);
return String(buf);
}
};
template <>
struct Formatter<const char*> {
static String fmt_to_str(const char* val) {
return String(val);
}
};
template <>
struct Formatter<char*> {
static String fmt_to_str(char* val) {
return String(val);
}
};
template <>
struct Formatter<float> {
static String fmt_to_str(const float& val) {
char buf[32];
snprintf(buf, sizeof(buf), "%f", val);
return String(buf);
}
};
template <>
struct Formatter<double> {
static String fmt_to_str(const double& val) {
char buf[32];
snprintf(buf, sizeof(buf), "%f", val);
return String(buf);
}
};
#endif // !_H_LIBCP_FORMAT_BASE_TYPES

View File

@ -1,12 +1,91 @@
#ifndef _H_LIBCP_FORMAT_FMT
#define _H_LIBCP_FORMAT_FMT
#include <stl/is_same.hpp>
#include <collections/vec.hpp>
#include <stddef.h>
#include <stl.hpp>
#include <string.h>
#include <string.hpp>
#include "format/base_types.hpp"
template <typename T>
concept Format = requires(T t) {
{ t.fmt_as_str() } -> same_as<String>;
{ t.fmt_to_str() } -> stl::same_as<String>;
} || requires(T t) {
{ Formatter<T>::fmt_to_str(t) } -> stl::same_as<String>;
};
#ifndef __CONCEPT_ONLY
namespace __internal {
String format(Vec<String>& args, String& _fmt) {
const char* fmt = _fmt.as_cstr();
String str = String();
size_t args_idx = 0;
for (size_t i = 0; i < strlen(fmt); i++) {
if (fmt[i] == '{') {
if (fmt[i + 1] == '{') {
str.push('{');
str.push('{');
i += 1;
continue;
}
if (fmt[i + 1] == '}') {
i += 1;
String* arg = args.nth(args_idx).unwrap();
str.push_str(*arg);
args_idx += 1;
}
continue;
}
str.push(fmt[i]);
}
return str;
}
template <Format T, typename... Types>
String format(Vec<String>& args, String& fmt, T var1, Types... var2) {
using ActualType = stl::remove_cvref_t<decltype(var1)>;
if constexpr (requires(ActualType v) { v.fmt_to_str(); }) {
args.push(var1.fmt_to_str());
} else {
args.push(Formatter<ActualType>::fmt_to_str(var1));
}
return __internal::format(args, fmt, var2...);
}
} // namespace __internal
template <typename... Types>
String format(const char* fmt, Types... vars) {
Vec<String> args = Vec<String>();
String fmt_ = String(fmt);
return __internal::format(args, fmt_, vars...);
}
template <Format T>
struct Formatter<Vec<T>> {
static String fmt_to_str(Vec<T>& val) {
String s = String();
s.push('[');
if (val.len() == 0) {
s.push(']');
return s;
}
String tmp1 = format("{}", *val.nth(0).unwrap()).fmt_to_str();
s.push_str(tmp1);
for (size_t i = 1; i < val.len(); i++) {
s.push_str(", ");
String tmp2 = format("{}", *val.nth(i).unwrap());
s.push_str(tmp2);
}
s.push(']');
return s;
}
};
#endif // __CONCEPT_ONLY
#endif // _H_LIBCP_FORMAT_FMT

23
src/include/io/print.hpp Normal file
View File

@ -0,0 +1,23 @@
#ifndef _H_LIBCP_IO_PRINT
#define _H_LIBCP_IO_PRINT
#include <format/fmt.hpp>
extern "C" {
int puts(const char* s);
int putchar(int c);
}
template <typename... Types>
void print(const char* fmt, Types... args) {
String s = format(fmt, args...);
puts(s.as_cstr());
}
template <typename... Types>
void println(const char* fmt, Types... args) {
print(fmt, args...);
putchar('\n');
}
#endif // !_H_LIBCP_IO_PRINT

View File

@ -2,9 +2,12 @@
#ifndef _H_LIBCP
#define _H_LIBCP
namespace std {
#include <format.hpp>
#include <stdint.h>
#include <string.hpp>
#include <collections/vec.hpp>
#include <format/fmt.hpp>
#include <stl.hpp>
} // namespace std
#include <io/print.hpp>
#endif // _H_LIBCP

View File

@ -1,30 +0,0 @@
#ifndef _H_LIBCP_STDDEF
#define _H_LIBCP_STDDEF
#if defined(__x86_64__) || defined(_M_X64)
using size_t = unsigned long;
using ptrdiff_t = long;
using intptr_t = long;
using uintptr_t = unsigned long;
#elif defined(__i386__) || defined(_M_IX86)
using size_t = unsigned int;
using ptrdiff_t = int;
using intptr_t = int;
using uintptr_t = unsigned int;
#else
#error "Unsupported architecture for my_std types nya~"
#endif
// Exact-width types — assuming standard model (you can static_assert later!)
using int8_t = signed char;
using int16_t = short;
using int32_t = int;
using int64_t = long long;
using uint8_t = unsigned char;
using uint16_t = unsigned short;
using uint32_t = unsigned int;
using uint64_t = unsigned long long;
#endif // _H_LIBCP_STDDEF

View File

@ -7,6 +7,7 @@ namespace stl {
#include <stl/is_convertible.hpp>
#include <stl/is_same.hpp>
#include <stl/move.hpp>
#include <stl/remove_cvref.hpp>
#include <stl/remove_reference.hpp>
} // namespace stl

View File

@ -0,0 +1,28 @@
#ifndef _H_LIBCP_STL_REMOVE_CVREF
#define _H_LIBCP_STL_REMOVE_CVREF
#include <stl/is_same.hpp>
// For gcc
// template <class _Tp>
// struct __remove_cvref_gcc {
// using type = __remove_cvref(_Tp);
// };
// template <class _Tp>
// using __remove_cvref_t _LIBCPP_NODEBUG = typename __remove_cvref_gcc<_Tp>::type;
template <class _Tp>
using __remove_cvref_t = __remove_cvref(_Tp);
template <class _Tp, class _Up>
using __is_same_uncvref = is_same<__remove_cvref_t<_Tp>, __remove_cvref_t<_Up>>;
template <class _Tp>
struct remove_cvref {
using type = __remove_cvref(_Tp);
};
template <class _Tp>
using remove_cvref_t = __remove_cvref_t<_Tp>;
#endif // _H_LIBCP_STL_REMOVE_CVREF

View File

@ -14,6 +14,11 @@ public:
size_t len();
void clear();
Vec<char>& chars();
char* const as_cstr();
void push(char c);
void push_str(String& str);
// void push_str(String&& str);
void push_str(const char* str);
// IMPL: Format
String fmt_to_str();
// IMPL: Clone

View File

@ -1 +0,0 @@

View File

@ -2,7 +2,7 @@
#include <string.hpp>
String::String(const char* str) {
this->_chars.from_ptr((char*)str, strlen(str));
this->_chars = Vec<char>::from_ptr((char*)str, strlen(str));
}
size_t String::len() {
@ -21,6 +21,30 @@ String String::fmt_to_str() {
return this->clone();
}
char* const String::as_cstr() {
return this->_chars.to_ptr();
}
void String::push(char c) {
this->_chars.push(c);
}
void String::push_str(String& str) {
Vec<char> chrs = str.chars();
for (size_t i = 0; i < chrs.len(); i++) {
// SAFETY: Always Valid
this->push(*chrs.nth(i).unwrap());
}
}
void String::push_str(const char* str) {
for (size_t i = 0; i < strlen(str); i++) {
this->push(str[i]);
}
}
// IMPL: Clone
String String::clone() {
String s = String();
s._chars = this->_chars.clone();

BIN
test/test

Binary file not shown.

View File

@ -1,13 +1,11 @@
#include <collections/vec.hpp>
#include <stddef.h>
#include <stdio.h>
#include <std.hpp>
int main(int argc, char* argv[]) {
Vec<size_t>* v = new Vec<size_t>();
v->push(69);
v->push(420);
size_t ft = v->pop().unwrap();
size_t sn = v->pop().unwrap();
printf("ft: %zu sn: %zu\n", ft, sn);
Vec<int> v = Vec<int>();
v.push(1);
v.push(2);
v.push(3);
v.push(4);
println("Hello!!!! {}, {}, {}, {}", "meow", 420.0, 49, v);
return 0;
}