Skip to content

Commit 3e24a77

Browse files
committed
[libc++] Use the default initializer for char_type in std::num_get::do_get
This is to fix an error that occurs when the char type is a class type. Thanks to Yichen Yan for the patch. Differential Revision: https://reviews.llvm.org/D100005
1 parent 9a90457 commit 3e24a77

File tree

2 files changed

+117
-1
lines changed

2 files changed

+117
-1
lines changed

libcxx/include/locale

+1-1
Original file line numberDiff line numberDiff line change
@@ -1085,7 +1085,7 @@ num_get<_CharT, _InputIterator>::do_get(iter_type __b, iter_type __e,
10851085
int __base = 16;
10861086
// Stage 2
10871087
char_type __atoms[26];
1088-
char_type __thousands_sep = 0;
1088+
char_type __thousands_sep = char_type();
10891089
string __grouping;
10901090
std::use_facet<ctype<_CharT> >(__iob.getloc()).widen(__num_get_base::__src,
10911091
__num_get_base::__src + 26, __atoms);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
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+
19+
#include "test_macros.h"
20+
21+
struct Char {
22+
Char() = default;
23+
Char(char c) : underlying_(c) {}
24+
Char(unsigned i) : underlying_(i) {}
25+
explicit Char(std::int32_t i) : underlying_(i) {}
26+
operator std::int32_t() const { return underlying_; }
27+
28+
char underlying_;
29+
};
30+
31+
namespace std {
32+
template <>
33+
struct char_traits<Char> {
34+
using char_type = Char;
35+
using int_type = int;
36+
using off_type = streamoff;
37+
using pos_type = streampos;
38+
using state_type = mbstate_t;
39+
40+
static void assign(char_type& a, const char_type& b) { a = b; }
41+
static bool eq(char_type a, char_type b) { return a.underlying_ == b.underlying_; }
42+
static bool lt(char_type a, char_type b) { return a.underlying_ < b.underlying_; }
43+
44+
static int compare(const char_type* s1, const char_type* s2, std::size_t n) {
45+
return char_traits<char>::compare(reinterpret_cast<const char*>(s1), reinterpret_cast<const char*>(s2), n);
46+
}
47+
static std::size_t length(const char_type* s) { return char_traits<char>::length(reinterpret_cast<const char*>(s)); }
48+
static const char_type* find(const char_type* p, std::size_t n, const char_type& c) {
49+
for (size_t i = 0; i != n; ++i) {
50+
if (static_cast<int32_t>(p[i]) == static_cast<int32_t>(c)) {
51+
return p + n;
52+
}
53+
}
54+
return nullptr;
55+
}
56+
static char_type* move(char_type* dest, const char_type* source, std::size_t count) {
57+
char_traits<char>::move(reinterpret_cast<char*>(dest), reinterpret_cast<const char*>(source), count);
58+
return dest;
59+
}
60+
static char_type* copy(char_type* dest, const char_type* source, std::size_t count) {
61+
char_traits<char>::copy(reinterpret_cast<char*>(dest), reinterpret_cast<const char*>(source), count);
62+
return dest;
63+
}
64+
static char_type* assign(char_type* dest, std::size_t n, char_type c) {
65+
char_traits<char>::assign(reinterpret_cast<char*>(dest), n, c.underlying_);
66+
return dest;
67+
}
68+
69+
static int_type not_eof(int_type i) { return char_traits<char>::not_eof(i); }
70+
static char_type to_char_type(int_type i) { return Char(char_traits<char>::to_char_type(i)); }
71+
static int_type to_int_type(char_type c) { return char_traits<char>::to_int_type(c.underlying_); }
72+
static bool eq_int_type(int_type i, int_type j) { return i == j; }
73+
static int_type eof() { return char_traits<char>::eof(); }
74+
};
75+
76+
template <>
77+
class ctype<Char> : public locale::facet {
78+
public:
79+
static locale::id id;
80+
Char toupper(Char c) const { return Char(std::toupper(c.underlying_)); }
81+
const char* widen(const char* first, const char* last, Char* dst) const {
82+
for (; first != last;)
83+
*dst++ = Char(*first++);
84+
return last;
85+
}
86+
};
87+
88+
locale::id ctype<Char>::id;
89+
90+
template <>
91+
class numpunct<Char> : public locale::facet {
92+
public:
93+
typedef basic_string<Char> string_type;
94+
static locale::id id;
95+
Char decimal_point() const { return Char('.'); }
96+
Char thousands_sep() const { return Char(','); }
97+
string grouping() const { return ""; }
98+
string_type truename() const {
99+
static Char yes[] = {Char('t')};
100+
return string_type(yes, 1);
101+
}
102+
string_type falsename() const {
103+
static Char no[] = {Char('f')};
104+
return string_type(no, 1);
105+
}
106+
};
107+
108+
locale::id numpunct<Char>::id;
109+
110+
} // namespace std
111+
112+
int main(int, char**) {
113+
std::locale l(std::locale::classic(), new std::num_get<Char>);
114+
115+
return 0;
116+
}

0 commit comments

Comments
 (0)