ESBMC
Efficient SMT-based Context-Bounded Model Checker
Loading...
Searching...
No Matches
char_traits.h
Go to the documentation of this file.
1// std::char_traits, shared by the string, string_view and stream OMs.
2//
3// Lifted out of <string> unchanged. It lived there, but <string> includes
4// <string_view>, so <string_view> could not reach it, and the stream OMs and
5// <iterator> only ever forward-declared it to name it as a default template
6// argument. Any program that touched a member from those headers -- fmt and
7// boost lexical_cast do -- failed to parse. See #5868.
8//
9// The `Traits = char_traits<CharT>` defaults stay with the templates that
10// declare them; repeating one here would be a second default for the same
11// parameter.
12#pragma once
13
14#include <cstddef> /* size_t, ptrdiff_t */
15#include <cstdio> /* EOF */
16#include <cstring> /* memcpy, memmove */
17
18#include "OM_compiler_defs.h"
19#include "min_max.h"
20
21namespace std
22{
23template <class charT>
25{
26 typedef charT char_type;
27 typedef int int_type;
28 typedef std::ptrdiff_t off_type;
29 typedef std::size_t pos_type;
30
31 static void assign(char_type &c1, const char_type &c2) OM_NOEXCEPT
32 {
33 c1 = c2;
34 }
35
36 static char_type *assign(char_type *s, std::size_t n, char_type a)
37 {
38 for (std::size_t i = 0; i < n; ++i)
39 s[i] = a;
40 return s;
41 }
42
44 {
45 return c1 == c2;
46 }
47
49 {
50 return c1 < c2;
51 }
52
53 static int compare(const char_type *s1, const char_type *s2, std::size_t n)
54 {
55 for (std::size_t i = 0; i < n; ++i)
56 {
57 if (lt(s1[i], s2[i]))
58 return -1;
59 if (lt(s2[i], s1[i]))
60 return 1;
61 }
62 return 0;
63 }
64
65 static OM_CONSTEXPR17 std::size_t length(const char_type *s)
66 {
67 std::size_t i = 0;
68 while (!eq(s[i], char_type(0)))
69 ++i;
70 return i;
71 }
72
73 static const char_type *
74 find(const char_type *s, std::size_t n, const char_type &a)
75 {
76 for (std::size_t i = 0; i < n; ++i)
77 if (eq(s[i], a))
78 return s + i;
79 return OM_NULLPTR;
80 }
81
82 static char_type *move(char_type *dest, const char_type *src, std::size_t n)
83 {
84 return static_cast<char_type *>(memmove(dest, src, n * sizeof(charT)));
85 }
86
87 static char_type *copy(char_type *dest, const char_type *src, std::size_t n)
88 {
89 return static_cast<char_type *>(memcpy(dest, src, n * sizeof(charT)));
90 }
91
93 {
94 return EOF;
95 }
96
98 {
99 return static_cast<unsigned char>(c);
100 }
101
103 {
104 return static_cast<char_type>(c);
105 }
106
108 {
109 return c1 == c2;
110 }
111
113 {
114 return (c == eof()) ? 0 : c;
115 }
116};
117
118// [char.traits.specializations.char]: "The two-argument members eq and lt are
119// defined identically to the built-in operators == and < for type unsigned
120// char." The generic template compares as char_type, which orders anything
121// above 0x7f wrongly wherever char is signed -- and compare(), hence
122// std::string's ordering, is built on lt. See #7048.
123template <>
124inline OM_CONSTEXPR bool char_traits<char>::eq(char c1, char c2) OM_NOEXCEPT
125{
126 return static_cast<unsigned char>(c1) == static_cast<unsigned char>(c2);
127}
128
129template <>
130inline OM_CONSTEXPR bool char_traits<char>::lt(char c1, char c2) OM_NOEXCEPT
131{
132 return static_cast<unsigned char>(c1) < static_cast<unsigned char>(c2);
133}
134} // namespace std
#define OM_NULLPTR
Definition OM_compiler_defs.h:19
#define OM_CONSTEXPR
Definition OM_compiler_defs.h:17
#define OM_CONSTEXPR17
Definition OM_compiler_defs.h:28
#define OM_NOEXCEPT
Definition OM_compiler_defs.h:18
typet char_type()
Definition c_types.cpp:166
Definition exception.cpp:51
#define EOF
Definition stdio.h:33
void * memmove(void *dest, const void *src, size_t n)
Definition string.c:389
void * memcpy(void *__ESBMC_restrict dst, const void *__ESBMC_restrict src, size_t n)
Definition char_traits.h:25
static char_type * copy(char_type *dest, const char_type *src, std::size_t n)
Definition char_traits.h:87
static OM_CONSTEXPR char_type to_char_type(int_type c) OM_NOEXCEPT
Definition char_traits.h:102
static const char_type * find(const char_type *s, std::size_t n, const char_type &a)
Definition char_traits.h:74
static char_type * assign(char_type *s, std::size_t n, char_type a)
Definition char_traits.h:36
static void assign(char_type &c1, const char_type &c2) OM_NOEXCEPT
Definition char_traits.h:31
static OM_CONSTEXPR int_type to_int_type(char_type c) OM_NOEXCEPT
Definition char_traits.h:97
static OM_CONSTEXPR int_type eof() OM_NOEXCEPT
Definition char_traits.h:92
static OM_CONSTEXPR bool eq(char_type c1, char_type c2) OM_NOEXCEPT
Definition char_traits.h:43
std::ptrdiff_t off_type
Definition char_traits.h:28
std::size_t pos_type
Definition char_traits.h:29
static OM_CONSTEXPR bool eq_int_type(int_type c1, int_type c2) OM_NOEXCEPT
Definition char_traits.h:107
static OM_CONSTEXPR bool lt(char_type c1, char_type c2) OM_NOEXCEPT
Definition char_traits.h:48
static OM_CONSTEXPR int_type not_eof(int_type c) OM_NOEXCEPT
Definition char_traits.h:112
charT char_type
Definition char_traits.h:26
static int compare(const char_type *s1, const char_type *s2, std::size_t n)
Definition char_traits.h:53
int int_type
Definition char_traits.h:27
static OM_CONSTEXPR17 std::size_t length(const char_type *s)
Definition char_traits.h:65
static char_type * move(char_type *dest, const char_type *src, std::size_t n)
Definition char_traits.h:82