Mercurial > projects > dynamin
annotate dynamin/core/global.d @ 106:acdbb30fee7e
Port to D2.
Most of the effort was dealing with immutable and const.
author | Jordan Miner <jminer7@gmail.com> |
---|---|
date | Mon, 17 Dec 2012 23:41:50 -0600 |
parents | 5c8c1c2e12c0 |
children |
rev | line source |
---|---|
0 | 1 |
2 /* | |
103
73060bc3f004
Change license to Boost 1.0 and MPL 2.0.
Jordan Miner <jminer7@gmail.com>
parents:
3
diff
changeset
|
3 * Copyright Jordan Miner |
0 | 4 * |
103
73060bc3f004
Change license to Boost 1.0 and MPL 2.0.
Jordan Miner <jminer7@gmail.com>
parents:
3
diff
changeset
|
5 * Distributed under the Boost Software License, Version 1.0. |
73060bc3f004
Change license to Boost 1.0 and MPL 2.0.
Jordan Miner <jminer7@gmail.com>
parents:
3
diff
changeset
|
6 * (See accompanying file BOOST_LICENSE.txt or copy at |
73060bc3f004
Change license to Boost 1.0 and MPL 2.0.
Jordan Miner <jminer7@gmail.com>
parents:
3
diff
changeset
|
7 * http://www.boost.org/LICENSE_1_0.txt) |
0 | 8 * |
9 */ | |
10 | |
11 module dynamin.core.global; | |
12 | |
13 import dynamin.core.string; | |
14 import tango.math.Math; | |
15 import tango.io.model.IFile; | |
16 import tango.core.Exception; | |
17 | |
18 public import tango.util.Convert; | |
19 | |
20 static if((void*).sizeof == 4) { | |
21 /** | |
22 * Defined as an int on 32-bit platforms and as a long on 64-bit platforms. | |
23 */ | |
24 alias int word; | |
25 /** | |
26 * Defined as a uint on 32-bit platforms and | |
27 * as a ulong on 64-bit platforms. | |
28 */ | |
29 alias uint uword; | |
30 } else static if((void*).sizeof == 8) { | |
31 /** | |
32 * Defined as an int on 32-bit platforms and as a long on 64-bit platforms. | |
33 */ | |
34 alias long word; | |
35 /** | |
36 * Defined as a uint on 32-bit platforms and | |
37 * as a ulong on 64-bit platforms. | |
38 */ | |
39 alias ulong uword; | |
40 } | |
41 | |
42 /** | |
43 * The string used to separate lines. | |
44 * This is "\r\n" under Windows and "\n" under Linux. | |
45 */ | |
106 | 46 enum string LineSeparator = FileConst.NewlineString; |
0 | 47 /** |
48 * The string used to separate directories in a path. | |
49 * This is "\\" under Windows and "/" under Linux. | |
50 */ | |
106 | 51 enum string DirSeparator = FileConst.PathSeparatorString; |
0 | 52 /// |
106 | 53 enum char DirSeparatorChar = FileConst.PathSeparatorChar; |
0 | 54 /** |
55 * The string used to separate paths. | |
56 * This is ";" under Windows and ":" under Linux | |
57 */ | |
106 | 58 enum string PathSeparator = FileConst.SystemPathString; |
0 | 59 /// |
106 | 60 enum char PathSeparatorChar = FileConst.SystemPathChar; |
0 | 61 |
62 /** | |
63 * Tests whether num1 and num2 are equal. They are considered equal | |
64 * if the difference between them is less than epsilon. | |
65 * Examples: | |
66 * ----- | |
67 * floatsEqual(3.14, 3.2, 0.1) == true | |
68 * floatsEqual(3.14, 3.3, 0.1) == false | |
69 * floatsEqual(3.14, 3.151, 0.01) == false | |
70 * ----- | |
71 */ | |
104
5c8c1c2e12c0
Change from real to double.
Jordan Miner <jminer7@gmail.com>
parents:
103
diff
changeset
|
72 bool floatsEqual(double num1, double num2, double epsilon) { |
0 | 73 return abs(num1 - num2) <= epsilon; |
74 } | |
75 unittest { | |
76 assert(floatsEqual(3.14, 3.2, 0.1) == true); | |
77 assert(floatsEqual(3.14, 3.3, 0.1) == false); | |
78 assert(floatsEqual(3.14, 3.151, 0.01) == false); | |
79 } | |
80 | |
81 /** | |
82 * Copies length elements starting at srcStart in srcData to destStart | |
83 * in destData. Data is copied as if srcData and destData are two separate | |
84 * arrays, even if they are the same. | |
85 */ | |
86 void arrayCopy(T)(T[] srcData, uint srcStart, T[] destData, uint destStart, uint length) { | |
87 if((srcData is destData && srcStart == destStart) || length == 0) | |
88 return; | |
89 if(srcStart > destStart) { | |
90 //copy forward | |
91 for(int i = 0; i < length; ++i) | |
92 destData[destStart + i] = srcData[srcStart + i]; | |
93 } else { | |
94 //copy reverse | |
95 for(int i = length-1; i >= 0; --i) | |
96 destData[destStart + i] = srcData[srcStart + i]; | |
97 } | |
98 } | |
99 unittest { | |
100 char[] c = "Computer".dup; | |
101 arrayCopy!(char)(c, 3, c, 2, 4); | |
102 assert(c == "Coputeer"); | |
103 c = "Computer".dup; | |
104 arrayCopy!(char)(c, 2, c, 3, 4); | |
105 assert(c == "Commputr"); | |
106 c = "hi".dup; | |
107 arrayCopy!(char)(c, 1, c, 0, 1); | |
108 assert(c == "ii"); | |
109 } | |
110 | |
111 /** | |
112 * Sets every byte of the specified memory block to value. | |
113 */ | |
114 void memoryFill(void* mem, uword count, ubyte value) { | |
115 ubyte* memB = cast(ubyte*)mem; | |
116 while(count != 0) { | |
117 *memB++ = value; | |
118 --count; | |
119 } | |
120 } | |
121 unittest { | |
122 char[] buff = "jEdit".dup; | |
123 memoryFill(buff.ptr+1, 3, 0x23); | |
124 assert(buff == "j\x23\x23\x23t"); | |
125 } | |
126 | |
127 /** | |
128 * Sets every byte of the specified memory block to zero. | |
129 */ | |
130 void memoryZero(void* mem, uword count) { | |
131 memoryFill(mem, count, 0); | |
132 } | |
133 unittest { | |
134 char[] buff = "jEdit".dup; | |
135 memoryZero(buff.ptr+1, 3); | |
136 assert(buff == "j\0\0\0t"); | |
137 } | |
138 | |
139 /** | |
140 * Copies the specified number of bytes from srcMem to destMem. The source | |
141 * and destination should not overlap, or the results will be undefined. | |
142 * Note that the source and destination parameters are opposite in | |
143 * order from the C function memcpy(). If count is a multiple of the | |
144 * native pointer size, the copy will be done in blocks of that size. | |
145 */ | |
146 void memoryCopy(void* srcMem, void* destMem, uword count) { | |
147 // copy in blocks of the pointer size, if possible | |
148 if(count % word.sizeof == 0) { | |
149 count /= word.sizeof; | |
150 uword* src = cast(uword*)srcMem; | |
151 uword* dest = cast(uword*)destMem; | |
152 while(count != 0) { | |
153 *dest++ = *src++; | |
154 --count; | |
155 } | |
156 } else { | |
157 ubyte* src = cast(ubyte*)srcMem; | |
158 ubyte* dest = cast(ubyte*)destMem; | |
159 while(count != 0) { | |
160 *dest++ = *src++; | |
161 --count; | |
162 } | |
163 } | |
164 } | |
165 unittest { | |
166 char[] buff = "Hello".dup; | |
167 memoryCopy(buff.ptr, buff.ptr+3, 2); | |
168 assert(buff == "HelHe"); | |
169 buff = "Longer text here".dup; | |
170 memoryCopy(buff.ptr+7, buff.ptr+12, 4); | |
171 assert(buff == "Longer text text"); | |
172 } | |
173 | |
174 /** | |
175 * Converts a number into its roman numeral form. The number must | |
176 * be between 0 and 3,999, inclusive. | |
177 * Examples: | |
178 * ----- | |
179 * toRomanNumerals(2) == "II" | |
180 * toRomanNumerals(58) == "LVIII" | |
181 * toRomanNumerals(194) == "CXCIV" | |
182 * ----- | |
183 */ | |
106 | 184 mstring toRomanNumerals(int num) { |
0 | 185 if(num > 3999 || num < 0) |
106 | 186 throw new IllegalArgumentException("toRomanNumerals():" ~ |
0 | 187 "highest convertable roman numeral is 3999"); |
2
fab13ec910ba
Speed up toRomanNumerals() by removing heap allocations. Much faster for Tango's debug build.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
188 static combos = [[0][0..0], [0], [0,0], [0,0,0], [0,1], |
0 | 189 [1], [1,0], [1,0,0], [1,0,0,0], [0,2]]; |
3
d806edad4300
Shorten code: ['I', 'V'] -> "IV"
Jordan Miner <jminer7@gmail.com>
parents:
2
diff
changeset
|
190 static letters = "IVXLCDM"; |
106 | 191 mstring str; |
0 | 192 int letterOffset = 0; |
193 while(num > 0) { | |
194 foreach_reverse(int c; combos[num % 10]) | |
195 str = letters[c+letterOffset] ~ str; | |
196 num /= 10; | |
197 letterOffset += 2; | |
198 } | |
199 return str; | |
200 } | |
201 unittest { | |
202 assert(toRomanNumerals(2) == "II"); | |
203 assert(toRomanNumerals(58) == "LVIII"); | |
204 assert(toRomanNumerals(194) == "CXCIV"); | |
205 | |
206 assert(toRomanNumerals(0) == ""); | |
207 assert(toRomanNumerals(1) == "I"); | |
208 assert(toRomanNumerals(10) == "X"); | |
209 assert(toRomanNumerals(500) == "D"); | |
210 assert(toRomanNumerals(18) == "XVIII"); | |
211 assert(toRomanNumerals(3949) == "MMMCMXLIX"); | |
212 } | |
213 | |
106 | 214 private int numeralToValue(char c) { |
0 | 215 switch(c) { |
216 case 'I': case 'i': return 1; | |
217 case 'V': case 'v': return 5; | |
218 case 'X': case 'x': return 10; | |
219 case 'L': case 'l': return 50; | |
220 case 'C': case 'c': return 100; | |
221 case 'D': case 'd': return 500; | |
222 case 'M': case 'm': return 1000; | |
223 default: return -1; | |
224 } | |
225 } | |
226 /** | |
227 * Parses the specified string of roman numerals and returns the value. | |
228 * The value must be less than or equal to 3,999. The string may be uppercase, | |
229 * lowercase, or a mixture of both cases. | |
230 * Examples: | |
231 * ----- | |
232 * parseRomanNumerals("II") == 2 | |
233 * parseRomanNumerals("LVIII") == 58 | |
234 * parseRomanNumerals("CXCIV") == 194 | |
235 * parseRomanNumerals("xxxxviiii") == 49 | |
236 * ----- | |
237 */ | |
106 | 238 int parseRomanNumerals(cstring str) { |
0 | 239 int num = 0; |
240 int largestSoFar = 1; | |
241 foreach_reverse(c; str) { | |
242 int value = numeralToValue(c); | |
243 if(value == -1) | |
244 throw new IllegalArgumentException("Invalid roman numeral: " ~ c); | |
245 if(value < largestSoFar) { | |
246 num -= value; | |
247 } else { | |
248 num += value; | |
249 largestSoFar = value; | |
250 } | |
251 } | |
252 if(num > 3999 || num < 0) | |
106 | 253 throw new IllegalArgumentException("parseRomanNumerals():" ~ |
0 | 254 "highest convertable roman numeral is 3999"); |
255 return num; | |
256 } | |
257 unittest { | |
258 assert(parseRomanNumerals("II") == 2); | |
259 assert(parseRomanNumerals("LVIII") == 58); | |
260 assert(parseRomanNumerals("CXCIV") == 194); | |
261 assert(parseRomanNumerals("xxxxviiii") == 49); | |
262 | |
263 assert(parseRomanNumerals("") == 0); | |
264 assert(parseRomanNumerals("I") == 1); | |
265 assert(parseRomanNumerals("X") == 10); | |
266 assert(parseRomanNumerals("D") == 500); | |
267 assert(parseRomanNumerals("XVIII") == 18); | |
268 assert(parseRomanNumerals("MMMCMXLIX") == 3949); | |
269 } | |
270 unittest { | |
271 for(int i = 0; i < 4000; ++i) | |
272 assert(toRomanNumerals(i).parseRomanNumerals() == i); | |
273 } | |
274 | |
275 /** | |
276 * Converts a number of bytes into a human friendly string. The units | |
277 * supported are bytes, KB, MB, GB, TB, PB, EB, ZB, and YB. | |
278 * Examples: | |
279 * ----- | |
280 * byteCountToString(202) == "202 bytes" | |
281 * byteCountToString(1021) == "1021 bytes" | |
282 * byteCountToString(106_496) == "104 KB" | |
283 * byteCountToString(620_705_792) == "591 MB" | |
284 * ----- | |
285 */ | |
106 | 286 mstring byteCountToString(ulong num) { |
287 enum factor = 1024; | |
0 | 288 //kilo, mega, giga, tera, peta, exa, zetta, yotta |
106 | 289 string[] units = [ |
0 | 290 " bytes", " KB", " MB", " GB", " TB", " PB", " EB", " ZB", " YB"]; |
291 uint unitIndex = 0; | |
292 ulong div = factor; | |
293 uint rem; | |
294 while(num > factor-1 && unitIndex < units.length-1) { | |
295 rem = num % factor; | |
296 num /= factor; | |
297 ++unitIndex; | |
298 } | |
299 //rem/1024 equals the fraction of unit | |
106 | 300 mstring str = to!(mstring)(num); |
0 | 301 if(str.length < 3) { |
106 | 302 str ~= "." ~ to!(mstring)(rem*10/factor); |
0 | 303 } |
304 str ~= units[unitIndex]; | |
305 return str; | |
306 } | |
307 unittest { | |
308 assert(byteCountToString(202) == "202 bytes"); | |
309 assert(byteCountToString(1021) == "1021 bytes"); | |
310 assert(byteCountToString(106_496) == "104 KB"); | |
311 assert(byteCountToString(620_705_792) == "591 MB"); | |
312 } | |
313 |