1//===----------------------------------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9// <locale>
10//
11// Make sure that num_get works with a user-defined char_type that has a
12// constructor making initialization from bare `int` invalid.
13
14#include <cstddef>
15#include <cstdint>
16#include <locale>
17#include <string>
18
19struct Char {
20 Char() = default;
21 Char(char c) : underlying_(c) {}
22 Char(unsigned i) : underlying_(i) {}
23 explicit Char(std::int32_t i) : underlying_(i) {}
24 operator std::int32_t() const { return underlying_; }
25
26 char underlying_;
27};
28
29template <>
30struct std::char_traits<Char> {
31 using char_type = Char;
32 using int_type = int;
33 using off_type = streamoff;
34 using pos_type = streampos;
35 using state_type = mbstate_t;
36
37 static void assign(char_type& a, const char_type& b) { a = b; }
38 static bool eq(char_type a, char_type b) { return a.underlying_ == b.underlying_; }
39 static bool lt(char_type a, char_type b) { return a.underlying_ < b.underlying_; }
40
41 static int compare(const char_type* s1, const char_type* s2, std::size_t n) {
42 return char_traits<char>::compare(s1: reinterpret_cast<const char*>(s1), s2: reinterpret_cast<const char*>(s2), n: n);
43 }
44 static std::size_t length(const char_type* s) { return char_traits<char>::length(s: reinterpret_cast<const char*>(s)); }
45 static const char_type* find(const char_type* p, std::size_t n, const char_type& c) {
46 for (size_t i = 0; i != n; ++i) {
47 if (static_cast<int32_t>(p[i]) == static_cast<int32_t>(c)) {
48 return p + n;
49 }
50 }
51 return nullptr;
52 }
53 static char_type* move(char_type* dest, const char_type* source, std::size_t count) {
54 char_traits<char>::move(s1: reinterpret_cast<char*>(dest), s2: reinterpret_cast<const char*>(source), n: count);
55 return dest;
56 }
57 static char_type* copy(char_type* dest, const char_type* source, std::size_t count) {
58 char_traits<char>::copy(s1: reinterpret_cast<char*>(dest), s2: reinterpret_cast<const char*>(source), n: count);
59 return dest;
60 }
61 static char_type* assign(char_type* dest, std::size_t n, char_type c) {
62 char_traits<char>::assign(s: reinterpret_cast<char*>(dest), n: n, a: c.underlying_);
63 return dest;
64 }
65
66 static int_type not_eof(int_type i) { return char_traits<char>::not_eof(c: i); }
67 static char_type to_char_type(int_type i) { return Char(char_traits<char>::to_char_type(c: i)); }
68 static int_type to_int_type(char_type c) { return char_traits<char>::to_int_type(c: c.underlying_); }
69 static bool eq_int_type(int_type i, int_type j) { return i == j; }
70 static int_type eof() { return char_traits<char>::eof(); }
71};
72
73// This ctype specialization treats all characters as spaces
74template <>
75class std::ctype<Char> : public locale::facet, public ctype_base {
76public:
77 using char_type = Char;
78 static locale::id id;
79 explicit ctype(std::size_t refs = 0) : locale::facet(refs) {}
80
81 bool is(mask m, char_type) const { return m & ctype_base::space; }
82 const char_type* is(const char_type* low, const char_type* high, mask* vec) const {
83 for (; low != high; ++low)
84 *vec++ = ctype_base::space;
85 return high;
86 }
87
88 const char_type* scan_is(mask m, const char_type* beg, const char_type* end) const {
89 for (; beg != end; ++beg)
90 if (this->is(m, *beg))
91 return beg;
92 return end;
93 }
94
95 const char_type* scan_not(mask m, const char_type* beg, const char_type* end) const {
96 for (; beg != end; ++beg)
97 if (!this->is(m, *beg))
98 return beg;
99 return end;
100 }
101
102 char_type toupper(char_type c) const { return c; }
103 const char_type* toupper(char_type*, const char_type* end) const { return end; }
104
105 char_type tolower(char_type c) const { return c; }
106 const char_type* tolower(char_type*, const char_type* end) const { return end; }
107
108 char_type widen(char c) const { return char_type(c); }
109 const char* widen(const char* beg, const char* end, char_type* dst) const {
110 for (; beg != end; ++beg, ++dst)
111 *dst = char_type(*beg);
112 return end;
113 }
114
115 char narrow(char_type c, char /*dflt*/) const { return c.underlying_; }
116 const char_type* narrow(const char_type* beg, const char_type* end, char /*dflt*/, char* dst) const {
117 for (; beg != end; ++beg, ++dst)
118 *dst = beg->underlying_;
119 return end;
120 }
121};
122
123std::locale::id std::ctype<Char>::id;
124
125template <>
126class std::numpunct<Char> : public locale::facet {
127public:
128 typedef basic_string<Char> string_type;
129 static locale::id id;
130 Char decimal_point() const { return Char('.'); }
131 Char thousands_sep() const { return Char(','); }
132 string grouping() const { return ""; }
133 string_type truename() const {
134 static Char yes[] = {Char('t')};
135 return string_type(yes, 1);
136 }
137 string_type falsename() const {
138 static Char no[] = {Char('f')};
139 return string_type(no, 1);
140 }
141};
142
143std::locale::id std::numpunct<Char>::id;
144
145int main(int, char**) {
146 std::locale l(std::locale::classic(), new std::num_get<Char>);
147
148 return 0;
149}
150

source code of libcxx/test/std/localization/locale.categories/category.numeric/locale.num.get/user_defined_char_type.pass.cpp