comparison 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
comparison
equal deleted inserted replaced
105:97997a544ac0 106:acdbb30fee7e
41 41
42 /** 42 /**
43 * The string used to separate lines. 43 * The string used to separate lines.
44 * This is "\r\n" under Windows and "\n" under Linux. 44 * This is "\r\n" under Windows and "\n" under Linux.
45 */ 45 */
46 const string LineSeparator = FileConst.NewlineString; 46 enum string LineSeparator = FileConst.NewlineString;
47 /** 47 /**
48 * The string used to separate directories in a path. 48 * The string used to separate directories in a path.
49 * This is "\\" under Windows and "/" under Linux. 49 * This is "\\" under Windows and "/" under Linux.
50 */ 50 */
51 const string DirSeparator = FileConst.PathSeparatorString; 51 enum string DirSeparator = FileConst.PathSeparatorString;
52 /// 52 ///
53 const char DirSeparatorChar = FileConst.PathSeparatorChar; 53 enum char DirSeparatorChar = FileConst.PathSeparatorChar;
54 /** 54 /**
55 * The string used to separate paths. 55 * The string used to separate paths.
56 * This is ";" under Windows and ":" under Linux 56 * This is ";" under Windows and ":" under Linux
57 */ 57 */
58 const string PathSeparator = FileConst.SystemPathString; 58 enum string PathSeparator = FileConst.SystemPathString;
59 /// 59 ///
60 const char PathSeparatorChar = FileConst.SystemPathChar; 60 enum char PathSeparatorChar = FileConst.SystemPathChar;
61 61
62 /** 62 /**
63 * Tests whether num1 and num2 are equal. They are considered equal 63 * Tests whether num1 and num2 are equal. They are considered equal
64 * if the difference between them is less than epsilon. 64 * if the difference between them is less than epsilon.
65 * Examples: 65 * Examples:
179 * toRomanNumerals(2) == "II" 179 * toRomanNumerals(2) == "II"
180 * toRomanNumerals(58) == "LVIII" 180 * toRomanNumerals(58) == "LVIII"
181 * toRomanNumerals(194) == "CXCIV" 181 * toRomanNumerals(194) == "CXCIV"
182 * ----- 182 * -----
183 */ 183 */
184 string toRomanNumerals(int num) { 184 mstring toRomanNumerals(int num) {
185 if(num > 3999 || num < 0) 185 if(num > 3999 || num < 0)
186 throw new IllegalArgumentException("ToRomanNumerals():" ~ 186 throw new IllegalArgumentException("toRomanNumerals():" ~
187 "highest convertable roman numeral is 3999"); 187 "highest convertable roman numeral is 3999");
188 static combos = [[0][0..0], [0], [0,0], [0,0,0], [0,1], 188 static combos = [[0][0..0], [0], [0,0], [0,0,0], [0,1],
189 [1], [1,0], [1,0,0], [1,0,0,0], [0,2]]; 189 [1], [1,0], [1,0,0], [1,0,0,0], [0,2]];
190 static letters = "IVXLCDM"; 190 static letters = "IVXLCDM";
191 string str = ""; 191 mstring str;
192 int letterOffset = 0; 192 int letterOffset = 0;
193 while(num > 0) { 193 while(num > 0) {
194 foreach_reverse(int c; combos[num % 10]) 194 foreach_reverse(int c; combos[num % 10])
195 str = letters[c+letterOffset] ~ str; 195 str = letters[c+letterOffset] ~ str;
196 num /= 10; 196 num /= 10;
209 assert(toRomanNumerals(500) == "D"); 209 assert(toRomanNumerals(500) == "D");
210 assert(toRomanNumerals(18) == "XVIII"); 210 assert(toRomanNumerals(18) == "XVIII");
211 assert(toRomanNumerals(3949) == "MMMCMXLIX"); 211 assert(toRomanNumerals(3949) == "MMMCMXLIX");
212 } 212 }
213 213
214 int numeralToValue(char c) { 214 private int numeralToValue(char c) {
215 switch(c) { 215 switch(c) {
216 case 'I': case 'i': return 1; 216 case 'I': case 'i': return 1;
217 case 'V': case 'v': return 5; 217 case 'V': case 'v': return 5;
218 case 'X': case 'x': return 10; 218 case 'X': case 'x': return 10;
219 case 'L': case 'l': return 50; 219 case 'L': case 'l': return 50;
233 * parseRomanNumerals("LVIII") == 58 233 * parseRomanNumerals("LVIII") == 58
234 * parseRomanNumerals("CXCIV") == 194 234 * parseRomanNumerals("CXCIV") == 194
235 * parseRomanNumerals("xxxxviiii") == 49 235 * parseRomanNumerals("xxxxviiii") == 49
236 * ----- 236 * -----
237 */ 237 */
238 int parseRomanNumerals(string str) { 238 int parseRomanNumerals(cstring str) {
239 int num = 0; 239 int num = 0;
240 int largestSoFar = 1; 240 int largestSoFar = 1;
241 foreach_reverse(c; str) { 241 foreach_reverse(c; str) {
242 int value = numeralToValue(c); 242 int value = numeralToValue(c);
243 if(value == -1) 243 if(value == -1)
248 num += value; 248 num += value;
249 largestSoFar = value; 249 largestSoFar = value;
250 } 250 }
251 } 251 }
252 if(num > 3999 || num < 0) 252 if(num > 3999 || num < 0)
253 throw new IllegalArgumentException("ParseRomanNumerals():" ~ 253 throw new IllegalArgumentException("parseRomanNumerals():" ~
254 "highest convertable roman numeral is 3999"); 254 "highest convertable roman numeral is 3999");
255 return num; 255 return num;
256 } 256 }
257 unittest { 257 unittest {
258 assert(parseRomanNumerals("II") == 2); 258 assert(parseRomanNumerals("II") == 2);
281 * byteCountToString(1021) == "1021 bytes" 281 * byteCountToString(1021) == "1021 bytes"
282 * byteCountToString(106_496) == "104 KB" 282 * byteCountToString(106_496) == "104 KB"
283 * byteCountToString(620_705_792) == "591 MB" 283 * byteCountToString(620_705_792) == "591 MB"
284 * ----- 284 * -----
285 */ 285 */
286 string byteCountToString(ulong num) { 286 mstring byteCountToString(ulong num) {
287 const factor = 1024; 287 enum factor = 1024;
288 //kilo, mega, giga, tera, peta, exa, zetta, yotta 288 //kilo, mega, giga, tera, peta, exa, zetta, yotta
289 char[][] units = [ 289 string[] units = [
290 " bytes", " KB", " MB", " GB", " TB", " PB", " EB", " ZB", " YB"]; 290 " bytes", " KB", " MB", " GB", " TB", " PB", " EB", " ZB", " YB"];
291 uint unitIndex = 0; 291 uint unitIndex = 0;
292 ulong div = factor; 292 ulong div = factor;
293 uint rem; 293 uint rem;
294 while(num > factor-1 && unitIndex < units.length-1) { 294 while(num > factor-1 && unitIndex < units.length-1) {
295 rem = num % factor; 295 rem = num % factor;
296 num /= factor; 296 num /= factor;
297 ++unitIndex; 297 ++unitIndex;
298 } 298 }
299 //rem/1024 equals the fraction of unit 299 //rem/1024 equals the fraction of unit
300 string str = to!(string)(num); 300 mstring str = to!(mstring)(num);
301 if(str.length < 3) { 301 if(str.length < 3) {
302 str ~= "." ~ to!(string)(rem*10/factor); 302 str ~= "." ~ to!(mstring)(rem*10/factor);
303 } 303 }
304 str ~= units[unitIndex]; 304 str ~= units[unitIndex];
305 return str; 305 return str;
306 } 306 }
307 unittest { 307 unittest {