utf-42
Loading...
Searching...
No Matches
/home/runner/work/utf-42/utf-42/utf42.h File Reference

Compile-time polymorphic string literal selection across character encodings. More...

#include <type_traits>
#include <cstddef>
#include <string>
Include dependency graph for utf42.h:

Go to the source code of this file.

Classes

struct  utf42::is_character< T >
 Type trait that checks whether a type is a supported character type. More...
 
class  utf42::basic_string_view< char_t >
 A class that provides a lightweight, non-owning view of a string. More...
 
struct  utf42::poly_enc
 Container holding all character-encoded views of a string literal. More...
 

Namespaces

namespace  utf42
 Main namespace of the library.
 

Macros

#define make_poly_enc(char_t, lit)
 Creates a compile-time polymorphic encoded string literal.
 
#define cons_poly_enc(lit)
 Constructs a compile-time polymorphic encoded string literal view.
 

Functions

template<typename char_t >
constexpr basic_string_view< char_tutf42::visit_poly_enc (const poly_enc &oPolyEnv)
 Selects the appropriate encoded string view for a given character type.
 

Detailed Description

Compile-time polymorphic string literal selection across character encodings.

This header provides a mechanism to define a single string literal and retrieve it as a std::basic_string_view of a desired character type (char, wchar_t, char8_t, char16_t, or char32_t) at compile time.

The approach relies on the compiler to generate all encoding variants of a string literal (via literal prefixes such as L, u8, u, and U) and selects the appropriate one using consteval dispatch.

This guarantees:

  • Zero runtime overhead
  • No heap allocation
  • No UTF transcoding at runtime
  • Full support for \uXXXX and \UXXXXXXXX escape sequences
Warning
The macro make_poly_enc must be used with a string literal. Passing non-literals results in undefined behavior.
Note
Requires C++11 or later. Additional features are available with higher C++ versions.

This file is part of utf42. Copyright (c) 2025 Dante Doménech Martínez

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.

Macro Definition Documentation

◆ cons_poly_enc

#define cons_poly_enc (   lit)
Value:
lit, \
L##lit, \
u##lit, \
U##lit, \
}
Container holding all character-encoded views of a string literal.
Definition utf42.h:298

Constructs a compile-time polymorphic encoded string literal view.

This macro generates all standard character-encoded versions of the provided string literal.

Parameters
litA string literal.
Returns
A std::basic_string_view<char_t> referring to the selected literal.

◆ make_poly_enc

#define make_poly_enc (   char_t,
  lit 
)
Value:
lit, \
L##lit, \
u##lit, \
U##lit, \
} \
)
A class that provides a lightweight, non-owning view of a string.
Definition utf42.h:198

Creates a compile-time polymorphic encoded string literal.

This macro generates all standard character-encoded versions of the provided string literal and selects the one matching char_t.

Parameters
char_tDesired character type (char, wchar_t, char8_t (if C++20), char16_t, or char32_t).
litA string literal.
Returns
A std::basic_string_view<char_t> referring to the selected literal.