|
JH-Toolkit v1.4.1
An engineering-oriented C++20 toolkit with duck-typed concepts, static design, async coroutines, and semantic containers — header-only, RTTI-free, and concurrency-friendly.
|
Compile-time string wrapper for use as a non-type template parameter (NTTP). More...
#include <jh/metax/t_str.h>
Public Types | |
| using | c_hash = jh::meta::c_hash |
Public Member Functions | |
| constexpr | t_str (const jh::pod::array< char, N > &arr) noexcept |
| build from underlying buffer | |
| constexpr | t_str (const char(&lit)[N]) noexcept |
| Construct from a regular string literal. | |
| constexpr | t_str (const char8_t(&lit)[N]) noexcept |
Construct from a char8_t-based string literal (u8""). | |
| constexpr const char * | val () const noexcept |
| Get a pointer to the stored string. | |
| constexpr std::uint64_t | size () const noexcept |
| Get the length of the string (excluding null terminator). | |
| constexpr std::string_view | view () const noexcept |
Get a std::string_view over the stored string. | |
| constexpr jh::pod::string_view | pod_view () const noexcept |
Get a jh::pod::string_view over the stored string. | |
| std::string | str () const |
Get a std::string as a copy of the stored string. | |
| constexpr std::uint64_t | hash (c_hash hash_method=c_hash::fnv1a64, bool include_null=false) const noexcept |
| Compute a constexpr hash of the stored string. | |
| template<std::uint16_t M> | |
| constexpr auto | operator+ (const t_str< M > &other) const noexcept |
Concatenate two t_str strings at compile time. | |
| template<std::uint16_t Pos, std::uint16_t Count = npos> | |
| constexpr auto | sub () const noexcept |
| Extract a substring at compile time. | |
| template<std::uint16_t Pos, std::uint16_t Count = npos> | |
| constexpr std::string_view | sub_view () const noexcept |
Obtain a std::string_view over a substring. | |
| template<std::uint16_t Pos, std::uint16_t Count = npos> | |
| constexpr jh::pod::string_view | sub_pod_view () const noexcept |
Obtain a jh::pod::string_view over a substring. | |
| constexpr bool | is_digit () const noexcept |
| Check if all characters are decimal digits (0-9). | |
| constexpr bool | is_number () const noexcept |
| Check if the string represents a valid decimal number. | |
| constexpr bool | is_alpha () const noexcept |
| Check if all characters are alphabetic (A-Z, a-z). | |
| constexpr bool | is_alnum () const noexcept |
| Check if all characters are alphanumeric (letters or digits). | |
| constexpr bool | is_ascii () const noexcept |
| Check if all characters are 7-bit ASCII. | |
| constexpr bool | is_printable_ascii () const noexcept |
| Check if all characters are printable 7-bit ASCII. | |
| constexpr bool | is_legal () const noexcept |
| Check if all characters are valid (printable ASCII or UTF-8). | |
| constexpr bool | is_hex () const noexcept |
| Check if the string is a valid hexadecimal sequence. | |
| constexpr bool | is_base64 () const noexcept |
| Check if the string is valid Base64. | |
| constexpr bool | is_base64url () const noexcept |
| Check if the string is valid Base64URL. | |
| template<bool AllowParent = false> | |
| constexpr bool | is_valid_relative_path () const noexcept |
| Validate a POSIX-style relative path at compile time. | |
| constexpr auto | to_upper () const noexcept |
| Convert all alphabetic characters to uppercase (A-Z). | |
| constexpr auto | to_lower () const noexcept |
| Convert all alphabetic characters to lowercase (a-z). | |
| constexpr auto | flip_case () const noexcept |
| Toggle the case of all alphabetic characters. | |
| template<std::uint16_t M> requires (M != N) | |
| constexpr bool | operator== (const t_str< M > &) const noexcept |
Equality comparison with another t_str of different size. | |
| constexpr bool | operator== (const t_str &) const noexcept=default |
Equality comparison with another t_str of the same size. | |
| constexpr | operator jh::pod::array< std::uint8_t, N - 1 > () const noexcept |
| Convert the string (excluding the null terminator) to a byte array. | |
| constexpr jh::pod::array< std::uint8_t, N - 1 > | to_bytes () const noexcept |
| Convert to an immutable byte buffer. | |
Static Public Member Functions | |
| static constexpr t_str | from_bytes (const jh::pod::array< std::uint8_t, N - 1 > &bytes) noexcept |
Construct a t_str<N> from a byte buffer. | |
Public Attributes | |
| const jh::pod::array< char, N > | storage |
| Fixed-size storage for the compile-time string (null-terminated). | |
Static Public Attributes | |
| static constexpr auto | npos = static_cast<std::uint16_t>(-1) |
| Sentinel value representing "no position" or "until the end". | |
Friends | |
| template<std::uint16_t M> | |
| struct | t_str |
Friendship is required for implementing operator+. | |
Compile-time string wrapper for use as a non-type template parameter (NTTP).
| N | The size of the string literal including null terminator. |
t_str<N> enables string literals to be bound directly as non-type template parameters (NTTP) in C++20. It provides constexpr construction, validation, transformation, concatenation, and hashing of string literals with zero runtime overhead.
|
inlineconstexprnoexcept |
Construct from a regular string literal.
This constructor is intentionally implicit. It enables string literals to be passed directly as non-type template parameters (NTTP) without requiring additional wrappers.
"..."_ts cannot be supported due to a fundamental language limitation. const char* (and a length), but it cannot encode that length as a template argument N. t_str<N> requires the string size (including the null terminator) to be part of the type, the size must be preserved at the type level. const char(&)[N] retains the compile-time array bound required for correct template deduction.
|
inlineconstexprnoexcept |
Construct from a char8_t-based string literal (u8"").
This constructor provides compatibility for platforms or codebases where u8"" string literals yield const char8_t[]. Each element is converted to char for uniform storage.
This is a non-standard compatibility feature — since in most modern platforms, "" literals are already UTF-8 encoded. The intent is only to allow u8"" literals to be used seamlessly as NTTP, consistent with regular string literals.
|
inlinenodiscardconstexprnoexcept |
Toggle the case of all alphabetic characters.
t_str with each character's case flipped.
|
inlinestaticnodiscardconstexprnoexcept |
Construct a t_str<N> from a byte buffer.
| bytes | A jh::pod::array<std::uint8_t, N - 1> representing a binary buffer. The buffer does not contain a null terminator. |
t_str<N> whose characters are taken directly from bytes, with a null terminator appended internally.bytes as pure binary data. t_str is always null-terminated internally, because t_str is semantically a C-string wrapper. t_str from it. to_bytes(). Example Usage:
|
inlinenodiscardconstexprnoexcept |
Compute a constexpr hash of the stored string.
| hash_method | The hash algorithm to use (default: c_hash::fnv1a64). Supported algorithms:
|
| include_null | If true, the null terminator is included in the hash computation. |
|
inlinenodiscardconstexprnoexcept |
Check if all characters are alphanumeric (letters or digits).
|
inlinenodiscardconstexprnoexcept |
Check if all characters are alphabetic (A-Z, a-z).
|
inlinenodiscardconstexprnoexcept |
Check if all characters are 7-bit ASCII.
|
inlinenodiscardconstexprnoexcept |
Check if the string is valid Base64.
Length must be a multiple of 4, padding ('=') allowed at the end.
|
inlinenodiscardconstexprnoexcept |
Check if the string is valid Base64URL.
'=' padding is optional. If present, length must be a multiple of 4.
|
inlinenodiscardconstexprnoexcept |
Check if all characters are decimal digits (0-9).
is_number() instead.
|
inlinenodiscardconstexprnoexcept |
Check if the string is a valid hexadecimal sequence.
Length must be even, and all characters must be hex digits.
|
inlinenodiscardconstexprnoexcept |
Check if all characters are valid (printable ASCII or UTF-8).
|
inlinenodiscardconstexprnoexcept |
Check if the string represents a valid decimal number.
true if the string is a valid number, otherwise false.Grammar (simplified BNF):
[ '+' | '-' ] DIGIT+ [ '.' DIGIT+ ] [ ( 'e' | 'E' ) [ '+' | '-' ] DIGIT+ ]
Equivalent regular expression:
^[+-]?[0-9]+(.[0-9]+)?([eE][+-]?[0-9]+)?$
Rules:
'+' or '-'.
|
inlinenodiscardconstexprnoexcept |
Check if all characters are printable 7-bit ASCII.
Verifies that every character lies within the printable 7-bit ASCII range (decimal 32-126).
is_printable_ascii() implies is_ascii() is_ascii() again is redundant. When used inside a requires clause, do not combine the two checks. is_legal() instead. is_legal() performs:
|
inlinenodiscardconstexprnoexcept |
Validate a POSIX-style relative path at compile time.
This function performs strict validation of a POSIX-style relative path. It is intended for project-internal path specifications and is designed to be evaluated at compile time.
[1, 128]. '/'). '/') are allowed. "./" segments. ".." handling: AllowParent == false → any ".." segment is rejected. AllowParent == true → leading "../" segments are allowed, but: "../". ".." is permitted once normal path content begins. [A-Za-z0-9_.-/]. This function is intended to be used in constant evaluation contexts. It is typically combined with C++20 constraints:
Explicit form (disallow parent paths):
Default form (equivalent to <false>):
Since AllowParent defaults to false, both forms are strictly equivalent.
On Windows or other platforms, path composition should be performed using std::filesystem rather than embedding platform-specific separators:
The validated string is treated purely as a logical POSIX-style relative path and may be combined with platform-native paths via std::filesystem.
| AllowParent | Whether leading "../" segments are permitted. |
|
inlineexplicitnodiscardconstexprnoexcept |
Convert the string (excluding the null terminator) to a byte array.
jh::pod::array<std::uint8_t, N - 1> containing the string's raw character bytes, excluding the null terminator.N - 1 bytes — the effective string length. std::memcpy is used for maximum efficiency.
|
inlinenodiscardconstexprnoexcept |
Concatenate two t_str strings at compile time.
| M | Size (including null terminator) of the right-hand operand. |
| other | Another t_str<M> to append. |
t_str whose size is (N - 1) + (M - 1) + 1, containing the concatenated characters and a null terminator.t_str_concat_legal (≤ 16 KB).
|
constexprdefaultnoexcept |
Equality comparison with another t_str of the same size.
| other | The other t_str<N>. |
true if and only if all characters match (including the null terminator).This operator is = default, meaning comparison is delegated to the underlying member const jh::pod::array<char, N> storage.
storage is a POD type, the compiler can optimize this into a direct memcmp-style comparison at compile time or runtime.
|
inlineconstexprnoexcept |
Equality comparison with another t_str of different size.
| M | Size of the other t_str. |
| Unused | The other string (ignored, since size mismatch short-circuits). |
false always, because string sizes differ.N != M, the comparison does not even check characters.
|
inlinenodiscardconstexprnoexcept |
Get a jh::pod::string_view over the stored string.
pod::string_view referencing the characters (excluding null terminator).
|
inlinenodiscardconstexprnoexcept |
Get the length of the string (excluding null terminator).
|
inlinenodiscard |
Get a std::string as a copy of the stored string.
std::string copying the template string.std::string (e.g. concatenation or formatting), it avoids forcing users to repeatedly write:Instead, S.str() provides a concise and explicit conversion entry point.
The function intentionally performs a copy and is meant only for runtime interop — it does not affect the compile-time nature of t_str.
|
inlinenodiscardconstexprnoexcept |
Extract a substring at compile time.
| Pos | Starting position of the substring. |
| Count | Number of characters to extract. If set to jh::meta::t_str<N>::npos, the substring extends from Pos to the end of the string. |
t_str containing the selected characters followed by a null terminator.ActualCount + 1 (including the null terminator). Count = npos acts as a sentinel meaning "until the end of the string". t_str_sub_legal. t_str object exists. t_str object. s.sub<Pos, Count>().pod_view() is valid but creates a temporary t_str that may lead to dangling views if not used carefully.
|
inlinenodiscardconstexprnoexcept |
Obtain a jh::pod::string_view over a substring.
| Pos | Starting position of the substring. |
| Count | Number of characters to expose. If set to jh::meta::t_str<N>::npos, the view extends from Pos to the end of the string. |
jh::pod::string_view referencing the selected range.sub_view(), but returns a POD-compatible view type. Count = npos means "until end". jh::pod::string_view.
|
inlinenodiscardconstexprnoexcept |
Obtain a std::string_view over a substring.
| Pos | Starting position of the substring. |
| Count | Number of characters to expose. If set to jh::meta::t_str<N>::npos, the view extends from Pos to the end of the string. |
std::string_view referencing the selected range.t_str. Count = npos means "until end". t_str object goes out of scope.
|
inlinenodiscardconstexprnoexcept |
Convert to an immutable byte buffer.
jh::pod::array<std::uint8_t, N - 1> containing the raw characters of the string (not null-terminated).This is equivalent to the explicit byte-array conversion operator.
|
inlinenodiscardconstexprnoexcept |
Convert all alphabetic characters to lowercase (a-z).
t_str with characters transformed to lowercase.
|
inlinenodiscardconstexprnoexcept |
Convert all alphabetic characters to uppercase (A-Z).
t_str with characters transformed to uppercase.
|
inlinenodiscardconstexprnoexcept |
Get a pointer to the stored string.
storage.data, which is a const char[N]. It decays to const char* on return.
|
inlinenodiscardconstexprnoexcept |
Get a std::string_view over the stored string.
string_view referencing the characters (excluding null terminator).
|
friend |
Friendship is required for implementing operator+.
| M | Size of the other string literal. |
Different t_str<M> instances must access each other's internal storage to perform constexpr concatenation.