2017-12-31 03:00:23 +01:00
|
|
|
/* String implementation details, mainly regarding Unicode support.
|
|
|
|
*
|
|
|
|
* This file is part of libostd. See COPYING.md for futher information.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <cstdint>
|
2018-01-02 22:23:03 +01:00
|
|
|
#include <cstdlib>
|
2017-12-31 03:00:23 +01:00
|
|
|
#include "ostd/string.hh"
|
2018-01-05 22:31:04 +01:00
|
|
|
#include "ostd/format.hh"
|
2017-12-31 03:00:23 +01:00
|
|
|
|
|
|
|
namespace ostd {
|
|
|
|
namespace utf {
|
|
|
|
|
2018-01-05 22:31:04 +01:00
|
|
|
/* place the vtable in here */
|
|
|
|
utf_error::~utf_error() {}
|
|
|
|
|
2018-01-05 18:55:34 +01:00
|
|
|
constexpr char32_t MaxCodepoint = 0x10FFFF;
|
2017-12-31 03:00:23 +01:00
|
|
|
|
2018-01-01 01:02:49 +01:00
|
|
|
namespace detail {
|
2018-01-05 18:55:34 +01:00
|
|
|
static inline std::size_t u8_decode(
|
|
|
|
unsigned char const *beg, unsigned char const *end, char32_t &cret
|
|
|
|
) noexcept {
|
|
|
|
static char32_t const ulim[] = { 0xFF, 0x7F, 0x7FF, 0xFFFF };
|
|
|
|
if (beg == end) {
|
|
|
|
return 0;
|
2017-12-31 03:00:23 +01:00
|
|
|
}
|
2018-01-05 18:55:34 +01:00
|
|
|
char32_t ch = *beg;
|
2018-01-01 01:02:49 +01:00
|
|
|
if (ch <= 0x7F) {
|
|
|
|
/* ASCII */
|
|
|
|
cret = ch;
|
2018-01-05 18:55:34 +01:00
|
|
|
return 1;
|
2018-01-01 01:02:49 +01:00
|
|
|
}
|
2018-01-05 18:55:34 +01:00
|
|
|
char32_t ret = 0;
|
|
|
|
unsigned char const *sbeg = beg + 1;
|
2018-01-01 01:02:49 +01:00
|
|
|
/* continuation bytes */
|
|
|
|
for (; ch & 0x40; ch <<= 1) {
|
|
|
|
/* need a continuation byte but nothing left in the string */
|
2018-01-05 18:55:34 +01:00
|
|
|
if (sbeg == end) {
|
|
|
|
return 0;
|
2018-01-01 01:02:49 +01:00
|
|
|
}
|
|
|
|
/* the continuation byte */
|
2018-01-05 18:55:34 +01:00
|
|
|
char32_t nch = *sbeg++;
|
2018-01-01 01:02:49 +01:00
|
|
|
/* lower 6 bits */
|
2018-01-05 18:55:34 +01:00
|
|
|
char32_t bch = nch & 0x3F;
|
2018-01-01 01:02:49 +01:00
|
|
|
/* not a continuation byte */
|
|
|
|
if ((nch ^ bch) != 0x80) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
/* the 6 bits go in the result */
|
|
|
|
ret = (ret << 6) | bch;
|
|
|
|
}
|
2018-01-05 18:55:34 +01:00
|
|
|
/* by how many bytes we advance (continuation bytes + 1) */
|
|
|
|
auto adv = std::size_t(sbeg - beg);
|
2018-01-01 01:02:49 +01:00
|
|
|
/* invalid sequence - too many continuation bits */
|
2018-01-05 18:55:34 +01:00
|
|
|
if (adv > 4) {
|
|
|
|
return 0;
|
2017-12-31 03:00:23 +01:00
|
|
|
}
|
2018-01-01 01:02:49 +01:00
|
|
|
/* add the up to 7 bits from the first byte, already shifted left by n */
|
2018-01-05 18:55:34 +01:00
|
|
|
ret |= (ch & 0x7F) << ((adv - 1) * 5);
|
2018-01-01 01:02:49 +01:00
|
|
|
/* invalid sequence - out of bounds */
|
2018-01-05 18:55:34 +01:00
|
|
|
if ((ret > MaxCodepoint) || (ret <= ulim[adv - 1])) {
|
|
|
|
return 0;
|
2018-01-01 01:02:49 +01:00
|
|
|
}
|
2018-01-01 02:36:05 +01:00
|
|
|
/* invalid sequence - surrogate code point */
|
2018-01-02 23:28:37 +01:00
|
|
|
if ((ret >= 0xD800) && (ret <= 0xDFFF)) {
|
2018-01-05 18:55:34 +01:00
|
|
|
return 0;
|
2018-01-01 02:36:05 +01:00
|
|
|
}
|
2018-01-01 01:02:49 +01:00
|
|
|
cret = ret;
|
2018-01-05 18:55:34 +01:00
|
|
|
return adv;
|
2017-12-31 03:00:23 +01:00
|
|
|
}
|
2018-01-01 01:02:49 +01:00
|
|
|
|
2018-01-05 18:55:34 +01:00
|
|
|
static inline std::size_t u16_decode(
|
|
|
|
char16_t const *beg, char16_t const *end, char32_t &cret
|
|
|
|
) noexcept {
|
|
|
|
if (beg == end) {
|
|
|
|
return 0;
|
2018-01-05 02:18:36 +01:00
|
|
|
}
|
2018-01-05 18:55:34 +01:00
|
|
|
char32_t ch = *beg;
|
2018-01-05 02:18:36 +01:00
|
|
|
/* lead surrogate code point */
|
|
|
|
if ((ch >= 0xD800) && (ch <= 0xDBFF)) {
|
|
|
|
/* unpaired */
|
2018-01-05 18:55:34 +01:00
|
|
|
if ((end - beg) < 2) {
|
|
|
|
return 0;
|
2018-01-05 02:18:36 +01:00
|
|
|
}
|
2018-01-05 18:55:34 +01:00
|
|
|
char32_t nch = beg[1];
|
2018-01-05 02:18:36 +01:00
|
|
|
/* trail surrogate code point */
|
2018-01-05 18:55:34 +01:00
|
|
|
if ((nch >= 0xDC00) && (nch <= 0xDFFF)) {
|
2018-01-05 02:18:36 +01:00
|
|
|
cret = 0x10000 + (((ch - 0xD800) << 10) | (nch - 0xDC00));
|
2018-01-05 18:55:34 +01:00
|
|
|
return 2;
|
2018-01-05 02:18:36 +01:00
|
|
|
}
|
2018-01-05 18:55:34 +01:00
|
|
|
return 0;
|
2018-01-05 02:18:36 +01:00
|
|
|
}
|
|
|
|
cret = ch;
|
2018-01-05 18:55:34 +01:00
|
|
|
return 1;
|
2018-01-05 02:18:36 +01:00
|
|
|
}
|
|
|
|
|
2018-01-05 18:55:34 +01:00
|
|
|
std::size_t u8_encode(
|
|
|
|
char (&ret)[4], char32_t ch
|
2018-01-01 01:02:49 +01:00
|
|
|
) noexcept {
|
|
|
|
if (ch <= 0x7F) {
|
2018-01-05 18:55:34 +01:00
|
|
|
ret[0] = char(ch);
|
2018-01-01 01:02:49 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if (ch <= 0x7FF) {
|
2018-01-05 18:55:34 +01:00
|
|
|
ret[0] = char(0xC0 | (ch >> 6));
|
|
|
|
ret[1] = char(0x80 | (ch & 0x3F));
|
2018-01-01 01:02:49 +01:00
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
if (ch <= 0xFFFF) {
|
|
|
|
/* TODO: optional WTF-8 semantics
|
|
|
|
* for now simply reject surrogate code points
|
|
|
|
*/
|
2018-01-02 23:28:37 +01:00
|
|
|
if ((ch >= 0xD800) && (ch <= 0xDFFF)) {
|
2018-01-01 01:02:49 +01:00
|
|
|
return 0;
|
|
|
|
}
|
2018-01-05 18:55:34 +01:00
|
|
|
ret[0] = char(0xE0 | (ch >> 12));
|
|
|
|
ret[1] = char(0x80 | ((ch >> 6) & 0x3F));
|
|
|
|
ret[2] = char(0x80 | (ch & 0x3F));
|
2018-01-01 01:02:49 +01:00
|
|
|
return 3;
|
|
|
|
}
|
|
|
|
if (ch <= MaxCodepoint) {
|
2018-01-05 18:55:34 +01:00
|
|
|
ret[0] = char(0xF0 | (ch >> 18));
|
|
|
|
ret[1] = char(0x80 | ((ch >> 12) | 0x3F));
|
|
|
|
ret[2] = char(0x80 | ((ch >> 6) | 0x3F));
|
|
|
|
ret[3] = char(0x80 | (ch | 0x3F));
|
2018-01-01 01:02:49 +01:00
|
|
|
return 4;
|
|
|
|
}
|
|
|
|
return 0;
|
2017-12-31 03:00:23 +01:00
|
|
|
}
|
2018-01-05 02:18:36 +01:00
|
|
|
|
2018-01-05 18:55:34 +01:00
|
|
|
std::size_t u16_encode(
|
|
|
|
char16_t (&ret)[2], char32_t ch
|
2018-01-05 02:18:36 +01:00
|
|
|
) noexcept {
|
|
|
|
/* surrogate code point or out of bounds */
|
|
|
|
if (((ch >= 0xD800) && (ch <= 0xDFFF)) || (ch > MaxCodepoint)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (ch <= 0xFFFF) {
|
2018-01-05 18:55:34 +01:00
|
|
|
ret[0] = char16_t(ch);
|
2018-01-05 02:18:36 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
/* 20-bit number */
|
|
|
|
ch -= 0x10000;
|
2018-01-05 18:55:34 +01:00
|
|
|
ret[0] = char16_t(0xD800 + (ch >> 10));
|
|
|
|
ret[1] = char16_t(0xDC00 + (ch & 0x3FF));
|
2018-01-05 02:18:36 +01:00
|
|
|
return 2;
|
|
|
|
}
|
2018-01-01 01:02:49 +01:00
|
|
|
} /* namespace detail */
|
2017-12-31 03:00:23 +01:00
|
|
|
|
2017-12-31 20:06:36 +01:00
|
|
|
bool decode(string_range &r, char32_t &ret) noexcept {
|
2018-01-05 18:55:34 +01:00
|
|
|
auto tn = r.size();
|
|
|
|
auto *beg = reinterpret_cast<unsigned char const *>(r.data());
|
2018-01-05 19:25:20 +01:00
|
|
|
if (std::size_t n; (n = detail::u8_decode(beg, beg + tn, ret))) {
|
2018-01-05 18:55:34 +01:00
|
|
|
r = r.slice(n, tn);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2017-12-31 03:26:15 +01:00
|
|
|
}
|
|
|
|
|
2018-01-05 02:18:36 +01:00
|
|
|
bool decode(u16string_range &r, char32_t &ret) noexcept {
|
2018-01-05 18:55:34 +01:00
|
|
|
auto tn = r.size();
|
2018-01-05 19:25:20 +01:00
|
|
|
auto *beg = r.data();
|
|
|
|
if (std::size_t n; (n = detail::u16_decode(beg, beg + tn, ret))) {
|
|
|
|
r = r.slice(n, tn);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool decode(wstring_range &r, char32_t &ret) noexcept {
|
|
|
|
std::size_t n, tn = r.size();
|
|
|
|
if constexpr(sizeof(wchar_t) == sizeof(char32_t)) {
|
|
|
|
if (!tn) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
ret = char32_t(r.front());
|
|
|
|
return true;
|
|
|
|
} else if constexpr(sizeof(wchar_t) == sizeof(char16_t)) {
|
|
|
|
auto *beg = reinterpret_cast<char16_t const *>(r.data());
|
|
|
|
n = detail::u16_decode(beg, beg + tn, ret);
|
|
|
|
} else {
|
|
|
|
auto *beg = reinterpret_cast<unsigned char const *>(r.data());
|
|
|
|
n = detail::u8_decode(beg, beg + tn, ret);
|
|
|
|
}
|
|
|
|
if (n) {
|
2018-01-05 18:55:34 +01:00
|
|
|
r = r.slice(n, tn);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2018-01-05 02:18:36 +01:00
|
|
|
}
|
|
|
|
|
2017-12-31 19:12:51 +01:00
|
|
|
std::size_t length(string_range r, string_range &cont) noexcept {
|
2017-12-31 03:00:23 +01:00
|
|
|
std::size_t ret = 0;
|
2018-01-06 00:27:04 +01:00
|
|
|
for (char32_t ch; utf::decode(r, ch); ++ret) {
|
2017-12-31 03:00:23 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
cont = r;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2018-01-06 00:27:04 +01:00
|
|
|
std::size_t length(string_range r) noexcept {
|
|
|
|
std::size_t ret = 0;
|
|
|
|
for (;; ++ret) {
|
|
|
|
if (char32_t ch; !utf::decode(r, ch)) {
|
|
|
|
if (r.empty()) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
r.pop_front();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2018-01-02 22:23:03 +01:00
|
|
|
/* unicode-aware ctype
|
|
|
|
* the other ones use custom tables for lookups
|
|
|
|
*/
|
|
|
|
|
2018-01-03 02:12:23 +01:00
|
|
|
bool isalnum(char32_t c) noexcept {
|
2018-01-02 22:23:03 +01:00
|
|
|
return (utf::isalpha(c) || utf::isdigit(c));
|
|
|
|
}
|
|
|
|
|
2018-01-03 02:12:23 +01:00
|
|
|
bool isblank(char32_t c) noexcept {
|
2018-01-02 22:23:03 +01:00
|
|
|
return ((c == ' ') || (c == '\t'));
|
|
|
|
}
|
|
|
|
|
2018-01-03 02:12:23 +01:00
|
|
|
bool isgraph(char32_t c) noexcept {
|
2018-01-02 22:23:03 +01:00
|
|
|
return (!utf::isspace(c) && utf::isprint(c));
|
|
|
|
}
|
|
|
|
|
2018-01-03 02:12:23 +01:00
|
|
|
bool isprint(char32_t c) noexcept {
|
2018-01-05 18:55:34 +01:00
|
|
|
switch (c) {
|
2018-01-02 22:23:03 +01:00
|
|
|
case 0x2028:
|
|
|
|
case 0x2029:
|
|
|
|
case 0xFFF9:
|
|
|
|
case 0xFFFA:
|
|
|
|
case 0xFFFB:
|
|
|
|
return false;
|
|
|
|
default:
|
|
|
|
return !utf::iscntrl(c);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-03 02:12:23 +01:00
|
|
|
bool ispunct(char32_t c) noexcept {
|
2018-01-02 22:23:03 +01:00
|
|
|
return (utf::isgraph(c) && !utf::isalnum(c));
|
|
|
|
}
|
|
|
|
|
2018-01-03 02:12:23 +01:00
|
|
|
bool isvalid(char32_t c) noexcept {
|
2018-01-02 22:23:03 +01:00
|
|
|
/* surrogate code points */
|
|
|
|
if ((c >= 0xD800) && (c <= 0xDFFF)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
/* non-characters */
|
|
|
|
if ((c >= 0xFDD0) && (c <= 0xFDEF)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
/* end of plane */
|
|
|
|
if ((c & 0xFFFE) == 0xFFFE) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
/* must be within range */
|
|
|
|
return (c <= MaxCodepoint);
|
|
|
|
}
|
|
|
|
|
2018-01-03 02:12:23 +01:00
|
|
|
bool isxdigit(char32_t c) noexcept {
|
2018-01-02 22:23:03 +01:00
|
|
|
if ((c >= '0') && (c <= '9')) {
|
|
|
|
return true;
|
|
|
|
}
|
2018-01-05 18:55:34 +01:00
|
|
|
auto uc = c | 32;
|
2018-01-02 22:23:03 +01:00
|
|
|
return ((uc >= 'a') && (uc <= 'f'));
|
|
|
|
}
|
|
|
|
|
2018-01-03 02:12:23 +01:00
|
|
|
inline int codepoint_cmp1(void const *a, void const *b) noexcept {
|
2018-01-02 22:23:03 +01:00
|
|
|
char32_t c1 = *static_cast<char32_t const *>(a);
|
|
|
|
char32_t c2 = *static_cast<char32_t const *>(b);
|
2018-01-03 17:09:28 +01:00
|
|
|
return (int(c1) - int(c2));
|
2018-01-02 22:23:03 +01:00
|
|
|
}
|
|
|
|
|
2018-01-03 02:12:23 +01:00
|
|
|
inline int codepoint_cmp2(void const *a, void const *b) noexcept {
|
2018-01-02 22:23:03 +01:00
|
|
|
char32_t c = *static_cast<char32_t const *>(a);
|
|
|
|
char32_t const *p = static_cast<char32_t const *>(b);
|
|
|
|
if ((c >= p[0]) && (c <= p[1])) {
|
|
|
|
return 0;
|
|
|
|
}
|
2018-01-03 17:09:28 +01:00
|
|
|
return (int(c) - int(p[0]));
|
2018-01-02 22:23:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
template<
|
2018-01-02 23:28:37 +01:00
|
|
|
std::size_t RN, std::size_t RS,
|
|
|
|
std::size_t L1N, std::size_t L1S,
|
|
|
|
std::size_t L2N, std::size_t L2S,
|
|
|
|
std::size_t SN, std::size_t SS
|
2018-01-02 22:23:03 +01:00
|
|
|
>
|
|
|
|
struct uctype_func {
|
2018-01-02 23:28:37 +01:00
|
|
|
template<std::size_t N, std::size_t S>
|
|
|
|
static char32_t *search(
|
|
|
|
char32_t c, void const *arr, int (*cmp)(void const *, void const *)
|
2018-01-03 02:12:23 +01:00
|
|
|
) noexcept {
|
2018-01-02 23:28:37 +01:00
|
|
|
return static_cast<char32_t *>(std::bsearch(&c, arr, N / S, S, cmp));
|
|
|
|
}
|
|
|
|
|
2018-01-02 22:23:03 +01:00
|
|
|
static bool do_is(
|
|
|
|
char32_t c,
|
|
|
|
void const *ranges [[maybe_unused]],
|
|
|
|
void const *laces1 [[maybe_unused]],
|
|
|
|
void const *laces2 [[maybe_unused]],
|
|
|
|
void const *singles [[maybe_unused]]
|
2018-01-03 02:12:23 +01:00
|
|
|
) noexcept {
|
2018-01-02 23:28:37 +01:00
|
|
|
if constexpr(RN != 0) {
|
|
|
|
char32_t *found = search<RN, RS>(c, ranges, codepoint_cmp2);
|
2018-01-02 22:23:03 +01:00
|
|
|
if (found) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2018-01-02 23:28:37 +01:00
|
|
|
if constexpr(L1N != 0) {
|
|
|
|
char32_t *found = search<L1N, L1S>(c, laces1, codepoint_cmp2);
|
2018-01-02 22:23:03 +01:00
|
|
|
if (found) {
|
|
|
|
return !((c - found[0]) % 2);
|
|
|
|
}
|
|
|
|
}
|
2018-01-02 23:28:37 +01:00
|
|
|
if constexpr(L2N != 0) {
|
|
|
|
char32_t *found = search<L2N, L2S>(c, laces2, codepoint_cmp2);
|
2018-01-02 22:23:03 +01:00
|
|
|
if (found) {
|
|
|
|
return !((c - found[0]) % 2);
|
|
|
|
}
|
|
|
|
}
|
2018-01-02 23:28:37 +01:00
|
|
|
if constexpr(SN != 0) {
|
|
|
|
char32_t *found = search<SN, SS>(c, singles, codepoint_cmp1);
|
2018-01-02 22:23:03 +01:00
|
|
|
if (found) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static char32_t do_to(
|
|
|
|
char32_t c,
|
|
|
|
void const *ranges [[maybe_unused]],
|
|
|
|
void const *laces1 [[maybe_unused]],
|
|
|
|
void const *laces2 [[maybe_unused]],
|
|
|
|
void const *singles [[maybe_unused]]
|
2018-01-03 02:12:23 +01:00
|
|
|
) noexcept {
|
2018-01-02 23:28:37 +01:00
|
|
|
if constexpr(RN != 0) {
|
|
|
|
char32_t *found = search<RN, RS>(c, ranges, codepoint_cmp2);
|
2018-01-02 22:23:03 +01:00
|
|
|
if (found) {
|
|
|
|
return (found[2] + (c - found[0]));
|
|
|
|
}
|
|
|
|
}
|
2018-01-02 23:28:37 +01:00
|
|
|
if constexpr(L1N != 0) {
|
|
|
|
char32_t *found = search<L1N, L1S>(c, laces1, codepoint_cmp2);
|
2018-01-02 22:23:03 +01:00
|
|
|
if (found) {
|
|
|
|
if ((c - found[0]) % 2) {
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
return c + 1;
|
|
|
|
}
|
|
|
|
}
|
2018-01-02 23:28:37 +01:00
|
|
|
if constexpr(L2N != 0) {
|
|
|
|
char32_t *found = search<L2N, L2S>(c, laces2, codepoint_cmp2);
|
2018-01-02 22:23:03 +01:00
|
|
|
if (found) {
|
|
|
|
if ((c - found[0]) % 2) {
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
return c - 1;
|
|
|
|
}
|
|
|
|
}
|
2018-01-02 23:28:37 +01:00
|
|
|
if constexpr(SN != 0) {
|
|
|
|
char32_t *found = search<SN, SS>(c, singles, codepoint_cmp1);
|
2018-01-02 22:23:03 +01:00
|
|
|
if (found) {
|
|
|
|
return found[1];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/* these are geneated */
|
2018-01-03 02:12:23 +01:00
|
|
|
bool isalpha(char32_t c) noexcept;
|
|
|
|
bool iscntrl(char32_t c) noexcept;
|
|
|
|
bool isdigit(char32_t c) noexcept;
|
|
|
|
bool islower(char32_t c) noexcept;
|
|
|
|
bool isspace(char32_t c) noexcept;
|
|
|
|
bool istitle(char32_t c) noexcept;
|
|
|
|
bool isupper(char32_t c) noexcept;
|
|
|
|
char32_t tolower(char32_t c) noexcept;
|
|
|
|
char32_t toupper(char32_t c) noexcept;
|
2018-01-02 22:23:03 +01:00
|
|
|
|
2018-01-03 00:37:31 +01:00
|
|
|
#if __has_include("string_utf.hh")
|
2018-01-02 22:23:03 +01:00
|
|
|
#include "string_utf.hh"
|
2018-01-03 00:37:31 +01:00
|
|
|
#else
|
|
|
|
|
|
|
|
/* break the cycle (build system and generator use libostd, but string_utf.hh
|
|
|
|
* is generated during build) by providing a bunch of ASCII only fallbacks
|
|
|
|
*/
|
|
|
|
|
2018-01-03 02:12:23 +01:00
|
|
|
bool isalpha(char32_t c) noexcept {
|
2018-01-03 00:37:31 +01:00
|
|
|
return (utf::isupper(c) || utf::islower(c));
|
|
|
|
}
|
|
|
|
|
2018-01-03 02:12:23 +01:00
|
|
|
bool iscntrl(char32_t c) noexcept {
|
2018-01-03 00:37:31 +01:00
|
|
|
return ((c <= 0x1F) || (c == 0x7F));
|
|
|
|
}
|
|
|
|
|
2018-01-03 02:12:23 +01:00
|
|
|
bool isdigit(char32_t c) noexcept {
|
2018-01-03 00:37:31 +01:00
|
|
|
return ((c >= '0') && (c <= '9'));
|
|
|
|
}
|
|
|
|
|
2018-01-03 02:12:23 +01:00
|
|
|
bool islower(char32_t c) noexcept {
|
2018-01-03 00:37:31 +01:00
|
|
|
return ((c >= 'a') && (c <= 'z'));
|
|
|
|
}
|
|
|
|
|
2018-01-03 02:12:23 +01:00
|
|
|
bool isspace(char32_t c) noexcept {
|
2018-01-03 00:37:31 +01:00
|
|
|
return ((c == ' ') || ((c >= 0x09) && (c <= 0x0D)));
|
|
|
|
}
|
|
|
|
|
2018-01-03 02:12:23 +01:00
|
|
|
bool istitle(char32_t) noexcept {
|
2018-01-03 00:37:31 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-01-03 02:12:23 +01:00
|
|
|
bool isupper(char32_t c) noexcept {
|
2018-01-03 00:37:31 +01:00
|
|
|
return ((c >= 'A') && (c <= 'Z'));
|
|
|
|
}
|
|
|
|
|
2018-01-03 02:12:23 +01:00
|
|
|
char32_t tolower(char32_t c) noexcept {
|
2018-01-03 00:37:31 +01:00
|
|
|
if (utf::isupper(c)) {
|
|
|
|
return c | 32;
|
|
|
|
}
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
2018-01-03 02:12:23 +01:00
|
|
|
char32_t toupper(char32_t c) noexcept {
|
2018-01-03 00:37:31 +01:00
|
|
|
if (utf::islower(c)) {
|
|
|
|
return c ^ 32;
|
|
|
|
}
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* __has_include("string_utf.hh") */
|
2018-01-02 22:23:03 +01:00
|
|
|
|
2018-01-03 01:22:07 +01:00
|
|
|
int case_compare(string_range s1, string_range s2) noexcept {
|
|
|
|
std::size_t s1l = s1.size(), s2l = s2.size(), ms = std::min(s1l, s2l);
|
|
|
|
s1 = s1.slice(0, ms);
|
|
|
|
s2 = s2.slice(0, ms);
|
|
|
|
for (;;) {
|
2018-01-03 17:09:28 +01:00
|
|
|
/* enforce correct semantics with signed chars; first convert to
|
|
|
|
* 8-bit unsigned and then to char32_t (which is always unsigned)
|
|
|
|
* in order to not get large values from 32-bit unsigned underflow
|
|
|
|
*/
|
2018-01-05 18:55:34 +01:00
|
|
|
auto ldec = char32_t(static_cast<unsigned char>(s1.front()));
|
|
|
|
auto rdec = char32_t(static_cast<unsigned char>(s2.front()));
|
2018-01-03 01:22:07 +01:00
|
|
|
if ((ldec <= 0x7F) || !utf::decode(s1, ldec)) {
|
|
|
|
s1.pop_front();
|
|
|
|
}
|
|
|
|
if ((rdec <= 0x7F) || !utf::decode(s2, rdec)) {
|
|
|
|
s2.pop_front();
|
|
|
|
}
|
|
|
|
int d = int(utf::tolower(ldec)) - int(utf::tolower(rdec));
|
|
|
|
if (d) {
|
|
|
|
return d;
|
|
|
|
}
|
2018-01-03 17:09:28 +01:00
|
|
|
if (s1.empty() || s2.empty()) {
|
|
|
|
s1l = s1.size();
|
|
|
|
s2l = s2.size();
|
|
|
|
break;
|
|
|
|
}
|
2018-01-03 01:22:07 +01:00
|
|
|
}
|
2018-01-03 17:09:28 +01:00
|
|
|
return (s1l < s2l) ? -1 : ((s1l > s2l) ? 1 : 0);
|
2018-01-03 01:22:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int case_compare(u32string_range s1, u32string_range s2) noexcept {
|
|
|
|
std::size_t s1l = s1.size(), s2l = s2.size();
|
|
|
|
for (std::size_t i = 0, ms = std::min(s1l, s2l); i < ms; ++i) {
|
|
|
|
int d = int(utf::tolower(s1[i])) - int(utf::tolower(s2[i]));
|
|
|
|
if (d) {
|
|
|
|
return d;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return (s1l < s2l) ? -1 : ((s1l > s2l) ? 1 : 0);
|
|
|
|
}
|
|
|
|
|
2017-12-31 03:00:23 +01:00
|
|
|
} /* namespace utf */
|
2018-01-05 22:31:04 +01:00
|
|
|
|
|
|
|
/* place the vtable in here */
|
|
|
|
format_error::~format_error() {}
|
|
|
|
|
2017-12-31 03:00:23 +01:00
|
|
|
} /* namespace ostd */
|