-
-
Notifications
You must be signed in to change notification settings - Fork 597
/
port.d
178 lines (156 loc) · 4.6 KB
/
port.d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/**
* Portable routines for functions that have different implementations on different platforms.
*
* Copyright: Copyright (C) 1999-2021 by The D Language Foundation, All Rights Reserved
* Authors: Walter Bright, http://www.digitalmars.com
* License: $(LINK2 http://www.boost.org/LICENSE_1_0.txt, Boost License 1.0)
* Source: $(LINK2 https://github.com/dlang/dmd/blob/master/src/dmd/root/port.d, root/_port.d)
* Documentation: https://dlang.org/phobos/dmd_root_port.html
* Coverage: https://codecov.io/gh/dlang/dmd/src/master/src/dmd/root/port.d
*/
module dmd.root.port;
import core.stdc.ctype;
import core.stdc.errno;
import core.stdc.string;
import core.stdc.stdio;
import core.stdc.stdlib;
nothrow @nogc:
private extern (C)
{
version(CRuntime_DigitalMars) __gshared extern const(char)* __locale_decpoint;
version(CRuntime_Microsoft)
{
enum _OVERFLOW = 3; /* overflow range error */
enum _UNDERFLOW = 4; /* underflow range error */
int _atoflt(float* value, const(char)* str);
int _atodbl(double* value, const(char)* str);
}
}
extern (C++) struct Port
{
nothrow @nogc:
static int memicmp(scope const char* s1, scope const char* s2, size_t n) pure
{
int result = 0;
foreach (i; 0 .. n)
{
char c1 = s1[i];
char c2 = s2[i];
result = c1 - c2;
if (result)
{
result = toupper(c1) - toupper(c2);
if (result)
break;
}
}
return result;
}
static char* strupr(char* s) pure
{
char* t = s;
while (*s)
{
*s = cast(char)toupper(*s);
s++;
}
return t;
}
static bool isFloat32LiteralOutOfRange(scope const(char)* s)
{
errno = 0;
version (CRuntime_DigitalMars)
{
auto save = __locale_decpoint;
__locale_decpoint = ".";
}
version (CRuntime_Microsoft)
{
float r;
int res = _atoflt(&r, s);
if (res == _UNDERFLOW || res == _OVERFLOW)
errno = ERANGE;
}
else
{
strtof(s, null);
}
version (CRuntime_DigitalMars) __locale_decpoint = save;
return errno == ERANGE;
}
static bool isFloat64LiteralOutOfRange(scope const(char)* s)
{
errno = 0;
version (CRuntime_DigitalMars)
{
auto save = __locale_decpoint;
__locale_decpoint = ".";
}
version (CRuntime_Microsoft)
{
double r;
int res = _atodbl(&r, s);
if (res == _UNDERFLOW || res == _OVERFLOW)
errno = ERANGE;
}
else
{
strtod(s, null);
}
version (CRuntime_DigitalMars) __locale_decpoint = save;
return errno == ERANGE;
}
// Little endian
static void writelongLE(uint value, scope void* buffer) pure
{
auto p = cast(ubyte*)buffer;
p[3] = cast(ubyte)(value >> 24);
p[2] = cast(ubyte)(value >> 16);
p[1] = cast(ubyte)(value >> 8);
p[0] = cast(ubyte)(value);
}
// Little endian
static uint readlongLE(scope const void* buffer) pure
{
auto p = cast(const ubyte*)buffer;
return (((((p[3] << 8) | p[2]) << 8) | p[1]) << 8) | p[0];
}
// Big endian
static void writelongBE(uint value, scope void* buffer) pure
{
auto p = cast(ubyte*)buffer;
p[0] = cast(ubyte)(value >> 24);
p[1] = cast(ubyte)(value >> 16);
p[2] = cast(ubyte)(value >> 8);
p[3] = cast(ubyte)(value);
}
// Big endian
static uint readlongBE(scope const void* buffer) pure
{
auto p = cast(const ubyte*)buffer;
return (((((p[0] << 8) | p[1]) << 8) | p[2]) << 8) | p[3];
}
// Little endian
static uint readwordLE(scope const void* buffer) pure
{
auto p = cast(const ubyte*)buffer;
return (p[1] << 8) | p[0];
}
// Big endian
static uint readwordBE(scope const void* buffer) pure
{
auto p = cast(const ubyte*)buffer;
return (p[0] << 8) | p[1];
}
static void valcpy(scope void *dst, ulong val, size_t size) pure
{
switch (size)
{
case 1: *cast(ubyte *)dst = cast(ubyte)val; break;
case 2: *cast(ushort *)dst = cast(ushort)val; break;
case 4: *cast(uint *)dst = cast(uint)val; break;
case 8: *cast(ulong *)dst = cast(ulong)val; break;
default: assert(0);
}
}
}