From 2a3de1b7050e107be850dd18fff811cee5a86c0b Mon Sep 17 00:00:00 2001 From: WerWolv Date: Sat, 22 Jun 2024 10:46:30 +0200 Subject: [PATCH] includes: Tabs -> Spaces --- includes/hex/type/mangled.pat | 6 +- includes/std/bit.pat | 100 +++--- includes/std/file.pat | 166 ++++----- includes/std/fxpt.pat | 156 ++++----- includes/std/limits.pat | 318 +++++++++--------- includes/std/math.pat | 616 +++++++++++++++++----------------- includes/std/string.pat | 10 +- includes/std/time.pat | 316 ++++++++--------- includes/type/base.pat | 4 +- includes/type/byte.pat | 4 +- includes/type/float16.pat | 2 +- includes/type/guid.pat | 56 ++-- includes/type/leb128.pat | 112 +++---- includes/type/mac.pat | 34 +- includes/type/path.pat | 86 ++--- 15 files changed, 991 insertions(+), 995 deletions(-) diff --git a/includes/hex/type/mangled.pat b/includes/hex/type/mangled.pat index e85de6e..1fbfbff 100644 --- a/includes/hex/type/mangled.pat +++ b/includes/hex/type/mangled.pat @@ -11,20 +11,20 @@ import hex.dec; */ namespace auto hex::type { - + /** A mangled name string that gets demangled when displayed */ struct MangledName { char value[]; } [[sealed, format("hex::type::impl::format_mangled_name")]]; - + namespace impl { fn format_mangled_name(ref MangledName name) { return hex::dec::demangle(name.value); }; } - + } diff --git a/includes/std/bit.pat b/includes/std/bit.pat index 46cbe2c..8d21ff3 100644 --- a/includes/std/bit.pat +++ b/includes/std/bit.pat @@ -3,61 +3,61 @@ import std.limits; /*! - This library contains various helper functions for common bit operations. + This library contains various helper functions for common bit operations. */ namespace auto std::bit { - /** - Calculates the number of 1 bits in a given number - @param x The number - @return The number of bits set to 1 in `x` - */ - fn popcount(u128 x) { - x = (x & (std::limits::u128_max() / 3)) + ((x >> 1) & (std::limits::u128_max() / 3)); - x = (x & (std::limits::u128_max() / 5)) + ((x >> 2) & (std::limits::u128_max() / 5)); - x = (x & (std::limits::u128_max() / 17)) + ((x >> 4) & (std::limits::u128_max() / 17)); - - return x % 0xFF; - }; + /** + Calculates the number of 1 bits in a given number + @param x The number + @return The number of bits set to 1 in `x` + */ + fn popcount(u128 x) { + x = (x & (std::limits::u128_max() / 3)) + ((x >> 1) & (std::limits::u128_max() / 3)); + x = (x & (std::limits::u128_max() / 5)) + ((x >> 2) & (std::limits::u128_max() / 5)); + x = (x & (std::limits::u128_max() / 17)) + ((x >> 4) & (std::limits::u128_max() / 17)); + + return x % 0xFF; + }; - /** - Checks if only a single bit is set to 1 in a given number - @param x The number - @return True if there's a single bit set to 1 in `x`, false otherwise - */ - fn has_single_bit(u128 x) { - return x != 0 && (x & (x - 1)) == 0; - }; - - /** - Rounds the given number up to the next bigger power of two - @param x The number - @return Next bigger power of two that can fit `x` - */ - fn bit_ceil(u128 x) { - if (x == 0) return 0; - - u8 i; - while ((1 << i) < x) - i = i + 1; - - return 1 << i; - }; + /** + Checks if only a single bit is set to 1 in a given number + @param x The number + @return True if there's a single bit set to 1 in `x`, false otherwise + */ + fn has_single_bit(u128 x) { + return x != 0 && (x & (x - 1)) == 0; + }; + + /** + Rounds the given number up to the next bigger power of two + @param x The number + @return Next bigger power of two that can fit `x` + */ + fn bit_ceil(u128 x) { + if (x == 0) return 0; + + u8 i; + while ((1 << i) < x) + i = i + 1; + + return 1 << i; + }; - /** - Rounds the given number down to the next bigger power of two - @param x The number - @return Next smaller power of two - */ - fn bit_floor(u128 x) { - if (x == 0) return 0; - - u8 i; - while ((x >> i) > 0) - i = i + 1; - - return 1 << (i - 1); - }; + /** + Rounds the given number down to the next bigger power of two + @param x The number + @return Next smaller power of two + */ + fn bit_floor(u128 x) { + if (x == 0) return 0; + + u8 i; + while ((x >> i) > 0) + i = i + 1; + + return 1 << (i - 1); + }; } diff --git a/includes/std/file.pat b/includes/std/file.pat index c8b16a1..48b78cf 100644 --- a/includes/std/file.pat +++ b/includes/std/file.pat @@ -1,120 +1,120 @@ #pragma once /*! - The File library allows reading and writing from/to external files using - a C-like File IO API. + The File library allows reading and writing from/to external files using + a C-like File IO API. - **These functions are considered dangerous and require the user to manually permit them** + **These functions are considered dangerous and require the user to manually permit them** */ namespace auto std::file { - /** + /** A handle representing a file that has been opened */ - using Handle = s32; - - /** + using Handle = s32; + + /** The mode to open a file in. - Read opens the file in read-only mode - Write opens the file in read and write mode - Create creates a new file if it doesn't exist and overwrites an existing file + Read opens the file in read-only mode + Write opens the file in read and write mode + Create creates a new file if it doesn't exist and overwrites an existing file */ - enum Mode : u8 { - Read = 1, - Write = 2, - Create = 3 - }; + enum Mode : u8 { + Read = 1, + Write = 2, + Create = 3 + }; - /** + /** Opens a file - @param path The path to the file to open - @param mode File open mode - @return Handle to the newly opened file + @param path The path to the file to open + @param mode File open mode + @return Handle to the newly opened file */ - fn open(str path, Mode mode) { - return builtin::std::file::open(path, u32(mode)); - }; + fn open(str path, Mode mode) { + return builtin::std::file::open(path, u32(mode)); + }; - /** + /** Closes a file handle that has been opened previously - @param handle The handle to close + @param handle The handle to close */ - fn close(Handle handle) { - builtin::std::file::close(handle); - }; + fn close(Handle handle) { + builtin::std::file::close(handle); + }; - - /** + + /** Reads the content of a file into a string - @param handle The file handle to read from - @param size Number of bytes to read - @return String containing the read data + @param handle The file handle to read from + @param size Number of bytes to read + @return String containing the read data */ - fn read(Handle handle, u64 size) { - return builtin::std::file::read(handle, size); - }; + fn read(Handle handle, u64 size) { + return builtin::std::file::read(handle, size); + }; - /** + /** Writes the content of a string into a file - @param handle The file handle to write to - @param data String or Pattern to write to the file + @param handle The file handle to write to + @param data String or Pattern to write to the file */ - fn write(Handle handle, auto data) { - builtin::std::file::write(handle, data); - }; + fn write(Handle handle, auto data) { + builtin::std::file::write(handle, data); + }; - /** + /** Sets the current cursor position in the given file handle - @param handle The file handle to set the cursor position in - @param offset The offset to move the cursor to + @param handle The file handle to set the cursor position in + @param offset The offset to move the cursor to */ - fn seek(Handle handle, u64 offset) { - builtin::std::file::seek(handle, offset); - }; + fn seek(Handle handle, u64 offset) { + builtin::std::file::seek(handle, offset); + }; - /** - Queries the size of a file - @param handle The handle of the file to get the size of - @return The file's size - */ - fn size(Handle handle) { - return builtin::std::file::size(handle); - }; + /** + Queries the size of a file + @param handle The handle of the file to get the size of + @return The file's size + */ + fn size(Handle handle) { + return builtin::std::file::size(handle); + }; - /** - Resizes a file - @param handle The handle of the file to resize - */ - fn resize(Handle handle, u64 size) { - builtin::std::file::resize(handle, size); - }; + /** + Resizes a file + @param handle The handle of the file to resize + */ + fn resize(Handle handle, u64 size) { + builtin::std::file::resize(handle, size); + }; - /** - Flushes changes made to a file to disk - @param handle The handle of the file to flush - */ - fn flush(Handle handle) { - builtin::std::file::flush(handle); - }; + /** + Flushes changes made to a file to disk + @param handle The handle of the file to flush + */ + fn flush(Handle handle) { + builtin::std::file::flush(handle); + }; - /** - Deletes a file from disk. This will also automatically close this file - @param handle The handle of the file to delete - */ - fn remove(Handle handle) { - builtin::std::file::remove(handle); - }; + /** + Deletes a file from disk. This will also automatically close this file + @param handle The handle of the file to delete + */ + fn remove(Handle handle) { + builtin::std::file::remove(handle); + }; - /** - Create all directories for the provided path - @param path The path for which all directories should be created - */ - fn create_directories(str path) { - builtin::std::file::create_directories(path); - }; + /** + Create all directories for the provided path + @param path The path for which all directories should be created + */ + fn create_directories(str path) { + builtin::std::file::create_directories(path); + }; } diff --git a/includes/std/fxpt.pat b/includes/std/fxpt.pat index ca00343..ae02b60 100644 --- a/includes/std/fxpt.pat +++ b/includes/std/fxpt.pat @@ -1,89 +1,89 @@ #pragma once /*! - Library for doing arithmetic with fixed point numbers and converting them from/to floating point numbers. + Library for doing arithmetic with fixed point numbers and converting them from/to floating point numbers. */ namespace auto std::fxpt { - /** - A fixed point value - */ - using fixed = s128; + /** + A fixed point value + */ + using fixed = s128; - /** - Converts a fixed point value into a floating point value - @param fxt The fixed point value to convert - @param precision The bits of precision the new value should have - @return The floating point representation of fxt - */ - fn to_float(fixed fxt, u32 precision) { - return double(fxt) / double((1 << precision)); - }; - - /** - Converts a floating point value into a fixed point value - @param flt The floating point value to convert - @param precision The bits of precision the new value should have - @return The fixed point representation of flt - */ - fn to_fixed(double flt, u32 precision) { - return s128((flt * (1 << precision))); - }; + /** + Converts a fixed point value into a floating point value + @param fxt The fixed point value to convert + @param precision The bits of precision the new value should have + @return The floating point representation of fxt + */ + fn to_float(fixed fxt, u32 precision) { + return double(fxt) / double((1 << precision)); + }; + + /** + Converts a floating point value into a fixed point value + @param flt The floating point value to convert + @param precision The bits of precision the new value should have + @return The fixed point representation of flt + */ + fn to_fixed(double flt, u32 precision) { + return s128((flt * (1 << precision))); + }; - /** - Changes the number of bits used to represent the decimal part of the given fixed point number - @param value The fixed point value to convert - @param start_precision The current number of bits used - @param end_precision The new number of bits used - @return `value` as a new fixed point number with `end_precision` bits of precision - */ - fn change_precision(fixed value, u32 start_precision, u32 end_precision) { - return std::fxpt::to_fixed(std::fxpt::to_float(value, start_precision), end_precision); - }; - - /** - Adds two fixed point numbers with a given precision together - @param a First fixed point number - @param b Second fixed point number - @param precision The precision of `a` and `b` - @return Result of the addition of `a` and `b` - */ - fn add(fixed a, fixed b, u32 precision) { - return a + b; - }; - - /** - Subtracts two fixed point numbers with a given precision together - @param a First fixed point number - @param b Second fixed point number - @param precision The precision of `a` and `b` - @return Result of the subtraction of `a` and `b` - */ - fn subtract(fixed a, fixed b, u32 precision) { - return a - b; - }; - - /** - Multiplies two fixed point numbers with a given precision together - @param a First fixed point number - @param b Second fixed point number - @param precision The precision of `a` and `b` - @return Result of the multiplication of `a` and `b` - */ - fn multiply(fixed a, fixed b, u32 precision) { - return (a * b) / (1 << precision); - }; - - /** - Divides two fixed point numbers with a given precision together - @param a First fixed point number - @param b Second fixed point number - @param precision The precision of `a` and `b` - @return Result of the division of `a` and `b` - */ - fn divide(fixed a, fixed b, u32 precision) { - return (a << precision) / b; - }; + /** + Changes the number of bits used to represent the decimal part of the given fixed point number + @param value The fixed point value to convert + @param start_precision The current number of bits used + @param end_precision The new number of bits used + @return `value` as a new fixed point number with `end_precision` bits of precision + */ + fn change_precision(fixed value, u32 start_precision, u32 end_precision) { + return std::fxpt::to_fixed(std::fxpt::to_float(value, start_precision), end_precision); + }; + + /** + Adds two fixed point numbers with a given precision together + @param a First fixed point number + @param b Second fixed point number + @param precision The precision of `a` and `b` + @return Result of the addition of `a` and `b` + */ + fn add(fixed a, fixed b, u32 precision) { + return a + b; + }; + + /** + Subtracts two fixed point numbers with a given precision together + @param a First fixed point number + @param b Second fixed point number + @param precision The precision of `a` and `b` + @return Result of the subtraction of `a` and `b` + */ + fn subtract(fixed a, fixed b, u32 precision) { + return a - b; + }; + + /** + Multiplies two fixed point numbers with a given precision together + @param a First fixed point number + @param b Second fixed point number + @param precision The precision of `a` and `b` + @return Result of the multiplication of `a` and `b` + */ + fn multiply(fixed a, fixed b, u32 precision) { + return (a * b) / (1 << precision); + }; + + /** + Divides two fixed point numbers with a given precision together + @param a First fixed point number + @param b Second fixed point number + @param precision The precision of `a` and `b` + @return Result of the division of `a` and `b` + */ + fn divide(fixed a, fixed b, u32 precision) { + return (a << precision) / b; + }; } diff --git a/includes/std/limits.pat b/includes/std/limits.pat index 9a13add..5b8751b 100644 --- a/includes/std/limits.pat +++ b/includes/std/limits.pat @@ -1,169 +1,169 @@ #pragma once /*! - Library to calculate the minimum and maximum values that fit into a given data type + Library to calculate the minimum and maximum values that fit into a given data type */ namespace auto std::limits { - - /** - Returns the minimum value that can be stored in a `u8`. - @return Minimum value - */ - fn u8_min() { - return u8(0); - }; - - /** - Returns the maximum value that can be stored in a `u8`. - @return Maximum value - */ - fn u8_max() { - return u8(-1); - }; + + /** + Returns the minimum value that can be stored in a `u8`. + @return Minimum value + */ + fn u8_min() { + return u8(0); + }; + + /** + Returns the maximum value that can be stored in a `u8`. + @return Maximum value + */ + fn u8_max() { + return u8(-1); + }; - /** - Returns the minimum value that can be stored in a `s8`. - @return Minimum value - */ + /** + Returns the minimum value that can be stored in a `s8`. + @return Minimum value + */ fn s8_min() { - return -s8((std::limits::u8_max() / 2)) - 1; - }; - - /** - Returns the maximum value that can be stored in a `s8`. - @return Maximum value - */ - fn s8_max() { - return s8((std::limits::u8_max() / 2)); - }; + return -s8((std::limits::u8_max() / 2)) - 1; + }; + + /** + Returns the maximum value that can be stored in a `s8`. + @return Maximum value + */ + fn s8_max() { + return s8((std::limits::u8_max() / 2)); + }; - /** - Returns the minimum value that can be stored in a `u16`. - @return Minimum value - */ - fn u16_min() { - return u16(0); - }; - - /** - Returns the maximum value that can be stored in a `u16`. - @return Maximum value - */ - fn u16_max() { - return u16(-1); - }; - - /** - Returns the minimum value that can be stored in a `s16`. - @return Minimum value - */ - fn s16_min() { - return -s16((std::limits::u16_max() / 2)) - 1; - }; - - /** - Returns the maximum value that can be stored in a `s16`. - @return Maximum value - */ - fn s16_max() { - return s16((std::limits::u16_max() / 2)); - }; - - /** - Returns the minimum value that can be stored in a `u32`. - @return Minimum value - */ - fn u32_min() { - return u32(0); - }; - - /** - Returns the maximum value that can be stored in a `u32`. - @return Maximum value - */ - fn u32_max() { - return u32(-1); - }; - - /** - Returns the minimum value that can be stored in a `s32`. - @return Minimum value - */ - fn s32_min() { - return -s32((std::limits::u32_max() / 2)) - 1; - }; - - /** - Returns the maximum value that can be stored in a `s32`. - @return Maximum value - */ - fn s32_max() { - return s32((std::limits::u32_max() / 2)); - }; - - /** - Returns the minimum value that can be stored in a `u64`. - @return Minimum value - */ - fn u64_min() { - return u64(0); - }; - - /** - Returns the maximum value that can be stored in a `u64`. - @return Maximum value - */ - fn u64_max() { - return u64(-1); - }; - - /** - Returns the minimum value that can be stored in a `s64`. - @return Minimum value - */ - fn s64_min() { - return -s64((std::limits::u64_max() / 2)) - 1; - }; - - /** - Returns the maximum value that can be stored in a `s64`. - @return Maximum value - */ - fn s64_max() { - return s64((std::limits::u64_max() / 2)); - }; - - /** - Returns the minimum value that can be stored in a `u128`. - @return Minimum value - */ - fn u128_min() { - return u128(0); - }; - - /** - Returns the maximum value that can be stored in a `u128`. - @return Maximum value - */ - fn u128_max() { - return u128(-1); - }; - - /** - Returns the minimum value that can be stored in a `s128`. - @return Minimum value - */ - fn s128_min() { - return -s128((std::limits::u128_max() / 2)) - 1; - }; - - /** - Returns the maximum value that can be stored in a `s128`. - @return Maximum value - */ - fn s128_max() { - return s128((std::limits::u128_max() / 2)); - }; - + /** + Returns the minimum value that can be stored in a `u16`. + @return Minimum value + */ + fn u16_min() { + return u16(0); + }; + + /** + Returns the maximum value that can be stored in a `u16`. + @return Maximum value + */ + fn u16_max() { + return u16(-1); + }; + + /** + Returns the minimum value that can be stored in a `s16`. + @return Minimum value + */ + fn s16_min() { + return -s16((std::limits::u16_max() / 2)) - 1; + }; + + /** + Returns the maximum value that can be stored in a `s16`. + @return Maximum value + */ + fn s16_max() { + return s16((std::limits::u16_max() / 2)); + }; + + /** + Returns the minimum value that can be stored in a `u32`. + @return Minimum value + */ + fn u32_min() { + return u32(0); + }; + + /** + Returns the maximum value that can be stored in a `u32`. + @return Maximum value + */ + fn u32_max() { + return u32(-1); + }; + + /** + Returns the minimum value that can be stored in a `s32`. + @return Minimum value + */ + fn s32_min() { + return -s32((std::limits::u32_max() / 2)) - 1; + }; + + /** + Returns the maximum value that can be stored in a `s32`. + @return Maximum value + */ + fn s32_max() { + return s32((std::limits::u32_max() / 2)); + }; + + /** + Returns the minimum value that can be stored in a `u64`. + @return Minimum value + */ + fn u64_min() { + return u64(0); + }; + + /** + Returns the maximum value that can be stored in a `u64`. + @return Maximum value + */ + fn u64_max() { + return u64(-1); + }; + + /** + Returns the minimum value that can be stored in a `s64`. + @return Minimum value + */ + fn s64_min() { + return -s64((std::limits::u64_max() / 2)) - 1; + }; + + /** + Returns the maximum value that can be stored in a `s64`. + @return Maximum value + */ + fn s64_max() { + return s64((std::limits::u64_max() / 2)); + }; + + /** + Returns the minimum value that can be stored in a `u128`. + @return Minimum value + */ + fn u128_min() { + return u128(0); + }; + + /** + Returns the maximum value that can be stored in a `u128`. + @return Maximum value + */ + fn u128_max() { + return u128(-1); + }; + + /** + Returns the minimum value that can be stored in a `s128`. + @return Minimum value + */ + fn s128_min() { + return -s128((std::limits::u128_max() / 2)) - 1; + }; + + /** + Returns the maximum value that can be stored in a `s128`. + @return Maximum value + */ + fn s128_max() { + return s128((std::limits::u128_max() / 2)); + }; + } diff --git a/includes/std/math.pat b/includes/std/math.pat index baa7d75..d95f466 100644 --- a/includes/std/math.pat +++ b/includes/std/math.pat @@ -3,338 +3,338 @@ import std.mem; /*! - Library containing more advanced mathematical operations. + Library containing more advanced mathematical operations. */ namespace auto std::math { - /** - Compares the values `a` and `b` with each other and returns the smaller of the two - @param a First value - @param b Second value - @return `a` if `a` is smaller than `b`, otherwise `b` - */ - fn min(auto a, auto b) { - if (a < b) - return a; - else - return b; - }; - - /** - Compares the values `a` and `b` with each other and returns the bigger of the two - @param a First value - @param b Second value - @return `a` if `a` is bigger than `b`, otherwise `b` - */ - fn max(auto a, auto b) { - if (a > b) - return a; - else - return b; - }; - - /** - Clamps the value of `x` between `min` and `max`. - @param x Value - @param min Minimum value - @param max Maximum value - @return `min` if `x` is smaller than `min`, `max` if `x` is bigger than `max`, `x` otherwise - */ - fn clamp(auto x, auto min, auto max) { - if (x < min) - return min; - else if (x > max) - return max; - else - return x; - }; - - /** - Returns the absolute value of `x`. - @param x Value - @return `x` if `x` is positive, `-x` otherwise - */ - fn abs(auto x) { - if (x < 0) - return -x; - else - return x; - }; - - /** - Returns the sign of `x`. - @param x Value - @return `1` if `x` is positive, `-1` if `x` is negative, `0` if `x` is zero - */ - fn sign(auto x) { - if (x > 0) - return 1; - else if (x < 0) - return -1; - else - return 0; - }; - - /** - Copies the sign of `y` to `x`. - @param x Value - @param y Value - @return `x` if `y` is positive, `-x` if `y` is negative - */ - fn copy_sign(auto x, auto y) { - if (y >= 0) - return std::math::abs(x); - else - return -std::math::abs(x); - }; - - /** - Calculates the factorial of `x`. - @param x Value - @return Factorial of `x` - */ - fn factorial(u128 x) { - u128 result; - - result = x; - while (x > 1) { - x = x - 1; - result = result * x; - } - - return result; - }; - - /** - Calculates the binomial coefficient of `n` and `k`. - @param n Value - @param k Value - @return Binomial coefficient of `n` and `k` - */ - fn comb(u128 n, u128 k) { - if (k > n) - return 0; - else - return std::math::factorial(n) / (std::math::factorial(k) * std::math::factorial(n - k)); - }; - - /** - Calculates the permutation of `n` and `k`. - @param n Value - @param k Value - @return Permutation of `n` and `k` - */ - fn perm(u128 n, u128 k) { - if (k > n) - return 0; - else - return std::math::factorial(n) / std::math::factorial(n - k); - }; - - /** - Floors the value of `value`. - @param value Value - @return `value` floored - */ - fn floor(auto value) { return builtin::std::math::floor(value); }; - - /** - Ceils the value of `value`. - @param value Value - @return `value` ceiled - */ - fn ceil(auto value) { return builtin::std::math::ceil(value); }; - - /** - Rounds the value of `value`. - @param value Value - @return `value` rounded - */ - fn round(auto value) { return builtin::std::math::round(value); }; - - /** - Truncates the value of `value`. - @param value Value - @return `value` truncated - */ - fn trunc(auto value) { return builtin::std::math::trunc(value); }; - - - /** - Calculates the logarithm of `value` with base 10. - @param value Value - @return Logarithm of `value` with base 10 - */ - fn log10(auto value) { return builtin::std::math::log10(value); }; - - /** - Calculates the logarithm of `value` with base 2. - @param value Value - @return Logarithm of `value` with base 2 - */ - fn log2(auto value) { return builtin::std::math::log2(value); }; - - /** - Calculates the natural logarithm of `value`. - @param value Value - @return Logarithm of `value` with base `e` - */ - fn ln(auto value) { return builtin::std::math::ln(value); }; - - /** - Calculates the floating point modulus of `value`. - @param value Value - @return Floating point modulus of `value` - */ - fn fmod(auto value) { return builtin::std::math::fmod(value); }; - - /** - Calculates the value of `base` raised to the power of `exp`. - @param base Base - @param exp Exponent - @return `base` raised to the power of `exp` - */ - fn pow(auto base, auto exp) { return builtin::std::math::pow(base, exp); }; + /** + Compares the values `a` and `b` with each other and returns the smaller of the two + @param a First value + @param b Second value + @return `a` if `a` is smaller than `b`, otherwise `b` + */ + fn min(auto a, auto b) { + if (a < b) + return a; + else + return b; + }; + + /** + Compares the values `a` and `b` with each other and returns the bigger of the two + @param a First value + @param b Second value + @return `a` if `a` is bigger than `b`, otherwise `b` + */ + fn max(auto a, auto b) { + if (a > b) + return a; + else + return b; + }; /** - Calculates the value of the natural number `e` raised to the power of `value`. - @param value Exponent - @return `e` raised to the power of `value` - */ + Clamps the value of `x` between `min` and `max`. + @param x Value + @param min Minimum value + @param max Maximum value + @return `min` if `x` is smaller than `min`, `max` if `x` is bigger than `max`, `x` otherwise + */ + fn clamp(auto x, auto min, auto max) { + if (x < min) + return min; + else if (x > max) + return max; + else + return x; + }; + + /** + Returns the absolute value of `x`. + @param x Value + @return `x` if `x` is positive, `-x` otherwise + */ + fn abs(auto x) { + if (x < 0) + return -x; + else + return x; + }; + + /** + Returns the sign of `x`. + @param x Value + @return `1` if `x` is positive, `-1` if `x` is negative, `0` if `x` is zero + */ + fn sign(auto x) { + if (x > 0) + return 1; + else if (x < 0) + return -1; + else + return 0; + }; + + /** + Copies the sign of `y` to `x`. + @param x Value + @param y Value + @return `x` if `y` is positive, `-x` if `y` is negative + */ + fn copy_sign(auto x, auto y) { + if (y >= 0) + return std::math::abs(x); + else + return -std::math::abs(x); + }; + + /** + Calculates the factorial of `x`. + @param x Value + @return Factorial of `x` + */ + fn factorial(u128 x) { + u128 result; + + result = x; + while (x > 1) { + x = x - 1; + result = result * x; + } + + return result; + }; + + /** + Calculates the binomial coefficient of `n` and `k`. + @param n Value + @param k Value + @return Binomial coefficient of `n` and `k` + */ + fn comb(u128 n, u128 k) { + if (k > n) + return 0; + else + return std::math::factorial(n) / (std::math::factorial(k) * std::math::factorial(n - k)); + }; + + /** + Calculates the permutation of `n` and `k`. + @param n Value + @param k Value + @return Permutation of `n` and `k` + */ + fn perm(u128 n, u128 k) { + if (k > n) + return 0; + else + return std::math::factorial(n) / std::math::factorial(n - k); + }; + + /** + Floors the value of `value`. + @param value Value + @return `value` floored + */ + fn floor(auto value) { return builtin::std::math::floor(value); }; + + /** + Ceils the value of `value`. + @param value Value + @return `value` ceiled + */ + fn ceil(auto value) { return builtin::std::math::ceil(value); }; + + /** + Rounds the value of `value`. + @param value Value + @return `value` rounded + */ + fn round(auto value) { return builtin::std::math::round(value); }; + + /** + Truncates the value of `value`. + @param value Value + @return `value` truncated + */ + fn trunc(auto value) { return builtin::std::math::trunc(value); }; + + + /** + Calculates the logarithm of `value` with base 10. + @param value Value + @return Logarithm of `value` with base 10 + */ + fn log10(auto value) { return builtin::std::math::log10(value); }; + + /** + Calculates the logarithm of `value` with base 2. + @param value Value + @return Logarithm of `value` with base 2 + */ + fn log2(auto value) { return builtin::std::math::log2(value); }; + + /** + Calculates the natural logarithm of `value`. + @param value Value + @return Logarithm of `value` with base `e` + */ + fn ln(auto value) { return builtin::std::math::ln(value); }; + + /** + Calculates the floating point modulus of `value`. + @param value Value + @return Floating point modulus of `value` + */ + fn fmod(auto value) { return builtin::std::math::fmod(value); }; + + /** + Calculates the value of `base` raised to the power of `exp`. + @param base Base + @param exp Exponent + @return `base` raised to the power of `exp` + */ + fn pow(auto base, auto exp) { return builtin::std::math::pow(base, exp); }; + + /** + Calculates the value of the natural number `e` raised to the power of `value`. + @param value Exponent + @return `e` raised to the power of `value` + */ fn exp(auto value) { return builtin::std::math::exp(value); }; - /** - Calculates the square root of `value`. - @param value Value - @return Square root of `value` - */ - fn sqrt(auto value) { return builtin::std::math::sqrt(value); }; + /** + Calculates the square root of `value`. + @param value Value + @return Square root of `value` + */ + fn sqrt(auto value) { return builtin::std::math::sqrt(value); }; - /** - Calculates the cubic root of `value`. - @param value Value - @return Cubic root of `value` - */ - fn cbrt(auto value) { return builtin::std::math::cbrt(value); }; + /** + Calculates the cubic root of `value`. + @param value Value + @return Cubic root of `value` + */ + fn cbrt(auto value) { return builtin::std::math::cbrt(value); }; - /** - Calculates the sine of `value`. - @param value Angle value in radians - @return Sine of `value` - */ - fn sin(auto value) { return builtin::std::math::sin(value); }; + /** + Calculates the sine of `value`. + @param value Angle value in radians + @return Sine of `value` + */ + fn sin(auto value) { return builtin::std::math::sin(value); }; - /** - Calculates the cosine of `value`. - @param value Angle value in radians - @return Cosine of `value` - */ - fn cos(auto value) { return builtin::std::math::cos(value); }; + /** + Calculates the cosine of `value`. + @param value Angle value in radians + @return Cosine of `value` + */ + fn cos(auto value) { return builtin::std::math::cos(value); }; - /** - Calculates the tangent of `value`. - @param value Angle value in radians - @return Tangent of `value` - */ - fn tan(auto value) { return builtin::std::math::tan(value); }; + /** + Calculates the tangent of `value`. + @param value Angle value in radians + @return Tangent of `value` + */ + fn tan(auto value) { return builtin::std::math::tan(value); }; - /** - Calculates the arc sine of `value`. - @param value Angle value in radians - @return Arc sine of `value` - */ - fn asin(auto value) { return builtin::std::math::asin(value); }; + /** + Calculates the arc sine of `value`. + @param value Angle value in radians + @return Arc sine of `value` + */ + fn asin(auto value) { return builtin::std::math::asin(value); }; - /** - Calculates the arc cosine of `value`. - @param value Value - @return Arc cosine of `value` in radians - */ - fn acos(auto value) { return builtin::std::math::acos(value); }; + /** + Calculates the arc cosine of `value`. + @param value Value + @return Arc cosine of `value` in radians + */ + fn acos(auto value) { return builtin::std::math::acos(value); }; - /** - Calculates the arc tangent of `value`. - @param value Value - @return Arc tangent of `value` in radians between `-pi/2` and `pi/2` - */ - fn atan(auto value) { return builtin::std::math::atan(value); }; + /** + Calculates the arc tangent of `value`. + @param value Value + @return Arc tangent of `value` in radians between `-pi/2` and `pi/2` + */ + fn atan(auto value) { return builtin::std::math::atan(value); }; - /** - Calculates the arc tangent of `value`. - @param y Value representing the proportion of the y-coordinate - @param x Value representing the proportion of the x-coordinate. - @return Arc tangent of `value` in radians between `-pi` and `pi` - */ - fn atan2(auto y, auto x) { return builtin::std::math::atan2(y, x); }; + /** + Calculates the arc tangent of `value`. + @param y Value representing the proportion of the y-coordinate + @param x Value representing the proportion of the x-coordinate. + @return Arc tangent of `value` in radians between `-pi` and `pi` + */ + fn atan2(auto y, auto x) { return builtin::std::math::atan2(y, x); }; - /** - Calculates the hyperbolic sine of `value`. - @param value Angle value in radians - @return Hyperbolic sine of `value` - */ - fn sinh(auto value) { return builtin::std::math::sinh(value); }; + /** + Calculates the hyperbolic sine of `value`. + @param value Angle value in radians + @return Hyperbolic sine of `value` + */ + fn sinh(auto value) { return builtin::std::math::sinh(value); }; - /** - Calculates the hyperbolic cosine of `value`. - @param value Angle value in radians - @return Hyperbolic cosine of `value` - */ - fn cosh(auto value) { return builtin::std::math::cosh(value); }; + /** + Calculates the hyperbolic cosine of `value`. + @param value Angle value in radians + @return Hyperbolic cosine of `value` + */ + fn cosh(auto value) { return builtin::std::math::cosh(value); }; - /** - Calculates the hyperbolic tangent of `value`. - @param value Angle value in radians - @return Hyperbolic tangent of `value` - */ - fn tanh(auto value) { return builtin::std::math::tanh(value); }; + /** + Calculates the hyperbolic tangent of `value`. + @param value Angle value in radians + @return Hyperbolic tangent of `value` + */ + fn tanh(auto value) { return builtin::std::math::tanh(value); }; - /** - Calculates the arc hyperbolic sine of `value`. - @param value Value - @return Arc hyperbolic sine of `value` - */ - fn asinh(auto value) { return builtin::std::math::asinh(value); }; + /** + Calculates the arc hyperbolic sine of `value`. + @param value Value + @return Arc hyperbolic sine of `value` + */ + fn asinh(auto value) { return builtin::std::math::asinh(value); }; - /** - Calculates the arc hyperbolic cosine of `value`. - @param value Value - @return Arc hyperbolic cosine of `value` - */ - fn acosh(auto value) { return builtin::std::math::acosh(value); }; + /** + Calculates the arc hyperbolic cosine of `value`. + @param value Value + @return Arc hyperbolic cosine of `value` + */ + fn acosh(auto value) { return builtin::std::math::acosh(value); }; - /** - Calculates the arc hyperbolic tangent of `value`. - @param value Value - @return Arc hyperbolic tangent of `value` - */ - fn atanh(auto value) { return builtin::std::math::atanh(value); }; + /** + Calculates the arc hyperbolic tangent of `value`. + @param value Value + @return Arc hyperbolic tangent of `value` + */ + fn atanh(auto value) { return builtin::std::math::atanh(value); }; - /** - Options to use with the `std::math::accumulate` function. - */ - enum AccumulateOperation : u8 { - Add = 0, - Multiply = 1, - Modulo = 2, - Min = 3, - Max = 4 - }; + /** + Options to use with the `std::math::accumulate` function. + */ + enum AccumulateOperation : u8 { + Add = 0, + Multiply = 1, + Modulo = 2, + Min = 3, + Max = 4 + }; - /** - Calculates the sum of all values in the specified memory range. - @param start Start address - @param end End address - @param valueSize Size of each value in bytes - @param [section] Section to use - @param [operation] Operation to use. Defaults to addition - @param [endian] Endianness to use. Defaults to native - @return Sum of all values in the specified memory range - */ - fn accumulate(u128 start, u128 end, u128 valueSize, std::mem::Section section = 0, AccumulateOperation operation = AccumulateOperation::Add, std::mem::Endian endian = std::mem::Endian::Native) { - return builtin::std::math::accumulate(start, end, valueSize, section, u128(operation), u128(endian)); - }; - + /** + Calculates the sum of all values in the specified memory range. + @param start Start address + @param end End address + @param valueSize Size of each value in bytes + @param [section] Section to use + @param [operation] Operation to use. Defaults to addition + @param [endian] Endianness to use. Defaults to native + @return Sum of all values in the specified memory range + */ + fn accumulate(u128 start, u128 end, u128 valueSize, std::mem::Section section = 0, AccumulateOperation operation = AccumulateOperation::Add, std::mem::Endian endian = std::mem::Endian::Native) { + return builtin::std::math::accumulate(start, end, valueSize, section, u128(operation), u128(endian)); + }; + } diff --git a/includes/std/string.pat b/includes/std/string.pat index 24442d4..cb37780 100644 --- a/includes/std/string.pat +++ b/includes/std/string.pat @@ -16,7 +16,7 @@ namespace auto std::string { struct SizedStringBase { SizeType size; DataType data[size]; - } [[sealed, format("std::string::impl::format_sized_string"), transform("std::string::impl::format_sized_string")]]; + } [[sealed, format("std::string::impl::format_string"), transform("std::string::impl::format_string")]]; /** A ASCII string with a prefixed size. @@ -36,7 +36,7 @@ namespace auto std::string { */ struct NullStringBase { DataType string[while($[$] != 0x00)]; - } [[format("std::string::impl::format_null_string")]]; + } [[sealed, format("std::string::impl::format_string"), transform("std::string::impl::format_string")]]; /** A null-terminated ASCII string. @@ -50,14 +50,10 @@ namespace auto std::string { namespace impl { - fn format_sized_string(ref auto string) { + fn format_string(ref auto string) { return string.data; }; - fn format_null_string(ref auto null_string) { - return null_string.string; - }; - } /** diff --git a/includes/std/time.pat b/includes/std/time.pat index 2b8673d..165be37 100644 --- a/includes/std/time.pat +++ b/includes/std/time.pat @@ -3,198 +3,198 @@ import std.io; /*! - Library to handle time and date related operations. + Library to handle time and date related operations. */ namespace auto std::time { - /** - A structured representation of a time and date. - */ - struct Time { - u8 sec; - u8 min; - u8 hour; - u8 mday; - u8 mon; - u16 year; - u8 wday; - u16 yday; - bool isdst; - } [[sealed]]; + /** + A structured representation of a time and date. + */ + struct Time { + u8 sec; + u8 min; + u8 hour; + u8 mday; + u8 mon; + u16 year; + u8 wday; + u16 yday; + bool isdst; + } [[sealed]]; - /** - A helper type to convert between Time and u128. - */ - union TimeConverter { - Time time; - u128 value; - }; + /** + A helper type to convert between Time and u128. + */ + union TimeConverter { + Time time; + u128 value; + }; - /** - A type to represent a time in seconds since the epoch. - */ - using EpochTime = u32; + /** + A type to represent a time in seconds since the epoch. + */ + using EpochTime = u32; - /** - A type to represent a time zone. - */ - enum TimeZone : u8 { - Local, - UTC - }; + /** + A type to represent a time zone. + */ + enum TimeZone : u8 { + Local, + UTC + }; - /** - A type to represent a DOS date. - */ - bitfield DOSDate { - day: 5; - month: 4; - year: 7; - } [[sealed]]; + /** + A type to represent a DOS date. + */ + bitfield DOSDate { + day: 5; + month: 4; + year: 7; + } [[sealed]]; - /** - A type to represent a DOS time. - */ - bitfield DOSTime { - seconds: 5; - minutes: 6; - hours: 5; - } [[sealed]]; + /** + A type to represent a DOS time. + */ + bitfield DOSTime { + seconds: 5; + minutes: 6; + hours: 5; + } [[sealed]]; - namespace impl { + namespace impl { - union DOSDateConverter { - DOSDate date; - u16 value; - }; + union DOSDateConverter { + DOSDate date; + u16 value; + }; - union DOSTimeConverter { - DOSTime time; - u16 value; - }; + union DOSTimeConverter { + DOSTime time; + u16 value; + }; - } + } - /** - Returns the current time in seconds since the epoch. - @return The current time in seconds since the epoch. - */ - fn epoch() { - return builtin::std::time::epoch(); - }; + /** + Returns the current time in seconds since the epoch. + @return The current time in seconds since the epoch. + */ + fn epoch() { + return builtin::std::time::epoch(); + }; - /** - Converts a time in seconds since the epoch to a local time. - @param epoch_time The time in seconds since the epoch. - @return The local time. - */ - fn to_local(EpochTime epoch_time) { - le TimeConverter converter; + /** + Converts a time in seconds since the epoch to a local time. + @param epoch_time The time in seconds since the epoch. + @return The local time. + */ + fn to_local(EpochTime epoch_time) { + le TimeConverter converter; - converter.value = builtin::std::time::to_local(epoch_time); + converter.value = builtin::std::time::to_local(epoch_time); - return converter.time; - }; + return converter.time; + }; - /** - Converts a time in seconds since the epoch to a UTC time. - @param epoch_time The time in seconds since the epoch. - @return The UTC time. - */ - fn to_utc(EpochTime epoch_time) { - le TimeConverter converter; + /** + Converts a time in seconds since the epoch to a UTC time. + @param epoch_time The time in seconds since the epoch. + @return The UTC time. + */ + fn to_utc(EpochTime epoch_time) { + le TimeConverter converter; - converter.value = builtin::std::time::to_utc(epoch_time); + converter.value = builtin::std::time::to_utc(epoch_time); - return converter.time; - }; + return converter.time; + }; - /** - Queries the current time in the specified time zone. - @param [time_zone] The time zone to query. Defaults to local. - @return The current time in the specified time zone. - */ - fn now(TimeZone time_zone = TimeZone::Local) { - le TimeConverter converter; + /** + Queries the current time in the specified time zone. + @param [time_zone] The time zone to query. Defaults to local. + @return The current time in the specified time zone. + */ + fn now(TimeZone time_zone = TimeZone::Local) { + le TimeConverter converter; - if (time_zone == TimeZone::Local) - converter.value = builtin::std::time::to_local(std::time::epoch()); - else if (time_zone == TimeZone::UTC) - converter.value = builtin::std::time::to_utc(std::time::epoch()); - else - converter.value = 0x00; + if (time_zone == TimeZone::Local) + converter.value = builtin::std::time::to_local(std::time::epoch()); + else if (time_zone == TimeZone::UTC) + converter.value = builtin::std::time::to_utc(std::time::epoch()); + else + converter.value = 0x00; - return converter.time; - }; + return converter.time; + }; - /** - Converts a value to a DOS date. - @param value The value to convert. - @return The DOS date. - */ - fn to_dos_date(u16 value) { - le impl::DOSDateConverter converter; + /** + Converts a value to a DOS date. + @param value The value to convert. + @return The DOS date. + */ + fn to_dos_date(u16 value) { + le impl::DOSDateConverter converter; - converter.value = value; + converter.value = value; - return converter.date; - }; + return converter.date; + }; - /** - Converts a value to a DOS time. - @param value The value to convert. - @return The DOS time. - */ - fn to_dos_time(u16 value) { - le impl::DOSTimeConverter converter; + /** + Converts a value to a DOS time. + @param value The value to convert. + @return The DOS time. + */ + fn to_dos_time(u16 value) { + le impl::DOSTimeConverter converter; - converter.value = value; + converter.value = value; - return converter.time; - }; + return converter.time; + }; - /** - Converts a FILETIME to unix time. - @param value The value to convert. - @return Timestamp formatted as unix time. - */ - fn filetime_to_unix(u64 value) { - return value / 10000000 - 11644473600; - }; + /** + Converts a FILETIME to unix time. + @param value The value to convert. + @return Timestamp formatted as unix time. + */ + fn filetime_to_unix(u64 value) { + return value / 10000000 - 11644473600; + }; - /** - Formats a time according to the specified format string. - @param time The time to format. - @param [format_string] The format string to use. Defaults to "%c". - @return The formatted time. - */ - fn format(Time time, str format_string = "%c") { - le TimeConverter converter; - converter.time = time; + /** + Formats a time according to the specified format string. + @param time The time to format. + @param [format_string] The format string to use. Defaults to "%c". + @return The formatted time. + */ + fn format(Time time, str format_string = "%c") { + le TimeConverter converter; + converter.time = time; - return builtin::std::time::format(format_string, converter.value); - }; + return builtin::std::time::format(format_string, converter.value); + }; - /** - Formats a DOS date according to the specified format string. - @param date The DOS date to format. - @param [format_string] The format string to use. Defaults to "{}/{}/{}". - @return The formatted DOS date. - */ - fn format_dos_date(DOSDate date, str format_string = "{}/{}/{}") { - return std::format(format_string, date.day, date.month, date.year + 1980); - }; + /** + Formats a DOS date according to the specified format string. + @param date The DOS date to format. + @param [format_string] The format string to use. Defaults to "{}/{}/{}". + @return The formatted DOS date. + */ + fn format_dos_date(DOSDate date, str format_string = "{}/{}/{}") { + return std::format(format_string, date.day, date.month, date.year + 1980); + }; - /** - Formats a DOS time according to the specified format string. - @param time The DOS time to format. - @param [format_string] The format string to use. Defaults to "{:02}:{:02}:{:02}". - @return The formatted DOS time. - */ - fn format_dos_time(DOSTime time, str format_string = "{:02}:{:02}:{:02}") { - return std::format(format_string, time.hours, time.minutes, time.seconds * 2); - }; + /** + Formats a DOS time according to the specified format string. + @param time The DOS time to format. + @param [format_string] The format string to use. Defaults to "{:02}:{:02}:{:02}". + @return The formatted DOS time. + */ + fn format_dos_time(DOSTime time, str format_string = "{:02}:{:02}:{:02}") { + return std::format(format_string, time.hours, time.minutes, time.seconds * 2); + }; } diff --git a/includes/type/base.pat b/includes/type/base.pat index 4b44b1d..e310d37 100644 --- a/includes/type/base.pat +++ b/includes/type/base.pat @@ -34,7 +34,7 @@ namespace auto type { */ using Bin = T [[format("type::impl::format_bin")]]; - namespace impl { + namespace impl { fn format_number(auto value, str fmt) { bool negative = value < 0; @@ -50,6 +50,6 @@ namespace auto type { fn format_dec(auto value) { return type::impl::format_number(value, "{}"); }; fn format_bin(auto value) { return type::impl::format_number(value, "0b{:08b}"); }; - } + } } diff --git a/includes/type/byte.pat b/includes/type/byte.pat index 7e3f8a5..1682e96 100644 --- a/includes/type/byte.pat +++ b/includes/type/byte.pat @@ -40,7 +40,7 @@ namespace auto type { } [[format("type::impl::format_byte"), single_color]]; - namespace impl { + namespace impl { fn format_byte(Byte byte) { return std::format("0x{0:02X} (0b{1:08b}) LSB:{2}, MSB:{3}", @@ -66,6 +66,6 @@ namespace auto type { return std::format("{{ {0:0X}, {1:0X} }}", nibbles.high, nibbles.low); }; - } + } } \ No newline at end of file diff --git a/includes/type/float16.pat b/includes/type/float16.pat index 5a2cfa4..1d5008a 100644 --- a/includes/type/float16.pat +++ b/includes/type/float16.pat @@ -22,7 +22,7 @@ namespace auto type { float floatValue; }; - fn format_float16(float16 value) { + fn format_float16(float16 value) { u32 sign = value >> 15; u32 exponent = (value >> 10) & 0x1F; u32 mantissa = value & 0x3FF; diff --git a/includes/type/guid.pat b/includes/type/guid.pat index 32a04fe..b14de1a 100644 --- a/includes/type/guid.pat +++ b/includes/type/guid.pat @@ -3,15 +3,15 @@ import std.io; /*! - Types to deal with UUIDs (Universally Unique Identifiers) / GUIDs (Globally Unique Identifiers) as described in RFC 4122 + Types to deal with UUIDs (Universally Unique Identifiers) / GUIDs (Globally Unique Identifiers) as described in RFC 4122 */ namespace auto type { - /** - Type representing a GUID value - */ - struct GUID { + /** + Type representing a GUID value + */ + struct GUID { u32 time_low; u16 time_mid; u16 time_high_and_version; @@ -20,31 +20,31 @@ namespace auto type { u8 node[6]; } [[sealed, format("type::impl::format_guid")]]; - /** - Alias name for GUID - */ - using UUID = GUID; + /** + Alias name for GUID + */ + using UUID = GUID; - namespace impl { + namespace impl { - fn format_guid(GUID guid) { - bool valid = ((le u16(guid.time_high_and_version) >> 12) <= 5) && (((guid.clock_seq_and_reserved >> 4) >= 8) || ((guid.clock_seq_and_reserved >> 4) == 0)); - - return std::format("{}{{{:08X}-{:04X}-{:04X}-{:02X}{:02X}-{:02X}{:02X}{:02X}{:02X}{:02X}{:02X}}}", - valid ? "" : "Invalid ", - le u32(guid.time_low), - le u16(guid.time_mid), - le u16(guid.time_high_and_version), - guid.clock_seq_and_reserved, - guid.clock_seq_low, - guid.node[0], - guid.node[1], - guid.node[2], - guid.node[3], - guid.node[4], - guid.node[5]); - }; + fn format_guid(GUID guid) { + bool valid = ((le u16(guid.time_high_and_version) >> 12) <= 5) && (((guid.clock_seq_and_reserved >> 4) >= 8) || ((guid.clock_seq_and_reserved >> 4) == 0)); + + return std::format("{}{{{:08X}-{:04X}-{:04X}-{:02X}{:02X}-{:02X}{:02X}{:02X}{:02X}{:02X}{:02X}}}", + valid ? "" : "Invalid ", + le u32(guid.time_low), + le u16(guid.time_mid), + le u16(guid.time_high_and_version), + guid.clock_seq_and_reserved, + guid.clock_seq_low, + guid.node[0], + guid.node[1], + guid.node[2], + guid.node[3], + guid.node[4], + guid.node[5]); + }; - } + } } \ No newline at end of file diff --git a/includes/type/leb128.pat b/includes/type/leb128.pat index c292a5b..2e95f85 100644 --- a/includes/type/leb128.pat +++ b/includes/type/leb128.pat @@ -4,69 +4,69 @@ import std.io; import std.mem; /*! - Types used to decode Little Endian Base 128 numbers used to store large numbers as space efficiently as possible + Types used to decode Little Endian Base 128 numbers used to store large numbers as space efficiently as possible */ namespace auto type { - - /** - Base LEB128 type. Use `uLEB128` and `sLEB128` instead. - */ - struct LEB128Base { - u8 array[while($ == addressof(this) || std::mem::read_unsigned($-1, 1) & 0x80 != 0)] [[hidden]]; - } [[sealed]]; + + /** + Base LEB128 type. Use `uLEB128` and `sLEB128` instead. + */ + struct LEB128Base { + u8 array[while($ == addressof(this) || std::mem::read_unsigned($-1, 1) & 0x80 != 0)] [[hidden]]; + } [[sealed]]; - /** - A unsigned variant of a LEB128 number - */ - using uLEB128 = LEB128Base [[format("type::impl::format_uleb128"), transform("type::impl::transform_uleb128")]]; + /** + A unsigned variant of a LEB128 number + */ + using uLEB128 = LEB128Base [[format("type::impl::format_uleb128"), transform("type::impl::transform_uleb128")]]; - /** - A signed variant of a LEB128 number - */ - using sLEB128 = LEB128Base [[format("type::impl::format_sleb128"), transform("type::impl::transform_sleb128")]]; + /** + A signed variant of a LEB128 number + */ + using sLEB128 = LEB128Base [[format("type::impl::format_sleb128"), transform("type::impl::transform_sleb128")]]; - /** - Legacy alias for uLEB128 - */ - using LEB128 = uLEB128; - - namespace impl { - - fn transform_uleb128_array(ref auto array) { - u128 res = array[0] & 0x7f; - for(u8 i = 1, array[i-1] & 0x80 != 0, i+=1) { - res |= u128(array[i] & 0x7f) << 7 * i; - } - return res; - }; + /** + Legacy alias for uLEB128 + */ + using LEB128 = uLEB128; + + namespace impl { + + fn transform_uleb128_array(ref auto array) { + u128 res = array[0] & 0x7f; + for(u8 i = 1, array[i-1] & 0x80 != 0, i+=1) { + res |= u128(array[i] & 0x7f) << 7 * i; + } + return res; + }; - fn transform_sleb128_array(ref auto array) { - s128 res = type::impl::transform_uleb128_array(array); - if (res & 0x40 != 0) { - res |= ~0 << (sizeof(array) / sizeof(u8)) * 7; - } - return res; - }; - - fn format_uleb128(ref auto leb128) { - u128 res = type::impl::transform_uleb128_array(leb128.array); - return std::format("{} ({:#x})", res, res); - }; - - fn transform_uleb128(ref auto leb128) { - return type::impl::transform_uleb128_array(leb128.array); - }; + fn transform_sleb128_array(ref auto array) { + s128 res = type::impl::transform_uleb128_array(array); + if (res & 0x40 != 0) { + res |= ~0 << (sizeof(array) / sizeof(u8)) * 7; + } + return res; + }; + + fn format_uleb128(ref auto leb128) { + u128 res = type::impl::transform_uleb128_array(leb128.array); + return std::format("{} ({:#x})", res, res); + }; + + fn transform_uleb128(ref auto leb128) { + return type::impl::transform_uleb128_array(leb128.array); + }; - fn format_sleb128(ref auto leb128) { - s128 res = type::impl::transform_sleb128_array(leb128.array); - return std::format("{} ({:#x})", res, res); - }; - - fn transform_sleb128(ref auto leb128) { - return type::impl::transform_sleb128_array(leb128.array); - }; + fn format_sleb128(ref auto leb128) { + s128 res = type::impl::transform_sleb128_array(leb128.array); + return std::format("{} ({:#x})", res, res); + }; + + fn transform_sleb128(ref auto leb128) { + return type::impl::transform_sleb128_array(leb128.array); + }; - } - + } + } diff --git a/includes/type/mac.pat b/includes/type/mac.pat index 9592005..040c1ca 100644 --- a/includes/type/mac.pat +++ b/includes/type/mac.pat @@ -3,30 +3,30 @@ import std.io; /*! - Types used to decode MAC Addresses + Types used to decode MAC Addresses */ namespace auto type { - /** - A MAC Address as used in the Internet Protocol - */ - struct MACAddress { - u8 bytes[6]; - } [[sealed, format("type::impl::format_mac_address")]]; + /** + A MAC Address as used in the Internet Protocol + */ + struct MACAddress { + u8 bytes[6]; + } [[sealed, format("type::impl::format_mac_address")]]; - namespace impl { + namespace impl { fn format_mac_address(MACAddress address) { - return std::format("{:02X}:{:02X}:{:02X}:{:02X}:{:02X}:{:02X}", - address.bytes[0], - address.bytes[1], - address.bytes[2], - address.bytes[3], - address.bytes[4], - address.bytes[5]); - }; + return std::format("{:02X}:{:02X}:{:02X}:{:02X}:{:02X}:{:02X}", + address.bytes[0], + address.bytes[1], + address.bytes[2], + address.bytes[3], + address.bytes[4], + address.bytes[5]); + }; - } + } } \ No newline at end of file diff --git a/includes/type/path.pat b/includes/type/path.pat index fa8cb3d..d91ebe0 100644 --- a/includes/type/path.pat +++ b/includes/type/path.pat @@ -1,53 +1,53 @@ import std.mem; /*! - Types dealing with various kinds of resource paths + Types dealing with various kinds of resource paths */ namespace auto type { - /** - Type representing a single path segment. Use the `Path` type instead of using this on its own - @tparam Delimeter The delimeter sequence used to separate two path segments - */ - struct PathSegment { - char string[while(std::mem::read_string($, std::string::length(Delimeter)) != Delimeter && std::mem::read_unsigned($, 1) != 0x00)]; - char separator [[hidden]]; - - if (separator == 0x00) { - $ -= 1; - break; - } - } [[sealed, format("type::impl::format_path_segment")]]; - - /** - A generic type representing a path with an arbitrary delimeter - @tparam Delimeter The delimeter sequence used to separate two path segments - */ - struct Path { - PathSegment segments[while(true)]; - } [[format("type::impl::format_path")]]; - - /** - A type representing a Unix path using a '/' forwardslash as delimeter - */ - using UnixPath = Path<"/">; + /** + Type representing a single path segment. Use the `Path` type instead of using this on its own + @tparam Delimeter The delimeter sequence used to separate two path segments + */ + struct PathSegment { + char string[while(std::mem::read_string($, std::string::length(Delimeter)) != Delimeter && std::mem::read_unsigned($, 1) != 0x00)]; + char separator [[hidden]]; + + if (separator == 0x00) { + $ -= 1; + break; + } + } [[sealed, format("type::impl::format_path_segment")]]; + + /** + A generic type representing a path with an arbitrary delimeter + @tparam Delimeter The delimeter sequence used to separate two path segments + */ + struct Path { + PathSegment segments[while(true)]; + } [[format("type::impl::format_path")]]; + + /** + A type representing a Unix path using a '/' forwardslash as delimeter + */ + using UnixPath = Path<"/">; - /** - A type representing a DOS path using a '\' backslash as delimeter - */ - using DOSPath = Path<"\\">; - - namespace impl { - - fn format_path_segment(ref auto segment) { - return segment.string; - }; - - fn format_path(ref auto path) { - return std::mem::read_string($, sizeof(path)); - }; - - } + /** + A type representing a DOS path using a '\' backslash as delimeter + */ + using DOSPath = Path<"\\">; + + namespace impl { + + fn format_path_segment(ref auto segment) { + return segment.string; + }; + + fn format_path(ref auto path) { + return std::mem::read_string($, sizeof(path)); + }; + + } }