Skip to content

Commit 924f8c8

Browse files
UUID support
1 parent aeebf57 commit 924f8c8

File tree

4 files changed

+381
-136
lines changed

4 files changed

+381
-136
lines changed

source/numem/conv.d

Lines changed: 155 additions & 117 deletions
Original file line numberDiff line numberDiff line change
@@ -9,148 +9,186 @@
99
Utilities for converting between some basic types
1010
*/
1111
module numem.conv;
12+
import numem.all;
13+
import core.stdc.stdlib;
14+
import core.stdc.stdio : snprintf;
15+
import std.traits;
16+
import numem.core.exception;
1217

13-
version(NoC) {
14-
// If there's no C this should be disabled.
18+
@nogc:
1519

16-
} else {
17-
import numem.all;
18-
import core.stdc.stdlib;
19-
import core.stdc.stdio : snprintf;
20-
import std.traits;
21-
import numem.core.exception;
20+
/**
21+
Convert nstring to signed integer
22+
*/
23+
T toInt(T)(nstring str) nothrow if (isSigned!T && isIntegral!T) {
24+
return cast(T)atol(str.toCString());
25+
}
2226

23-
@nogc:
27+
/**
28+
Convert string slice to signed integer
29+
*/
30+
T toInt(T)(string str) nothrow if (isSigned!T && isIntegral!T) {
31+
return cast(T)strtol(str.ptr, str.ptr+str.length);
32+
}
2433

25-
/**
26-
Convert nstring to signed integer
27-
*/
28-
T toInt(T)(nstring str) nothrow if (isSigned!T && isIntegral!T) {
29-
return cast(T)atol(str.toCString());
30-
}
34+
/**
35+
Convert nstring to unsigned integer
36+
*/
37+
T toInt(T)(nstring str, int base) nothrow if (isUnsigned!T && isIntegral!T) {
38+
const(char)* ptr = str.toCString();
39+
return cast(T)strtoull(ptr, null, base);
40+
}
3141

32-
/**
33-
Convert string slice to signed integer
34-
*/
35-
T toInt(T)(string str) nothrow if (isSigned!T && isIntegral!T) {
36-
return cast(T)strtol(str.ptr, str.ptr+str.length);
37-
}
42+
/**
43+
Convert string slice to unsigned integer
44+
*/
45+
T toInt(T)(string str, int base) nothrow if (isUnsigned!T && isIntegral!T) {
46+
enum tSize = T.sizeof*2;
47+
size_t toCopy =
48+
str.length < tSize ?
49+
str.length :
50+
tSize;
51+
52+
nstring tmp;
53+
tmp ~= str[0..toCopy];
54+
return cast(T)strtoull(tmp.toCString(), null, base);
55+
}
3856

39-
/**
40-
Convert nstring to unsigned integer
41-
*/
42-
T toInt(T)(nstring str, int base) nothrow if (isUnsigned!T && isIntegral!T) {
43-
const(char)* ptr = str.toCString();
44-
return cast(T)strtoull(ptr, ptr+str.size(), base);
45-
}
57+
/**
58+
Convert nstring to float
59+
*/
60+
T toFloat(T)(nstring str) nothrow if (isFloatingPoint!T) {
61+
return cast(T)atof(str.toCString());
62+
}
4663

47-
/**
48-
Convert string slice to unsigned integer
49-
*/
50-
T toInt(T)(string str, int base) nothrow if (isUnsigned!T && isIntegral!T) {
51-
return cast(T)strtoull(str.ptr, str.ptr+str.length, base);
52-
}
64+
/**
65+
Convert string slice to float
66+
*/
67+
T toFloat(T)(string str) nothrow if (isFloatingPoint!T) {
68+
return cast(T)strtof(str.ptr, str.ptr+str.length);
69+
}
5370

54-
/**
55-
Convert nstring to float
56-
*/
57-
T toFloat(T)(nstring str) nothrow if (isFloatingPoint!T) {
58-
return cast(T)atof(str.toCString());
71+
/**
72+
Converts an integer type into a hex string
73+
*/
74+
nstring toHexString(T, bool upper=false)(T num, bool pad=false) if (isIntegral!T && isUnsigned!T) {
75+
const string hexStr = upper ? "0123456789ABCDEF" : "0123456789abcdef";
76+
nstring str;
77+
78+
ptrdiff_t i = T.sizeof*2;
79+
while(num > 0) {
80+
str ~= hexStr[num % 16];
81+
i--;
82+
num /= 16;
5983
}
6084

61-
/**
62-
Convert string slice to float
63-
*/
64-
T toFloat(T)(string str) nothrow if (isFloatingPoint!T) {
65-
return cast(T)strtof(str.ptr, str.ptr+str.length);
85+
str.reverse();
86+
87+
// Add padding
88+
if (pad) {
89+
nstring padding;
90+
while(i-- > 0)
91+
padding ~= '0';
92+
93+
padding ~= str;
94+
return padding;
6695
}
6796

68-
/**
69-
Convert signed integers to nstring
70-
*/
71-
nstring toString(T)(T item) if (isSigned!T && isIntegral!T && !is(T == enum)) {
72-
nstring str;
97+
// No padding
98+
return str;
99+
}
73100

74-
size_t count = snprintf(null, 0, "%lli", cast(ulong)item);
75-
str.resize(count);
101+
@("conv: toHexString")
102+
unittest {
103+
nstring hexstr = 0xDEADBEEF.toHexString();
104+
assert(hexstr == "deadbeef", hexstr[]);
105+
}
76106

77-
char* istr = cast(char*)str.toCString();
78-
snprintf(istr, count+1, "%lli", cast(ulong)item);
79-
return str;
80-
}
107+
/**
108+
Convert signed integers to nstring
109+
*/
110+
nstring toString(T)(T item) if (isSigned!T && isIntegral!T && !is(T == enum)) {
111+
nstring str;
81112

82-
/**
83-
Convert unsigned integers to nstring
84-
*/
85-
nstring toString(T)(T item) if (isUnsigned!T && isIntegral!T && !is(T == enum)) {
86-
nstring str;
113+
size_t count = snprintf(null, 0, "%lli", cast(ulong)item);
114+
str.resize(count);
87115

88-
size_t count = snprintf(null, 0, "%llu", cast(ulong)item);
89-
str.resize(count);
116+
char* istr = cast(char*)str.toCString();
117+
snprintf(istr, count+1, "%lli", cast(ulong)item);
118+
return str;
119+
}
90120

91-
char* istr = cast(char*)str.toCString();
92-
snprintf(istr, count+1, "%llu", cast(ulong)item);
93-
return str;
94-
}
121+
/**
122+
Convert unsigned integers to nstring
123+
*/
124+
nstring toString(T)(T item) if (isUnsigned!T && isIntegral!T && !is(T == enum)) {
125+
nstring str;
95126

96-
/**
97-
Convert floating point numbers to nstring
98-
*/
99-
nstring toString(T)(T item) if (isFloatingPoint!T) {
100-
nstring str;
127+
size_t count = snprintf(null, 0, "%llu", cast(ulong)item);
128+
str.resize(count);
101129

102-
size_t count = snprintf(null, 0, "%lf", item);
103-
str.resize(count);
130+
char* istr = cast(char*)str.toCString();
131+
snprintf(istr, count+1, "%llu", cast(ulong)item);
132+
return str;
133+
}
104134

105-
char* istr = cast(char*)str.toCString();
106-
snprintf(istr, count+1, "%lf", item);
107-
return str;
108-
}
135+
/**
136+
Convert floating point numbers to nstring
137+
*/
138+
nstring toString(T)(T item) if (isFloatingPoint!T) {
139+
nstring str;
109140

110-
/**
111-
Convert bool to nstring
112-
*/
113-
nstring toString(T)(T item) if (is(T == bool)) {
114-
return item ? nstring("true") : nstring("false");
115-
}
141+
size_t count = snprintf(null, 0, "%lf", item);
142+
str.resize(count);
116143

117-
/**
118-
Convert enum to nstring
119-
*/
120-
nstring toString(T)(T item) if (is(T == enum)) {
121-
static foreach(member; EnumMembers!T) {
122-
if (item == member) return nstring(member.stringof);
123-
}
124-
throw nogc_new!NuException(nstring("Index out of range"));
125-
}
144+
char* istr = cast(char*)str.toCString();
145+
snprintf(istr, count+1, "%lf", item);
146+
return str;
147+
}
126148

127-
/**
128-
Allows types to implement a toNString function
129-
*/
130-
nstring toString(T)(T item) if (__traits(hasMember, T, "toNString")) {
131-
return item.toNString();
132-
}
149+
/**
150+
Convert bool to nstring
151+
*/
152+
nstring toString(T)(T item) if (is(T == bool)) {
153+
return item ? nstring("true") : nstring("false");
154+
}
133155

134-
@("toString")
135-
unittest {
136-
import core.stdc.stdio : printf;
137-
assert(32u.toString() == "32");
138-
assert(12.0f.toString() == "12.000000");
139-
assert(true.toString() == "true");
140-
assert(false.toString() == "false");
141-
assert((-10_000).toString() == "-10000");
156+
/**
157+
Convert enum to nstring
158+
*/
159+
nstring toString(T)(T item) if (is(T == enum)) {
160+
static foreach(member; EnumMembers!T) {
161+
if (item == member) return nstring(member.stringof);
142162
}
163+
throw nogc_new!NuException(nstring("Index out of range"));
164+
}
143165

144-
@("toString w/ enum")
145-
unittest {
146-
enum TestEnum {
147-
a,
148-
b,
149-
longerName
150-
}
151-
152-
assert(TestEnum.a.toString() == "a");
153-
assert(TestEnum.b.toString() == "b");
154-
assert(TestEnum.longerName.toString() == "longerName");
166+
/**
167+
Allows types to implement a toNString function
168+
*/
169+
nstring toString(T)(T item) if (__traits(hasMember, T, "toNString")) {
170+
return item.toNString();
171+
}
172+
173+
@("toString")
174+
unittest {
175+
import core.stdc.stdio : printf;
176+
assert(32u.toString() == "32");
177+
assert(12.0f.toString() == "12.000000");
178+
assert(true.toString() == "true");
179+
assert(false.toString() == "false");
180+
assert((-10_000).toString() == "-10000");
181+
}
182+
183+
@("toString w/ enum")
184+
unittest {
185+
enum TestEnum {
186+
a,
187+
b,
188+
longerName
155189
}
190+
191+
assert(TestEnum.a.toString() == "a");
192+
assert(TestEnum.b.toString() == "b");
193+
assert(TestEnum.longerName.toString() == "longerName");
156194
}

source/numem/core/random.d

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,14 @@ private:
9393
}
9494
public:
9595

96+
/**
97+
Constructs a random number generator with the current system clock as a seed
98+
*/
99+
this() {
100+
import core.stdc.time : time;
101+
this(cast(size_t)time(null));
102+
}
103+
96104
/**
97105
Constructs a random number generator with a set seed.
98106
*/

0 commit comments

Comments
 (0)