Ch26.8: char_category
1. What char_category Is
fast_io::char_category is a compile-time, locale-independent character
classification layer. It answers the questions that <cctype> answers
in the C locale, plus fullwidth/halfwidth and a few path/HTML helpers, without a locale
object and without a function call at runtime when the compiler can fold it.
Every predicate is constexpr and noexcept. The layer accepts
the character types fast_io uses: char, wchar_t,
char8_t, char16_t and char32_t.
2. Predicates
| Predicate | True when |
|---|---|
is_c_alnum(ch) | Letter or digit. |
is_c_alpha(ch) | Letter. |
is_c_blank(ch) | Space or tab. |
is_c_cntrl(ch) | Control character. |
is_c_digit(ch) | Decimal digit. |
is_c_graph(ch) | Graphical character (alphanumeric or punctuation). |
is_c_lower(ch) | Lowercase letter. |
is_c_print(ch) | Printable character, including space. |
is_c_punct(ch) | Punctuation character. |
is_c_space(ch) | Whitespace: space, tab, newline, carriage return, form feed, vertical tab. |
is_c_upper(ch) | Uppercase letter. |
is_c_xdigit(ch) | Hexadecimal digit. |
is_c_halfwidth(ch) | Halfwidth character. |
is_c_fullwidth(ch) | Fullwidth character. |
is_html_whitespace(ch) | HTML whitespace: space, tab, LF, FF, CR. |
is_dos_file_invalid_character(ch) | Character invalid in a DOS/Windows file name. |
is_dos_path_invalid_prefix_character(ch) | Character invalid at the start of a DOS path prefix. Header-side. |
static_assert(::fast_io::char_category::is_c_digit('7'));
static_assert(!::fast_io::char_category::is_c_space('x'));
3. Case and Width Conversion
The conversion functions are function objects, not macros, and they are also
constexpr:
| Entry | Effect |
|---|---|
to_c_lower(ch) |
Map an uppercase character to lowercase where the C locale defines the mapping. |
to_c_upper(ch) |
Map a lowercase character to uppercase. |
to_c_halfwidth(ch) |
Map a fullwidth punctuation or form to its halfwidth form where defined. |
ranges::to_c_lower(first, last[, proj]) |
Apply the mapping to an iterator/sentinel range; returns the final iterator. |
ranges::to_c_upper(range[, proj]) |
Apply the mapping to a range; returns a borrowed iterator. |
ranges::to_c_halfwidth(range[, proj]) |
Apply halfwidth conversion to a range. |
char c{'A'};
c = ::fast_io::char_category::to_c_lower(c); // 'a'
::fast_io::string s{"Hello"};
::fast_io::char_category::ranges::to_c_upper(s);
The range versions accept a projection, so they compose with a member accessor:
::fast_io::char_category::ranges::to_c_lower(
first, last, [](auto const &e) { return e.initial; });
4. char_category_traits
char_category_traits<family, negate> turns a family into a callable
predicate. It exposes family, is_negate and
char_is(ch), and its operator() calls char_is.
This is the form to store in a table or pass to an algorithm.
char_category_family |
Meaning |
|---|---|
c_alnum | Alphanumeric. |
c_alpha | Alphabetic. |
c_blank | Space or tab. |
c_cntrl | Control. |
c_digit | Digit. |
c_fullwidth | Fullwidth. |
c_graph | Graphical. |
c_halfwidth | Halfwidth. |
c_lower | Lowercase. |
c_print | Printable. |
c_punct | Punctuation. |
c_space | Whitespace. |
c_upper | Uppercase. |
c_xdigit | Hexadecimal digit. |
dos_file_invalid_character | Invalid DOS/Windows file-name character. |
html_whitespace | HTML whitespace. |
using digit_pred = ::fast_io::char_category::char_category_traits<
::fast_io::char_category::char_category_family::c_digit, false>;
static_assert(digit_pred::char_is('5'));
static_assert(!digit_pred{}( 'x' ));
5. Module and Header Boundary
The fast_io module exports:
char_category_familyandchar_category_traits;to_c_lower,to_c_upper,to_c_halfwidth;- the
ranges::to_c_lower,ranges::to_c_upperandranges::to_c_halfwidthrange forms; is_dos_file_invalid_character.
The individual is_c_* predicates are header-side today. So are
is_html_whitespace and
is_dos_path_invalid_prefix_character. They are all available through
#include <fast_io.h>; if you are using the module, include the header
for the predicate you need until the export list is extended.
char_category is not the
fast_io.i18n layer. It is the C-locale classification and conversion layer.
Use the locale module when the answer must change with the user's locale.
Key Takeaways
char_categoryis compile-time, locale-independent character classification.- The core predicates mirror the C
<cctype>families, plus fullwidth/halfwidth, HTML whitespace and DOS file-name validation. to_c_lower,to_c_upperandto_c_halfwidthare constexpr function objects; theranges::forms apply them to a range with an optional projection.char_category_traits<family, negate>is the callable, storable form of a family.- The module exports the traits, the three conversion objects, their range forms and
is_dos_file_invalid_character; the individualis_c_*predicates remain header-side. - This layer is not a locale; use
fast_io.i18nfor locale-dependent formatting.