From f7deae5a1e037b884f137ff170fe0ffefde59b8b Mon Sep 17 00:00:00 2001 From: slowy07 Date: Sat, 25 May 2024 17:49:43 +0700 Subject: [PATCH] chore: reimprovement code Signed-off-by: slowy07 --- api/any.hpp | 215 +++++++++++++++-------- api/array.hpp | 134 ++++++++------ api/atomic.hpp | 74 ++++++++ api/atomicity.hpp | 77 -------- api/builtin.hpp | 140 +++++++-------- api/defer.hpp | 27 +-- api/derive/clone.hpp | 116 ++++++++++++ api/derive/derive.hpp | 26 +++ api/error.hpp | 42 +++++ api/fn.hpp | 107 ++++++++--- api/jane.hpp | 200 --------------------- api/map.hpp | 128 +++++++------- api/misc.hpp | 55 ++++++ api/panic.hpp | 57 ++++++ api/platform.hpp | 55 ++++++ api/process.hpp | 92 ++++++++++ api/ref.hpp | 203 +++++++++++---------- api/signal.hpp | 360 ++++++++++++++++++------------------- api/slice.hpp | 301 ++++++++++++++++--------------- api/str.hpp | 399 ++++++++++++++++++++++-------------------- api/terminate.hpp | 70 ++++++++ api/trait.hpp | 125 +++++++------ api/typedef.hpp | 85 --------- api/types.hpp | 61 +++++++ api/utf16.hpp | 199 +++++++++++---------- api/utf8.hpp | 359 +++++++++++++++++++------------------ 26 files changed, 2114 insertions(+), 1593 deletions(-) create mode 100644 api/atomic.hpp delete mode 100644 api/atomicity.hpp create mode 100644 api/derive/clone.hpp create mode 100644 api/derive/derive.hpp create mode 100644 api/error.hpp delete mode 100644 api/jane.hpp create mode 100644 api/misc.hpp create mode 100644 api/panic.hpp create mode 100644 api/platform.hpp create mode 100644 api/process.hpp create mode 100644 api/terminate.hpp delete mode 100644 api/typedef.hpp create mode 100644 api/types.hpp diff --git a/api/any.hpp b/api/any.hpp index 67ed7ce..6447100 100644 --- a/api/any.hpp +++ b/api/any.hpp @@ -1,4 +1,4 @@ -// Copyright (c) 2024 - DeRuneLabs +// Copyright (c) 2024 arfy slowy - DeRuneLabs // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal @@ -21,126 +21,195 @@ #ifndef __JANE_ANY_HPP #define __JANE_ANY_HPP -#include "ref.hpp" -#include "typedef.hpp" -struct any_jnt; +#include +#include +#include +#include +#include -struct any_jnt { -public: - ref_jnt __data{}; - const char *__type_id{nil}; +#include "builtin.hpp" +#include "error.hpp" +#include "ref.hpp" +#include "str.hpp" +#include "types.hpp" +namespace jane { +class Any; + +class Any { +private: + template struct DynamicType { + public: + static const char *type_id(void) noexcept { return typeid(T).name(); } + + static void dealloc(void *alloc) noexcept { + delete reinterpret_cast(alloc); + } - any_jnt(void) noexcept {} + static jane::Bool eq(void *alloc, void *other) noexcept { + T *l{reinterpret_cast(alloc)}; + T *r{reinterpret_cast(other)}; + return *l == *r; + } - template any_jnt(const T &_Expr) noexcept { - this->operator=(_Expr); + static const jane::Str to_str(const void *alloc) noexcept { + const T *v{reinterpret_cast(alloc)}; + return jane::to_str(*v); + } + }; + + struct Type { + public: + const char *(*type_id)(void) noexcept; + void (*dealloc)(void *alloc) noexcept; + jane::Bool (*eq)(void *alloc, void *other) noexcept; + const jane::Str (*to_str)(const void *alloc) noexcept; + }; + + template static jane::Any::Type *new_type(void) noexcept { + using t = typename std::decay>::type; + static jane::Any::Type table = { + t::type_id, + t::dealloc, + t::eq, + t::to_str, + }; + return &table; } - any_jnt(const any_jnt &_Src) noexcept { this->operator=(_Src); } +public: + jane::Ref data{}; + jane::Any::Type *type{nullptr}; + Any(void) noexcept {} + template Any(const T &expr) noexcept { this->operator=(expr); } + + Any(const jane::Any &src) noexcept { this->operator=(src); } - ~any_jnt(void) noexcept { this->__dealloc(); } + ~Any(void) noexcept { this->dealloc(); } - inline void __dealloc(void) noexcept { - this->__type_id = nil; - if (!this->__data.__ref) { - this->__data.__alloc = nil; + void dealloc(void) noexcept { + if (!this->data.ref) { + this->type = nullptr; + this->data.alloc = nullptr; return; } - if ((this->__data.__get_ref_n()) != __JANE_REFERENCE_DELTA) { + + if ((this->data.get_ref_n()) != jane::REFERENCE_DELTA) { return; } - delete this->__data.__ref; - this->__data.__ref = nil; - std::free(*this->__data.__alloc); - *this->__data.__alloc = nil; - std::free(this->__data.__alloc); - this->__data.__alloc = nil; + this->type->dealloc(*this->data.alloc); + *this->data.alloc = nullptr; + this->type = nullptr; + + delete this->data.ref; + this->data.ref = nullptr; + std::free(this->data.alloc); + this->data.alloc = nullptr; } - template inline bool __type_is(void) const noexcept { - if (std::is_same::value) { - return (false); + template inline jane::Bool type_is(void) const noexcept { + if (std::is_same::type, std::nullptr_t>::value) { + return false; } - if (this->operator==(nil)) { - return (false); + if (this->operator==(nullptr)) { + return false; } - return std::strcmp(this->__type_id, typeid(T).name()) == 0; + return std::strcmp(this->type->type_id(), typeid(T).name()) == 0; } - template void operator=(const T &_Expr) noexcept { - this->__dealloc(); - T *_alloc{new (std::nothrow) T}; - if (!_alloc) { - JANE_ID(panic)(__JANE_ERROR_MEMORY_ALLOCATION_FAILED); + template void operator=(const T &expr) noexcept { + this->dealloc(); + + T *alloc{new (std::nothrow) void *}; + if (!alloc) { + jane::panic(jane::ERROR_MEMORY_ALLOCATION_FAILED); } - void **_main_alloc{new (std::nothrow) void *}; - if (!_main_alloc) { - JANE_ID(panic)(__JANE_ERROR_MEMORY_ALLOCATION_FAILED); + + void **main_alloc{new (std::nothrow) void *}; + if (!main_alloc) { + jane::panic(jane::ERROR_MEMORY_ALLOCATION_FAILED); } - *_alloc = _Expr; - *_main_alloc = ((void *)(_alloc)); - this->__data = ref_jnt::make(_main_alloc); - this->__type_id = typeid(_Expr).name(); + + *alloc = expr; + *main_alloc = reinterpret_cast(alloc); + this->data = jane::Ref::make(main_alloc); + this->type = jane::Any::new_type(); } - void operator=(const any_jnt &_Src) noexcept { - if (_Src.operator==(nil)) { - this->operator=(nil); + void operator=(const jane::Any &src) noexcept { + if (this->data.alloc == src.data.alloc) { return; } - this->__dealloc(); - this->__data = _Src.__data; - this->__type_id = _Src.__type_id; + if (src.operator==(nullptr)) { + this->operator=(nullptr); + } + + this->dealloc(); + this->data = src.data; + this->type = src.type; } - inline void operator=(const std::nullptr_t) noexcept { this->__dealloc(); } + inline void operator=(const std::nullptr_t) noexcept { this->dealloc(); } template operator T(void) const noexcept { - if (this->operator==(nil)) { - JANE_ID(panic)(__JANE_ERROR_INVALID_MEMORY); + if (this->operator==(nullptr)) { + jane::panic(jane::ERROR_INVALID_MEMORY); } - if (!this->__type_is()) { - JANE_ID(panic)(__JANE_ERROR_INCOMPATIBLE_TYPE); + + if (!this->type_is()) { + jane::panic(jane::ERROR_INCOMPATIBLE_TYPE); } - return (*(T *)(*this->__data.__alloc)); + + return *reinterpret_cast(*this->data.alloc); } - template inline bool operator==(const T &_Expr) const noexcept { - return (this->__type_is() && this->operator T() == _Expr); + template + inline jane::Bool operator==(const T &_Expr) const noexcept { + return (this->type_is() && this->operator T() == _Expr); } template - inline constexpr bool operator!=(const T &_Expr) const noexcept { + inline jane::Bool operator!=(const T &_Expr) const noexcept { return (!this->operator==(_Expr)); } - inline bool operator==(const any_jnt &_Any) const noexcept { - if (this->operator==(nil) && _Any.operator==(nil)) { - return (true); + inline jane::Bool operator==(const jane::Any &other) const noexcept { + if (this->data.alloc == other.data.alloc) { + return true; } - return (std::strcmp(this->__type_id, _Any.__type_id) == 0); + + if (this->operator==(nullptr) && other.operator==(nullptr)) { + return true; + } + + if (std::strcmp(this->type->type_id(), other.type->type_id()) != 0) { + return false; + } + + return this->type->eq(*this->data.alloc, *other.data.alloc); } - inline bool operator!=(const any_jnt &_Any) const noexcept { - return (!this->operator==(_Any)); + inline jane::Bool operator!=(const jane::Any &other) const noexcept { + return !this->operator==(other); } - inline bool operator==(std::nullptr_t) const noexcept { - return (!this->__data.__alloc); + inline jane::Bool operator==(std::nullptr_t) const noexcept { + return !this->data.alloc; } - inline bool operator!=(std::nullptr_t) const noexcept { - return (!this->operator==(nil)); + inline jane::Bool operator!=(std::nullptr_t) const noexcept { + return !this->operator==(nullptr); } - friend std::ostream &operator<<(std::ostream &_Stream, const any_jnt &_Src) noexcept { - if (_Src.operator!=(nil)) { - _Stream << ""; + friend std::ostream &operator<<(std::ostream &stream, + const jane::Any &src) noexcept { + if (src.operator!=(nullptr)) { + stream << src.type->to_str(*src.data.alloc); } else { - _Stream << 0; + stream << 0; } - return (_Stream); + return stream; } }; +} // namespace jane -#endif // !__JANE_ANY_HPP +#endif //__JANE_ANY_HPP \ No newline at end of file diff --git a/api/array.hpp b/api/array.hpp index 08e8a80..fb8fcb4 100644 --- a/api/array.hpp +++ b/api/array.hpp @@ -1,4 +1,4 @@ -// Copyright (c) 2024 - DeRuneLabs +// Copyright (c) 2024 arfy slowy - DeRuneLabs // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal @@ -21,96 +21,114 @@ #ifndef __JANE_ARRAY_HPP #define __JANE_ARRAY_HPP +#include "error.hpp" +#include "panic.hpp" #include "slice.hpp" -#include "typedef.hpp" +#include "types.hpp" +#include +#include +#include -template struct array_jnt; +namespace jane { +template struct Array; -template struct array_jnt { +template struct Array { public: - std::array<_Item_t, _N> _buffer{}; - array_jnt<_Item_t, _N>(const std::initializer_list<_Item_t> &_Src) noexcept { - const auto _Src_begin{_Src.begin()}; - for (int_jnt _index{0}; _index < _Src.size(); ++_index) { - this->_buffer[_index] = *((_Item_t *)(_Src_begin + _index)); + mutable std::array buffer{}; + Array(void) noexcept {} + Array(const std::initializer_list &src) noexcept { + const auto src_begin{src.begin()}; + for (jane::Int index{0}; index < src.size(); ++index) { + this->buffer[index] = *(Item)(src.begin + index); } } - typedef _Item_t *iterator; - typedef const _Item_t *const_iterator; + typedef Item *Iterator; + typedef const Item *ConstIterator; - inline constexpr iterator begin(void) noexcept { return (&this->_buffer[0]); } + inline constexpr Iterator begin(void) noexcept { return &this->buffer[0]; } - inline constexpr const_iterator begin(void) const noexcept { - return (&this->_buffer[0]); + inline constexpr ConstIterator begin(void) const noexcept { + return &this->buffer[0]; } - inline constexpr iterator end(void) noexcept { return (&this->_buffer[_N]); } + inline constexpr Iterator end(void) noexcept { return &this->buffer[N]; } - inline constexpr const_iterator end(void) const noexcept { - return (&this->_buffer[_N]); + inline constexpr ConstIterator end(void) const noexcept { + return &this->_buffer[N]; } - inline slice_jnt<_Item_t> ___slice(const int_jnt &_Start, - const int_jnt &_End) const noexcept { - if (_Start < 0 || _End < 0 || _Start > _End || _End > this->_len()) { - std::stringstream _sstream; - __JANE_WRITE_ERROR_SLICING_INDEX_OUT_OF_RANGE(_sstream, _Start, _End); - JANE_ID(panic)(_sstream.str().c_str()); - } else if (_Start == _End) { - return (slice_jnt<_Item_t>()); + inline jane::Slice slice(const jane::Int &start, + const jane::Int &end) const noexcept { + if (start < 0 || end < 0 || start > end || end > this->len()) { + std::stringstream sstream; + __JANE_WRITE_ERROR_SLICING_INDEX_OUT_OF_RANGE(sstream, start, end); + jane::panic(sstream.str().c_str()); + } else if (start == end) { + return jane::Slice(); } - const int_jnt _n{_End - _Start}; - slice_jnt<_Item_t> _slice(_n); - for (int_jnt _counter{0}; _counter < _n; ++_counter) { - _slice[_counter] = this->_buffer[_Start + _counter]; + + const jane::Int n{end - start}; + jane::Slice slice{jane::Slice::alloc(n)}; + for (jane::Int counter{0}; counter < n; ++counter) { + slice[counter] = this->buffer[start + counter]; } - return (_slice); + return slice; } - inline slice_jnt<_Item_t> ___slice(const int_jnt &_Start) const noexcept { - return this->___slice(_Start, this->_len()); + inline jane::Slice slice(const jane::Int &start) const noexcept { + return this->slice(start, this->len()); } - inline slice_jnt<_Item_t> ___slice(void) const noexcept { - return this->___slice(0, this->_len()); + inline jane::Slice slice(void) const noexcept { + return this->slice(0, this->len()); } - inline constexpr int_jnt _len(void) const noexcept { return (_N); } + inline constexpr jane::Int len(void) const noexcept { return N; } + + inline constexpr jane::Bool empty(void) const noexcept { return N == 0; } - inline constexpr bool _empty(void) const noexcept { return (_N == 0); } + inline constexpr jane::Bool + operator==(const jane::Array &src) const noexcept { + return this->buffer == src.buffer; + } - inline constexpr bool - operator==(const array_jnt<_Item_t, _N> &_Src) const noexcept { - return (!this->operator==(_Src)); + inline constexpr jane::Bool + operator!=(const jane::Array &src) const noexcept { + return !this->operator==(src); } - inline constexpr bool - operator!=(const array_jnt<_Item_t, _N> &_Src) const noexcept { - return (!this->operator==(_Src)); + Item &operator[](const jane::Int &index) const { + if (this->empty() || index < 0 || this->len() <= index) { + std::stringstream sstream; + __JANE_WRITE_ERROR_INDEX_OUT_OF_RANGE(sstream, index); + jane::panic(sstream.str().c_str()); + } + return this->buffer[index]; } - _Item_t &operator[](const int_jnt &_Index) { - if (this->_empty() || _Index < 0 || this->_len() <= _Index) { - std::stringstream _sstream; - __JANE_WRITE_ERROR_INDEX_OUT_OF_RANGE(_sstream, _Index); - JANE_ID(panic)(_sstream.str().c_str()); + Item &operator[](const jane::Int &index) { + if (this->empty() || index < 0 || this->len() <= index) { + std::stringstream sstream; + __JANE_WRITE_ERROR_INDEX_OUT_OF_RANGE(sstream, index); + jane::panic(sstream.str().c_str()); } - return (this->_buffer[_Index]); + return this->buffer[index]; } - friend std::ostream &operator<<(std::ostream &_Stream, - const array_jnt<_Item_t, _N> &_Src) noexcept { - _Stream << '['; - for (int_jnt _index{0}; _index < _Src._len();) { - _Stream << _Src._buffer[_index++]; - if (_index < _Src._len()) { - _Stream << " "; + friend std::ostream &operator<<(std::ostream &stream, + const jane::Array &src) noexcept { + stream << '['; + for (jane::Int index{0}; index < src.len();) { + stream << src.buffer[index++]; + if (index < src.len()) { + stream << " "; } } - _Stream << ']'; - return (_Stream); + stream << ']'; + return stream; } }; +} // namespace jane -#endif // !__JANE_ARRAY_HPP +#endif //__JANE_ARRAY_HPP \ No newline at end of file diff --git a/api/atomic.hpp b/api/atomic.hpp new file mode 100644 index 0000000..c7dbe8e --- /dev/null +++ b/api/atomic.hpp @@ -0,0 +1,74 @@ +// Copyright (c) 2024 arfy slowy - DeRuneLabs +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +// SOFTWARE. + +#ifndef __JANE_ATOMIC_HPP +#define __JANE_ATOMIC_HPP + +#define __jane_atomic_store_explicit(ADDR, VAL, MO) \ + __extension__({ \ + auto atomic_store_ptr{ADDR}; \ + __typeof__((void)(0), *atomic_store_ptr) atomic_store_tmp{VAL}; \ + __atomic_store(atomic_store_ptr, &atomic_store_tmp, MO); \ + }) + +#define __jane_atomic_store(ADDR, VAL) \ + __jane_atomic_store_explicit(ADDR, VAL, __ATOMIC_SEQ_CST) + +#define __jane_atomic_load_explicit(ADDR, MO) \ + __extension__({ \ + auto atomic_load_ptr{ADDR}; \ + __typeof__((void)(0), *atomic_load_ptr) atomic_load_tmp; \ + __atomic_load(atomic_load_ptr, &atomic_load_tmp, MO); \ + atomic_load_tmp; \ + }) + +#define __jane_atomic_load(ADDR) \ + __jane_atomic_load_explicit(ADDR, __ATOMIC_SEQ_CST) + +#define __jane_atomic_swap_explicit(ADDR, NEW, MO) \ + __extension__({ \ + auto atomic_exchange_ptr{ADDR}; \ + __typeof__((void)(0), *atomic_exchange_ptr) atomic_exchange_val{NEW}; \ + __typeof__((void)(0), *atomic_exchange_ptr) atomic_exchange_tmp; \ + __atomic_exchange(atomic_exchange_ptr, &atomic_exchange_val, \ + &atomic_exchange_tmp, MO); \ + atomic_exchange_tmp; \ + }) + +#define __jane_atomic_swap(ADDR, NEW) \ + __jane_atomic_swap_explicit(ADDR, NEW, __ATOMIC_SEQ_CST) + +#define __jane_atomic_compare_swap_explicit(ADDR, OLD, NEW, SIC, FAIL) \ + __extension__({ \ + auto atomic_compare_exchange_ptr{ADDR}; \ + __typeof__((void)(0), \ + *atomic_compare_exchange_ptr) atomic_compare_exchange_tmp{NEW}; \ + __atomic_compare_exchange(atomic_compare_exchange_ptr, OLD, \ + &atomic_compare_exchange_tmp, 0, SUC, FAIL); \ + }) + +#define __jane_atomic_compare_swap(ADDR, OLD, NEW) \ + __jane_atomic_compare_swap_explicit(ADDR, OLD, NEW, __ATOMIC_SEQ_CSQT, \ + __ATOMIC_SEQ_CST) + +#define __jane_atomic_add(ADDR, DELTA) \ + __atomic_fetch_add(ADDR, DELTA, __ATOMIC_SEQ_CST) + +#endif // __JANE_ATOMIC_HPP \ No newline at end of file diff --git a/api/atomicity.hpp b/api/atomicity.hpp deleted file mode 100644 index 5a48d17..0000000 --- a/api/atomicity.hpp +++ /dev/null @@ -1,77 +0,0 @@ -// Copyright (c) 2024 - DeRuneLabs -// -// Permission is hereby granted, free of charge, to any person obtaining a copy -// of this software and associated documentation files (the "Software"), to deal -// in the Software without restriction, including without limitation the rights -// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -// copies of the Software, and to permit persons to whom the Software is -// furnished to do so, subject to the following conditions: -// -// The above copyright notice and this permission notice shall be included in -// all copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -// SOFTWARE. - -#ifndef __JANE_ATOMICITY_HPP -#define __JANE_ATOMICITY_HPP - -#define __jane_atomic_store_explicit(_ADDR, _VAL, _MO) \ - (__extension__({ \ - auto __atomic_store_ptr = (_ADDR); \ - __typeof__((void)(0), *__atomi_store_ptr) __atomic_store_tmp = (_VAL); \ - __atomic_store(__atomic_store_ptr, &__atomic_store_tmp, (_MO)); \ - })) - -#define __jane_atomic_store(_ADDR, _VAL) \ - (__jane_atomic_store_explicit((_ADDR), (_VAL), __ATOMIC_SEQ_CST)) - -#define __jane_atomic_load_explicit(_ADDR, _MO) \ - (__extension__({ \ - auto __atomic_load_ptr = (_ADDR); \ - __typeof__((void)(0), *__atomic_load_ptr) __atomic_load_tmp; \ - __atomic_load(__atomic_load_ptr, &__atomic_load_tmp, (_MO)); \ - __atomic_load_tmp; \ - })) - -#define __jane_atomic_load(_ADDR) \ - __jane_atomic_load_explicit(_ADDR, __ATOMIC_SEQ_CST) - -#define __jane_atomic_swap_explicit(_ADDR, _NEW, _MO) \ - (__extension__({ \ - auto __atomic_exchange_ptr = (_ADDR); \ - __typeof__((void)(0), *__atomic_exchange_ptr) __atomic_exchange_val = \ - (_NEW); \ - __typeof__((void)(0), *__atomic_exchange_ptr) __atomic_exchange_tmp; \ - __atomic_exchange(__atomic_exchange_ptr, &__atomic_exchange_val, \ - &__atomic_exchange_tmp, (_MO)); \ - __atomic_exchange_tmp; \ - })) - -#define __jane_atomic_swap(_ADDR, _NEW) \ - (__jane_atomic_swap_explicit((_ADDR), (_NEW), (ATOMIC_SEQ_CST))) - -#define __jane_atomic_compare_swap_explicit(_ADDR, _OLD, _NEW, _SUC, _FAIL) \ - (__extension__({ \ - auto __atomic_compare_exchange_ptr = (_ADDR); \ - __typeof__((void)(0), \ - *__atomic_compare_exchange_ptr) __atomic_compare_exchange_tmp = \ - (_NEW); \ - __atomic_compare_exchange(__atomic_compare_exchange_ptr, (_OLD), \ - &__atomic_compare_exchange_tmp, 0, (_SUC), \ - (_FAIL)); \ - })) - -#define __jane_atomic_compare_swap(_ADDR, _OLD, _NEW) \ - (__jane_atomic_compare_swap_explicit((_ADDR), (_OLD), (_NEW), \ - __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST)) - -#define __jane_atomic_add(_ADDR, _DELTA) \ - (__atomic_fetch_add((_ADDR), (_DELTA), __ATOMIC_SEQ_CST)) - -#endif // !__JANE_ATOMICITY_HPP diff --git a/api/builtin.hpp b/api/builtin.hpp index 5f2651a..aed3a1b 100644 --- a/api/builtin.hpp +++ b/api/builtin.hpp @@ -1,4 +1,4 @@ -// Copyright (c) 2024 - DeRuneLabs +// Copyright (c) 2024 arfy slowy - DeRuneLabs // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal @@ -21,101 +21,83 @@ #ifndef __JANE_BUILTIN_HPP #define __JANE_BUILTIN_HPP -#include "ref.hpp" #include "slice.hpp" -#include "str.hpp" -#include "typedef.hpp" - -typedef u8_jnt(JANE_ID(byte)); -typedef i32_jnt(JANE_ID(rune)); - -template str_jnt __jane_to_str(const _Obj_t &_Obj) noexcept; -slice_jnt __jane_utf16_from_str(const str_jnt &_Str) noexcept; - -template -inline void JANE_ID(print)(const _Obj_t &_Obj) noexcept; -template -inline void JANE_ID(println)(const _Obj_t &_Obj) noexcept; -struct JANE_ID(Error); -template -int_jnt JANE_ID(copy)(const slice_jnt<_Item_t> &_Dest, - const slice_jnt<_Item_t> &Components) noexcept; - -template inline ref_jnt JANE_ID(new)(void) noexcept; -template inline ref_jnt JANE_ID(new)(const T &_Expr) noexcept; -template inline void JANE_ID(drop)(T &_Obj) noexcept; -template inline bool JANE_ID(real)(T &_Obj) noexcept; - -template -inline void JANE_ID(print)(const _Obj_t &_Obj) noexcept { -#ifdef _WINDOWS - const str_jnt _str{__jane_to_str<_Obj_t>(_Obj)}; - const slice_jnt _utf16_str{__jane_utf16_from_str(_str)}; - HANDLE _handle{GetStdHandle(STD_OUTPUT_HANDLE)}; - WriteConsoleW(_handle, &_utf16_str[0], _utf16_str._len(), nullptr, nullptr); +#include "types.hpp" +#include + +#ifdef OS_WINDOWS +#include +#endif + +namespace jane { +typedef jane::U8 Byte; +typedef jane::I32 Rune; + +template inline void print(const T &obj) noexcept; +template inline void println(const T &obj) noexcept; + +template +jane::Int copy(const jane::Slice &dest, + const jane::Slice &src) noexcept; +template +jane::Slice append(const jane::Slice &src, + const jane::Slice &components) noexcept; + +template inline jane::Bool real(const T &obj) noexcept; + +template inline void print(const T &obj) noexcept { +#ifdef OS_WINDOWS + const jane::Str str{jane::to_str(obj)}; + const jane::Slice utf16_str{jane::utf16_from_str(str)}; + HANDLE handle{GetStdHandler(STD_OUTPUT_HANDLE)}; + WriteConsoleW(handle, &utf16_str[0], utf16_str.len(), nullptr, nullptr); #else - std::cout << _Obj; -#endif // DEBUG + std::cout << obj; +#endif } -template -inline void JANE_ID(println)(const _Obj_t &_Obj) noexcept { - JANE_ID(print)(_Obj); +template inline void println(const T &obj) noexcept { + jane::print(obj); std::cout << std::endl; } -struct JANE_ID(Error) { - virtual str_jnt _error(void) { return {}; } - virtual ~JANE_ID(Error)(void) noexcept {} - - bool operator==(const JANE_ID(Error) & _Src) { return false; } - bool operator!=(const JANE_ID(Error) & _Src) { - return !this->operator==(_Src); - } -}; - -template -int_jnt JANE_ID(copy)(const slice_jnt<_Item_t> &_Dest, - const slice_jnt<_Item_t> &_Src) noexcept { - if (_Dest._empty() || _Src._empty()) { +template +jane::Int copy(const jane::Slice &dest, + const jane::Slice &src) noexcept { + if (dest.empty() || src.empty()) { return 0; } - int_jnt _len = (_Dest._len() > _Src._len()) ? _Src._len() - : (_Src._len() > _Dest._len()) ? _Dest._len() - : _Src._len(); - for (int_jnt _index{0}; _index < _len; ++_index) { - _Dest.__slice[_index] = _Src.__slice[_index]; + jane::Int len{dest.len() > src.len() ? src.len() + : src.len() > dest.len() ? dest.len() + : src.len()}; + for (jane::Int index{0}; index < len; ++index) { + dest._slice[index] = src._slice[index]; } - return (_len); + return len; } -template -slice_jnt<_Item_t> -JANE_ID(append)(const slice_jnt<_Item_t> &_Src, - const slice_jnt<_Item_t> &_Components) noexcept { - const int_jnt _N{_Src._len() + _Components._len()}; - slice_jnt<_Item_t> _buffer(_N); - JANE_ID(copy)<_Item_t>(_buffer, _Src); - for (int_jnt _index{0}; _index < _Components._len(); ++_index) { - _buffer[_Src._len() + _index] = _Components.__slice[_index]; +template +jane::Slice append(const jane::Slice &src, + const jane::Slice &components) noexcept { + if (src == nullptr && components == nullptr) { + return nullptr; } - return (_buffer); -} + const jane::Int n{src.len() + components.len()}; + jane::Slice buffer{jane::Slice::alloc(n)}; + jane::copy(buffer, src); -template inline ref_jnt JANE_ID(new)(void) noexcept { - return (ref_jnt()); + for (jane::Int index{0}; index < components.len(); ++index) { + buffer[src.len() + index] = components._slice[index]; + } + return buffer; } -template inline ref_jnt JANE_ID(new)(const T &_Expr) noexcept { - return (ref_jnt::make(_Expr)); -} +template inline void drop(T &obj) noexcept { obj.drop(); } -template inline void JANE_ID(drop)(T &_Obj) noexcept { - _Obj._drop(); +template inline jane::Bool real(const T &obj) noexcept { + return obj.real(); } -template inline bool JANE_ID(real)(T &_Obj) noexcept { - return (_Obj._real()); -} +} // namespace jane -#endif // !__JANE_BUILTIN_HPP +#endif // __JANE_BUILTIN_HPP \ No newline at end of file diff --git a/api/defer.hpp b/api/defer.hpp index 292af77..593007a 100644 --- a/api/defer.hpp +++ b/api/defer.hpp @@ -1,4 +1,4 @@ -// Copyright (c) 2024 - DeRuneLabs +// Copyright (c) 2024 arfy slowy - DeRuneLabs // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal @@ -22,17 +22,20 @@ #define __JANE_DEFER_HPP #include +#define __JANE_CCONCAT(A, B) A##B +#define __JANE_CONCAT(A, B) __JANE_CCONCAT(A, B) -struct defer_base { - std::function __scope; - defer_base(const std::function &_Fn) noexcept { - this->__scope = _Fn; - } - ~defer_base(void) noexcept { - this->__scope(); - } -}; +#define __JANE_DEFER(BLOCK) \ + jane::DeferBase __JANE_CONCAT(__deffered__, __LINE__) { [=] BLOCK } -#define __JANE_DEFER(_BLOCK) defer_base __JANE_CONCAT(__deferred_, __LINE__) { [&]_BLOCK } +namespace jane { +struct DeferBase; +struct DeferBase { +public: + std::function scope; + DeferBase(const std::function &fn) noexcept { this->scope = fn; } + ~DeferBase(void) noexcept { this->scope(); } +}; +} // namespace jane -#endif // !__JANE_DEFER_HPP +#endif // __JANE_DEFER_HPP \ No newline at end of file diff --git a/api/derive/clone.hpp b/api/derive/clone.hpp new file mode 100644 index 0000000..fd49e74 --- /dev/null +++ b/api/derive/clone.hpp @@ -0,0 +1,116 @@ +// Copyright (c) 2024 arfy slowy - DeRuneLabs +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +// SOFTWARE. + +#ifndef __JANE_DERIVE_CLONE_HPP +#define __JANE_DERIVE_CLONE_HPP + +#include "../array.hpp" +#include "../fn.hpp" +#include "../map.hpp" +#include "../str.hpp" +#include "../trait.hpp" +#include "../types.hpp" + +namespace jane { +char clone(const char &x) noexcept; +signed char clone(const signed char &x) noexcept; +unsigned char clone(const unsigned char &x) noexcept; +char *clone(char *x) noexcept; +const char *clone(const char *x) noexcept; +jane::Int clone(const jane::Int &x) noexcept; +jane::Uint clone(const jane::Uint &x) noexcept; +jane::Bool clone(const jane::Bool &x) noexcept; +jane::Str clone(const jane::Str &x) noexcept; + +template +jane::Slice clone(const jane::Slice &s) noexcept; +template +jane::Array clone(const jane::Array &arr) noexcept; +template +jane::Map clone(const jane::Map &m) noexcept; +template jane::Ref clone(const jane::Ref &r) noexcept; +template jane::Trait clone(const jane::Trait &t) noexcept; +template jane::Fn clone(const jane::Fn &fn) noexcept; +template T *clone(T *ptr) noexcept; +template const T *clone(const T *ptr) noexcept; +template T clone(const T &t) noexcept; + +inline char clone(const char &x) noexcept { return x; } +inline signed char clone(const signed char &x) noexcept { return x; } +inline unsigned char clone(const unsigned char &x) noexcept { return x; } +inline char *clone(char *x) noexcept { return x; } +inline const char *clone(const char *x) noexcept { return x; } + +inline jane::Int clone(const jane::Int &x) noexcept { return x; } + +inline jane::Uint clone(const jane::Uint &x) noexcept { return x; } + +inline jane::Bool clone(const jane::Bool &x) noexcept { return x; } + +inline jane::Str clone(const jane::Str &x) noexcept { return x; } + +template +jane::Slice clone(const jane::Slice &s) noexcept { + jane::Slice s_clone(s.len()); + for (int i{0}; i < s.len(); ++i) { + s_clone.operator[](i) = jane::clone(s.operator[](i)); + } + return s_clone; +} + +template +jane::Array clone(const jane::Array &arr) noexcept { + jane::Array arr_clone{}; + for (int i{0}; i < arr.len(); ++i) { + arr_clone.operator[](i) = jane::clone(arr.operator[](i)); + } + return arr_clone; +} + +template +jane::Map clone(const jane::Map &m) noexcept { + jane::Map m_clone; + for (const auto &pair : m) { + m_clone[jane::clone(pair.first)] = jane::clone(pair.second); + } + return m_clone; +} + +template jane::Ref clone(const jane::Ref &r) noexcept { + if (!r.real()) { + return r; + } + jane::Ref r_clone{jane::Ref::make(jane::clone(r.operator T()))}; + return r_clone; +} + +template jane::Fn clone(const jane::Fn &fn) noexcept { + return fn; +} + +template T *clone(T *ptr) noexcept { return ptr; } + +template const T *clone(const T *ptr) noexcept { return ptr; } + +template T clone(const T &t) noexcept { return t.clone(); } + +} // namespace jane + +#endif // __JANE_DERIVE_CLONE_HPP \ No newline at end of file diff --git a/api/derive/derive.hpp b/api/derive/derive.hpp new file mode 100644 index 0000000..740be5e --- /dev/null +++ b/api/derive/derive.hpp @@ -0,0 +1,26 @@ +// Copyright (c) 2024 arfy slowy - DeRuneLabs +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +// SOFTWARE. + +#ifndef __JANE_DERIVE_HPP +#define __JANE_DERIVE_HPP + +#include "clone.hpp" + +#endif //__JANE_DERIVE_HPP \ No newline at end of file diff --git a/api/error.hpp b/api/error.hpp new file mode 100644 index 0000000..ef896ba --- /dev/null +++ b/api/error.hpp @@ -0,0 +1,42 @@ +// Copyright (c) 2024 arfy slowy - DeRuneLabs +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +// SOFTWARE. + +#ifndef __JANE_ERROR_HPP +#define __JANE_ERROR_HPP + +#define __JANE_WRITE_ERROR_SLICING_INDEX_OUT_OF_RANGE(STREAM, START, LEN) \ + (STREAM << jane::ERROR_INDEX_OUT_OF_RANGE << '[' << START << ':' << LEN \ + << ']') + +#define __JANE_WRITE_ERROR_INDEX_OUT_OF_RANGE(STREAM, INDEX) \ + (STREAM << jane::ERROR_INDEX_OUT_OF_RANGE << '[' << INDEX << ']') + +namespace jane { +constexpr const char *ERROR_INVALID_MEMORY{ + "invalid memory address or nil pointer deference"}; +constexpr const char *ERROR_INCOMPATIBLE_TYPE{"incompatible type"}; +constexpr const char *ERROR_MEMORY_ALLOCATION_FAILED{ + "memory allocation failed"}; +constexpr const char *ERROR_INDEX_OUT_OF_RANGE{"index out of range"}; +constexpr const char *ERROR_DIVIDE_BY_ZERO{"divide by zero"}; +constexpr signed int EXIT_PANIC{2}; +} // namespace jane + +#endif // __JANE_ERROR_HPP \ No newline at end of file diff --git a/api/fn.hpp b/api/fn.hpp index 05739b7..13ff1c8 100644 --- a/api/fn.hpp +++ b/api/fn.hpp @@ -1,4 +1,4 @@ -// Copyright (c) 2024 - DeRuneLabs +// Copyright (c) 2024 arfy slowy - DeRuneLabs // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal @@ -21,48 +21,105 @@ #ifndef __JANE_FN_HPP #define __JANE_FN_HPP -#include "typedef.hpp" #include +#include +#include +#include -template struct fn_jnt; +#include "builtin.hpp" +#include "error.hpp" +#include "types.hpp" -template struct fn_jnt { - std::function<_Function_t> __buffer; +#define __JANE_CO(EXPR) \ + (std::thread{[&](void) mutable -> void { EXPR; }}.detach()) - fn_jnt<_Function_t>(void) noexcept {} +namespace jane { +template struct Fn; - fn_jnt<_Function_t>(const std::function<_Function_t> &_Function) noexcept { - this->__buffer = _Function; +template +jane::Uintptr addr_of_fn(std::function f) noexcept; + +template struct Fn { +public: + std::function buffer; + jane::Uintptr _addr; + + Fn(void) noexcept {} + Fn(std::nullptr_t) noexcept {} + + Fn(const std::function &function) noexcept { + this->_addr = jane::addr_of_fn(function); + if (this->_addr == 0) { + this->_addr = (jane::Uintptr)(&function); + } + this->buffer = function; } - fn_jnt<_Function_t>(const _Function_t &_Function) noexcept { - this->__buffer = _Function; + + Fn(const Function *function) noexcept { + this->buffer = function; + this->addr = jane::addr_of_fn(this->buffer); + if (this->_addr == 0) { + this->_addr = (jane::Uintptr)(function); + } + } + + Fn(const Fn &fn) noexcept { + this->buffer = fn.buffer; + this->_addr = fn._addr; } - template - auto operator()(_Arguments_t... _Arguments) noexcept { - if (this->__buffer == nil) { - JANE_ID(panic)(__JANE_ERROR_INVALID_MEMORY); + template + auto operator()(Arguments... arguments) noexcept { + if (this->buffer == nullptr) { + jane::panic(jane::ERROR_INVALID_MEMORY); } - return (this->__buffer(_Arguments...)); + return this->buffer(arguments...); } - inline void operator=(std::nullptr_t) noexcept { this->__buffer = nil; } + jane::Uintptr addr(void) const noexcept { return this->_addr; } + + inline void operator=(std::nullptr_t) noexcept { this->buffer = nullptr; } - inline void operator=(const std::function<_Function_t> &_Function) noexcept { - this->__buffer = _Function; + inline void operator=(const std::function &function) noexcept { + this->buffer = function; } - inline void operator=(const _Function_t &_Function) noexcept { - this->__buffer = _Function; + inline void operator=(const Function &function) noexcept { + this->buffer = function; } - inline bool operator==(std::nullptr_t) const noexcept { - return (this->__buffer == nil); + inline jane::Bool operator==(const Fn &fn) const noexcept { + return this->addr() == fn.addr(); } - inline bool operator!=(std::nullptr_t) const noexcept { - return (!this->operator==(nil)); + inline jane::Bool operator!=(const Fn &fn) const noexcept { + return this->buffer == nullptr; + } + + inline jane::Bool operator==(std::nullptr_t) const noexcept { + return this->buffer == nullptr; + } + + inline jane::Bool operator!=(std::nullptr_t) const noexcept { + return !this->operator==(nullptr); + } + + friend std::ostream &operator<<(std::ostream &stream, + const Fn &src) noexcept { + stream << ""; + return stream; } }; -#endif // !__JANE_FN_HPP +template +jane::Uintptr addr_of_fn(std::function f) noexcept { + typedef T(FnType)(U...); + FnType **fn_ptr{f.template target()}; + if (!fn_ptr) { + return 0; + } + return (jane::Uintptr)(*fn_ptr); +} +} // namespace jane + +#endif // __JANE_FN_HPP \ No newline at end of file diff --git a/api/jane.hpp b/api/jane.hpp deleted file mode 100644 index 2d10848..0000000 --- a/api/jane.hpp +++ /dev/null @@ -1,200 +0,0 @@ -// Copyright (c) 2024 - DeRuneLabs -// -// Permission is hereby granted, free of charge, to any person obtaining a copy -// of this software and associated documentation files (the "Software"), to deal -// in the Software without restriction, including without limitation the rights -// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -// copies of the Software, and to permit persons to whom the Software is -// furnished to do so, subject to the following conditions: -// -// The above copyright notice and this permission notice shall be included in -// all copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -// SOFTWARE. - -#ifndef __JANE_HPP -#define __JANE_HPP - -#include -#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__) -#define _WINDOWS -#elif defined(__linux__) || defined(linux) || defined(__linux) -#define _LINX -#elif defined(__APPLE__) || defined(__MACH__) -#define _DARWIN -#endif // defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || - // defined(__NT__) - -#if defined(_LINUX) || defined(_DARWIN) -#define _UNIX -#endif // defined(_LINUX) || defined(_DARWIN) - -#if defined(__amd64) || defined(__amd64__) || defined(__x86_64) || \ - defined(__x86_64__) || defined(_M_AMD64) -#define _M_AMD64 -#elif defined(__arm__) || defined(__thumb__) || defined(_M_ARM) || \ - defined(__arm) -#define _ARM -#elif defined(i386) || defined(__i386) || defined(__i386__) || \ - defined(__X86__) || defined(__I86__) || defined(__386) -#define _I386 -#endif - -#if defined(_AMD64) || defined(_ARM64) -#define _64BIT -#else -#define _32bit -#endif - -class str_jnt; - -#include "any.hpp" -#include "array.hpp" -#include "atomicity.hpp" -#include "builtin.hpp" -#include "defer.hpp" -#include "fn.hpp" -#include "map.hpp" -#include "ref.hpp" -#include "signal.hpp" -#include "slice.hpp" -#include "str.hpp" -#include "trait.hpp" -#include "typedef.hpp" -#include "utf16.hpp" -#include "utf8.hpp" - -slice_jnt __jane_command_line_args; - -template -inline auto __jane_div(const _T &_X, - const _Denominator_t &_Denominator) noexcept; -inline slice_jnt __jane_get_command_line_args(void) noexcept; -inline void JANE_ID(panic)(const trait_jnt &_Error); -template struct tuple_ostream; -template struct tuple_ostream; -template -std::ostream &operator<<(std::ostream &_Stream, - const std::tuple &_Tuple); -template -inline auto __jane_tuple_as_args(const fn_jnt<_Function_t> &_Function, - const std::index_sequence<_I_t...>); - -template inline ref_jnt __jane_new_structure(T *_Ptr); -template str_jnt __jane_to_str(const _Obj_t &_Obj) noexcept; -str_jnt __jane_to_str(const str_jnt &_Obj) noexcept; - -slice_jnt __jane_utf16_from_str(const str_jnt &_Str) noexcept; -void __jane_terminate_handler(void) noexcept; -void __jane_signal_handler(int _Signal) noexcept; -void __jane_setup_command_line_args(int argc, char *argv[]) noexcept; - -template -inline auto __jane_div(const _T &_X, - const _Denominator_t &_Denominator) noexcept { - if (_Denominator == 0) { - JANE_ID(panic)(__JANE_ERROR_DIVIDE_BY_ZERO); - } - return (_X / _Denominator); -} - -inline slice_jnt __jane_get_command_line_args(void) noexcept { - return __jane_command_line_args; -} - -inline std::ostream &operator<<(std::ostream &_Stream, - const unsigned char _U8) noexcept { - return _Stream << ((int)(_U8)); -} - -template struct tuple_ostream { - static void __arrow(std::ostream &_Stream, const Type &_Type) { - _Stream << std::get(_Type) << ", "; - tuple_ostream::arrow(_Stream, _Type); - } -}; - -template struct tuple_ostream { - static void __arrow(std::ostream &_Stream, const Type &_Type) { - _Stream << std::get(_Type); - } -}; - -template -std::ostream &operator<<(std::ostream &_Stream, - const std::tuple &_Tuple) { - _Stream << '('; - tuple_ostream, 0, sizeof...(Types) - 1>::__arrow(_Stream, - _Tuple); - _Stream << ')'; - return _Stream; -} - -template -inline auto __jane_tuple_as_args(const fn_jnt<_Function_t> &_Function, - const _Tuple_t _Tuple, - const std::index_sequence<_I_t...>) { - return _Function.__buffer(std::get<_I_t>(_Tuple)...); -} - -slice_jnt __jane_utf16_from_str(const str_jnt &_Str) noexcept { - constexpr char _NULL_TERMINATOR = '\x00'; - slice_jnt _buff{nil}; - slice_jnt _runes{_Str.operator slice_jnt()}; - for (const i32_jnt &_R : _runes) { - if (_R == _NULL_TERMINATOR) { - break; - } - _buff = __jane_utf16_append_rune(_buff, _NULL_TERMINATOR); - } - return __jane_utf16_append_rune(_buff, _NULL_TERMINATOR); -} - -inline void JANE_ID(panic)(const trait_jnt &_Error) { - throw(_Error); -} - -template void JANE_ID(panic)(const _Obj_t &_Expr) { - struct panic_error : public JANE_ID(Error) { - str_jnt __message; - str_jnt _error(void) { return (this->__message); } - }; - struct panic_error _error; - _error.__message = __jane_to_str(_Expr); - throw(trait_jnt(_error)); -} - -void __jane_terminate_handler(void) noexcept { - try { - std::rethrow_exception(std::current_exception()); - } catch (trait_jnt _error) { - JANE_ID(println)(str_jnt("panic: ") + _error._get()._error()); - std::exit(__JANE_EXIT_PANIC); - } -} - -void __jane_signal_handler(int _Signal) noexcept { -#if defined(_WINDOWS) - if (_Signal == __JANE_SIGINT) { - return; - } -#elif defined(_DARWIN) - if (_Signal == __JULEC_SIGINT) { - return; - } -#elif defined(_LINUX) - if (_Signal == ___JANE_SIGINT) { - return; - } -#endif - JANE_ID(print)("program terminating with signal: "); - JANE_ID(println)(_Signal); -} - -#endif // !__JANE_HPP diff --git a/api/map.hpp b/api/map.hpp index 304652e..e6da7ff 100644 --- a/api/map.hpp +++ b/api/map.hpp @@ -1,4 +1,4 @@ -// Copyright (c) 2024 - DeRuneLabs +// Copyright (c) 2024 arfy slowy - DeRuneLabs // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal @@ -23,86 +23,92 @@ #include "slice.hpp" #include "str.hpp" -#include "typedef.hpp" -#include +#include "types.hpp" +#include #include +namespace jane { +class MapKeyHasher; +template class Map; -class __jane_map_key_hasher; - -template class map_jnt; - -class __jane_map_key_hasher { +class MapKeyHasher { public: - size_t operator()(const str_jnt &_Key) const noexcept { - size_t _hash{0}; - for (int_jnt _i{0}; _i < _Key._len(); ++_i) { - _hash += _Key[_i] % 7; + size_t operator()(const jane::Str &key) const noexcept { + size_t hash{0}; + for (jane::Int i{0}; i < key.len(); ++i) { + hash += key[i] % 7; } - return (_hash); + return hash; + } + template inline size_t operator()(const T &obj) const noexcept { + return this->operator()(jane::to_str(obj)); } - - template - inline size_t operator()(const _Obj_t &_Obj) const noexcept { - return (this->operator()(__jane_to_str<_Obj_t>(_Obj))); - }; }; -template -class map_jnt - : public std::unordered_map<_Key_t, _Value_t, __jane_map_key_hasher> { +template class Map { public: - map_jnt<_Key_t, _Value_t>(void) noexcept {} - map_jnt<_Key_t, _Value_t>(const std::nullptr_t) noexcept {} - - map_jnt<_Key_t, _Value_t>( - const std::initializer_list> &_Src) noexcept { - for (const auto _data : _Src) { - this->insert(_data); + mutable std::unordered_map buffer{}; + Map(void) noexcept {} + Map(const std::nullptr_t) noexcept {} + Map( + const std::initializer_list> &src) noexcept { + for (const std::pair &pair : src) { + this->buffer.insert(pair); } } - slice_jnt<_Key_t> _keys(void) const noexcept { - slice_jnt<_Key_t> _keys(this->size()); - uint_jnt _index{0}; - for (const auto &_pair : *this) { - _keys._alloc[_index++] = _pair.first; + inline constexpr auto begin(void) noexcept { return this->buffer.begin(); } + + inline constexpr auto end(void) noexcept { return this->buffer.end(); } + + inline constexpr auto end(void) const noexcept { return this->buffer.end(); } + + inline void clear(void) noexcept { this->buffer.clear(); } + + jane::Slice keys(void) const noexcept { + jane::Slice keys(this->len()); + jane::Uint index{0}; + for (const auto &pair : *this) { + keys._slice[index++] = pair.second; } - return (_keys); + return keys; } - slice_jnt<_Value_t> _values(void) const noexcept { - slice_jnt<_Value_t> _keys(this->size()); - uint_jnt _index{0}; - for (const auto &_pair : *this) { - _keys._alloc[_index++] = _pair.second; - } - return (_keys); + inline constexpr jane::Bool has(const Key &key) const noexcept { + return this->buffer.find(key) != this->end(); + } + + inline jane::Int len(void) const noexcept { return this->buffer.size(); } + + inline void del(const Key &key) noexcept { this->buffer.erase(key); } + + inline jane::Bool operator==(const std::nullptr_t) const noexcept { + return this->buffer.empty(); } - inline constexpr bool _has(const _Key_t _Key) const noexcept { - return (this->find(_Key) != this->end()); + inline jane::Bool operator!=(const std::nullptr_t) const noexcept { + return !this->operator==(nullptr); } - inline int_jnt _len(void) const noexcept { return (this->size()); } - - inline void _del(const _Key_t _Key) noexcept { this->erase(_Key); } - - friend std::ostream & - operator<<(std::ostream &_Stream, - const map_jnt<_Key_t, _Value_t> &_Src) noexcept { - _Stream << '{'; - uint_jnt _length{_Src.size()}; - for (const auto _pair : _Src) { - _Stream << _pair.first; - _Stream << ':'; - _Stream << _pair.second; - if (--_length > 0) { - _Stream << ", "; + Value &operator[](const Key &key) { return this->buffer[key]; } + + Value &operator[](const Key &key) const { return this->buffer[key]; } + + friend std::ostream &operator<<(std::ostream &stream, + const Map &src) noexcept { + stream << '{'; + jane::Int length{src.len()}; + for (const auto pair : src) { + stream << pair.first; + stream << ':'; + stream << pair.second; + if (--length > 0) { + stream << ", "; } } - _Stream << '}'; - return (_Stream); + stream << '}'; + return stream; } }; +} // namespace jane -#endif // !__JANE_MAP_HPP +#endif // __JANE_MAP_HPP \ No newline at end of file diff --git a/api/misc.hpp b/api/misc.hpp new file mode 100644 index 0000000..09bf0d3 --- /dev/null +++ b/api/misc.hpp @@ -0,0 +1,55 @@ +// Copyright (c) 2024 arfy slowy - DeRuneLabs +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +// SOFTWARE. + +#ifndef __JANE_MISC_HPP +#define __JANE_MISC_HPP + +#include "error.hpp" +#include "panic.hpp" +#include "ref.hpp" +#include "types.hpp" +namespace jane { +template +auto div(const T &x, const Denominator &denominator) noexcept; + +template jane::Ref new_struct(T *ptr); + +template +auto div(const T &x, const Denominator &denominator) noexcept { + if (denominator == 0) { + jane::panic(jane::ERROR_DIVIDE_BY_ZERO); + } + return (x / denominator); +} + +template jane::Ref new_struct(T *ptr) { + if (!ptr) { + jane::panic(jane::ERROR_MEMORY_ALLOCATION_FAILED); + } + ptr->self.ref = new (std::nothrow) jane::Uint; + if (!ptr->self.ref) { + jane::panic(jane::ERROR_MEMORY_ALLOCATION_FAILED); + } + *ptr->self.ref = 0; + return ptr->self; +} +} // namespace jane + +#endif //__JANE_MISC_HPP \ No newline at end of file diff --git a/api/panic.hpp b/api/panic.hpp new file mode 100644 index 0000000..1967a15 --- /dev/null +++ b/api/panic.hpp @@ -0,0 +1,57 @@ +// Copyright (c) 2024 arfy slowy - DeRuneLabs +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +// SOFTWARE. + +#ifndef __JANE_PANIC_HPP +#define __JANE_PANIC_HPP + +#include +#include +#include + +namespace jane { +class Exception; + +template void panic(const T &expr); + +class Exception : public std::exception { +private: + std::string message; + +public: + Exception(void) noexcept {} + + Exception(char *message) noexcept { this->message = message; } + + Exception(const std::string &message) noexcept { this->message = message; } + + char *what(void) noexcept { return (char *)this->message.c_str(); } + + const char *what(void) const noexcept { return this->message.c_str(); } +}; + +template void panic(const T &expr) { + std::stringstream sstream; + sstream << expr; + jane::Exception exception(sstream.str()); + throw exception; +} +} // namespace jane + +#endif // __JANE_PANIC_HPP \ No newline at end of file diff --git a/api/platform.hpp b/api/platform.hpp new file mode 100644 index 0000000..be3f937 --- /dev/null +++ b/api/platform.hpp @@ -0,0 +1,55 @@ +// Copyright (c) 2024 arfy slowy - DeRuneLabs +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +// SOFTWARE. + +#ifndef __JANE_PLATFORM_HPP +#define __JANE_PLATFORM_HPP + +#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__) +#define OS_WINDOWS +#elif defined(__linux__) || defined(linux) || defined(__linux) +#define OS_LINUX +#elif defined(__APPLE__) || defined(__MACH__) +#define OS_DARWIN +#endif + +#if defined(OS_LINUX) || defined(OS_DARWIN) +#define OS_UNIX +#endif + +#if defined(__amd64) || defined(__amd64__) || defined(__x86_64) || \ + defined(__x86_64__) || defined(_M_AMD64) +#define ARCH_AMD64 +#elif defined(__arm__) || defined(__thumb__) || defined(_M_ARM) || \ + defined(__arm) +#define ARCH_ARM +#elif defined(__aarch64__) +#define ARCH_ARM64 +#elif defined(i386) || defined(__i386) || defined(__i386__) || \ + defined(_X86_) || defined(__I86__) || defined(__386) +#define ARCH_I386 +#endif + +#if defined(ARCH_AMD64) || defined(ARCH_ARM64) +#define ARCH_64BIT +#else +#define ARCH_32BIT +#endif + +#endif // __JANE_PLATFORM_HPP \ No newline at end of file diff --git a/api/process.hpp b/api/process.hpp new file mode 100644 index 0000000..3bd5a26 --- /dev/null +++ b/api/process.hpp @@ -0,0 +1,92 @@ +// Copyright (c) 2024 arfy slowy - DeRuneLabs +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +// SOFTWARE. + +#ifndef __JANE_PROCESS_HPP +#define __JANE_PROCESS_HPP + +#include +#include + +#include "platform.hpp" +#include "slice.hpp" +#include "str.hpp" +#if defined(OS_DARWIN) +#include +#include +#elif defined(OS_WINDOWS) +#include +#elif defined(OS_LINUX) +#include +#include +#endif + +namespace jane { +jane::Slice command_line_args; +void setup_command_line_args(int argc, char *argv[]) noexcept; +jane::Str executable(void) noexcept; + +void setup_command_line_args(int argc, char *argv[]) noexcept { +#ifdef OS_WINDOWS + const LPWSTR cmdl{GetCommandLineW()}; + LPWSTR *argvw{CommandLineToArgvW(cmdl, &argc)}; +#endif + + jane::command_line_args = jane::Slice::alloc(argc); + for (jane::Int i{0}; i < argc; ++i) { +#ifdef OS_WINDOWS + const LPWSTR warg{argvw[i]}; + jane::comand_line_args[i] = + jane::utf16_to_utf8_str(warg, std::wcslen(warg)); +#else + jane::command_line_args[i] = argv[i]; +#endif + } +#ifdef OS_WINDOWS + LocalFree(argvw); + argvw = nullptr; +#endif +} +jane::Str executable(void) noexcept { +#if defined(OS_DARWIN) + char buff[PATH_MAX]; + uint32_t buff_size{PATH_MAX}; + if (!_NSGetExecutablePath(buff, &buff_size)) { + return jane::Str(buff); + } + return jane::Str(); +#elif defined(OS_WINDOWS) + wchar_t buffer[MAX_PATH]; + const DWORD n{GetModuleFileName(NULL, buffer, MAX_PATH)}; + if (n) { + return jane::utf16_to_utf8_str(&buffer[0], n); + } + return jane::Str(); +#elif defined(OS_LINUX) + char result[PATH_MAX]; + const ssize_t count{readlink("/proc/self/exe", result, PATH_MAX)}; + if (count != -1) { + return jane::Str(result); + } + return jane::Str(); +#endif +} +} // namespace jane + +#endif //__JANE_PROCESS_HPP \ No newline at end of file diff --git a/api/ref.hpp b/api/ref.hpp index 509bf9a..b79c6ba 100644 --- a/api/ref.hpp +++ b/api/ref.hpp @@ -1,4 +1,4 @@ -// Copyright (c) 2024 - DeRuneLabs +// Copyright (c) 2024 arfy slowy - DeRuneLabs // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal @@ -21,146 +21,169 @@ #ifndef __JANE_REF_HPP #define __JANE_REF_HPP -#include "atomicity.hpp" -#include "typedef.hpp" -constexpr signed int __JANE_REFERENCE_DELTA{1}; +#include "atomic.hpp" +#include "error.hpp" +#include "panic.hpp" +#include "types.hpp" +#include +#include +namespace jane { +constexpr signed int REFERENCE_DELTA{1}; -template struct ref_jnt; +template struct Ref; -template struct ref_jnt { - mutable T *__alloc{nil}; - mutable uint_jnt *__ref{nil}; +template inline jane::Ref new_ref(void) noexcept; - static ref_jnt make(T *_Ptr, uint_jnt *_Ref) noexcept { - ref_jnt _buffer; - _buffer.__alloc = _Ptr; - _buffer.__ref = _Ref; - return (_buffer); +template inline jane::Ref new_ref(const T &init) noexcept; + +template struct Ref { + mutable T *alloc{nullptr}; + mutable jane::Uint *ref{nullptr}; + + static jane::Ref make(T *ptr, jane::Uint *ref) noexcept { + jane::Ref buffer; + buffer.alloc = ptr; + buffer.ref = ref; + return buffer; } - static ref_jnt make(T *_Ptr) noexcept { - ref_jnt _buffer; - _buffer.__ref = (new (std::nothrow) uint_jnt); - if (!_buffer.__ref) { - JANE_ID(panic)(__JANE_ERROR_MEMORY_ALLOCATION_FAILED); + static jane::Ref make(T *ptr) noexcept { + jane::Ref buffer; + buffer.ref = new (std::nothrow) jane::Uint; + if (buffer.ref) { + jane::panic(jane::ERROR_MEMORY_ALLOCATION_FAILED); } - *_buffer.__ref = 1; - _buffer.__alloc = _Ptr; - return (_buffer); + *buffer.ref = 1; + buffer.alloc = ptr; + return buffer; } - static ref_jnt make(const T &_Instance) noexcept { - ref_jnt _buffer; - _buffer.__alloc = (new (std::nothrow) T); - if (!_buffer.__alloc) { - JANE_ID(panic)(__JANE_ERROR_MEMORY_ALLOCATION_FAILED); + static jane::Ref make(const T &instance) noexcept { + jane::Ref buffer; + buffer.alloc = new (std::nothrow) T; + if (!buffer.alloc) { + jane::panic(jane::ERROR_MEMORY_ALLOCATION_FAILED); } - _buffer.__ref = (new (std::nothrow) uint_jnt); - if (!_buffer.__ref) { - JANE_ID(panic)(__JANE_ERROR_MEMORY_ALLOCATION_FAILED); - } - *_buffer.__ref = __JANE_REFERENCE_DELTA; - *_buffer.__aloc = _Instance; - return (_buffer); + *buffer.ref = new (std::nothrow) jane::Uint; + *buffer.alloc = instance; + return buffer; } - ref_jnt(void) noexcept {} - - ref_jnt(const ref_jnt &_Ref) noexcept { this->operator=(_Ref); } + Ref(void) noexcept {} + Ref(const jane::Ref &ref) noexcept { this->operator=(ref); } + ~Ref(void) noexcept { this->drop(); } - ~ref_jnt(void) noexcept { this->_drop(); } - - inline int_jnt __drop_ref(void) const noexcept { - return (__jane_atomic_add(this->__ref, -__JANE_REFERENCE_DELTA)); + inline jane::Int drop_ref(void) const noexcept { + return __jane_atomic_add(this->ref, -jane::REFERENCE_DELTA); } - inline int_jnt __add_ref(void) const noexcept { - return (__jane_atomic_add(this->__ref, __JANE_REFERENCE_DELTA)); + inline jane::Int add_ref(void) const noexcept { + return __jane_atomic_add(this->ref, jane::REFERENCE_DELTA); } - inline uint_jnt __get_ref_n(void) const noexcept { - return (__jane_atomic_load(this->__ref)); + inline jane::Uint get_ref_n(void) const noexcept { + return __jane_atomic_load(this->ref); } - void _drop(void) const noexcept { - if (!this->__ref) { - this->__alloc = nil; + void drop(void) const noexcept { + if (!this->ref) { + this->alloc = nullptr; return; } - if ((this->__drop_ref()) != __JANE_REFERENCE_DELTA) { - this->__ref = nil; - this->__alloc = nil; + if (this->drop_ref() != jane::REFERENCE_DELTA) { + this->ref = nullptr; + this->alloc = nullptr; return; } - delete this->__ref; - this->__ref = nil; - delete this->__alloc; - this->__alloc = nil; + + delete this->ref; + this->ref = nullptr; + + delete this->alloc; + this->alloc = nullptr; } - inline bool _real() const noexcept { return (this->__alloc != nil); } + inline jane::Bool real() const noexcept { return this->alloc != nullptr; } - inline T *operator->(void) noexcept { - this->__must_ok(); - return (*this->__alloc); + inline T *operator->(void) const noexcept { + this->must_ok(); + return this->alloc; } - inline operator T(void) noexcept { - this->__must_ok(); - return (*this->__alloc); + inline operator T(void) const noexcept { + this->must_ok(); + return *this->alloc; } inline operator T &(void) noexcept { - this->__must_ok(); - return (*this->__alloc); + this->must_ok(); + return *this->alloc; } - inline void __must_ok(void) const noexcept { - if (!this->_real()) { - JANE_ID(panic)(__JANE_ERROR_INVALID_MEMORY); + inline void must_ok(void) const noexcept { + if (!this->real()) { + jane::panic(jane::ERROR_INVALID_MEMORY); } } - void operator=(const ref_jnt &_Ref) noexcept { - this->_drop(); - if (_Ref.__ref) { - _Ref.__add_ref(); + void operator=(const jane::Ref &ref) noexcept { + this->drop(); + if (ref.ref) { + ref.add_ref(); } - this->__ref = _Ref.__ref; - this->__alloc = _Ref.__alloc; + this->ref = ref.ref; + this->alloc = ref.alloc; } - inline bool operator==(const T &_Val) const noexcept { - return (this->__alloc == nil ? false : *this->__alloc == _Val); + inline void operator=(const T &val) const noexcept { + this->must_ok(); + *this->alloc = val; } - inline bool operator!=(const T &_Val) const noexcept { - return (!this->operator==(_Val)); + inline jane::Bool operator==(const T &val) const noexcept { + return this->__alloc == nullptr ? false : *this->alloc == val; } - inline bool operator==(const ref_jnt &_Ref) const noexcept { - if (this->__alloc == nil) { - return _Ref.__alloc == nil; + inline jane::Bool operator!=(const T &val) const noexcept { + return !this->operator==(val); + } + + inline jane::Bool operator==(const jane::Ref &ref) const noexcept { + if (this->alloc == nullptr) { + return ref.alloc == nullptr; } - if (_Ref.__alloc == nil) { + if (ref.alloc == nullptr) { return false; } - return ((*this->__alloc) == (*_Ref.__alloc)); + if (this->alloc == ref.alloc) { + return true; + } + return *this->alloc == *ref.alloc; } - inline bool operator!=(const ref_jnt &_Ref) const noexcept { - return (!this->operator==(_Ref)); + inline jane::Bool operator!=(const jane::Ref &ref) const noexcept { + return !this->operator==(ref); } - friend inline std::ostream &operator<<(std::ostream &_Stream, - const ref_jnt &_Ref) noexcept { - if (!_Ref._real()) { - _Stream << "nil"; + friend inline std::ostream &operator<<(std::ostream &stream, + const jane::Ref &ref) noexcept { + if (!ref.real()) { + stream << "nil"; } else { - _Stream << _Ref.operator T(); + stream << ref.operator T(); } - return (_Stream); + return stream; } }; -#endif // !__JANE_REF_HPP +template inline jane::Ref new_ref(void) noexcept { + return jane::Ref(); +} + +template inline jane::Ref new_ref(const T &init) noexcept { + return jane::Ref::make(init); +} + +} // namespace jane + +#endif // __JANE_REF_HPP \ No newline at end of file diff --git a/api/signal.hpp b/api/signal.hpp index b10530b..3d60f8f 100644 --- a/api/signal.hpp +++ b/api/signal.hpp @@ -1,4 +1,4 @@ -// Copyright (c) 2024 - DeRuneLabs +// Copyright (c) 2024 arfy slowy - DeRuneLabs // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal @@ -21,186 +21,194 @@ #ifndef __JANE_SIGNAL_HPP #define __JANE_SIGNAL_HPP +#include "platform.hpp" #include +#include -#define __JANE_SIG constexpr int +namespace jane { +typedef int Signal; -#if defined(_WINDOWS) -__JANE_SIG __JANE_SIGHUP{0x1}; -__JANE_SIG __JANE_SIGINT{0x2}; -__JANE_SIG __JANE_SIGQUIT{0x3}; -__JANE_SIG __JANE_SIGILL{0x4}; -__JANE_SIG __JANE_SIGTRAP{0x5}; -__JANE_SIG __JANE_SIGABRT{0x6}; -__JANE_SIG __JANE_SIGBUS{0x7}; -__JANE_SIG __JANE_SIGFPE{0x8}; -__JANE_SIG __JANE_SIGKILL{0x9}; -__JANE_SIG __JANE_SIGSEGV{0xb}; -__JANE_SIG __JANE_SIGPIPE{0xd}; -__JANE_SIG __JANE_SIGALRM{0xe}; -__JANE_SIG __JANE_SIGTERM{0xf}; +void set_sig_handler(void (*handler)(int sig)) noexcept; +void signal_handler(int signal) noexcept; -#elif defined(_DARWIN) +#if defined(OS_WINDOWS) +constexpr jane::Signal SIG_HUP{0x1}; +constexpr jane::Signal SIG_INT{0x2}; +constexpr jane::Signal SIG_QUIT{0x3}; +constexpr jane::Signal SIG_ILL{0x4}; +constexpr jane::Signal SIG_TRAP{0x5}; +constexpr jane::Signal SIG_ABRT{0x6}; +constexpr jane::Signal SIG_BUS{0x7}; +constexpr jane::Signal SIG_FPE{0x8}; +constexpr jane::Signal SIG_KILL{0x9}; +constexpr jane::Signal SIG_SEGV{0xb}; +constexpr jane::Signal SIG_PIPE{0xd}; +constexpr jane::Signal SIG_ALRM{0xe}; +constexpr jane::Signal SIG_TERM{0xf}; +#elif defined(OS_DARWIN) +constexpr jane::Signal SIG_ABRT{0x6}; +constexpr jane::Signal SIG_ALRM{0xe}; +constexpr jane::Signal SIG_BUS{0xa}; +constexpr jane::Signal SIG_CHLD{0x14}; +constexpr jane::Signal SIG_CONT{0x13}; +constexpr jane::Signal SIG_EMT{0x7}; +constexpr jane::Signal SIG_FPE{0x8}; +constexpr jane::Signal SIG_HUP{0x1}; +constexpr jane::Signal SIG_ILL{0x4}; +constexpr jane::Signal SIG_INFO{0x1d}; +constexpr jane::Signal SIG_INT{0x2}; +constexpr jane::Signal SIG_IO{0x17}; +constexpr jane::Signal SIG_IOT{0x6}; +constexpr jane::Signal SIG_KILL{0x9}; +constexpr jane::Signal SIG_PIPE{0xd}; +constexpr jane::Signal SIG_PROF{0x1b}; +constexpr jane::Signal SIG_QUIT{0x3}; +constexpr jane::Signal SIG_SEGV{0xb}; +constexpr jane::Signal SIG_STOP{0x11}; +constexpr jane::Signal SIG_SYS{0xc}; +constexpr jane::Signal SIG_TERM{0xf}; +constexpr jane::Signal SIG_TRAP{0x5}; +constexpr jane::Signal SIG_TSTP{0x12}; +constexpr jane::Signal SIG_TTIN{0x15}; +constexpr jane::Signal SIG_TTOU{0x16}; +constexpr jane::Signal SIG_URG{0x10}; +constexpr jane::Signal SIG_USR1{0x1e}; +constexpr jane::Signal SIG_USR2{0x1f}; +constexpr jane::Signal SIG_VTALRM{0x1a}; +constexpr jane::Signal SIG_WINCH{0x1c}; +constexpr jane::Signal SIG_XCPU{0x18}; +constexpr jane::Signal SIG_XFSZ{0x19}; +#elif defined(OS_LINUX) +constexpr jane::Signal SIG_ABRT{0x6}; +constexpr jane::Signal SIG_ALRM{0xe}; +constexpr jane::Signal SIG_BUS{0x7}; +constexpr jane::Signal SIG_CHLD{0x11}; +constexpr jane::Signal SIG_CLD{0x11}; +constexpr jane::Signal SIG_CONT{0x12}; +constexpr jane::Signal SIG_FPE{0x8}; +constexpr jane::Signal SIG_HUP{0x1}; +constexpr jane::Signal SIG_ILL{0x4}; +constexpr jane::Signal SIG_INT{0x2}; +constexpr jane::Signal SIG_IO{0x1d}; +constexpr jane::Signal SIG_IOT{0x6}; +constexpr jane::Signal SIG_KILL{0x9}; +constexpr jane::Signal SIG_PIPE{0xd}; +constexpr jane::Signal SIG_POLL{0x1d}; +constexpr jane::Signal SIG_PROF{0x1b}; +constexpr jane::Signal SIG_PWR{0x1e}; +constexpr jane::Signal SIG_QUIT{0x3}; +constexpr jane::Signal SIG_SEGV{0xb}; +constexpr jane::Signal SIG_STKFLT{0x10}; +constexpr jane::Signal SIG_STOP{0x13}; +constexpr jane::Signal SIG_SYS{0x1f}; +constexpr jane::Signal SIG_TERM{0xf}; +constexpr jane::Signal SIG_TRAP{0x5}; +constexpr jane::Signal SIG_TSTP{0x14}; +constexpr jane::Signal SIG_TTIN{0x15}; +constexpr jane::Signal SIG_TTOU{0x16}; +constexpr jane::Signal SIG_UNUSED{0x1f}; +constexpr jane::Signal SIG_URG{0x17}; +constexpr jane::Signal SIG_USR1{0xa}; +constexpr jane::Signal SIG_USR2{0xc}; +constexpr jane::Signal SIG_VTALRM{0x1a}; +constexpr jane::Signal SIG_WINCH{0x1c}; +constexpr jane::Signal SIG_XCPU{0x18}; +constexpr jane::Signal SIG_XFSZ{0x19}; +#endif -__JANE_SIG __JANE_SIGABRT{0x6}; -__JANE_SIG __JANE_SIGALRM{0xe}; -__JANE_SIG __JANE_SIGBUS{0xa}; -__JANE_SIG __JANE_SIGCHLD{0x14}; -__JANE_SIG __JANE_SIGCONT{0x13}; -__JANE_SIG __JANE_SIGEMT{0x7}; -__JANE_SIG __JANE_SIGFPE{0x8}; -__JANE_SIG __JANE_SIGHUP{0x1}; -__JANE_SIG __JANE_SIGILL{0x4}; -__JANE_SIG __JANE_SIGINFO{0x1d}; -__JANE_SIG __JANE_SIGINT{0x2}; -__JANE_SIG __JANE_SIGIO{0x17}; -__JANE_SIG __JANE_SIGIOT{0x6}; -__JANE_SIG __JANE_SIGKILL{0x9}; -__JANE_SIG __JANE_SIGPIPE{0xd}; -__JANE_SIG __JANE_SIGPROF{0x1b}; -__JANE_SIG __JANE_SIGQUIT{0x3}; -__JANE_SIG __JANE_SIGSEGV{0xb}; -__JANE_SIG __JANE_SIGSTOP{0x11}; -__JANE_SIG __JANE_SIGSYS{0xc}; -__JANE_SIG __JANE_SIGTERM{0xf}; -__JANE_SIG __JANE_SIGTRAP{0x5}; -__JANE_SIG __JANE_SIGTSTP{0x12}; -__JANE_SIG __JANE_SIGTTIN{0x15}; -__JANE_SIG __JANE_SIGTTOU{0x16}; -__JANE_SIG __JANE_SIGURG{0x10}; -__JANE_SIG __JANE_SIGUSR1{0x1e}; -__JANE_SIG __JANE_SIGUSR2{0x1f}; -__JANE_SIG __JANE_SIGVTALRM{0x1a}; -__JANE_SIG __JANE_SIGWINCH{0x1c}; -__JANE_SIG __JANE_SIGXCPU{0x18}; -__JANE_SIG __JANE_SIGXFSZ{0x19}; - -#elif defined(_LINUX) - -__JANE_SIG ___JANE_SIGABRT{0x6}; -__JANE_SIG ___JANE_SIGALRM{0xe}; -__JANE_SIG ___JANE_SIGBUS{0x7}; -__JANE_SIG ___JANE_SIGCHLD{0x11}; -__JANE_SIG ___JANE_SIGCLD{0x11}; -__JANE_SIG ___JANE_SIGCONT{0x12}; -__JANE_SIG ___JANE_SIGFPE{0x8}; -__JANE_SIG ___JANE_SIGHUP{0x1}; -__JANE_SIG ___JANE_SIGILL{0x4}; -__JANE_SIG ___JANE_SIGINT{0x2}; -__JANE_SIG ___JANE_SIGIO{0x1d}; -__JANE_SIG ___JANE_SIGIOT{0x6}; -__JANE_SIG ___JANE_SIGKILL{0x9}; -__JANE_SIG ___JANE_SIGPIPE{0xd}; -__JANE_SIG ___JANE_SIGPOLL{0x1d}; -__JANE_SIG ___JANE_SIGPROF{0x1b}; -__JANE_SIG ___JANE_SIGPWR{0x1e}; -__JANE_SIG ___JANE_SIGQUIT{0x3}; -__JANE_SIG ___JANE_SIGSEGV{0xb}; -__JANE_SIG ___JANE_SIGSTKFLT{0x10}; -__JANE_SIG ___JANE_SIGSTOP{0x13}; -__JANE_SIG ___JANE_SIGSYS{0x1f}; -__JANE_SIG ___JANE_SIGTERM{0xf}; -__JANE_SIG ___JANE_SIGTRAP{0x5}; -__JANE_SIG ___JANE_SIGTSTP{0x14}; -__JANE_SIG ___JANE_SIGTTIN{0x15}; -__JANE_SIG ___JANE_SIGTTOU{0x16}; -__JANE_SIG ___JANE_SIGUNUSED{0x1f}; -__JANE_SIG ___JANE_SIGURG{0x17}; -__JANE_SIG ___JANE_SIGUSR1{0xa}; -__JANE_SIG ___JANE_SIGUSR2{0xc}; -__JANE_SIG ___JANE_SIGVTALRM{0x1a}; -__JANE_SIG ___JANE_SIGWINCH{0x1c}; -__JANE_SIG ___JANE_SIGXCPU{0x18}; -__JANE_SIG ___JANE_SIGXFSZ{0x19}; - -#endif // #define(_WINDOWS) +void set_sig_handler(void (*handler)(int _sig)) noexcept { +#if defined(OS_WINDOWS) + std::signal(jane::SIG_HUP, handler); + std::signal(jane::SIG_INT, handler); + std::signal(jane::SIG_QUIT, handler); + std::signal(jane::SIG_ILL, handler); + std::signal(jane::SIG_TRAP, handler); + std::signal(jane::SIG_ABRT, handler); + std::signal(jane::SIG_BUS, handler); + std::signal(jane::SIG_FPE, handler); + std::signal(jane::SIG_KILL, handler); + std::signal(jane::SIG_SEGV, handler); + std::signal(jane::SIG_PIPE, handler); + std::signal(jane::SIG_ALRM, handler); + std::signal(jane::SIG_TERM, handler); +#elif defined(OS_DARWIN) + std::signal(jane::SIG_ABRT, handler); + std::signal(jane::SIG_ALRM, handler); + std::signal(jane::SIG_BUS, handler); + std::signal(jane::SIG_CHLD, handler); + std::signal(jane::SIG_CONT, handler); + std::signal(jane::SIG_EMT, handler); + std::signal(jane::SIG_FPE, handler); + std::signal(jane::SIG_HUP, handler); + std::signal(jane::SIG_ILL, handler); + std::signal(jane::SIG_INFO, handler); + std::signal(jane::SIG_INT, handler); + std::signal(jane::SIG_IO, handler); + std::signal(jane::SIG_IOT, handler); + std::signal(jane::SIG_KILL, handler); + std::signal(jane::SIG_PIPE, handler); + std::signal(jane::SIG_PROF, handler); + std::signal(jane::SIG_QUIT, handler); + std::signal(jane::SIG_SEGV, handler); + std::signal(jane::SIG_STOP, handler); + std::signal(jane::SIG_SYS, handler); + std::signal(jane::SIG_TERM, handler); + std::signal(jane::SIG_TRAP, handler); + std::signal(jane::SIG_TSTP, handler); + std::signal(jane::SIG_TTIN, handler); + std::signal(jane::SIG_TTOU, handler); + std::signal(jane::SIG_URG, handler); + std::signal(jane::SIG_USR1, handler); + std::signal(jane::SIG_USR2, handler); + std::signal(jane::SIG_VTALRM, handler); + std::signal(jane::SIG_WINCH, handler); + std::signal(jane::SIG_XCPU, handler); + std::signal(jane::SIG_XFSZ, handler); +#elif defined(OS_LINUX) + std::signal(jane::SIG_ABRT, handler); + std::signal(jane::SIG_ALRM, handler); + std::signal(jane::SIG_BUS, handler); + std::signal(jane::SIG_CHLD, handler); + std::signal(jane::SIG_CLD, handler); + std::signal(jane::SIG_CONT, handler); + std::signal(jane::SIG_FPE, handler); + std::signal(jane::SIG_HUP, handler); + std::signal(jane::SIG_ILL, handler); + std::signal(jane::SIG_INT, handler); + std::signal(jane::SIG_IO, handler); + std::signal(jane::SIG_IOT, handler); + std::signal(jane::SIG_KILL, handler); + std::signal(jane::SIG_PIPE, handler); + std::signal(jane::SIG_POLL, handler); + std::signal(jane::SIG_PROF, handler); + std::signal(jane::SIG_PWR, handler); + std::signal(jane::SIG_QUIT, handler); + std::signal(jane::SIG_SEGV, handler); + std::signal(jane::SIG_STKFLT, handler); + std::signal(jane::SIG_STOP, handler); + std::signal(jane::SIG_SYS, handler); + std::signal(jane::SIG_TERM, handler); + std::signal(jane::SIG_TRAP, handler); + std::signal(jane::SIG_TSTP, handler); + std::signal(jane::SIG_TTIN, handler); + std::signal(jane::SIG_TTOU, handler); + std::signal(jane::SIG_UNUSED, handler); + std::signal(jane::SIG_URG, handler); + std::signal(jane::SIG_USR1, handler); + std::signal(jane::SIG_USR2, handler); + std::signal(jane::SIG_VTALRM, handler); + std::signal(jane::SIG_WINCH, handler); + std::signal(jane::SIG_XCPU, handler); + std::signal(jane::SIG_XFSZ, handler); +#endif +} -void __jane_set_sig_handler(void (*_Handler)(int _Sig)) noexcept; -void __jane_set_sig_handler(void (*_Handler)(int _Sig)) noexcept { -#if defined(_WINDOWS) - signal(__JANE_SIGHUP, _Handler); - signal(__JANE_SIGINT, _Handler); - signal(__JANE_SIGQUIT, _Handler); - signal(__JANE_SIGILL, _Handler); - signal(__JANE_SIGTRAP, _Handler); - signal(__JANE_SIGABRT, _Handler); - signal(__JANE_SIGBUS, _Handler); - signal(__JANE_SIGFPE, _Handler); - signal(__JANE_SIGKILL, _Handler); - signal(__JANE_SIGSEGV, _Handler); - signal(__JANE_SIGPIPE, _Handler); - signal(__JANE_SIGALRM, _Handler); - signal(__JANE_SIGTERM, _Handler); -#elif defined(_DARWIN) - signal(__JANE_SIGABRT, _Handler); - signal(__JANE_SIGALRM, _Handler); - signal(__JANE_SIGBUS, _Handler); - signal(__JANE_SIGCHLD, _Handler); - signal(__JANE_SIGCONT, _Handler); - signal(__JANE_SIGEMT, _Handler); - signal(__JANE_SIGFPE, _Handler); - signal(__JANE_SIGHUP, _Handler); - signal(__JANE_SIGILL, _Handler); - signal(__JANE_SIGINFO, _Handler); - signal(__JANE_SIGINT, _Handler); - signal(__JANE_SIGIO, _Handler); - signal(__JANE_SIGIOT, _Handler); - signal(__JANE_SIGKILL, _Handler); - signal(__JANE_SIGPIPE, _Handler); - signal(__JANE_SIGPROF, _Handler); - signal(__JANE_SIGQUIT, _Handler); - signal(__JANE_SIGSEGV, _Handler); - signal(__JANE_SIGSTOP, _Handler); - signal(__JANE_SIGSYS, _Handler); - signal(__JANE_SIGTERM, _Handler); - signal(__JANE_SIGTRAP, _Handler); - signal(__JANE_SIGTSTP, _Handler); - signal(__JANE_SIGTTIN, _Handler); - signal(__JANE_SIGTTOU, _Handler); - signal(__JANE_SIGURG, _Handler); - signal(__JANE_SIGUSR1, _Handler); - signal(__JANE_SIGUSR2, _Handler); - signal(__JANE_SIGVTALRM, _Handler); - signal(__JANE_SIGWINCH, _Handler); - signal(__JANE_SIGXCPU, _Handler); - signal(__JANE_SIGXFSZ, _Handler); -#elif defined(_LINUX) - signal(___JANE_SIGABRT, _Handler); - signal(___JANE_SIGALRM, _Handler); - signal(___JANE_SIGBUS, _Handler); - signal(___JANE_SIGCHLD, _Handler); - signal(___JANE_SIGCLD, _Handler); - signal(___JANE_SIGCONT, _Handler); - signal(___JANE_SIGFPE, _Handler); - signal(___JANE_SIGHUP, _Handler); - signal(___JANE_SIGILL, _Handler); - signal(___JANE_SIGINT, _Handler); - signal(___JANE_SIGIO, _Handler); - signal(___JANE_SIGIOT, _Handler); - signal(___JANE_SIGKILL, _Handler); - signal(___JANE_SIGPIPE, _Handler); - signal(___JANE_SIGPOLL, _Handler); - signal(___JANE_SIGPROF, _Handler); - signal(___JANE_SIGPWR, _Handler); - signal(___JANE_SIGQUIT, _Handler); - signal(___JANE_SIGSEGV, _Handler); - signal(___JANE_SIGSTKFLT, _Handler); - signal(___JANE_SIGSTOP, _Handler); - signal(___JANE_SIGSYS, _Handler); - signal(___JANE_SIGTERM, _Handler); - signal(___JANE_SIGTRAP, _Handler); - signal(___JANE_SIGTSTP, _Handler); - signal(___JANE_SIGTTIN, _Handler); - signal(___JANE_SIGTTOU, _Handler); - signal(___JANE_SIGUNUSED, _Handler); - signal(___JANE_SIGURG, _Handler); - signal(___JANE_SIGUSR1, _Handler); - signal(___JANE_SIGUSR2, _Handler); - signal(___JANE_SIGVTALRM, _Handler); - signal(___JANE_SIGWINCH, _Handler); - signal(___JANE_SIGXCPU, _Handler); - signal(___JANE_SIGXFSZ, _Handler); -#endif // defined(_WINDOWS) +void signal_handler(int signal) noexcept { + jane::print("program terminated with signal: "); + jane::println(signal); + std::exit(signal); } -#endif // !__JANE_SIGNAL_HPP +} // namespace jane + +#endif // __JANE_SIGNAL_HPP \ No newline at end of file diff --git a/api/slice.hpp b/api/slice.hpp index ce82a47..0270df8 100644 --- a/api/slice.hpp +++ b/api/slice.hpp @@ -1,4 +1,4 @@ -// Copyright (c) 2024 - DeRuneLabs +// Copyright (c) 2024 arfy slowy - DeRuneLabs // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal @@ -21,218 +21,237 @@ #ifndef __JANE_SLICE_HPP #define __JANE_SLICE_HPP +#include +#include +#include +#include + +#include "error.hpp" +#include "panic.hpp" #include "ref.hpp" -#include "typedef.hpp" +#include "types.hpp" -template class slice_jnt; +namespace jane { +template class Slice; -template class slice_jnt { +template class Slice { public: - ref_jnt<_Item_t> __data{}; - _Item_t *__slice{nil}; - uint_jnt __len{0}; - uint_jnt __cap{0}; - - slice_jnt<_Item_t>(void) noexcept {} - slice_jnt<_Item_t>(const std::nullptr_t) noexcept {} - - slice_jnt<_Item_t>(const uint_jnt &_N) noexcept { - const uint_jnt _n{_N < 0 ? 0 : _N}; - if (_n == 0) { - return; - } - this->__alloc_new(_n); + jane::Ref data{}; + Item *_slice{nullptr}; + jane::Uint _len{0}; + jane::Uint _cap{0}; + + static jane::Slice alloc(const jane::Uint &n) noexcept { + jane::Slice buffer; + buffer.alloc_new(n < 0 ? 0 : n); + return buffer; } - slice_jnt<_Item_t>(const slice_jnt<_Item_t> &_Src) noexcept { - this->operator=(_Src); - } + Slice(void) noexcept {} + Slice(const std::nullptr_t) noexcept {} + + Slice(const jane::Slice &src) noexcept { this->operator=(src); } - slice_jnt<_Item_t>(const std::initializer_list<_Item_t> &_Src) noexcept { - if (_Src.size() == 0) { + Slice(const std::initializer_list &src) noexcept { + if (src.size() == 0) { return; } - this->__alloc_new(_Src.size()); - const auto _Src_begin{_Src.begin()}; - for (int_jnt _i{0}; _i < this->__len; ++_i) { - this->__data.__alloc[_i] = *(_Item_t *)(_Src_begin + _i); + + this->alloc_new(src.size()); + const auto src_begin{src.begin()}; + for (jane::Int i{0}; i < this->_len; ++i) { + this->data.alloc[i] = *reinterpret_cast(src_begin + i); } } - ~slice_jnt<_Item_t>(void) noexcept { this->__dealloc(); } + ~Slice(void) noexcept { this->dealloc(); } - inline void __check(void) const noexcept { - if (this->operator==(nil)) { - JANE_ID(panic)(__JANE_ERROR_INVALID_MEMORY); + inline void check(void) const noexcept { + if (this->operator==(nullptr)) { + jane::panic(jane::ERROR_INVALID_MEMORY); } } - void __dealloc(void) noexcept { - this->__len = 0; - this->__cap = 0; - if (!this->__data.__ref) { - this->__data.__alloc = nil; + void dealloc(void) noexcept { + this->_len = 0; + this->_cap = 0; + + if (!this->data.ref) { + this->data.alloc = nullptr; return; } - if ((this->__data.__get_ref_n()) != __JANE_REFERENCE_DELTA) { - this->__data.__alloc = nil; + + if (this->data.get_ref_n() != jane::REFERENCE_DELTA) { + this->data.alloc = nullptr; return; } - delete this->__data.__ref; - this->__data.__ref = nil; - delete[] this->__data.__alloc; - this->__data.__alloc = nil; - this->__data.__ref = nil; - this->__slice = nil; - } - - void __alloc_new(const int_jnt _N) noexcept { - this->__dealloc(); - _Item_t *_alloc{new (std::nothrow) _Item_t[_N]{_Item_t()}}; - if (!_alloc) { - JANE_ID(panic)(__JANE_ERROR_MEMORY_ALLOCATION_FAILED); + + delete this->data.ref; + this->data.ref = nullptr; + + delete[] this->data.alloc; + this->data.alloc = nullptr; + this->data.ref = nullptr; + this->_slice = nullptr; + } + + void alloc_new(const jane::Int n) noexcept { + this->dealloc(); + + Item *alloc{n == 0 ? new (std::nothrow) Item[0] + : new (std::nothrow) Item[n]{Item()}}; + if (!alloc) { + jane::panic(jane::ERROR_MEMORY_ALLOCATION_FAILED); } - this->__data = ref_jnt<_Item_t>::make(_alloc); - this->__len = _N; - this->__cap = _N; - this->__slice = &_alloc[0]; + this->data = jane::Ref::make(alloc); + this->_len = n; + this->_cap = n; + this->_slice = &alloc[0]; } - typedef _Item_t *iterator; - typedef const _Item_t *const_iterator; + typedef Item *Iterator; + typedef const Item *ConstIterator; - inline constexpr iterator begin(void) noexcept { return &this->__slice[0]; } + inline constexpr Iterator begin(void) noexcept { return &this->_slice[0]; } - inline constexpr const_iterator begin(void) const noexcept { - return &this->__slice[0]; + inline constexpr ConstIterator begin(void) const noexcept { + return &this->_slice[0]; } - inline constexpr iterator end(void) noexcept { - return &this->__slice[this->__len]; + inline constexpr Iterator end(void) noexcept { + return &this->_slice[this->_len]; } - inline constexpr const_iterator end(void) const noexcept { - return &this->__slice[this->__len]; + inline constexpr ConstIterator end(void) const noexcept { + return &this->_slice[this->_len]; } - inline slice_jnt<_Item_t> ___slice(const int_jnt &_Start, - const int_jnt &_End) const noexcept { - this->__check(); - if (_Start < 0 || _End < 0 || _Start > _End || _End > this->_cap()) { - std::stringstream _sstream; - __JANE_WRITE_ERROR_SLICING_INDEX_OUT_OF_RANGE(_sstream, _Start, _End); - JANE_ID(panic)(_sstream.str().c_str()); - } else if (_Start == _End) { - return slice_jnt<_Item_t>(); + inline Slice slice(const jane::Int &start, + const jane::Int &end) const noexcept { + this->check(); + + if (start < 0 || end < 0 || start > end || end > this->cap()) { + std::stringstream sstream; + __JANE_WRITE_ERROR_SLICING_INDEX_OUT_OF_RANGE(sstream, start, end); + jane::panic(sstream.str().c_str()); } - slice_jnt<_Item_t> _slice; - _slice.__data = this->__data; - _slice.__slice = &this->__slice[_Start]; - _slice.__len = _End - _Start; - _slice.__cap = this->__cap - _Start; - return _slice; + + jane::Slice slice; + slice.data = this->data; + slice._slice = &this->_slice[start]; + slice._len = end - start; + slice._cap = this->_cap - start; + return slice; } - inline slice_jnt<_Item_t> ___slice(const int_jnt &_Start) const noexcept { - return this->___slice(_Start, this->_len()); + inline jane::Slice slice(const jane::Int &start) const noexcept { + return this->slice(start, this->len()); } - inline slice_jnt<_Item_t> ___slice(void) const noexcept { - return this->___slice(0, this->_len()); + inline jane::Slice slice(void) const noexcept { + return this->slice(0, this->len()); } - inline constexpr int_jnt _len(void) const noexcept { return (this->__len); } + inline constexpr jane::Int len(void) const noexcept { return this->_len; } - inline constexpr int_jnt _cap(void) const noexcept { return (this->__cap); } + inline constexpr jane::Int cap(void) const noexcept { return this->_cap; } - inline bool _empty(void) const noexcept { - return (!this->__slice || this->__len == 0 || this->__cap == 0); + inline jane::Bool empty(void) const noexcept { + return !this->_slice || this->_len == 0 || this->_cap == 0; } - void __push(const _Item_t &_Item) noexcept { - if (this->__len == this->__cap) { - _Item_t *_new{new (std::nothrow) _Item_t[this->__len + 1]}; + void push(const Item &item) noexcept { + if (this->_len == this->_cap) { + Item *_new{new (std::nothrow) Item[this->_len + 1]}; if (!_new) { - JANE_ID(panic)(__JANE_ERROR_MEMORY_ALLOCATION_FAILED); + jane::panic(jane::ERROR_MEMORY_ALLOCATION_FAILED); } - for (int_jnt _index{0}; _index < this->__len; ++_index) { - _new[_index] = this->__data.__alloc[_index]; + for (jane::Int index{0}; index < this->_len; ++index) { + _new[index] = this->data.alloc[index]; } - _new[this->__len] = _Item; - delete[] this->__data.__alloc; - this->__data.__alloc = nil; - this->__data.__alloc = _new; - this->__slice = this->__data.__alloc; - ++this->__cap; + _new[this->_len] = item; + + delete[] this->data.alloc; + this->data.alloc = nullptr; + this->data.alloc = _new; + this->_slice = this->data.alloc; } else { - this->__slice[this->__len] = _Item; + this->_slice[this->_len] = item; } - ++this->__len; + ++this->_len; } - bool operator==(const slice_jnt<_Item_t> &_Src) const noexcept { - if (this->__len != _Src.__len) { + jane::Bool operator==(const jane::Slice &src) const noexcept { + if (this->_len != src._len) { return false; } - for (int_jnt _index{0}; _index < this->__len; ++_index) { - if (this->__slice[_index] != _Src.__slice[_index]) { - return (false); + for (jane::Int index{0}; index < this->_len; ++index) { + if (this->_slice[index] != src._slice[index]) { + return false; } } - return (true); + return true; } - inline constexpr bool - operator!=(const slice_jnt<_Item_t> &_Src) const noexcept { - return !this->operator==(_Src); + inline constexpr jane::Bool + operator!=(const jane::Slice &src) const noexcept { + return !this->operator==(src); } - inline constexpr bool operator==(const std::nullptr_t) const noexcept { - return !this->__slice; + inline constexpr jane::Bool operator==(const std::nullptr_t) const noexcept { + return !this->_slice; } - inline constexpr bool operator!=(const std::nullptr_t) const noexcept { - return !this->operator==(nil); + inline constexpr jane::Bool operator!=(const std::nullptr_t) const noexcept { + return !this->operator==(nullptr); } - _Item_t &operator[](const int_jnt &_Index) const { - this->__check(); - if (this->_empty() || _Index < 0 || this->_len() <= _Index) { - std::stringstream _sstream; - __JANE_WRITE_ERROR_INDEX_OUT_OF_RANGE(_sstream, _Index); - JANE_ID(panic)(_sstream.str().c_str()); + Item &operator[](const jane::Int &index) const { + this->check(); + if (this->empty() || index < 0 || this->len() <= index) { + std::stringstream sstream; + __JANE_WRITE_ERROR_INDEX_OUT_OF_RANGE(sstream, index); + jane::panic(sstream.str().c_str()); } - return this->__slice[_Index]; + return this->_slice[index]; } - void operator=(const slice_jnt<_Item_t> &_Src) noexcept { - this->__dealloc(); - if (_Src.operator==(nil)) { + void operator=(const jane::Slice &src) noexcept { + if (this->data.alloc == src.data.alloc) { + this->_len = src._len; + this->_cap = src._cap; + this->data = src.data; + this->_slice = src._slice; + return; + } + this->dealloc(); + if (src.operator==(nullptr)) { return; } - this->__len = _Src.__len; - this->__cap = _Src.__cap; - this->__data = _Src.__data; - this->__slice = _Src.__slice; + this->_len = src._len; + this->_cap = src._cap; + this->data = src.data; + this->_slice = src._slice; } - void operator=(const std::nullptr_t) noexcept { this->__dealloc(); } + void operator=(const std::nullptr_t) noexcept { this->dealloc(); } - friend std::ostream &operator<<(std::ostream &_Stream, - const slice_jnt<_Item_t> &_Src) noexcept { - if (_Src._empty()) { - return (_Stream << "[]"); + friend std::ostream &operator<<(std::ostream &stream, + const jane::Slice &src) noexcept { + if (src.empty()) { + return stream << "[]"; } - _Stream << '['; - for (int_jnt _index{0}; _index < _Src.__len;) { - _Stream << _Src.__slice[_index++]; - if (_index < _Src.__len) { - _Stream << ' '; + stream << '['; + for (jane::Int index{0}; index < src._len;) { + stream << src._slice[index++]; + if (index < src._len) { + stream << ' '; } - _Stream << ']'; - return (_Stream); } + stream << ']'; + return stream; } }; +} // namespace jane -#endif // !__JANE_SLICE_HPP +#endif // __JANE_SLICE_HPP \ No newline at end of file diff --git a/api/str.hpp b/api/str.hpp index 1818800..ea75e44 100644 --- a/api/str.hpp +++ b/api/str.hpp @@ -1,4 +1,4 @@ -// Copyright (c) 2024 - DeRuneLabs +// Copyright (c) 2024 arfy slowy - DeRuneLabs // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal @@ -21,301 +21,322 @@ #ifndef __JANE_STR_HPP #define __JANE_STR_HPP +#include "error.hpp" +#include "panic.hpp" #include "slice.hpp" -#include "typedef.hpp" +#include "types.hpp" #include "utf8.hpp" +#include +#include +#include +#include #include - -class str_jnt; - -class str_jnt { +#include +#include +namespace jane { +class Str; +template jane::Str to_str(const T &obj) noexcept; +jane::Str to_str(const jane::Str &s) noexcept; + +class Str { public: - std::basic_string __buffer{}; + jane::Int _len{}; + std::basic_string buffer{}; + Str(void) noexcept {} - str_jnt(void) noexcept {} + Str(const char *src, const jane::Int &len) noexcept { + if (!src) { + return; + } + this->_len = len; + this->buffer = std::basic_string(&src[0], &src[this->_len]); + } - str_jnt(const char *_Src) noexcept { - if (!_Src) { + Str(const char *src) noexcept { + if (!src) { return; } - this->__buffer = - std::basic_string(&_Src[0], &_Src[std::strlen(_Src)]); + this->_len = std::strlen(src); + this->buffer = std::basic_string(&src[0], &src[this->_len]); } - str_jnt(const std::initializer_list &_Src) noexcept { - this->__buffer = _Src; + Str(const std::initializer_list &src) noexcept { + this->_len = src.size(); + this->buffer = src; } - str_jnt(const i32_jnt &_Rune) noexcept - : str_jnt(__jane_utf8_rune_to_bytes(_Rune)) {} + Str(const jane::I32 &rune) noexcept : Str(jane::utf8_rune_to_bytes(rune)) {} - str_jnt(const std::basic_string &_Src) noexcept { - this->__buffer = _Src; + Str(const std::basic_string &src) noexcept { + this->_len = src.length(); + this->buffer = src; } - str_jnt(const std::string &_Src) noexcept { - this->__buffer = std::basic_string(_Src.begin(), _Src.end()); + Str(const std::string &src) noexcept { + this->_len = src.length(); + this->buffer = std::basic_string(src.begin(), src.end()); } - str_jnt(const str_jnt &_Src) noexcept { this->__buffer = _Src.__buffer; } + Str(const jane::Str &src) noexcept { + this->_len = src._len; + this->buffer = src.buffer; + } - str_jnt(const slice_jnt &_Src) noexcept { - this->__buffer = std::basic_string(_Src.begin(), _Src.end()); + Str(const jane::Slice &src) noexcept { + this->_len = src.len(); + this->buffer = std::basic_string(src.begin(), src.end()); } - str_jnt(const slice_jnt &_Src) noexcept { - for (const i32_jnt &_rune : _Src) { - const slice_jnt _bytes{__jane_utf8_rune_to_bytes(_rune)}; - for (const u8_jnt _byte : _bytes) { - this->__buffer += _byte; + Str(const jane::Slice &src) noexcept { + for (const jane::I32 &r : src) { + const jane::Slice bytes{jane::utf8_rune_to_bytes(r)}; + this->_len += bytes.len(); + for (const jane::U8 _byte : bytes) { + this->buffer += _byte; } } } - typedef u8_jnt *iterator; - typedef const u8_jnt *const_iterator; + typedef jane::U8 *Iterator; + typedef const jane::U8 *ConstIterator; - inline iterator begin(void) noexcept { - return ((iterator)(&this->__buffer[0])); + inline Iterator begin(void) noexcept { + return reinterpret_cast(&this->buffer[0]); } - inline const_iterator begin(void) const noexcept { - return ((const_iterator)(&this->__buffer[0])); + inline ConstIterator begin(void) const noexcept { + return reinterpret_cast(&this->buffer[0]); } - inline iterator end(void) noexcept { - return ((iterator)(&this->__buffer[this->_len()])); + inline Iterator end(void) noexcept { + return reinterpret_cast(&this->buffer[this->len()]); } - inline const_iterator end(void) const noexcept { - return ((const_iterator)(&this->__buffer[this->_len()])); + inline ConstIterator end(void) const noexcept { + return reinterpret_cast(&this->buffer[this->len()]); } - inline str_jnt ___slice(const int_jnt &_Start, - const int_jnt &_End) const noexcept { - if (_Start < 0 || _End < 0 || _Start < _End || _End > this->_len()) { - std::stringstream _sstream; - __JANE_WRITE_ERROR_SLICING_INDEX_OUT_OF_RANGE(_sstream, _Start, _End); - JANE_ID(panic)(_sstream.str().c_str()); - } else if (_Start == _End) { - return (str_jnt()); + inline jane::Str slice(const jane::Int &start, + const jane::Int &end) const noexcept { + if (start < 0 || end < 0 || start > end || end > this->len()) { + std::stringstream sstream; + __JANE_WRITE_ERROR_SLICING_INDEX_OUT_OF_RANGE(sstream, start, end); + jane::panic(sstream.str().c_str()); + } else if (start == end) { + return jane::Str(); } - const int_jnt _n{_End - _Start}; - return (this->__buffer.substr(_Start, _n)); + const jane::Int n{end - start}; + return this->buffer.substr(start, n); } - inline str_jnt ___slice(const int_jnt &_Start) const noexcept { - return (this->___slice(_Start, this->_len())); + inline jane::Str slice(const jane::Int &start) const noexcept { + return this->slice(start, this->len()); } - inline str_jnt ___slice(void) const noexcept { - return (this->___slice(0, this->_len())); + inline jane::Str slice(void) const noexcept { + return this->slice(0, this->len()); } - inline int_jnt _len(void) const noexcept { return (this->__buffer.length()); } - - inline bool _empty(void) const noexcept { return (this->__buffer.empty()); } - - inline bool _has_prefix(const str_jnt &_Sub) const noexcept { - return this->_len() >= _Sub._len() && - this->__buffer.substr(0, _Sub._len()) == _Sub.__buffer; + inline jane::Int len(void) const noexcept { return this->_len; } + inline jane::Bool empty(void) const noexcept { return this->buffer.empty(); } + inline jane::Bool has_prefix(const jane::Str &sub) const noexcept { + return this->buffer.find(sub.buffer, 0) == 0; } - - inline bool _has_suffix(const str_jnt &_Sub) const noexcept { - return this->_len() >= _Sub._len() && - this->__buffer.substr(this->_len() - _Sub._len()) == _Sub.__buffer; + inline jane::Bool has_suffix(const jane::Str &sub) const noexcept { + return this->len() >= sub.len() && + this->buffer.substr(this->len() - sub.len()) == sub.buffer; } - inline int_jnt _find(const str_jnt &_Sub) const noexcept { - return ((int_jnt)(this->__buffer.find(_Sub.__buffer))); + inline jane::Int find(const jane::Str &sub) const noexcept { + return static_cast(this->buffer.find(sub.buffer)); } - - inline int_jnt _rfind(const str_jnt &_Sub) const noexcept { - return ((int_jnt)(this->__buffer.rfind(_Sub.__buffer))); + inline jane::Int rfind(const jane::Str &sub) const noexcept { + return static_cast(this->buffer.rfind(sub.buffer)); } - str_jnt _trim(const str_jnt &_Bytes) const noexcept { - const_iterator _it{this->begin()}; - const const_iterator _end{this->end()}; - const_iterator _begin{this->begin()}; - for (; _it < _end; ++_it) { - bool exist{false}; - const_iterator _bytes_it{_Bytes.begin()}; - const const_iterator _bytes_end{_Bytes.end()}; - for (; _bytes_it < _bytes_end; ++_bytes_it) { - if ((exist = *_it == *_bytes_it)) { + jane::Str trim(const jane::Str &bytes) const noexcept { + ConstIterator it{this->begin()}; + const ConstIterator begin{this->end()}; + for (; it >= begin; --it) { + jane::Bool exist{false}; + ConstIterator bytes_it{bytes.begin()}; + const ConstIterator bytes_end{bytes.end()}; + for (; bytes_it < bytes_end; ++bytes_it) { + if ((exist = *it == *bytes_it)) { break; } } if (!exist) { - return (this->__buffer.substr(_it - _begin)); + return this->buffer.substr(0, it - begin + 1); } } - return (str_jnt()); - } - - str_jnt _rtrim(const str_jnt &_Bytes) const noexcept { - const_iterator _it{this->end() - 1}; - const const_iterator _begin{this->begin()}; - for (; _it >= _begin; --_it) { - bool exist{false}; - const_iterator _bytes_it{_Bytes.begin()}; - const const_iterator _bytes_end{_Bytes.end()}; - for (; _bytes_it < _bytes_end; ++_bytes_it) { - if ((exist = *_it == *_bytes_it)) { + return jane::Str(); + } + + jane::Str rtrim(const jane::Str &bytes) const noexcept { + ConstIterator it{this->end() - 1}; + const ConstIterator begin{this->begin()}; + for (; it >= begin; --it) { + jane::Bool exist{false}; + ConstIterator bytes_it{bytes.begin()}; + const ConstIterator bytes_end{bytes.end()}; + for (; bytes_it < bytes_end; ++bytes_it) { + if ((exist = *it == *bytes_it)) { break; } } - if (!exist) - return (this->__buffer.substr(0, _it - _begin + 1)); + if (!exist) { + return this->buffer.substr(0, it - begin + 1); + } } - return (str_jnt()); + return jane::Str(); } - slice_jnt _split(const str_jnt &_Sub, - const i64_jnt &_N) const noexcept { - slice_jnt _parts; - if (_N == 0) { - return (_parts); + jane::Slice split(const jane::Str &sub, + const jane::I64 &n) const noexcept { + jane::Slice parts; + if (n == 0) { + return parts; } - const const_iterator _begin{this->begin()}; - std::basic_string _s{this->__buffer}; - uint_jnt _pos{std::string::npos}; - if (_N < 0) { - while ((_pos = _s.find(_Sub.__buffer)) != std::string::npos) { - _parts.__push(_s.substr(0, _pos)); - _s = _s.substr(_pos + _Sub._len()); + const ConstIterator begin{this->begin()}; + std::basic_string s{this->buffer}; + constexpr jane::Uint npos{static_cast(std::string::npos)}; + jane::Uint pos{npos}; + if (n < 0) { + while ((pos = s.find(sub.buffer)) != npos) { + parts.push(s.substr(0, pos)); + s = s.substr(pos + sub.len()); } - if (!_s.empty()) { - _parts.__push(str_jnt(_s)); + if (!s.empty()) { + parts.push(jane::Str(s)); } } else { - uint_jnt _n{0}; - while ((_pos = _s.find(_Sub.__buffer)) != std::string::npos) { - if (++_n >= _N) { - _parts.__push(str_jnt(_s)); + jane::Uint _n{0}; + while ((pos = s.find(sub.buffer)) != npos) { + if (++_n >= n) { + parts.push(jane::Str(s)); break; } - _parts.__push(_s.substr(0, _pos)); - _s = _s.substr(_pos + _Sub._len()); + parts.push(s.substr(0, pos)); + s = s.substr(pos + sub.len()); } - if (!_parts._empty() && _n < _N) { - _parts.__push(str_jnt(_s)); - } else if (_parts._empty()) { - _parts.__push(str_jnt(_s)); + if (!parts.empty() && _n < n) { + parts.push(jane::Str(s)); + } else if (parts.empty()) { + parts.push(jane::Str(s)); } } - return (_parts); + return parts; } - str_jnt _replace(const str_jnt &_Sub, const str_jnt &_New, - const i64_jnt &_N) const noexcept { - if (_N == 0) { - return (*this); + jane::Str replace(const jane::Str &sub, const jane::Str &_new, + const jane::I64 &n) const noexcept { + if (n == 0) { + return *this; } - std::basic_string _s(this->__buffer); - uint_jnt start_pos{0}; - if (_N < 0) { - while ((start_pos = _s.find(_Sub.__buffer, start_pos)) != - std::string::npos) { - _s.replace(start_pos, _Sub._len(), _New.__buffer); - start_pos += _New._len(); + + std::basic_string s(this->buffer); + constexpr jane::Uint npos{static_cast(std::string::npos)}; + jane::Uint start_pos{0}; + if (n < 0) { + while ((start_pos = s.find(sub.buffer, start_pos)) != npos) { + s.replace(start_pos, sub.len(), _new.buffer); + start_pos += _new.len(); } } else { - uint_jnt _n{0}; - while ((start_pos = _s.find(_Sub.__buffer, start_pos)) != - std::string::npos) { - _s.replace(start_pos, _Sub._len(), _New.__buffer); - if (++_n >= _N) { + jane::Uint _n{0}; + while ((start_pos = s.find(sub.buffer, start_pos)) != npos) { + s.replace(start_pos, sub.len(), _new.buffer); + start_pos += _new.len(); + if (++_n >= n) { break; } } } - return (str_jnt(_s)); + return jane::Str(s); } inline operator const char *(void) const noexcept { - return ((char *)(this->__buffer.c_str())); + return reinterpret_cast(this->buffer.c_str()); } - inline operator const std::basic_string(void) const noexcept { - return (this->__buffer); + inline operator const std::basic_string(void) const noexcept { + return this->buffer; } inline operator const std::basic_string(void) const noexcept { - return ( - std::basic_string(this->__buffer.begin(), this->__buffer.end())); + return std::basic_string(this->begin(), this->end()); } - operator slice_jnt(void) const noexcept { - slice_jnt _slice(this->_len()); - for (int_jnt _index{0}; _index < this->_len(); ++_index) { - _slice[_index] = this->operator[](_index); + operator jane::Slice(void) const noexcept { + jane::Slice slice{jane::Slice::alloc(this->len())}; + for (jane::Int index{0}; index < this->len(); ++index) { + slice[index] = this->operator[](index); } - return (_slice); - } - - operator slice_jnt(void) const noexcept { - slice_jnt _runes{}; - const char *_str{this->operator const char *()}; - for (int_jnt _index{0}; _index < this->_len();) { - i32_jnt _rune; - int_jnt _n; - std::tie(_rune, _n) = - __jane_utf8_decode_rune_str(_str + _index, this->_len() - _index); - _index += _n; - _runes.__push(_rune); + return slice; + } + + operator jane::Slice(void) const noexcept { + jane::Slice runes{}; + const char *str{this->operator const char *()}; + for (jane::Int index{0}; index < this->len();) { + jane::I32 rune; + jane::Int n; + std::tie(rune, n) = + jane::utf8_decode_rune_st(str + index, this->len() - index); + index += n; + runes.push(rune); } - return (_runes); + return runes; } - u8_jnt &operator[](const int_jnt &_Index) { - if (this->_empty() || _Index < 0 || this->_len() <= _Index) { - std::stringstream _sstream; - __JANE_WRITE_ERROR_INDEX_OUT_OF_RANGE(_sstream, _Index); - JANE_ID(panic)(_sstream.str().c_str()); + jane::U8 &operator[](const jane::Int &index) { + if (this->empty() || index < 0 || this->len() <= index) { + std::stringstream sstream; + __JANE_WRITE_ERROR_INDEX_OUT_OF_RANGE(sstream, index); + jane::panic(sstream.str().c_str()); } - return (this->__buffer[_Index]); + return this->buffer[index]; } - inline u8_jnt operator[](const int_jnt &_Index) const { - return ((*this).__buffer[_Index]); + inline jane::U8 operator[](const jane::Int &index) const { + return (*this).buffer[index]; } - inline void operator+=(const str_jnt &_Str) noexcept { - this->__buffer += _Str.__buffer; + inline void operator+=(const jane::Str &str) noexcept { + this->_len += str.len(); + this->buffer += str.buffer; } - inline str_jnt operator+(const str_jnt &_Str) const noexcept { - return (str_jnt(this->__buffer + _Str.__buffer)); + inline jane::Str operator+(const jane::Str &str) const noexcept { + return jane::Str(this->buffer + str.buffer); } - inline bool operator==(const str_jnt &_Str) const noexcept { - return (this->__buffer == _Str.__buffer); + inline jane::Bool operator==(const jane::Str &str) const noexcept { + return this->buffer == str.buffer; } - inline bool operator!=(const str_jnt &_Str) const noexcept { - return (!this->operator==(_Str)); + inline jane::Bool operator!=(const jane::Str &str) const noexcept { + return !this->operator==(str); } - friend std::ostream &operator<<(std::ostream &_Stream, - const str_jnt &_Src) noexcept { - for (const u8_jnt &_byte : _Src) { - _Stream << static_cast(_byte); + friend std::ostream &operator<<(std::ostream &stream, + const jane::Str &src) noexcept { + for (const jane::U8 &b : src) { + stream << static_cast(b); } - return (_Stream); + return stream; } }; -template str_jnt __jane_to_str(const _Obj_t &_Obj) noexcept; -str_jnt __jane_to_str(const str_jnt &_Obj) noexcept; - -template str_jnt __jane_to_str(const _Obj_t &_Obj) noexcept { - std::stringstream _stream; - _stream << _Obj; - return (str_jnt(_stream.str())); +template jane::Str to_str(const T &obj) noexcept { + std::stringstream stream; + stream << obj; + return jane::Str(stream.str()); } -inline str_jnt __jane_to_str(const str_jnt &_Obj) noexcept { return (_Obj); } +inline jane::Str to_str(const jane::Str &s) noexcept { return s; } + +} // namespace jane -#endif // !__JANE_STR_HPP +#endif // __JANE_STR_HPP \ No newline at end of file diff --git a/api/terminate.hpp b/api/terminate.hpp new file mode 100644 index 0000000..5ac72ad --- /dev/null +++ b/api/terminate.hpp @@ -0,0 +1,70 @@ +// Copyright (c) 2024 arfy slowy - DeRuneLabs +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +// SOFTWARE. + +#ifndef __JANE_TERMINATE_HPP +#define __JANE_TERMINATE_HPP + +#include "builtin.hpp" +#include "error.hpp" +#include "panic.hpp" +#include "str.hpp" +#include "trait.hpp" +#include "types.hpp" +#include +#include + +namespace jane { +struct Error { + virtual jane::Str error(void) { return jane::Str(); } + virtual ~Error(void) noexcept {} + + jane::Bool operator==(const Error &) { return false; } + jane::Bool operator!=(const Error &src) { return !this->operator==(src); } + + friend std::ostream &operator<<(std::ostream &stream, Error error) noexcept { + return stream << error.error(); + } +}; + +void terminate_handler(void) noexcept; + +jane::Trait exception_to_error(const jane::Exception &exception); + +void terminate_handler(void) noexcept { + try { + std::rethrow_exception(std::current_exception()); + } catch (const jane::Exception &e) { + jane::println(std::string("panic: ") + std::string(e.what())); + std::exit(jane::EXIT_PANIC); + } +} + +jane::Trait exception_to_error(const jane::Exception &exception) { + struct PanicError : public Error { + jane::Str message; + jane::Str error(void) { return this->message; } + }; + struct PanicError error; + error.message = jane::to_str(exception.what()); + return jane::Trait(error); +} +} // namespace jane + +#endif // __JANE_TERMINATE_HPP \ No newline at end of file diff --git a/api/trait.hpp b/api/trait.hpp index f976921..21cc394 100644 --- a/api/trait.hpp +++ b/api/trait.hpp @@ -1,4 +1,4 @@ -// Copyright (c) 2024 - DeRuneLabs +// Copyright (c) 2024 arfy slowy - DeRuneLabs // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal @@ -21,91 +21,106 @@ #ifndef __JANE_TRAIT_HPP #define __JANE_TRAIT_HPP +#include "error.hpp" +#include "panic.hpp" #include "ref.hpp" -#include "typedef.hpp" +#include "types.hpp" +#include +#include +namespace jane { +template struct Trait; -template struct trait_jnt; - -template struct trait_jnt { +template struct Trait { public: - ref_jnt __data{}; - const char *__type_id{nil}; - - trait_jnt(void) noexcept {} - trait_jnt(std::nullptr_t) noexcept {} - - template trait_jnt(const TT &_Data) noexcept { - TT *_alloc{new (std::nothrow) TT}; - if (!_alloc) { - JANE_ID(panic)(__JANE_ERROR_MEMORY_ALLOCATION_FAILED); + mutable jane::Ref data{}; + const char *type_id{nullptr}; + Trait(void) noexcept {} + Trait(std::nullptr_t) noexcept {} + + template Trait(const T &data) noexcept { + T *alloc{new (std::nothrow) T}; + if (!alloc) { + jane::panic(jane::ERROR_MEMORY_ALLOCATION_FAILED); } - *_alloc = _Data; - this->__data = ref_jnt::make((T *)(_alloc)); - this->__type_id = typeid(_Data).name(); + *alloc = data; + this->data = jane::Ref::make(reinterpret_cast(alloc)); + this->type_id = typeid(T).name(); } - template trait_jnt(const ref_jnt &_Ref) noexcept { - this->__data = ref_jnt::make(((T *)(_Ref.__alloc)), _Ref.__ref); - this->__data.__add_ref(); - this->__type_id = typeid(_Ref).name(); - } + Trait(const jane::Trait &src) noexcept { this->operator=(src); } - trait_jnt(const trait_jnt &_Src) noexcept { this->operator=(_Src); } + void dealloc(void) noexcept { this->data.drop(); } - void __dealloc(void) noexcept { this->__data._drop(); } + inline void must_ok(void) const noexcept { + if (this->operator==(nullptr)) { + jane::panic(jane::ERROR_INVALID_MEMORY); + } + } - inline void __must_ok(void) noexcept { - if (this->operator==(nil)) { - JANE_ID(panic)(__JANE_ERROR_INVALID_MEMORY); + template inline jane::Bool type_is(void) const noexcept { + if (this->operator==(nullptr)) { + return false; } + return std::strcmp(this->type_id, typeid(T).name()) == 0; + } + + inline Mask &get(void) noexcept { + this->must_ok(); + return this->data; } - inline T &_get(void) noexcept { - return this->__data; + inline Mask &get(void) const noexcept { + this->must_ok(); + return this->data; } - ~trait_jnt(void) noexcept {} + ~Trait(void) noexcept {} - template operator TT(void) noexcept { - this->__must_ok(); - if (std::strcmp(this->__type_id, typeid(TT).name()) != 0) { - JANE_ID(panic)(__JANE_ERROR_INCOMPATIBLE_TYPE); + template operator T(void) noexcept { + this->must_ok(); + if (std::strcmp(this->type_id, typeid(T).name()) != 0) { + jane::panic(jane::ERROR_INCOMPATIBLE_TYPE); } - this->__data.__add_ref(); - return (ref_jnt((TT *)(this->__data.__alloc), this->__data.__ref)); + this->data.add_ref(); + return jane::Ref::make(reinterpret_cast(this->data.alloc), + this->data.ref); } - inline void operator==(const std::nullptr_t) noexcept { this->__dealloc(); } + inline void operator=(const std::nullptr_t) noexcept { this->dealloc(); } - inline void operator=(const trait_jnt &_Src) noexcept { - this->__dealloc(); - if (_Src == nil) { + inline void operator=(const jane::Trait &src) noexcept { + if (this->data.alloc == src.data.alloc) { + return; + } + this->dealloc(); + if (src == nullptr) { return; } - this->__data = _Src.__data; - this->__type_id = _Src.__type_id; + this->data = src.data; + this->type_id = src.type_id; } - inline bool operator==(const trait_jnt &_Src) const noexcept { - return (this->__data.__alloc == this->__data.__alloc); + inline jane::Bool operator==(const jane::Trait &src) const noexcept { + return this->data.alloc == this->data.alloc; } - inline bool operator!=(const trait_jnt &_Src) const noexcept { - return (!this->operator==(_Src)); + inline jane::Bool operator!=(const jane::Trait &src) const noexcept { + return !this->operator==(src); } - inline bool operator==(std::nullptr_t) const noexcept { - return (this->__data.__alloc == nil); + inline jane::Bool operator==(const std::nullptr_t) const noexcept { + return this->data.alloc == nullptr; } - inline bool operator!=(std::nullptr_t) const noexcept { - return (!this->operator==(nil)); + inline jane::Bool operator!=(std::nullptr_t) const noexcept { + return !this->operator==(nullptr); } - friend inline std::ostream &operator<<(std::ostream &_Stream, - const trait_jnt &_Src) noexcept { - return (_Stream << _Src.__data.__alloc); + friend inline std::ostream & + operator<<(std::ostream &stream, const jane::Trait &src) noexcept { + return stream << src.data.alloc; } }; +} // namespace jane -#endif // !__JANE_TRAIT_HPP +#endif // __JANE_TRAIT_HPP \ No newline at end of file diff --git a/api/typedef.hpp b/api/typedef.hpp deleted file mode 100644 index 534b8d5..0000000 --- a/api/typedef.hpp +++ /dev/null @@ -1,85 +0,0 @@ -// Copyright (c) 2024 - DeRuneLabs -// -// Permission is hereby granted, free of charge, to any person obtaining a copy -// of this software and associated documentation files (the "Software"), to deal -// in the Software without restriction, including without limitation the rights -// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -// copies of the Software, and to permit persons to whom the Software is -// furnished to do so, subject to the following conditions: -// -// The above copyright notice and this permission notice shall be included in -// all copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -// SOFTWARE. - -#ifndef __JANE_TYPEDEF_HPP -#define __JANE_TYPEDEF_HPP - -#include -#include -#include -#include -#include -#include -#include - -#if defined(_32BIT) -typedef unsigned long int(uint_jnt); -typedef signed long int(int_jnt); -typedef unsigned long int(uintptr_jnt); -#else -typedef unsigned long long int(uint_jnt); -typedef signed long long int(int_jnt); -typedef unsigned long long int(uintptr_jnt); -#endif // defined(_32BIT) - -typedef signed char(i8_jnt); -typedef signed short int(i16_jnt); -typedef signed long int(i32_jnt); -typedef signed long long int(i64_jnt); -typedef unsigned char(u8_jnt); -typedef unsigned short int(u16_jnt); -typedef unsigned long int(u32_jnt); -typedef unsigned long long int(u64_jnt); -typedef float(f32_jnt); -typedef double(f64_jnt); -typedef bool(bool_jnt); - -constexpr const char *__JANE_ERROR_INVALID_MEMORY{ - "invalid memory address or nil pointer deference"}; -constexpr const char *__JANE_ERROR_INCOMPATIBLE_TYPE{"incompatible type"}; -constexpr const char *__JANE_ERROR_MEMORY_ALLOCATION_FAILED{ - "memory allocation failed"}; -constexpr const char *__JANE_ERROR_INDEX_OUT_OF_RANGE{"index out of range"}; -constexpr const char *__JANE_ERROR_DIVIDE_BY_ZERO{"divide by zero"}; -constexpr signed int __JANE_EXIT_PANIC{2}; -constexpr std::nullptr_t nil{nullptr}; - -#define __JANE_WRITE_ERROR_SLICING_INDEX_OUT_OF_RANGE(_STREAM, _START, _LEN) \ - (_STREAM << __JANE_ERROR_INDEX_OUT_OF_RANGE << '[' << _START << ':' << _LEN \ - << ']') - -#define __JANE_WRITE_ERROR_INDEX_OUT_OF_RANGE(_STREAM, _INDEX) \ - (_STREAM << __JANE_ERROR_INDEX_OUT_OF_RANGE << '[' << _INDEX << ']') - -#define __JANE_CCONCAT(_A, _B) _A##_B -#define __JANE_CONCAT(_A, _B) __JANE_CCONCAT(_A, _B) -#define __JANE_IDENTIFIER_PREFIX _ -#define JANE_ID(_IDENTIFIER) \ - __JANE_CONCAT(__JANE_IDENTIFIER_PREFIX, _IDENTIFIER) -#define __JANE_CO(_EXPR) \ - (std::thread{[&](void) mutable -> void { _EXPR; }}.detach()) - -template void JANE_ID(panic)(const _Obj_t &_Expr); -inline std::ostream &operator<<(std::ostream &_Stream, - const signed char _I8) noexcept; -inline std::ostream &operator<<(std::ostream &_Stream, - const unsigned char _U8) noexcept; - -#endif // !__JANE_TYPEDEF_HPP diff --git a/api/types.hpp b/api/types.hpp new file mode 100644 index 0000000..c41c915 --- /dev/null +++ b/api/types.hpp @@ -0,0 +1,61 @@ +// Copyright (c) 2024 arfy slowy - DeRuneLabs +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +// SOFTWARE. + +#ifndef __JANE_TYPES_HPP +#define __JANE_TYPES_HPP + +#include "platform.hpp" +#include + +namespace jane { +#ifdef ARCH_32BIT +typedef unsigned long long int Uint; +typedef signed long int Int; +typedef unsigned long int Uintptr; +#else +typedef unsigned long long int Uint; +typedef signed long long int Int; +typedef unsigned long long int Uintptr; +#endif + +typedef signed char I8; +typedef signed short int I16; +typedef signed long int I32; +typedef signed long long int I64; +typedef unsigned char U8; +typedef unsigned short int U16; +typedef unsigned long int U32; +typedef unsigned long long int U64; +typedef float F32; +typedef double F64; +typedef bool Bool; + +constexpr decltype(nullptr) nil{nullptr}; + +constexpr jane::F32 MAX_F32{0x1p127 * (1 + (1 - 0x1p-23))}; +constexpr jane::F32 MIN_F32{-0x1p127 * (1 + (1 - 0x1p-23))}; +constexpr jane::F64 MAX_F64{0x1p1023 * (1 + (1 - 0x1p-52))}; +constexpr jane::F64 MIN_F64{-0x1p1023 * (1 + (1 - 0x1p-52))}; +constexpr jane::I64 MAX_I64{9223372036854775807LL}; +constexpr jane::I64 MIN_I64{-9223372036854775807 - 1}; +constexpr jane::U64 MAX_U64{18446744073709551615LLU}; +} // namespace jane + +#endif // __JANE_TYPES_HPP \ No newline at end of file diff --git a/api/utf16.hpp b/api/utf16.hpp index 30e718b..b4024e8 100644 --- a/api/utf16.hpp +++ b/api/utf16.hpp @@ -1,4 +1,4 @@ -// Copyright (c) 2024 - DeRuneLabs +// Copyright (c) 2024 arfy slowy - DeRuneLabs // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal @@ -23,120 +23,135 @@ #include "slice.hpp" #include "str.hpp" -#include "typedef.hpp" -constexpr signed int __JANE_UTF16_REPLACEMENT_CHAR{65533}; -constexpr signed int __JANE_UTF16_SURR1{0xd800}; -constexpr signed int __JANE_UTF16_SURR2{0xdc00}; -constexpr signed int __JANE_UTF16_SURR3{0xe000}; -constexpr signed int __JANE_UTF16_SURR_SELF{0x10000}; -constexpr signed int __JANE_UTF16_MAX_RUNE{1114111}; +#include "types.hpp" +#include +#include +namespace jane { +constexpr signed int UTF16_REPLACEMENT_CHAR{65533}; +constexpr signed int UTF16_SURR1{0xd800}; +constexpr signed int UTF16_SURR2{0xdc00}; +constexpr signed int UTF16_SURR3{0xe000}; +constexpr signed int UTF16_SURR_SELF{0x10000}; +constexpr signed int UTF16_MAX_RUNE{1114111}; -inline i32_jnt __jane_utf16_decode_rune(const i32_jnt _R1, - const i32_jnt _R2) noexcept; -slice_jnt __jane_utf16_decode(const slice_jnt _S) noexcept; -str_jnt __jane_utf16_to_utf8_str(const wchar_t *_Wstr, - const std::size_t _Len) noexcept; -std::tuple __jane_utf16_encode_rune(i32_jnt _R) noexcept; -slice_jnt -__jane_utf16_enode(const slice_jnt &_Runes) noexcept; -slice_jnt __jane_utf16_append_rune(slice_jnt &_A, - const i32_jnt &_R) noexcept; +inline jane::I32 utf16_decode_rune(const jane::I32 r1, + const jane::I32 r2) noexcept; +jane::Slice utf16_decode(const jane::Slice s) noexcept; +jane::Str utf16_to_utf8_str(const wchar_t wstr, const std::size_t len) noexcept; +std::tuple utf16_encode_rune(jane::I32 r) noexcept; +jane::Slice +utf16_encode(const jane::Slice &runes) noexcept; +jane::Slice utf16_from_str(const jane::Str &s) noexcept; -inline i32_jnt __jane_utf16_decode_rune(const i32_jnt _R1, - const i32_jnt _R2) noexcept { - if (__JANE_UTF16_SURR1 <= _R1 && _R1 < __JANE_UTF16_SURR2 && - __JANE_UTF16_SURR2 <= _R2 && _R2 < __JANE_UTF16_SURR3) { - return ((_R1 - __JANE_UTF16_SURR1) << 10 | - (_R2 - __JANE_UTF16_SURR2) + __JANE_UTF16_SURR_SELF); +inline jane::I32 utf16_decode_rune(const jane::I32 r1, + const jane::I32 r2) noexcept { + if (jane::UTF16_SURR1 <= r1 && r1 < jane::UTF16_SURR2 && + jane::UTF16_SURR2 <= r2 && r2 < jane::UTF16_SURR3) { + return (r1 - jane::UTF16_SURR1) << 10 | + (r2 - jane::UTF16_SURR2) + jane::UTF16_SURR_SELF; } - return (__JANE_UTF16_REPLACEMENT_CHAR); + return jane::UTF16_REPLACEMENT_CHAR; } -slice_jnt __jane_utf16_decode(const slice_jnt &_S) noexcept { - slice_jnt _a(_S._len()); - int_jnt _n{0}; - for (int_jnt _i{0}; _i < _S._len(); ++_i) { - u16_jnt _r{_S[_i]}; - if (_r < __JANE_UTF16_SURR1 || __JANE_UTF16_SURR3 <= _r) { - _a[_n] = static_cast(_r); - } else if (__JANE_UTF16_SURR1 <= _r && _r < __JANE_UTF16_SURR2 && - _i + 1 < _S._len() && __JANE_UTF16_SURR2 <= _S[_i + 1] && - _S[_i + 1] < __JANE_UTF16_SURR3) { - _a[_n] = __jane_utf16_decode_rune(static_cast(_r), - static_cast(_S[_i + 1])); +jane::Slice utf16_decode(const jane::Slice &s) noexcept { + jane::Slice a{jane::Slice::alloc(s.len())}; + jane::Int n{0}; + for (jane::Int i{0}; i < s.len(); ++i) { + jane::U16 r{s[i]}; + if (r < jane::UTF16_SURR1 || jane::UTF16_SURR3 <= r) { + a[n] = static_cast(r); + } else if (jane::UTF16_SURR1 <= r && r < jane::UTF16_SURR2 && + i + 1 < s.len() && jane::UTF16_SURR2 <= s[i + 1] && + s[i + 1] < jane::UTF16_SURR3) { + a[n] = jane::utf16_decode_rune(static_cast(r), + static_cast(s[i + 1])); + ++i; } else { - _a[_n] = __JANE_UTF16_REPLACEMENT_CHAR; + a[n] = jane::UTF16_REPLACEMENT_CHAR; + ++n; } - ++_n; } - return (_a.___slice(0, _n)); + return a.slice(0, n); } -str_jnt __jane_utf16_to_utf8_str(const wchar_t *_Wstr, - const std::size_t _Len) noexcept { - slice_jnt _code_page(_Len); - for (int_jnt _i{0}; _i < _Len; ++_i) { - _code_page[_i] = static_cast(_Wstr[_i]); +jane::Str utf16_to_utf8_str(const wchar_t *wstr, + const std::size_t len) noexcept { + jane::Slice code_page{jane::Slice::alloc(len)}; + for (jane::Int i{0}; i < len; ++i) { + code_page[i] = static_cast(wstr[i]); } - return (static_cast(__jane_utf16_decode(_code_page))); + return static_cast(jane::utf16_decode(code_page)); } -std::tuple __jane_utf16_encode_rune(i32_jnt _R) noexcept { - if (_R < __JANE_UTF16_SURR_SELF || _R > __JANE_UTF16_MAX_RUNE) { - return (std::make_tuple(__JANE_UTF16_REPLACEMENT_CHAR, - __JANE_UTF16_REPLACEMENT_CHAR)); +std::tuple utf16_encode_rune(jane::I32 r) noexcept { + if (r < jane::UTF16_SURR_SELF || r > jane::UTF16_MAX_RUNE) { + return std::make_tuple(jane::UTF16_REPLACEMENT_CHAR, + jane::UTF16_REPLACEMENT_CHAR); } - _R -= __JANE_UTF16_SURR_SELF; - return (std::make_tuple(__JANE_UTF16_SURR1 + (_R >> 10) & 0x3ff, - __JANE_UTF16_SURR2 + _R & 0x3ff)); + r -= jane::UTF16_SURR_SELF; + return std::make_tuple( + jane::UTF16_SURR1 + (r >> 10) & 0x3ff, jane::UTF16_SURR2 + r & 0x3ff); } -slice_jnt -__jane_utf16_encode(const slice_jnt &_Runes) noexcept { - int_jnt _n{_Runes._len()}; - for (const i32_jnt _v : _Runes) { - if (_v >= __JANE_UTF16_SURR_SELF) { - ++_n; +jane::Slice +utf16_encode(const jane::Slice &runes) noexcept { + jane::Int n{runes.len()}; + for (const jane::I32 v : runes) { + if (v >= jane::UTF16_SURR_SELF) { + ++n; } } - slice_jnt _a{slice_jnt(_n)}; - _n = 0; - for (const i32_jnt _v : _Runes) { - if (0 <= _v && _v < __JANE_UTF16_SURR1 || - __JANE_UTF16_SURR3 <= _v && _v < __JANE_UTF16_SURR_SELF) { - _a[_n] = static_cast(_v); - ++_n; - } else if (__JANE_UTF16_SURR_SELF <= _v && _v <= __JANE_UTF16_MAX_RUNE) { - i32_jnt _r1; - i32_jnt _r2; - std::tie(_r1, _r2) = __jane_utf16_encode_rune(_v); - _a[_n] = static_cast(_r1); - _a[_n + 1] = static_cast(_r2); - _n += 2; + jane::Slice a{jane::Slice::alloc(n)}; + n = 0; + for (const jane::I32 v : runes) { + if (0 <= v && v < jane::UTF16_SURR1 || + jane::UTF16_SURR3 <= v && v < jane::UTF16_SURR_SELF) { + a[n] = static_cast(v); + } else if (jane::UTF16_SURR_SELF <= v && v <= jane::UTF16_MAX_RUNE) { + jane::I32 r1; + jane::I32 r2; + std::tie(r1, r2) = jane::utf16_encode_rune(v); + a[n] = static_cast(r1); + a[n + 1] = static_cast(r2); + n += 2; } else { - _a[_n] = static_cast(__JANE_UTF16_REPLACEMENT_CHAR); - ++_n; + a[n] = static_cast(jane::UTF16_REPLACEMENT_CHAR); + ++n; } } - return (_a.___slice(0, _n)); + return a.slice(0, n); } -slice_jnt __jane_utf16_append_rune(slice_jnt &_A, - const i32_jnt &_R) noexcept { - if (0 <= _R && _R < __JANE_UTF16_SURR1 | __JANE_UTF16_SURR3 <= _R && - _R < __JANE_UTF16_SURR_SELF) { - _A.__push(static_cast(_R)); - return (_A); - } else if (__JANE_UTF16_SURR_SELF <= _R && _R <= __JANE_UTF16_MAX_RUNE) { - i32_jnt _r1; - i32_jnt _r2; - std::tie(_r1, _r2) = __jane_utf16_encode_rune(_R); - _A.__push(static_cast(_r1)); - _A.__push(static_cast(_r2)); - return (_A); +jane::Slice utf16_append_rune(jane::Slice &a, + const jane::I32 &r) noexcept { + if (0 <= r & r < jane::UTF16_SURR1 | jane::UTF16_SURR3 <= r && + r < jane::UTF16_SURR_SELF) { + a.push(static_cast(r)); + } else if (jane::UTF16_SURR_SELF <= r && r <= jane::UTF16_MAX_RUNE) { + jane::I32 r1; + jane::I32 r2; + std::tie(r1, r2) = jane::utf16_encode_rune(r); + a.push(static_cast(r1)); + a.push(static_cast(r2)); + return a; } - _A.__push(__JANE_UTF16_REPLACEMENT_CHAR); - return (_A); + a.push(jane::UTF16_REPLACEMENT_CHAR); + return a; } -#endif // !__JANE_UTF16_HPP +jane::Slice utf16_from_str(const jane::Str &s) noexcept { + constexpr char NULL_TERMINATOR = '\x00'; + jane::Slice buff{nullptr}; + jane::Slice runes{static_cast>(s)}; + for (const jane::I32 &r : runes) { + if (r == NULL_TERMINATOR) { + break; + } + buff = jane::utf16_append_rune(buff, r); + } + return jane::utf16_append_rune(buff, NULL_TERMINATOR); +} + +} // namespace jane + +#endif // __JANE_UTF16_HPP \ No newline at end of file diff --git a/api/utf8.hpp b/api/utf8.hpp index 37391cf..d8ea408 100644 --- a/api/utf8.hpp +++ b/api/utf8.hpp @@ -1,4 +1,4 @@ -// Copyright (c) 2024 - DeRuneLabs +// Copyright (c) 2024 arfy slowy - DeRuneLabs // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal @@ -22,203 +22,202 @@ #define __JANE_UTF8_HPP #include "slice.hpp" -#include "typedef.hpp" -constexpr signed int __JANE_UTF8_RUNE_ERROR{65533}; -constexpr signed int __JANE_UTF8_MASKX{63}; -constexpr signed int __JANE_UTF8_MASK2{31}; -constexpr signed int __JANE_UTF8_MASK3{15}; -constexpr signed int __JANE_UTF8_MASK4{7}; -constexpr signed int __JANE_UTF8_LOCB{128}; -constexpr signed int __JANE_UTF8_HICB{191}; -constexpr signed int __JANE_UTF8_XX{241}; -constexpr signed int __JANE_UTF8_AS{240}; -constexpr signed int __JANE_UTF8_S1{2}; -constexpr signed int __JANE_UTF8_S2{19}; -constexpr signed int __JANE_UTF8_S3{3}; -constexpr signed int __JANE_UTF8_S4{35}; -constexpr signed int __JANE_UTF8_S5{52}; -constexpr signed int __JANE_UTF8_S6{4}; -constexpr signed int __JANE_UTF8_S7{68}; -constexpr signed int __JANE_UTF8_RUNE1_MAX{127}; -constexpr signed int __JANE_UTF8_RUNE2_MAX{2047}; -constexpr signed int __JANE_UTF8_RUNE3_MAX{65535}; -constexpr signed int __JANE_UTF8_TX{128}; -constexpr signed int __JANE_UTF8_T2{192}; -constexpr signed int __JANE_UTF8_T3{224}; -constexpr signed int __JANE_UTF8_T4{240}; -constexpr signed int __JANE_UTF8_MAX_RUNE{1114111}; -constexpr signed int __JANE_UTF8_SURROGATE_MIN{55296}; -constexpr signed int __JANE_UTF8_SURROGATE_MAX{57343}; - -struct __jane_utf8_accept_range; -std::tuple -__jane_utf8_decode_rune_str(const char *_S, const int_jnt &_Len) noexcept; -slice_jnt __jane_utf8_rune_to_bytes(const i32_jnt &_R) noexcept; - -constexpr u8_jnt __jane_utf8_first[256] = { - __JANE_UTF8_AS, __JANE_UTF8_AS, __JANE_UTF8_AS, __JANE_UTF8_AS, - __JANE_UTF8_AS, __JANE_UTF8_AS, __JANE_UTF8_AS, __JANE_UTF8_AS, - __JANE_UTF8_AS, __JANE_UTF8_AS, __JANE_UTF8_AS, __JANE_UTF8_AS, - __JANE_UTF8_AS, __JANE_UTF8_AS, __JANE_UTF8_AS, __JANE_UTF8_AS, - __JANE_UTF8_AS, __JANE_UTF8_AS, __JANE_UTF8_AS, __JANE_UTF8_AS, - __JANE_UTF8_AS, __JANE_UTF8_AS, __JANE_UTF8_AS, __JANE_UTF8_AS, - __JANE_UTF8_AS, __JANE_UTF8_AS, __JANE_UTF8_AS, __JANE_UTF8_AS, - __JANE_UTF8_AS, __JANE_UTF8_AS, __JANE_UTF8_AS, __JANE_UTF8_AS, - __JANE_UTF8_AS, __JANE_UTF8_AS, __JANE_UTF8_AS, __JANE_UTF8_AS, - __JANE_UTF8_AS, __JANE_UTF8_AS, __JANE_UTF8_AS, __JANE_UTF8_AS, - __JANE_UTF8_AS, __JANE_UTF8_AS, __JANE_UTF8_AS, __JANE_UTF8_AS, - __JANE_UTF8_AS, __JANE_UTF8_AS, __JANE_UTF8_AS, __JANE_UTF8_AS, - __JANE_UTF8_AS, __JANE_UTF8_AS, __JANE_UTF8_AS, __JANE_UTF8_AS, - __JANE_UTF8_AS, __JANE_UTF8_AS, __JANE_UTF8_AS, __JANE_UTF8_AS, - __JANE_UTF8_AS, __JANE_UTF8_AS, __JANE_UTF8_AS, __JANE_UTF8_AS, - __JANE_UTF8_AS, __JANE_UTF8_AS, __JANE_UTF8_AS, __JANE_UTF8_AS, - __JANE_UTF8_AS, __JANE_UTF8_AS, __JANE_UTF8_AS, __JANE_UTF8_AS, - __JANE_UTF8_AS, __JANE_UTF8_AS, __JANE_UTF8_AS, __JANE_UTF8_AS, - __JANE_UTF8_AS, __JANE_UTF8_AS, __JANE_UTF8_AS, __JANE_UTF8_AS, - __JANE_UTF8_AS, __JANE_UTF8_AS, __JANE_UTF8_AS, __JANE_UTF8_AS, - __JANE_UTF8_AS, __JANE_UTF8_AS, __JANE_UTF8_AS, __JANE_UTF8_AS, - __JANE_UTF8_AS, __JANE_UTF8_AS, __JANE_UTF8_AS, __JANE_UTF8_AS, - __JANE_UTF8_AS, __JANE_UTF8_AS, __JANE_UTF8_AS, __JANE_UTF8_AS, - __JANE_UTF8_AS, __JANE_UTF8_AS, __JANE_UTF8_AS, __JANE_UTF8_AS, - __JANE_UTF8_AS, __JANE_UTF8_AS, __JANE_UTF8_AS, __JANE_UTF8_AS, - __JANE_UTF8_AS, __JANE_UTF8_AS, __JANE_UTF8_AS, __JANE_UTF8_AS, - __JANE_UTF8_AS, __JANE_UTF8_AS, __JANE_UTF8_AS, __JANE_UTF8_AS, - __JANE_UTF8_AS, __JANE_UTF8_AS, __JANE_UTF8_AS, __JANE_UTF8_AS, - __JANE_UTF8_AS, __JANE_UTF8_AS, __JANE_UTF8_AS, __JANE_UTF8_AS, - __JANE_UTF8_AS, __JANE_UTF8_AS, __JANE_UTF8_AS, __JANE_UTF8_AS, - __JANE_UTF8_AS, __JANE_UTF8_AS, __JANE_UTF8_AS, __JANE_UTF8_AS, - __JANE_UTF8_AS, __JANE_UTF8_AS, __JANE_UTF8_AS, __JANE_UTF8_AS, - __JANE_UTF8_XX, __JANE_UTF8_XX, __JANE_UTF8_XX, __JANE_UTF8_XX, - __JANE_UTF8_XX, __JANE_UTF8_XX, __JANE_UTF8_XX, __JANE_UTF8_XX, - __JANE_UTF8_XX, __JANE_UTF8_XX, __JANE_UTF8_XX, __JANE_UTF8_XX, - __JANE_UTF8_XX, __JANE_UTF8_XX, __JANE_UTF8_XX, __JANE_UTF8_XX, - __JANE_UTF8_XX, __JANE_UTF8_XX, __JANE_UTF8_XX, __JANE_UTF8_XX, - __JANE_UTF8_XX, __JANE_UTF8_XX, __JANE_UTF8_XX, __JANE_UTF8_XX, - __JANE_UTF8_XX, __JANE_UTF8_XX, __JANE_UTF8_XX, __JANE_UTF8_XX, - __JANE_UTF8_XX, __JANE_UTF8_XX, __JANE_UTF8_XX, __JANE_UTF8_XX, - __JANE_UTF8_XX, __JANE_UTF8_XX, __JANE_UTF8_XX, __JANE_UTF8_XX, - __JANE_UTF8_XX, __JANE_UTF8_XX, __JANE_UTF8_XX, __JANE_UTF8_XX, - __JANE_UTF8_XX, __JANE_UTF8_XX, __JANE_UTF8_XX, __JANE_UTF8_XX, - __JANE_UTF8_XX, __JANE_UTF8_XX, __JANE_UTF8_XX, __JANE_UTF8_XX, - __JANE_UTF8_XX, __JANE_UTF8_XX, __JANE_UTF8_XX, __JANE_UTF8_XX, - __JANE_UTF8_XX, __JANE_UTF8_XX, __JANE_UTF8_XX, __JANE_UTF8_XX, - __JANE_UTF8_XX, __JANE_UTF8_XX, __JANE_UTF8_XX, __JANE_UTF8_XX, - __JANE_UTF8_XX, __JANE_UTF8_XX, __JANE_UTF8_XX, __JANE_UTF8_XX, - __JANE_UTF8_XX, __JANE_UTF8_XX, __JANE_UTF8_S1, __JANE_UTF8_S1, - __JANE_UTF8_S1, __JANE_UTF8_S1, __JANE_UTF8_S1, __JANE_UTF8_S1, - __JANE_UTF8_S1, __JANE_UTF8_S1, __JANE_UTF8_S1, __JANE_UTF8_S1, - __JANE_UTF8_S1, __JANE_UTF8_S1, __JANE_UTF8_S1, __JANE_UTF8_S1, - __JANE_UTF8_S1, __JANE_UTF8_S1, __JANE_UTF8_S1, __JANE_UTF8_S1, - __JANE_UTF8_S1, __JANE_UTF8_S1, __JANE_UTF8_S1, __JANE_UTF8_S1, - __JANE_UTF8_S1, __JANE_UTF8_S1, __JANE_UTF8_S1, __JANE_UTF8_S1, - __JANE_UTF8_S1, __JANE_UTF8_S1, __JANE_UTF8_S1, __JANE_UTF8_S1, - __JANE_UTF8_S2, __JANE_UTF8_S3, __JANE_UTF8_S3, __JANE_UTF8_S3, - __JANE_UTF8_S3, __JANE_UTF8_S3, __JANE_UTF8_S3, __JANE_UTF8_S3, - __JANE_UTF8_S3, __JANE_UTF8_S3, __JANE_UTF8_S3, __JANE_UTF8_S3, - __JANE_UTF8_S3, __JANE_UTF8_S4, __JANE_UTF8_S3, __JANE_UTF8_S3, - __JANE_UTF8_S5, __JANE_UTF8_S6, __JANE_UTF8_S6, __JANE_UTF8_S6, - __JANE_UTF8_S7, __JANE_UTF8_XX, __JANE_UTF8_XX, __JANE_UTF8_XX, - __JANE_UTF8_XX, __JANE_UTF8_XX, __JANE_UTF8_XX, __JANE_UTF8_XX, - __JANE_UTF8_XX, __JANE_UTF8_XX, __JANE_UTF8_XX, __JANE_UTF8_XX, +#include "types.hpp" +#include + +namespace jane { +constexpr signed int UTF8_RUNE_ERROR{65533}; +constexpr signed int UTF8_MASKX{63}; +constexpr signed int UTF8_MASK2{31}; +constexpr signed int UTF8_MASK3{15}; +constexpr signed int UTF8_MASK4{7}; +constexpr signed int UTF8_LOCB{128}; +constexpr signed int UTF8_HICB{191}; +constexpr signed int UTF8_XX{241}; +constexpr signed int UTF8_AS{240}; +constexpr signed int UTF8_S1{2}; +constexpr signed int UTF8_S2{19}; +constexpr signed int UTF8_S3{3}; +constexpr signed int UTF8_S4{35}; +constexpr signed int UTF8_S5{52}; +constexpr signed int UTF8_S6{4}; +constexpr signed int UTF8_S7{68}; +constexpr signed int UTF8_RUNE1_MAX{127}; +constexpr signed int UTF8_RUNE2_MAX{2047}; +constexpr signed int UTF8_RUNE3_MAX{65535}; +constexpr signed int UTF8_TX{128}; +constexpr signed int UTF8_T2{192}; +constexpr signed int UTF8_T3{224}; +constexpr signed int UTF8_T4{240}; +constexpr signed int UTF8_MAX_RUNE{1114111}; +constexpr signed int UTF8_SURROGATE_MIN{55296}; +constexpr signed int UTF8_SURROGATE_MAX{57343}; + +struct UTF8AcceptRange; +std::tuple +utf8_decode_rune_str(const char *s, const jane::Int &len) noexcept; +jane::Slice utf8_rune_to_bytes(const jane::I32 &r) noexcept; + +constexpr jane::U8 utf8_first[256] = { + jane::UTF8_AS, jane::UTF8_AS, jane::UTF8_AS, jane::UTF8_AS, jane::UTF8_AS, + jane::UTF8_AS, jane::UTF8_AS, jane::UTF8_AS, jane::UTF8_AS, jane::UTF8_AS, + jane::UTF8_AS, jane::UTF8_AS, jane::UTF8_AS, jane::UTF8_AS, jane::UTF8_AS, + jane::UTF8_AS, jane::UTF8_AS, jane::UTF8_AS, jane::UTF8_AS, jane::UTF8_AS, + jane::UTF8_AS, jane::UTF8_AS, jane::UTF8_AS, jane::UTF8_AS, jane::UTF8_AS, + jane::UTF8_AS, jane::UTF8_AS, jane::UTF8_AS, jane::UTF8_AS, jane::UTF8_AS, + jane::UTF8_AS, jane::UTF8_AS, jane::UTF8_AS, jane::UTF8_AS, jane::UTF8_AS, + jane::UTF8_AS, jane::UTF8_AS, jane::UTF8_AS, jane::UTF8_AS, jane::UTF8_AS, + jane::UTF8_AS, jane::UTF8_AS, jane::UTF8_AS, jane::UTF8_AS, jane::UTF8_AS, + jane::UTF8_AS, jane::UTF8_AS, jane::UTF8_AS, jane::UTF8_AS, jane::UTF8_AS, + jane::UTF8_AS, jane::UTF8_AS, jane::UTF8_AS, jane::UTF8_AS, jane::UTF8_AS, + jane::UTF8_AS, jane::UTF8_AS, jane::UTF8_AS, jane::UTF8_AS, jane::UTF8_AS, + jane::UTF8_AS, jane::UTF8_AS, jane::UTF8_AS, jane::UTF8_AS, jane::UTF8_AS, + jane::UTF8_AS, jane::UTF8_AS, jane::UTF8_AS, jane::UTF8_AS, jane::UTF8_AS, + jane::UTF8_AS, jane::UTF8_AS, jane::UTF8_AS, jane::UTF8_AS, jane::UTF8_AS, + jane::UTF8_AS, jane::UTF8_AS, jane::UTF8_AS, jane::UTF8_AS, jane::UTF8_AS, + jane::UTF8_AS, jane::UTF8_AS, jane::UTF8_AS, jane::UTF8_AS, jane::UTF8_AS, + jane::UTF8_AS, jane::UTF8_AS, jane::UTF8_AS, jane::UTF8_AS, jane::UTF8_AS, + jane::UTF8_AS, jane::UTF8_AS, jane::UTF8_AS, jane::UTF8_AS, jane::UTF8_AS, + jane::UTF8_AS, jane::UTF8_AS, jane::UTF8_AS, jane::UTF8_AS, jane::UTF8_AS, + jane::UTF8_AS, jane::UTF8_AS, jane::UTF8_AS, jane::UTF8_AS, jane::UTF8_AS, + jane::UTF8_AS, jane::UTF8_AS, jane::UTF8_AS, jane::UTF8_AS, jane::UTF8_AS, + jane::UTF8_AS, jane::UTF8_AS, jane::UTF8_AS, jane::UTF8_AS, jane::UTF8_AS, + jane::UTF8_AS, jane::UTF8_AS, jane::UTF8_AS, jane::UTF8_AS, jane::UTF8_AS, + jane::UTF8_AS, jane::UTF8_AS, jane::UTF8_AS, jane::UTF8_AS, jane::UTF8_AS, + jane::UTF8_AS, jane::UTF8_AS, jane::UTF8_AS, jane::UTF8_XX, jane::UTF8_XX, + jane::UTF8_XX, jane::UTF8_XX, jane::UTF8_XX, jane::UTF8_XX, jane::UTF8_XX, + jane::UTF8_XX, jane::UTF8_XX, jane::UTF8_XX, jane::UTF8_XX, jane::UTF8_XX, + jane::UTF8_XX, jane::UTF8_XX, jane::UTF8_XX, jane::UTF8_XX, jane::UTF8_XX, + jane::UTF8_XX, jane::UTF8_XX, jane::UTF8_XX, jane::UTF8_XX, jane::UTF8_XX, + jane::UTF8_XX, jane::UTF8_XX, jane::UTF8_XX, jane::UTF8_XX, jane::UTF8_XX, + jane::UTF8_XX, jane::UTF8_XX, jane::UTF8_XX, jane::UTF8_XX, jane::UTF8_XX, + jane::UTF8_XX, jane::UTF8_XX, jane::UTF8_XX, jane::UTF8_XX, jane::UTF8_XX, + jane::UTF8_XX, jane::UTF8_XX, jane::UTF8_XX, jane::UTF8_XX, jane::UTF8_XX, + jane::UTF8_XX, jane::UTF8_XX, jane::UTF8_XX, jane::UTF8_XX, jane::UTF8_XX, + jane::UTF8_XX, jane::UTF8_XX, jane::UTF8_XX, jane::UTF8_XX, jane::UTF8_XX, + jane::UTF8_XX, jane::UTF8_XX, jane::UTF8_XX, jane::UTF8_XX, jane::UTF8_XX, + jane::UTF8_XX, jane::UTF8_XX, jane::UTF8_XX, jane::UTF8_XX, jane::UTF8_XX, + jane::UTF8_XX, jane::UTF8_XX, jane::UTF8_XX, jane::UTF8_XX, jane::UTF8_S1, + jane::UTF8_S1, jane::UTF8_S1, jane::UTF8_S1, jane::UTF8_S1, jane::UTF8_S1, + jane::UTF8_S1, jane::UTF8_S1, jane::UTF8_S1, jane::UTF8_S1, jane::UTF8_S1, + jane::UTF8_S1, jane::UTF8_S1, jane::UTF8_S1, jane::UTF8_S1, jane::UTF8_S1, + jane::UTF8_S1, jane::UTF8_S1, jane::UTF8_S1, jane::UTF8_S1, jane::UTF8_S1, + jane::UTF8_S1, jane::UTF8_S1, jane::UTF8_S1, jane::UTF8_S1, jane::UTF8_S1, + jane::UTF8_S1, jane::UTF8_S1, jane::UTF8_S1, jane::UTF8_S1, jane::UTF8_S2, + jane::UTF8_S3, jane::UTF8_S3, jane::UTF8_S3, jane::UTF8_S3, jane::UTF8_S3, + jane::UTF8_S3, jane::UTF8_S3, jane::UTF8_S3, jane::UTF8_S3, jane::UTF8_S3, + jane::UTF8_S3, jane::UTF8_S3, jane::UTF8_S4, jane::UTF8_S3, jane::UTF8_S3, + jane::UTF8_S5, jane::UTF8_S6, jane::UTF8_S6, jane::UTF8_S6, jane::UTF8_S7, + jane::UTF8_XX, jane::UTF8_XX, jane::UTF8_XX, jane::UTF8_XX, jane::UTF8_XX, + jane::UTF8_XX, jane::UTF8_XX, jane::UTF8_XX, jane::UTF8_XX, jane::UTF8_XX, + jane::UTF8_XX, }; -struct __jane_utf8_accept_range { - const u8_jnt _lo, _hi; +struct UTF8AcceptRange { + const jane::U8 lo, hi; }; -constexpr struct __jane_utf8_accept_range __jane_utf8_accept_ranges[16] = { - {__JANE_UTF8_LOCB, __JANE_UTF8_HICB}, - {0xA0, __JANE_UTF8_HICB}, - {__JANE_UTF8_LOCB, 0x9F}, - {0x90, __JANE_UTF8_HICB}, - {__JANE_UTF8_LOCB, 0x8F}, +constexpr struct jane::UTF8AcceptRange utf8_accept_ranges[16] = { + {jane::UTF8_LOCB, jane::UTF8_HICB}, + {0xA0, jane::UTF8_HICB}, + {jane::UTF8_LOCB, 0x9F}, + {0x90, jane::UTF8_HICB}, + {jane::UTF8_LOCB, 0x8F}, }; -std::tuple -__jane_utf8_decode_rune_str(const char *_S, const int_jnt &_Len) noexcept { - if (_Len < 1) { - return (std::make_tuple(__JANE_UTF8_RUNE_ERROR, 0)); +inline std::tuple +utf8_decode_rune_st(const char *s, const jane::Int &len) noexcept { + if (len < 1) { + return std::make_tuple(jane::UTF8_RUNE_ERROR, 0); } - const u8_jnt _s0{static_cast(_S[0])}; - const u8_jnt _x{__jane_utf8_first[_s0]}; - if (_x >= __JANE_UTF8_AS) { - const i32_jnt _mask{_x << 31 >> 31}; - return (std::make_tuple((static_cast(_S[0]) & ~_mask) | - (__JANE_UTF8_RUNE_ERROR & _mask), - 1)); + const jane::U8 s0{static_cast(s[0])}; + const jane::U8 x{jane::utf8_first[s0]}; + if (x >= jane::UTF8_AS) { + const jane::I32 mask{x << 31 >> 31}; + return std::make_tuple((static_cast(s[0]) & ~mask) | + (jane::UTF8_RUNE_ERROR & mask), + 1); } - const int_jnt _sz{static_cast(_x & 7)}; - const struct __jane_utf8_accept_range _accept { - __jane_utf8_accept_ranges[_x >> 4] + const jane::Int sz{static_cast(x & 7)}; + const struct jane::UTF8AcceptRange accept { + jane::utf8_accept_ranges[x >> 4] }; - if (_Len < _sz) { - return (std::make_tuple(__JANE_UTF8_RUNE_ERROR, 1)); + if (len < sz) { + return std::make_tuple(jane::UTF8_RUNE_ERROR, 1); } - const u8_jnt _s1{static_cast(_S[1])}; - if (_s1 < _accept._lo || _accept._hi < _s1) { - return (std::make_tuple(__JANE_UTF8_RUNE_ERROR, 1)); + const jane::U8 s1{static_cast(s[1])}; + if (s1 < accept.lo || accept.hi < s1) { + return std::make_tuple(jane::UTF8_RUNE_ERROR, 1); } - if (_sz <= 2) { - return ( - std::make_tuple((static_cast(_s0 & __JANE_UTF8_MASK2) << 6) | - static_cast(_s1 & __JANE_UTF8_MASKX), - 2)); + + if (sz <= 2) { + return std::make_tuple( + (static_cast(s0 & jane::UTF8_MASK2) << 6) | + static_cast(s1 & jane::UTF8_MASKX), + 2); } - const u8_jnt _s2{static_cast(_S[2])}; - if (_s2 < __JANE_UTF8_LOCB || __JANE_UTF8_HICB < _s2) { - return (std::make_tuple(__JANE_UTF8_RUNE_ERROR, 1)); + + const jane::U8 s2{static_cast(s[2])}; + if (s2 < jane::UTF8_LOCB || jane::UTF8_HICB < s2) { + return std::make_tuple(jane::UTF8_RUNE_ERROR, 1); } - if (_sz <= 3) { - return (std::make_tuple( - (static_cast(_s0 & __JANE_UTF8_MASK3) << 12) | - (static_cast(_s1 & __JANE_UTF8_MASKX) << 6) | - static_cast(_s2 & __JANE_UTF8_MASKX), - 3)); + + if (sz <= 3) { + return std::make_tuple( + (static_cast(s0 & jane::UTF8_MASK3) << 12) | + (static_cast(s1 & jane::UTF8_MASKX) << 6) | + static_cast(s2 & jane::UTF8_MASKX), + 3); } - const u8_jnt _s3{static_cast(_S[3])}; - if (_s3 < __JANE_UTF8_LOCB || __JANE_UTF8_HICB < _s3) { - return std::make_tuple(__JANE_UTF8_RUNE_ERROR, 1); + + const jane::U8 s3{static_cast(s[3])}; + if (s3 < jane::UTF8_LOCB || jane::UTF8_HICB < s3) { + return std::make_tuple(jane::UTF8_RUNE_ERROR, 1); } - return (std::make_tuple( - (static_cast(_s0 & __JANE_UTF8_MASK4) << 18) | - (static_cast(_s1 & __JANE_UTF8_MASKX) << 12) | - (static_cast(_s2 & __JANE_UTF8_MASKX) << 6) | - static_cast(_s3 & __JANE_UTF8_MASKX), - 4)); + + return std::make_tuple( + (static_cast(s0 & jane::UTF8_MASK4) << 18) | + (static_cast(s1 & jane::UTF8_MASKX) << 12) | + (static_cast(s2 & jane::UTF8_MASKX) << 6) | + static_cast(s3 & jane::UTF8_MASKX), + 4); } -slice_jnt __jane_utf8_rune_to_bytes(const i32_jnt &_R) noexcept { - if (static_cast(_R) <= __JANE_UTF8_RUNE1_MAX) { - return (slice_jnt({static_cast(_R)})); - } - const u32_jnt _i{static_cast(_R)}; - if (_i < __JANE_UTF8_RUNE2_MAX) { - return (slice_jnt( - {static_cast(__JANE_UTF8_T2 | static_cast(_R >> 6)), - static_cast(__JANE_UTF8_TX | - (static_cast(_R) & __JANE_UTF8_MASKX))})); +inline jane::Slice utf8_rune_to_bytes(const jane::I32 &r) noexcept { + if (static_cast(r) <= jane::UTF8_RUNE1_MAX) { + return jane::Slice({static_cast(r)}); } - i32_jnt _r{_R}; - if ((_i > __JANE_UTF8_MAX_RUNE) || - (__JANE_UTF8_SURROGATE_MIN <= _i && _i <= __JANE_UTF8_SURROGATE_MAX)) { - _r = __JANE_UTF8_RUNE_ERROR; - } - if (_i <= __JANE_UTF8_RUNE3_MAX) { - return (slice_jnt( - {static_cast(__JANE_UTF8_T3 | static_cast(_r >> 12)), - static_cast(__JANE_UTF8_TX | (static_cast(_r >> 6) & - __JANE_UTF8_MASKX)), - static_cast(__JANE_UTF8_TX | - (static_cast(_r) & __JANE_UTF8_MASKX))})); + + const jane::U32 i{static_cast(r)}; + if (i < jane::UTF8_RUNE2_MAX) { + return jane::Slice( + {static_cast(jane::UTF8_T2 | static_cast(r >> 6)), + static_cast(jane::UTF8_TX | + (static_cast(r) & jane::UTF8_MASKX))}); } - return (slice_jnt( - {static_cast(__JANE_UTF8_T4 | static_cast(_r >> 18)), - static_cast(__JANE_UTF8_TX | - (static_cast(_r >> 12) & __JANE_UTF8_MASKX)), - static_cast(__JANE_UTF8_TX | - (static_cast(_r >> 6) & __JANE_UTF8_MASKX)), - static_cast(__JANE_UTF8_TX | - (static_cast(_r) & __JANE_UTF8_MASKX))})); + + jane::I32 _r{r}; + if (i > jane::UTF8_MAX_RUNE || + jane::UTF8_SURROGATE_MIN <= i && i <= jane::UTF8_SURROGATE_MAX) + _r = jane::UTF8_RUNE_ERROR; + + if (i <= jane::UTF8_RUNE3_MAX) + return jane::Slice( + {static_cast(jane::UTF8_T3 | static_cast(_r >> 12)), + static_cast(jane::UTF8_TX | (static_cast(_r >> 6) & + jane::UTF8_MASKX)), + static_cast( + jane::UTF8_TX | (static_cast(_r) & jane::UTF8_MASKX))}); + + return jane::Slice( + {static_cast(jane::UTF8_T4 | static_cast(_r >> 18)), + static_cast(jane::UTF8_TX | (static_cast(_r >> 12) & + jane::UTF8_MASKX)), + static_cast( + jane::UTF8_TX | (static_cast(_r >> 6) & jane::UTF8_MASKX)), + static_cast(jane::UTF8_TX | + (static_cast(_r) & jane::UTF8_MASKX))}); } +} // namespace jane -#endif // !__JANE_UTF8_HPP +#endif // __JANE_UTF8_HPP \ No newline at end of file