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.
Loading...
Searching...
No Matches
jh::pod::string_view Struct Referencefinal

Read-only string view with POD layout. More...

#include <jh/pods/string_view.h>

Public Types

using value_type = char
 Character type.
using size_type = std::uint64_t
 Size type (64-bit).
using difference_type = std::ptrdiff_t
 Difference type.
using reference = value_type &
 Reference to character.
using const_reference = const value_type &
 Const reference to character.
using pointer = value_type *
 Pointer to character.
using const_pointer = const value_type *
 Const pointer to character.

Public Member Functions

constexpr const_reference operator[] (const std::uint64_t index) const noexcept
 Index access (no bounds checking).
constexpr const_pointer begin () const noexcept
 Pointer to the beginning of data.
constexpr const_pointer end () const noexcept
 Pointer to the end of data (data + len).
constexpr size_type size () const noexcept
 View length in bytes.
constexpr bool empty () const noexcept
 Whether the view is empty (len == 0).
constexpr bool operator== (const string_view &rhs) const noexcept
 Compare two views for byte-wise equality.
constexpr string_view sub (std::uint64_t offset, std::uint64_t length=npos) const noexcept
 Returns a substring starting at offset, for length bytes.
constexpr int compare (const string_view &rhs) const noexcept
 Lexical comparison (similar to strcmp()).
constexpr bool starts_with (const string_view &prefix) const noexcept
 Check whether this view starts with the given prefix.
constexpr bool ends_with (const string_view &suffix) const noexcept
 Check whether this view ends with the given suffix.
constexpr std::uint64_t find (const char ch) const noexcept
 Returns the index of the first occurrence of a character.
constexpr std::uint64_t hash (jh::meta::c_hash hash_method=jh::meta::c_hash::fnv1a64) const noexcept
 Hash the view content using a selectable non-cryptographic algorithm.
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.
void copy_to (char *buffer, const std::uint64_t max_len) const noexcept
 Copy the view into a C-style null-terminated buffer.
constexpr std::uint64_t semantic_len () const noexcept
 Returns the semantic length of the UTF-8 string.
constexpr operator std::string_view () const noexcept
 Explicit conversion to std::string_view.
constexpr std::string_view to_std () const noexcept
 Named conversion helper to obtain a std::string_view.
constexpr std::strong_ordering operator<=> (const string_view &rhs) const noexcept
 Three-way comparison operator (spaceship operator).

Static Public Member Functions

template<std::size_t N>
static constexpr string_view from_literal (const char(&lit)[N]) noexcept
 Construct a string_view from a string literal.

Public Attributes

const char * data
 Pointer to string data (not null-terminated).
std::uint64_t len
 Number of valid bytes in the view.

Static Public Attributes

static constexpr auto npos = static_cast<std::uint64_t>(-1)
 Sentinel value representing "no position" or "until the end".

Detailed Description

Read-only string view with POD layout.

Holds a raw pointer and a length (not null-terminated). Provides slicing, comparison, and constexpr hashing, all while remaining fully POD (const char* + uint64_t).

Key differences from std::string_view:

  • Always POD layout (aggregate struct, no constructors)
  • operator== performs deep comparison (memcmp)
  • hash() is constexpr-safe and usable in consteval contexts
  • No exceptions, no allocator awareness

Initialization semantics:

  • This type is an aggregate; initialization normally requires both a const char* and an explicit length.
  • If only a const char* is provided (without len), then len defaults to 0, producing an empty view.
  • A special helper from_literal() exists for string literals: it deduces the array size at compile time and creates a view of length N-1 (excluding the null terminator).
  • At runtime, you must always provide both pointer and size explicitly โ€” this class never computes length automatically.

Usage Model:

  • Typically used as a safe view for immutable_str
  • Can represent string literals or arena-allocated strings
  • Compile-time hashing supports jh::meta::t_str

Member Function Documentation

◆ compare()

int jh::pod::string_view::compare ( const string_view & rhs) const
inlinenodiscardconstexprnoexcept

Lexical comparison (similar to strcmp()).

Returns
<0 if this < rhs, 0 if equal, >0 if this > rhs.

◆ copy_to()

void jh::pod::string_view::copy_to ( char * buffer,
const std::uint64_t max_len ) const
inlinenoexcept

Copy the view into a C-style null-terminated buffer.

Warning
This is not POD-safe. Intended for debugging or interop only.
Parameters
bufferOutput character buffer.
max_lenMaximum bytes to write (including null terminator).

◆ end()

const_pointer jh::pod::string_view::end ( ) const
inlinenodiscardconstexprnoexcept

Pointer to the end of data (data + len).

Note
This is not null-terminated. Use len for bounds.

◆ find()

std::uint64_t jh::pod::string_view::find ( const char ch) const
inlinenodiscardconstexprnoexcept

Returns the index of the first occurrence of a character.

Parameters
chTarget character to search for.
Returns
Offset index if found, or -1 (as uint64_t) if not found.

◆ from_literal()

template<std::size_t N>
constexpr string_view jh::pod::string_view::from_literal ( const char(&) lit[N])
inlinestaticnodiscardconstexprnoexcept

Construct a string_view from a string literal.

Template Parameters
NSize of the string literal including the null terminator.
Parameters
litReference to the string literal (must be null-terminated).
Returns
A string_view pointing to the literal characters with size() == N - 1.

Semantics:

  • N always counts the null terminator.
  • The resulting view excludes the null terminator, so length is N - 1.
  • Empty string literal "" is valid (N == 1, view length = 0).
Note
This overload guarantees constexpr evaluation and can be used in consteval contexts.

◆ hash()

std::uint64_t jh::pod::string_view::hash ( jh::meta::c_hash hash_method = jh::meta::c_hash::fnv1a64) const
inlinenodiscardconstexprnoexcept

Hash the view content using a selectable non-cryptographic algorithm.

Provides stable 64-bit hashing over the view contents.

Parameters
hash_methodAlgorithm to use for hashing (default: fnv1a64).
Returns
64-bit hash of the view data, or -1 if data == nullptr.
Note
  • This is not cryptographic; do not use it for security-sensitive logic.
  • If data is null, the return value is -1 (sentinel).
  • Hashing is based only on contents and length, not on pointer identity.
  • Unlike bytes_view::hash, this function is valid in consteval contexts.
    • bytes_view relies on reinterpret_cast, so it cannot be evaluated at compile time.
    • string_view operates directly on characters, so compile-time hashing of string literals is both well-defined and semantically meaningful.
    • This design enables features such as jh::meta::t_str to compute hashes fully at compile time.

◆ is_alnum()

bool jh::pod::string_view::is_alnum ( ) const
inlinenodiscardconstexprnoexcept

Check if all characters are alphanumeric (letters or digits).

Returns
true if all characters are alphanumeric, false otherwise.

◆ is_alpha()

bool jh::pod::string_view::is_alpha ( ) const
inlinenodiscardconstexprnoexcept

Check if all characters are alphabetic (A-Z, a-z).

Returns
true if all characters are alphabetic, false otherwise.

◆ is_ascii()

bool jh::pod::string_view::is_ascii ( ) const
inlinenodiscardconstexprnoexcept

Check if all characters are 7-bit ASCII.

Returns
true if all characters are in range 0-127, false otherwise.

◆ is_base64()

bool jh::pod::string_view::is_base64 ( ) const
inlinenodiscardconstexprnoexcept

Check if the string is valid Base64.

Length must be a multiple of 4, padding ('=') allowed at the end.

Returns
true if valid Base64, false otherwise.

◆ is_base64url()

bool jh::pod::string_view::is_base64url ( ) const
inlinenodiscardconstexprnoexcept

Check if the string is valid Base64URL.

'=' padding is optional. If present, length must be a multiple of 4.

Returns
true if valid Base64URL, false otherwise.

◆ is_digit()

bool jh::pod::string_view::is_digit ( ) const
inlinenodiscardconstexprnoexcept

Check if all characters are decimal digits (0-9).

Note
This only checks that each character is a digit. To validate if the whole string represents a number (with optional sign, decimal point, or exponent), use is_number() instead.
Returns
true if all characters are digits, false otherwise.

◆ is_hex()

bool jh::pod::string_view::is_hex ( ) const
inlinenodiscardconstexprnoexcept

Check if the string is a valid hexadecimal sequence.

Length must be even, and all characters must be hex digits.

Returns
true if valid hex string, false otherwise.

◆ is_legal()

bool jh::pod::string_view::is_legal ( ) const
inlinenodiscardconstexprnoexcept

Check if all characters are valid (printable ASCII or UTF-8).

Returns
true if all characters are valid, false otherwise.

< constexpr, avoid using [[likely/unlikely]]

◆ is_number()

bool jh::pod::string_view::is_number ( ) const
inlinenodiscardconstexprnoexcept

Check if the string represents a valid decimal number.

Returns
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:

  • The first character may be '+' or '-'.
  • At least one digit must appear before optional '.' or 'e/E'.
  • If '.' appears, at least one digit must follow (either before or after '.').
  • If 'e' or 'E' appears, it must be followed by an optional sign and at least one digit.
  • Only decimal notation is supported (no hex, octal, binary, or locale-specific formats).

do NOT apply [[likely]] as this is constexpr

◆ is_printable_ascii()

bool jh::pod::string_view::is_printable_ascii ( ) const
inlinenodiscardconstexprnoexcept

Check if all characters are printable 7-bit ASCII.

Returns
true if all characters are in range 32-126, false otherwise.

Verifies that every character lies within the printable 7-bit ASCII range (decimal 32-126).

Note
Printable ASCII is a strict subset of 7-bit ASCII. Therefore: is_printable_ascii() implies is_ascii()
If this function returns true, calling is_ascii() again is redundant. When used inside a requires clause, do not combine the two checks.
This function only permits ASCII characters. If the intention is to validate fully printable text including multi-byte UTF-8 sequences, use is_legal() instead.
is_legal() performs:
  • UTF-8 structural validation
  • rejection of invalid UTF-8 byte combinations
  • rejection of illegal ASCII control characters

◆ operator std::string_view()

jh::pod::string_view::operator std::string_view ( ) const
inlineexplicitconstexprnoexcept

Explicit conversion to std::string_view.

Provides safe, zero-overhead interoperability with the standard library. This conversion preserves both pointer and length semantics without affecting POD compatibility.

Semantics:

  • Conversion is explicit โ€” requires static_cast or brace-init form.
  • Performs no allocation or copy; simply wraps existing data.
  • Pointer and size are preserved exactly (1:1 mapping).
Note
Explicit to avoid unintended implicit conversions in overload resolution. See also: to_std() for named equivalent.

◆ operator<=>()

std::strong_ordering jh::pod::string_view::operator<=> ( const string_view & rhs) const
inlineconstexprnoexcept

Three-way comparison operator (spaceship operator).

Performs a lexicographical three-way comparison between two string_view instances, returning a value of type std::strong_ordering.

Semantics:

  • Returns std::strong_ordering::less if *this < rhs
  • Returns std::strong_ordering::equal if *this == rhs
  • Returns std::strong_ordering::greater if *this > rhs

The comparison is implemented in terms of compare(), and therefore follows identical lexicographic ordering rules. This ensures bitwise consistency between compare(), operator==, and all derived relational operators.

Properties:

  • Guaranteed constexpr and noexcept.
  • Implements a strict total ordering (same as std::string_view).
  • Automatically enables all relational operators (<, <=, >, >=) via the compiler.
Parameters
rhsThe right-hand side string_view to compare against.
Returns
std::strong_ordering value indicating the lexicographic relation.
See also
compare()
operator==()

◆ operator==()

bool jh::pod::string_view::operator== ( const string_view & rhs) const
inlineconstexprnoexcept

Compare two views for byte-wise equality.

Performs a deep comparison of contents using memcmp, rather than checking pointer identity.

Parameters
rhsAnother string_view to compare.
Returns
true if contents are equal, false otherwise.

◆ semantic_len()

std::uint64_t jh::pod::string_view::semantic_len ( ) const
inlinenodiscardconstexprnoexcept

Returns the semantic length of the UTF-8 string.

Counts the number of Unicode code points represented in this view, rather than the number of raw bytes. This function assumes that the underlying data is valid UTF-8.

Definition:

  • A new code point is identified by a byte that is not a UTF-8 continuation byte (10xxxxxx).
  • Continuation bytes are excluded from the count.

Evaluation Model:

  • In constant-evaluated contexts, a fully constexpr UTF-8 scan is performed.
  • At runtime, the implementation may delegate to optimized standard library algorithms.

Important Notes:

  • This function counts Unicode code points, not grapheme clusters.
  • Multi-code-point sequences (e.g. emoji ZWJ sequences or combining characters) are counted individually.
  • No UTF-8 validation is performed.
Returns
Number of Unicode code points in the view, or 0 if the view is empty.
Note
The computation of grapheme clusters will never be provided, as it is evident that in software development, this is a front-end requirement rather than a back-end one, and the systems upon which grapheme clusters depend are excessively cumbersome.

◆ sub()

string_view jh::pod::string_view::sub ( std::uint64_t offset,
std::uint64_t length = npos ) const
inlinenodiscardconstexprnoexcept

Returns a substring starting at offset, for length bytes.

  • If length == jh::pod::string_view::npos, the view extends to the end.
  • If length == 0, the result is an empty view.
  • If offset > len, returns an empty view.
Parameters
offsetStarting byte index (0-based).

param length Number of bytes. Use jh::pod::string_view::npos to read until the end of the view.

Returns
A new string_view into the specified subrange.
Note
This behavior intentionally mirrors the semantics of std::string_view::substr, where npos represents "read until the end".

◆ to_std()

std::string_view jh::pod::string_view::to_std ( ) const
inlinenodiscardconstexprnoexcept

Named conversion helper to obtain a std::string_view.

Functionally identical to explicit operator std::string_view(), but callable in normal expressions without static_cast.

Use cases:

  • Improves readability in non-template or mixed API contexts.
  • Convenient when passing to standard library functions expecting std::string_view.
See also
operator std::string_view()

The documentation for this struct was generated from the following file: