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_alnumAlphanumeric.
c_alphaAlphabetic.
c_blankSpace or tab.
c_cntrlControl.
c_digitDigit.
c_fullwidthFullwidth.
c_graphGraphical.
c_halfwidthHalfwidth.
c_lowerLowercase.
c_printPrintable.
c_punctPunctuation.
c_spaceWhitespace.
c_upperUppercase.
c_xdigitHexadecimal digit.
dos_file_invalid_characterInvalid DOS/Windows file-name character.
html_whitespaceHTML 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:

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.

Not a locale. 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