Add formatting, printing, new stl stuff, unfuck vec, change clangd styling
This commit is contained in:
parent
a45b7cbb21
commit
3b1b2bfb06
|
@ -177,7 +177,7 @@ RequiresClausePosition: OwnLine
|
||||||
RequiresExpressionIndentation: OuterScope
|
RequiresExpressionIndentation: OuterScope
|
||||||
SeparateDefinitionBlocks: Always
|
SeparateDefinitionBlocks: Always
|
||||||
ShortNamespaceLines: 1
|
ShortNamespaceLines: 1
|
||||||
SortIncludes: CaseSensitive
|
SortIncludes: Never
|
||||||
SortJavaStaticImport: Before
|
SortJavaStaticImport: Before
|
||||||
SortUsingDeclarations: LexicographicNumeric
|
SortUsingDeclarations: LexicographicNumeric
|
||||||
SpaceAfterCStyleCast: false
|
SpaceAfterCStyleCast: false
|
||||||
|
|
33
.vscode/launch.json
vendored
Normal file
33
.vscode/launch.json
vendored
Normal 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
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|
||||||
|
]
|
||||||
|
}
|
|
@ -3,13 +3,13 @@
|
||||||
|
|
||||||
#include "result.hpp"
|
#include "result.hpp"
|
||||||
#include <option.hpp>
|
#include <option.hpp>
|
||||||
|
#include <stdint.h>
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
#include <stdio.h>
|
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
class Vec {
|
class Vec {
|
||||||
private:
|
private:
|
||||||
T* items;
|
T* items = nullptr;
|
||||||
size_t count = 0;
|
size_t count = 0;
|
||||||
size_t capacity = 0;
|
size_t capacity = 0;
|
||||||
|
|
||||||
|
@ -24,14 +24,14 @@ public:
|
||||||
if (this->count <= n) {
|
if (this->count <= n) {
|
||||||
return Option<T*>();
|
return Option<T*>();
|
||||||
}
|
}
|
||||||
return Option<T*>(this->items[n]);
|
return Option<T*>(&this->items[n]);
|
||||||
}
|
}
|
||||||
|
|
||||||
Option<const T*> nth(const size_t n) const {
|
Option<const T*> nth(const size_t n) const {
|
||||||
if (this->count <= n) {
|
if (this->count <= n) {
|
||||||
return Option<const T*>();
|
return Option<const T*>();
|
||||||
}
|
}
|
||||||
return Option<const T*>(this->items[n]);
|
return Option<const T*>(&this->items[n]);
|
||||||
}
|
}
|
||||||
|
|
||||||
Option<T> pop() {
|
Option<T> pop() {
|
||||||
|
@ -88,6 +88,17 @@ public:
|
||||||
this->count = 0;
|
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) {
|
static Vec<T> from_ptr(const T* ptr, size_t len) {
|
||||||
Vec<T> vec = Vec();
|
Vec<T> vec = Vec();
|
||||||
for (size_t i = 0; i < len; i++) {
|
for (size_t i = 0; i < len; i++) {
|
||||||
|
@ -100,11 +111,13 @@ public:
|
||||||
return this->count;
|
return this->count;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// IMPL: Clone
|
||||||
Vec clone() {
|
Vec clone() {
|
||||||
Vec nv = Vec();
|
Vec nv = Vec();
|
||||||
nv.items = new T(*this->items);
|
nv = *this;
|
||||||
nv.count = this->count;
|
// nv.items = new T(*this->items);
|
||||||
nv.capacity = this->capacity;
|
// nv.count = this->count;
|
||||||
|
// nv.capacity = this->capacity;
|
||||||
return nv;
|
return nv;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
54
src/include/format/base_types.hpp
Normal file
54
src/include/format/base_types.hpp
Normal 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
|
|
@ -1,12 +1,91 @@
|
||||||
#ifndef _H_LIBCP_FORMAT_FMT
|
#ifndef _H_LIBCP_FORMAT_FMT
|
||||||
#define _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 <string.hpp>
|
||||||
|
|
||||||
|
#include "format/base_types.hpp"
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
concept Format = requires(T 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
|
#endif // _H_LIBCP_FORMAT_FMT
|
23
src/include/io/print.hpp
Normal file
23
src/include/io/print.hpp
Normal 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
|
|
@ -2,9 +2,12 @@
|
||||||
#ifndef _H_LIBCP
|
#ifndef _H_LIBCP
|
||||||
#define _H_LIBCP
|
#define _H_LIBCP
|
||||||
|
|
||||||
namespace std {
|
#include <stdint.h>
|
||||||
#include <format.hpp>
|
|
||||||
|
#include <string.hpp>
|
||||||
|
#include <collections/vec.hpp>
|
||||||
|
#include <format/fmt.hpp>
|
||||||
#include <stl.hpp>
|
#include <stl.hpp>
|
||||||
} // namespace std
|
#include <io/print.hpp>
|
||||||
|
|
||||||
#endif // _H_LIBCP
|
#endif // _H_LIBCP
|
|
@ -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
|
|
|
@ -7,6 +7,7 @@ namespace stl {
|
||||||
#include <stl/is_convertible.hpp>
|
#include <stl/is_convertible.hpp>
|
||||||
#include <stl/is_same.hpp>
|
#include <stl/is_same.hpp>
|
||||||
#include <stl/move.hpp>
|
#include <stl/move.hpp>
|
||||||
|
#include <stl/remove_cvref.hpp>
|
||||||
#include <stl/remove_reference.hpp>
|
#include <stl/remove_reference.hpp>
|
||||||
} // namespace stl
|
} // namespace stl
|
||||||
|
|
||||||
|
|
28
src/include/stl/remove_cvref.hpp
Normal file
28
src/include/stl/remove_cvref.hpp
Normal 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
|
|
@ -14,6 +14,11 @@ public:
|
||||||
size_t len();
|
size_t len();
|
||||||
void clear();
|
void clear();
|
||||||
Vec<char>& chars();
|
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
|
// IMPL: Format
|
||||||
String fmt_to_str();
|
String fmt_to_str();
|
||||||
// IMPL: Clone
|
// IMPL: Clone
|
||||||
|
|
|
@ -1 +0,0 @@
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
#include <string.hpp>
|
#include <string.hpp>
|
||||||
|
|
||||||
String::String(const char* str) {
|
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() {
|
size_t String::len() {
|
||||||
|
@ -21,6 +21,30 @@ String String::fmt_to_str() {
|
||||||
return this->clone();
|
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 String::clone() {
|
||||||
String s = String();
|
String s = String();
|
||||||
s._chars = this->_chars.clone();
|
s._chars = this->_chars.clone();
|
||||||
|
|
|
@ -1,13 +1,11 @@
|
||||||
#include <collections/vec.hpp>
|
#include <std.hpp>
|
||||||
#include <stddef.h>
|
|
||||||
#include <stdio.h>
|
|
||||||
|
|
||||||
int main(int argc, char* argv[]) {
|
int main(int argc, char* argv[]) {
|
||||||
Vec<size_t>* v = new Vec<size_t>();
|
Vec<int> v = Vec<int>();
|
||||||
v->push(69);
|
v.push(1);
|
||||||
v->push(420);
|
v.push(2);
|
||||||
size_t ft = v->pop().unwrap();
|
v.push(3);
|
||||||
size_t sn = v->pop().unwrap();
|
v.push(4);
|
||||||
printf("ft: %zu sn: %zu\n", ft, sn);
|
println("Hello!!!! {}, {}, {}, {}", "meow", 420.0, 49, v);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user