Skip to content

Instantly share code, notes, and snippets.

@ktprezes
Created May 14, 2026 21:48
Show Gist options
  • Select an option

  • Save ktprezes/8e07da333584bfaa35ee272b3cecb33a to your computer and use it in GitHub Desktop.

Select an option

Save ktprezes/8e07da333584bfaa35ee272b3cecb33a to your computer and use it in GitHub Desktop.
Some potentially useful Unicode constants for use in C++ code
//
// Created on 2026.04.23
// some potentially useful string constants and the 'std::ostream& operator<<' overloaded for 'std::u8string_view'
// NOTE: keep this file utf-8 encoded
//
#pragma once
#ifndef UNICODE_STRING_CONSTS_H
#define UNICODE_STRING_CONSTS_H
#include <string>
#include <string_view> // needed for ‘std::string_view’, and for ‘"…"sv’ operator
#include <ostream>
using namespace std::string_view_literals;
using namespace std::string_literals;
namespace ktp_str_consts {
// WARNING !!! 'string_view' does NOT guarantee that the fragment cut by '.substr' will end with the char '\\0'
constexpr std::string_view DIGITS = "0123456789"sv;
constexpr std::string_view BIN_DIGITS = "01"sv;
constexpr std::string_view OCT_DIGITS = "01234567"sv;
constexpr std::string_view HEX_LETTERS_UPPERCASE = "ABCDEF"sv;
constexpr std::string_view HEX_LETTERS_LOWERCASE = "abcdef"sv;
constexpr std::string_view HEX_LETTERS = "ABCDEFabcdef"sv;
constexpr std::string_view HEX_DIGITS_UPPERCASE = "0123456789ABCDEF"sv; // DIGITS + HEX_LETTERS_UPPERCASE;
constexpr std::string_view HEX_DIGITS_LOWERCASE = "0123456789abcdef"sv; // DIGITS + HEX_LETTERS_LOWERCASE;
constexpr std::string_view HEX_DIGITS = "0123456789ABCDEFabcdef"sv; // DIGITS + HEX_LETTERS;
constexpr std::string_view ASCII_UPPERCASE = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"sv;
constexpr std::string_view ASCII_LOWERCASE = "abcdefghijklmnopqrstuvwxyz"sv;
constexpr std::string_view ASCII_LETTERS =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"sv; // sorted according to ASCII rules - uppercase 1st!
constexpr std::string_view ASCII_PUNCTUATION = "!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~"sv; // sorted by ASCII codes
constexpr std::string_view ASCII_WHITESPACE = "\t\n\v\f\r "sv; // DEC: TAB 9, NL/LF 10, VT 11, FF 12, CR 13, SPC 32
// see also U_SPACES below…
constexpr std::string_view ASCII_CONTROL_CHARS = "\007\010\033\177"sv; // BEL 7, BSP 8, ESC 27, DEL 127
// Only ctrl chars that are likely to be found in texts/terminals were given – excluding those for transmission etc.
// The NUL 0 char had to be removed because C/C++ treated it as the end of string and didn't iterate over the rest.
constexpr std::u8string_view DE_UPPERCASE = u8"ÄÖÜẞ"sv;
constexpr std::u8string_view DE_LOWERCASE = u8"äöüß"sv;
constexpr std::u8string_view DE_LETTERS = u8"ÄäÖöÜüẞß"sv; // sorted in DE order
constexpr std::u8string_view DE_ALL_LETTERS = // sorted in DE order: uppercase 1st, DE after ASCII
u8"AaÄäBbCcDdEeFfGgHhIiJjKkLlMmNnOoÖöPpQqRrSsẞßTtUuÜüVvWwXxYyZz"sv;
constexpr std::u8string_view PL_UPPERCASE = u8"ĄĆĘŁŃÓŚŹŻ"sv;
constexpr std::u8string_view PL_LOWERCASE = u8"ąćęłńóśźż"sv;
constexpr std::u8string_view PL_LETTERS = u8"ąĄćĆęĘłŁńŃóÓśŚźŹżŻ"sv; // sorted according to Polish rules
constexpr std::u8string_view PL_ALL_LETTERS = // sorted according to Polish rules - lowercase 1st, PL after ASCII
u8"aAąĄbBcCćĆdDeEęĘfFgGhHiIjJkKlLłŁmMnNńŃoOóÓpPqQrRsSśŚtTuUvVwWxXyYzZźŹżŻ"sv;
// the U_BRACKETS_VERTICAL constexpr, after the first 3 chars, contains 3 groups of two (2) zero-width symbols,
// that look like brackets () {} [], but vertically, and after these, the last 20 chars - 29 utf8 chars in total
constexpr std::u8string_view U_BRACKETS_VERTICAL = // adjacent strings are combined into one
u8"⎴⎵⎶" // 3 chars - the '⎶' is unpaired
u8"⏜⏝" // " ⏜ ⏝ " (expanded version with spaces added; just for illustration)
u8"⏞⏟" // " ⏞ ⏟ "
u8"⏠⏡" // " ⏠ ⏡ "
u8"︗︘︵︶︷︸︹︺︻︼︽︾︿﹀﹁﹂﹃﹄﹇﹈"sv; // 20 characters - all paired; 'sv' at the end of all strings
constexpr std::u8string_view U_BRACKETS_HORIZONTAL = // adjacent strings are combined into one
u8"()<>[]{|}༺༻༼༽᚛᚜⁅⁆⁽⁾₍₎⌈⌉⌊⌋⌜⌝⌞⌟〈〉" // 31 chars - the '|' is unpaired
u8"⎛⎜⎝⎞⎟⎠⎡⎢⎣⎤⎥⎦⎧⎨⎩⎪⎫⎬⎭⎰⎱⎸⎹" // 23 chars - 6 groups of 3 chars, 1 upaired char '⎪' and 2 pairs '⎰⎱⎸⎹'
u8"❨❩❪❫❬❭❮❯❰❱❲❳❴❵⟅⟆⟓⟔" // 18 chars - all paired
u8"⟦⟧⟨⟩⟪⟫⟬⟭⟮⟯⦃⦄⦅⦆⦇⦈⦉⦊⦋⦌⦍⦎⦏⦐⦑⦒⦓⦔⦕⦖⦗⦘⧘⧙⧚⧛⧼⧽⸂⸃⸄⸅⸉⸊⸌⸍⸜⸝⸢⸣⸤⸥⸦⸧⸨⸩⹕⹖⹗⹘⹙⹚⹛⹜" // 64 chars - all paired
u8"〈〉《》「」『』【】〔〕〖〗〘〙〚〛﴾﴿﹙﹚﹛﹜﹝﹞()<>[]{}⦅⦆「」"sv; // 38 full-width chars - all paired
constexpr std::u8string_view U_DEGREES = u8"\u00b0\u2103\u2109"sv; // ° ℃ ℉, sorted by Unicode value
constexpr std::u8string_view U_PRIMES = u8"ʹʺ′″‴‵‶‷⁗"sv; // ‘prime(s)’ symbol(s), sorted by Unicode values
/*
U+02B9 ʹ MODIFIER LETTER PRIME
U+02BA ʺ MODIFIER LETTER DOUBLE PRIME
U+2032 ′ PRIME (&prime;) - foot/feet, arch minute(s)
U+2033 ″ DOUBLE PRIME (&Prime;) - inch/inches, arch second(s)
U+2034 ‴ TRIPLE PRIME (&tprime;)
U+2035 ‵ REVERSED PRIME (&backprime;, &bprime;)
U+2036 ‶ REVERSED DOUBLE PRIME
U+2037 ‷ REVERSED TRIPLE PRIME
U+2057 ⁗ QUADRUPLE PRIME (&qprime;)
*/
constexpr std::u8string_view U_QUOTATIONS = u8"\"\',«»‘’‚‛“”„‟‹›〝〞"sv; // sorted by Unicode value
constexpr std::u8string_view U_SPACES = // Unicode common spaces (Space Separator - Zs) - 19 chars
u8"\u0020" // space (SP): The standard spacebar space.
u8"\u00A0" // no-break space (NBSP): Prevents a line break between words.
u8"\u1680" // ogham space mark: Used in Ogham script.
u8"\u180e" // mongolian vowel separator.
u8"\u2000" // en quad: width is 1/2 em.
u8"\u2001" // em quad: width is roughly the size of the font's point size 1 em.
u8"\u2002" // en space: (ENSP) width is 1/2 em, equal to (u2000).
u8"\u2003" // em space: (EMSP) width is 1 em.
u8"\u2004" // three-per-em space: 1/3 em.
u8"\u2005" // four-per-em space: 1/4 em.
u8"\u2006" // six-per-em space: 1/6 em.
u8"\u2007" // figure space: width of a digit/number.
u8"\u2008" // punctuation space: Width of a comma/period.
u8"\u2009" // thin space: a narrow space.
u8"\u200A" // hair space: very narrow space.
u8"\u202F" // narrow no-break space (NNBSP): Narrow non-breaking space.
u8"\u205F" // medium mathematical space (MMSP): Used in math formulas.
u8"\u3000" // ideographic space: space used in CJK (Chinese, Japanese, Korean) text.
u8"\uFEFF"sv; // zero width no-break space (ZWNBS), often used as Byte Order Mark (BOM).
} // namespace ktp_str_consts
// right now (2025.04), standard c++23 'std::ostream& operator <<' does NOT work with 'std::u8string_view' (u8sv)
// btw… we use 'os.write(… , … ) here (with the explicit length),
// because using just: 'std::cout << reinterpret_cast<const char*>(u8sv.data()) << std::endl;'
// works unpredictable e.g. when the u8sv is created as part of larger string/string_view with '*.substr()',
// and is not ended with the '\\0' char… - it may output all chars to the end of original string/string_view
/* // btw… for both these operators to work properly in Windows console, we should run the code:
#ifdef _WIN32
SetConsoleCP(CP_UTF8); // console input CP
SetConsoleOutputCP(CP_UTF8); // console output CP
#endif
*/
std::ostream& operator<<(std::ostream& os, const std::u8string_view& u8sv) {
return os.write(reinterpret_cast<const char*>(u8sv.data()), u8sv.size());
}
std::ostream& operator<<(std::ostream& os, const std::u8string& u8s) {
return os.write(reinterpret_cast<const char*>(u8s.data()), u8s.size());
}
// probably the '<<' operator should be also overloaded for u16string_view/u32string_view,
// but I don't need that right now
/*
* related topics: (possibly for future constants purposes)
* sample unicode homoglyphs:
* https://github.com/codebox/homoglyph/tree/master
* https://github.com/codebox/homoglyph/blob/master/raw_data/chars.txt
* https://github.com/codebox/homoglyph/blob/master/raw_data/char_codes.txt
* ligatures:
* https://en.wikipedia.org/wiki/Ligature_(writing)
* https://en.wikipedia.org/wiki/List_of_Latin-script_letters#Digraphs_and_Ligatures
* multigraphs:
* https://en.wikipedia.org/wiki/Multigraph_(orthography)
* https://en.wikipedia.org/wiki/Digraph_(orthography)
* https://en.wikipedia.org/wiki/List_of_Latin-script_digraphs
* https://en.wikipedia.org/wiki/Trigraph_(orthography)
* https://en.wikipedia.org/wiki/List_of_Latin-script_trigraphs
* https://en.wikipedia.org/wiki/Tetragraph
* https://en.wikipedia.org/wiki/Pentagraph
* https://en.wikipedia.org/wiki/Hexagraph
* https://en.wikipedia.org/wiki/Heptagraph
*/
#endif //UNICODE_STRING_CONSTS_H
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment