Skip to content

Commit 2e75310

Browse files
committed
C++: Improve char_traits stubs
1 parent bb9f5a1 commit 2e75310

File tree

3 files changed

+113
-6
lines changed

3 files changed

+113
-6
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#ifndef _GHLIBCPP_CWCHAR
2+
#define _GHLIBCPP_CWCHAR
3+
4+
#include "stddef.h"
5+
6+
namespace std {
7+
// Character classification and conversion types
8+
typedef struct {
9+
int __count;
10+
union {
11+
unsigned int __wch;
12+
char __wchb[4];
13+
} __value;
14+
} mbstate_t;
15+
16+
typedef unsigned int wint_t;
17+
18+
// Wide character constants
19+
static const wint_t WEOF = static_cast<wint_t>(-1);
20+
21+
} // namespace std
22+
23+
#endif // _GHLIBCPP_CWCHAR

cpp/common/test/includes/standard-library/ios.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
namespace std {
66
typedef size_t streamsize;
77
typedef int pos_type;
8+
typedef long long streamoff;
9+
typedef pos_type streampos;
10+
typedef pos_type wstreampos;
811

912
// Bitmask type as specified by [bitmask.types]
1013
// Operators omitted as not required for our test cases

cpp/common/test/includes/standard-library/string

Lines changed: 87 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,92 @@
11
#ifndef _GHLIBCPP_STRING
22
#define _GHLIBCPP_STRING
3+
#include "cwchar"
34
#include "initializer_list"
5+
#include "ios.h"
46
#include "iosfwd.h"
57
#include "iterator.h"
68
#include "stddef.h"
79

810
namespace std {
9-
template <class charT> struct char_traits;
11+
template <class charT> struct char_traits {
12+
typedef charT char_type;
13+
typedef int int_type;
14+
typedef streamoff off_type;
15+
typedef streampos pos_type;
16+
typedef mbstate_t state_type;
17+
18+
static void assign(char_type &c1, const char_type &c2);
19+
static bool eq(const char_type &c1, const char_type &c2);
20+
static bool lt(const char_type &c1, const char_type &c2);
21+
22+
static int compare(const char_type *s1, const char_type *s2, size_t n);
23+
static size_t length(const char_type *s);
24+
static const char_type *find(const char_type *s, size_t n,
25+
const char_type &a);
26+
static char_type *move(char_type *s1, const char_type *s2, size_t n);
27+
static char_type *copy(char_type *s1, const char_type *s2, size_t n);
28+
static char_type *assign(char_type *s, size_t n, char_type a);
29+
30+
static int_type not_eof(const int_type &c);
31+
static char_type to_char_type(const int_type &c);
32+
static int_type to_int_type(const char_type &c);
33+
static bool eq_int_type(const int_type &c1, const int_type &c2);
34+
static int_type eof();
35+
};
36+
37+
// Specialization for char
38+
template <> struct char_traits<char> {
39+
typedef char char_type;
40+
typedef int int_type;
41+
typedef streamoff off_type;
42+
typedef streampos pos_type;
43+
typedef mbstate_t state_type;
44+
45+
static void assign(char_type &c1, const char_type &c2);
46+
static bool eq(const char_type &c1, const char_type &c2);
47+
static bool lt(const char_type &c1, const char_type &c2);
48+
49+
static int compare(const char_type *s1, const char_type *s2, size_t n);
50+
static size_t length(const char_type *s);
51+
static const char_type *find(const char_type *s, size_t n,
52+
const char_type &a);
53+
static char_type *move(char_type *s1, const char_type *s2, size_t n);
54+
static char_type *copy(char_type *s1, const char_type *s2, size_t n);
55+
static char_type *assign(char_type *s, size_t n, char_type a);
56+
57+
static int_type not_eof(const int_type &c);
58+
static char_type to_char_type(const int_type &c);
59+
static int_type to_int_type(const char_type &c);
60+
static bool eq_int_type(const int_type &c1, const int_type &c2);
61+
static int_type eof();
62+
};
63+
64+
// Specialization for wchar_t
65+
template <> struct char_traits<wchar_t> {
66+
typedef wchar_t char_type;
67+
typedef wint_t int_type;
68+
typedef streamoff off_type;
69+
typedef wstreampos pos_type;
70+
typedef mbstate_t state_type;
71+
72+
static void assign(char_type &c1, const char_type &c2);
73+
static bool eq(const char_type &c1, const char_type &c2);
74+
static bool lt(const char_type &c1, const char_type &c2);
75+
76+
static int compare(const char_type *s1, const char_type *s2, size_t n);
77+
static size_t length(const char_type *s);
78+
static const char_type *find(const char_type *s, size_t n,
79+
const char_type &a);
80+
static char_type *move(char_type *s1, const char_type *s2, size_t n);
81+
static char_type *copy(char_type *s1, const char_type *s2, size_t n);
82+
static char_type *assign(char_type *s, size_t n, char_type a);
83+
84+
static int_type not_eof(const int_type &c);
85+
static char_type to_char_type(const int_type &c);
86+
static int_type to_int_type(const char_type &c);
87+
static bool eq_int_type(const int_type &c1, const int_type &c2);
88+
static int_type eof();
89+
};
1090

1191
template <class T> class allocator {
1292
public:
@@ -114,14 +194,15 @@ public:
114194
basic_string &replace(size_type pos, size_type n1, size_type n2, charT c);
115195
basic_string &replace(__const_iterator i1, __const_iterator i2,
116196
const basic_string &str);
117-
basic_string &replace(__const_iterator i1, __const_iterator i2, const charT *s,
118-
size_type n);
119-
basic_string &replace(__const_iterator i1, __const_iterator i2, const charT *s);
197+
basic_string &replace(__const_iterator i1, __const_iterator i2,
198+
const charT *s, size_type n);
199+
basic_string &replace(__const_iterator i1, __const_iterator i2,
200+
const charT *s);
120201
basic_string &replace(__const_iterator i1, __const_iterator i2, size_type n,
121202
charT c);
122203
template <class InputIterator>
123-
basic_string &replace(__const_iterator i1, __const_iterator i2, InputIterator j1,
124-
InputIterator j2);
204+
basic_string &replace(__const_iterator i1, __const_iterator i2,
205+
InputIterator j1, InputIterator j2);
125206
basic_string &replace(__const_iterator, __const_iterator,
126207
initializer_list<charT>);
127208

0 commit comments

Comments
 (0)