diff options
Diffstat (limited to 'libk')
-rw-r--r-- | libk/makefile | 4 | ||||
-rw-r--r-- | libk/stdlib.h | 1 | ||||
-rw-r--r-- | libk/string.h | 106 | ||||
-rw-r--r-- | libk/string/integerview.cc | 40 | ||||
-rw-r--r-- | libk/string/string.cc (renamed from libk/string.cc) | 0 | ||||
-rw-r--r-- | libk/string/test.cc | 14 | ||||
-rw-r--r-- | libk/type_traits.h | 16 | ||||
-rw-r--r-- | libk/types.h | 35 | ||||
-rw-r--r-- | libk/types/test.cc (renamed from libk/type_traits/test.cc) | 14 |
9 files changed, 120 insertions, 110 deletions
diff --git a/libk/makefile b/libk/makefile index 941d93d..ea684ba 100644 --- a/libk/makefile +++ b/libk/makefile @@ -1,6 +1,6 @@ -CXX_OBJ := string.o stdlib/console.o -CXX_TEST_OBJ := type_traits/test.o +CXX_OBJ := string/string.o string/integerview.o stdlib/console.o +CXX_TEST_OBJ := types/test.o string/test.o libk.a: $(CXX_OBJ) $(AR) rcs $@ $(CXX_OBJ) diff --git a/libk/stdlib.h b/libk/stdlib.h index f031408..e1d3dd2 100644 --- a/libk/stdlib.h +++ b/libk/stdlib.h @@ -1,7 +1,6 @@ #pragma once #include <string.h> -#include <type_traits.h> class Console { public: diff --git a/libk/string.h b/libk/string.h index 0675cea..03c0942 100644 --- a/libk/string.h +++ b/libk/string.h @@ -1,5 +1,4 @@ #pragma once -#include <type_traits.h> #include <types.h> /** @@ -8,13 +7,15 @@ */ constexpr size_t strlen(const char* str) { size_t len = 0; - while (str[len]) + while (str[len] != '\0') ++len; return len; } -/* reverse: reverse string s in place */ -constexpr void reverse(char s[], int strlen) { +/** + * Reverse array in place + */ +constexpr void reverse(char s[], size_t strlen) { int i, j; char c; @@ -25,57 +26,29 @@ constexpr void reverse(char s[], int strlen) { } } -/* itoa: convert n to characters in s */ -template <typename T, int base = 10> -constexpr size_t itoa(T n, char s[]) { - const bool is_negative = (n < 0); - if constexpr (!is_unsigned<T>()) { - if (n < 0) - n = -n; - } - - int i = 0; - do { /* generate digits in reverse order */ - s[i++] = "0123456789abcdef"[n % base]; /* get next digit */ - } while ((n /= base) > 0); /* delete it */ - - if (is_negative) - s[i++] = '-'; - - s[i] = '\0'; - - reverse(s, i); - return i; -} - class ViewIterator { public: - virtual char next() = 0; - virtual operator bool() const = 0; -}; - -class StringView { -public: - class Iterator : public ViewIterator { - friend class StringView; + char next() { return buffer[reverse ? pos-- : pos++]; } + operator bool() const { return reverse ? (pos >= 0) : (pos < length); } - public: - char next() override { return p->buffer[pos++]; } - operator bool() const override { return (pos < p->m_length); } - - private: - Iterator(const StringView* s) : p(s) {} - - size_t pos = 0; - const StringView* p; + explicit ViewIterator(const char* b, size_t l, bool r = false) : buffer(b), length(l), reverse(r) { + if (reverse) + pos = length - 1; }; - friend class Iterator; +private: + ssize_t pos = 0; + const char* const buffer; // const pointer to const char* + const size_t length; + const bool reverse; +}; +class StringView { +public: StringView(const char* d) : buffer{d}, m_length{strlen(d)} {} StringView(const char* d, size_t l) : buffer{d}, m_length{l} {} - Iterator begin() const { return Iterator(this); } + auto begin() const { return ViewIterator(buffer, m_length); } private: const char* const buffer; @@ -84,42 +57,25 @@ private: class IntegerView { public: - template <typename T> - struct HexFormat { - T n; + struct HexFormat_int32 { + int32_t n; }; - - class Iterator : public ViewIterator { - friend class IntegerView; - - public: - char next() override { return _p->buffer[pos++]; }; - operator bool() const override { return (pos < _p->length); } - - private: - Iterator(const IntegerView* v) : _p(v) {} - - size_t pos = 0; - const IntegerView* _p; + struct HexFormat_uint32 { + uint32_t n; }; - IntegerView(int32_t n) { length = itoa(n, buffer); } - - template <typename T> - IntegerView(HexFormat<T> f) { - buffer[0] = '0'; - buffer[1] = 'x'; - length = itoa<T, 16>(f.n, buffer + 2) + 2; - } + IntegerView(int32_t n); + IntegerView(HexFormat_int32 f); + IntegerView(HexFormat_uint32 f); - Iterator begin() const { return Iterator(this); } + auto begin() const { return ViewIterator(buffer, length, true); } private: - char buffer[128]; + char buffer[32]; size_t length = 0; }; -using hex = IntegerView::HexFormat<int32_t>; -using uhex = IntegerView::HexFormat<uint32_t>; +using hex = IntegerView::HexFormat_int32; +using uhex = IntegerView::HexFormat_uint32; -using hex_addr = IntegerView::HexFormat<uint64_t>; // TODO causes crash +// using hex_addr = IntegerView::HexFormat<uint64_t>; // TODO causes crash diff --git a/libk/string/integerview.cc b/libk/string/integerview.cc new file mode 100644 index 0000000..beeb561 --- /dev/null +++ b/libk/string/integerview.cc @@ -0,0 +1,40 @@ +#include <string.h> + +/* itoa: convert n to characters in s */ +template <typename T, int base = 10> +constexpr size_t itoa(T n, char s[]) { + const bool is_negative = (n < 0); + if constexpr (!is_unsigned<T>()) { + if (n < 0) + n = -n; + } + + int i = 0; + do { /* generate digits in reverse order */ + s[i++] = "0123456789abcdef"[n % base]; /* get next digit */ + } while ((n /= base) > 0); /* delete it */ + + if (is_negative) + s[i++] = '-'; + + s[i] = '\0'; + + // reverse(s, i); + return i; +} + +IntegerView::IntegerView(int32_t n) { + length = itoa(n, buffer); +} + +IntegerView::IntegerView(HexFormat_int32 f) { + length = itoa<int32_t, 16>(f.n, buffer); + buffer[length++] = 'x'; + buffer[length++] = '0'; +} + +IntegerView::IntegerView(HexFormat_uint32 f) { + length = itoa<uint32_t, 16>(f.n, buffer); + buffer[length++] = 'x'; + buffer[length++] = '0'; +} diff --git a/libk/string.cc b/libk/string/string.cc index c75feb1..c75feb1 100644 --- a/libk/string.cc +++ b/libk/string/string.cc diff --git a/libk/string/test.cc b/libk/string/test.cc new file mode 100644 index 0000000..4d7d340 --- /dev/null +++ b/libk/string/test.cc @@ -0,0 +1,14 @@ +#include <string.h> + +// strlen +static_assert(strlen("") == 0); +static_assert(strlen("0") == 1); +static_assert(strlen("hello") == 5); +static_assert(strlen("world") == 5); + +// reverse +static_assert([]() { + char s[] = "xyz"; + reverse(s, strlen(s)); + return s[0] == 'z' && s[1] == 'y' && s[2] == 'x' && s[3] == '\0'; +}()); diff --git a/libk/type_traits.h b/libk/type_traits.h deleted file mode 100644 index dc70339..0000000 --- a/libk/type_traits.h +++ /dev/null @@ -1,16 +0,0 @@ -#pragma once - -template <class T, T v> -struct integral_constant { - constexpr T operator()() const { return v; } - constexpr operator T() const { return v; } -}; - -template <class T, class U> -struct is_same : integral_constant<bool, false> {}; - -template <class T> -struct is_same<T, T> : integral_constant<bool, true> {}; - -template <typename T> -struct is_unsigned : integral_constant<bool, (T{0} < static_cast<T>(-1))> {}; diff --git a/libk/types.h b/libk/types.h index fc20d22..4018ab8 100644 --- a/libk/types.h +++ b/libk/types.h @@ -1,28 +1,33 @@ #pragma once typedef unsigned short size_t; -static_assert(sizeof(size_t) >= 2); +typedef int ssize_t; typedef unsigned char uint8_t; -static_assert(sizeof(uint8_t) == 1); - typedef unsigned short uint16_t; -static_assert(sizeof(uint16_t) == 2); - typedef unsigned int uint32_t; -static_assert(sizeof(uint32_t) == 4); - typedef unsigned long long int uint64_t; -static_assert(sizeof(uint64_t) == 8); typedef char int8_t; -static_assert(sizeof(int8_t) == 1); - typedef short int16_t; -static_assert(sizeof(int16_t) == 2); - typedef int int32_t; -static_assert(sizeof(int32_t) == 4); - typedef long long int int64_t; -static_assert(sizeof(int64_t) == 8); + +// Type Traits + +template <class T, T v> +struct integral_constant { + constexpr T operator()() const { return v; } + constexpr operator T() const { return v; } +}; + +/* is_same */ +template <class T, class U> +struct is_same : integral_constant<bool, false> {}; + +template <class T> +struct is_same<T, T> : integral_constant<bool, true> {}; + +/* is_unsigned */ +template <typename T> +struct is_unsigned : integral_constant<bool, (T{0} < static_cast<T>(-1))> {}; diff --git a/libk/type_traits/test.cc b/libk/types/test.cc index 1aa11df..f93d883 100644 --- a/libk/type_traits/test.cc +++ b/libk/types/test.cc @@ -1,6 +1,18 @@ -#include <type_traits.h> #include <types.h> +// types +static_assert(sizeof(size_t) >= 2); + +static_assert(sizeof(uint8_t) == 1); +static_assert(sizeof(uint16_t) == 2); +static_assert(sizeof(uint32_t) == 4); +static_assert(sizeof(uint64_t) == 8); + +static_assert(sizeof(int8_t) == 1); +static_assert(sizeof(int16_t) == 2); +static_assert(sizeof(int32_t) == 4); +static_assert(sizeof(int64_t) == 8); + // is_same static_assert(is_same<int, int>() == true); static_assert(is_same<int, unsigned int>() == false); |