Changeset 361
- Timestamp:
- 06/17/08 20:44:12 (6 months ago)
- Files:
-
- trunk/tools/tools/base.d (modified) (6 diffs)
- trunk/tools/tools/downloader.d (modified) (2 diffs)
- trunk/tools/tools/ini.d (modified) (2 diffs)
- trunk/tools/tools/log.d (modified) (1 diff)
- trunk/tools/tools/mersenne.d (modified) (2 diffs)
- trunk/tools/tools/stackthreads_impl.d (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/tools/tools/base.d
r360 r361 361 361 } 362 362 363 template CarefulReplace(string S, string WHAT, string WITH ) {364 const Pos = FindOr!(S, WHAT, -1);365 static if ( Pos == -1) const string repl = S;363 template CarefulReplace(string S, string WHAT, string WITH, int OFFS = 0) { 364 const _Pos = FindOr!(S[OFFS .. $], WHAT, -1); 365 static if (_Pos == -1) const string repl = S[OFFS .. $]; 366 366 else { 367 static if (Pos == 0) const string repl = WITH ~ CarefulReplace!(S[WHAT.length .. $], WHAT, WITH).repl; 367 const Pos = _Pos + OFFS; 368 static if (Pos == 0) const string repl = WITH ~ CarefulReplace!(S, WHAT, WITH, WHAT.length).repl; 368 369 else static if (Pos + WHAT.length == S.length) { // match is at very end of string 369 static if (S[Pos-1] == '.') const string repl = S ; // no changes370 else const string repl = CarefulReplace!(S[0 .. $-WHAT.length], WHAT, WITH ).repl ~ WITH;370 static if (S[Pos-1] == '.') const string repl = S[OFFS .. $]; // no changes 371 else const string repl = CarefulReplace!(S[0 .. $-WHAT.length], WHAT, WITH, OFFS).repl ~ WITH; 371 372 } else { 372 373 static if (!isCharacter!(S[Pos - 1]) && !isCharacter!(S[Pos + WHAT.length]) && S[Pos-1] != '.') 373 const string repl = S[ 0 .. Pos] ~ WITH ~ CarefulReplace!(S[Pos+WHAT.length .. $], WHAT, WITH).repl;374 const string repl = S[OFFS .. Pos] ~ WITH ~ CarefulReplace!(S, WHAT, WITH, Pos + WHAT.length).repl; 374 375 // else skip the replace 375 else const string repl = S[ 0 .. Pos+WHAT.length] ~ CarefulReplace!(S[Pos+WHAT.length .. $], WHAT, WITH).repl;376 else const string repl = S[OFFS .. Pos+WHAT.length] ~ CarefulReplace!(S, WHAT, WITH, Pos + WHAT.length).repl; 376 377 } 377 378 } … … 379 380 380 381 static assert(CarefulReplace!("foobar b", "b", "xx").repl == "foobar xx"); 382 static assert(CarefulReplace!("a.foobaa()", "a", "#").repl == "#.foobaa()"); 381 383 382 384 // V is the array/tuple used to store values, … … 672 674 } 673 675 674 template cutOff(string what, string where) { 675 static if (what.length<where.length) const string cutOff=what; 676 else static if (what[0..where.length]==where) const string cutOff=""; 677 else const string cutOff=what[0]~cutOff!(what[1..$], where); 676 string cutOff(string what, string where) { 677 if (what.length < where.length) return what; 678 for (int i = 0; i < what.length - where.length; ++i) { 679 if (what[i .. i+where.length] == where) return what[0 .. i]; 680 } 681 return what; 678 682 } 679 683 … … 707 711 708 712 string cut_off(string what, string where) { 713 if (what.length < where.length) return what; 709 714 for (int i = 0; i < what.length-where.length; ++i) { 710 715 if (what[i .. i+where.length] == where) return what[0 .. i]; … … 753 758 } else if (ch == ')') { // super done 754 759 assert(sup, "Parenthesis mismatch!"); 755 res_assign ~= "_"~cut_off(buffer, "=")~"); "; buffer = ""; 760 if (buffer.length) 761 res_assign ~= "_"~cut_off(buffer, "="); 762 res_assign ~= "); "; 763 buffer = ""; 756 764 sup = false; 757 765 } else if (ch == ',' || ch == ';') { // assign closing elements … … 994 1002 } 995 1003 res ~= "] "; 1004 } else static if (is(typeof(elem.classinfo))) { 1005 auto obj = cast(Object) elem; 1006 if (!obj) res ~= "(null)"; 1007 else res ~= obj.toString(); 996 1008 } else static if (is(typeof(elem.toString()): string)) { 997 1009 res ~= elem.toString(); trunk/tools/tools/downloader.d
r348 r361 246 246 ubyte digit=0; 247 247 foreach (ch; hex.tolower()) { 248 if (ch>='a' && ch<='f') digit=c h+10-'a'; else249 if (ch>='0' && ch<='9') digit=c h-'0'; else248 if (ch>='a' && ch<='f') digit=cast(ubyte) (ch+10-'a'); else 249 if (ch>='0' && ch<='9') digit=cast(ubyte) (ch-'0'); else 250 250 throw new Exception("Invalid character '"~ch~"' in hex string"); 251 251 res=res*16+digit; … … 262 262 } 263 263 received=received[line_end+2 .. $]; 264 chunks ~= received[0..c hars];265 received=received[c hars+2 .. $];264 chunks ~= received[0..cast(uint) chars]; 265 received=received[cast(uint) chars+2 .. $]; 266 266 return receiveChunked; 267 267 } else { trunk/tools/tools/ini.d
r360 r361 81 81 } 82 82 } 83 string[] section(string name) { 84 synchronized(this) { 85 string[] res; 86 bool found = false; 87 foreach (line; lines) { 88 if (found) { 89 if (line.length && line[0] == '[') return res; 90 else { 91 if (line.find("#") != -1) line = line[0 .. line.find("#")]; 92 res ~= line; 93 } 94 } else { 95 if (line.length && line[0] == '[') { 96 if (line[1 .. line.find("]")].strip() == name) found = true; 97 } 98 } 99 } 100 return res; 101 } 102 } 83 103 void set(T)(string section, string label, T what) { 84 104 synchronized(this) { … … 117 137 // :) 118 138 T get(T)(string section, string label, lazy T deflt) { 119 auto pos = findEntry(section, label); 120 if (pos == size_t.max) return deflt; 121 string value=lines[pos]; 122 value=value[value.find("=")+1..$].strip(); 123 static if(is(string: T)) return value; else 124 static if(is(T==bool)) { 125 if (value.tolower()=="true") return true; 126 if (value.tolower()=="false") return false; 127 throw new Exception("Cannot parse "~value~" as bool"); 128 } else 129 static if(is(T: long)||is(T: ulong)) return cast(T)(value.atoi()); else 130 static assert(false, "Invalid/unsupported type: "~T.stringof); 139 synchronized(this) { 140 auto pos = findEntry(section, label); 141 if (pos == size_t.max) return deflt; 142 string value=lines[pos]; 143 value=value[value.find("=")+1..$].strip(); 144 static if(is(string: T)) return value; else 145 static if(is(T==bool)) { 146 if (value.tolower()=="true") return true; 147 if (value.tolower()=="false") return false; 148 throw new Exception("Cannot parse "~value~" as bool"); 149 } else 150 static if(is(T: long)||is(T: ulong)) return cast(T)(value.atoi()); else 151 static assert(false, "Invalid/unsupported type: "~T.stringof); 152 } 131 153 } 132 154 //~this() { save; } trunk/tools/tools/log.d
r321 r361 34 34 else static if (is(typeof(part)==void *)) { _printf("(%p)", cast(ulong)part); } 35 35 else static if (isPointer!(typeof(part))) { _log(typeof(part).stringof, cast(void *)part); } 36 else static if (is(typeof(part )==class)||is(typeof(part)==interface)) {36 else static if (is(typeof(part.classinfo))) { 37 37 auto c=cast(Object) part; 38 38 if (c) _log(c.toString()); trunk/tools/tools/mersenne.d
r359 r361 4 4 const MATRIX_A=0x9908b0dfUL, UPPER_MASK=0x80000000UL, LOWER_MASK=0x7fffffffUL; 5 5 6 import tools.downloader, tools.base; 7 ubyte[] get_hotbits(int count) { 8 ubyte[] res; 9 while (res.length < count) { 10 logln("Downloading random data from Hotbits"); 11 res ~= cast(ubyte[]) download(Format("http://www.fourmilab.ch/cgi-bin/Hotbits?nbytes=", count-res.length, "&fmt=bin")); 12 } 13 return res; 14 } 15 6 16 final class Mersenne { 7 size_t mt[N];17 size_t[N] mt; 8 18 int mti=N; 9 19 void seed(uint s) { … … 14 24 } 15 25 } 26 void seed_hotbits() { mt[] = cast(size_t[]) get_hotbits(N*4); } 16 27 void init_by_array(size_t init_key[], size_t key_length) { 17 28 int i=1, j=0, k=(N>key_length ? N : key_length); trunk/tools/tools/stackthreads_impl.d
r287 r361 14 14 pragma(msg, "This may, in certain not-so-corner-cases, cause memory leaks. Blame the lack of Phobos maintenance."); 15 15 } 16 17 //TLS!(void*) original_stack_start; 18 //static this() { New(original_stack_start, { return &(new Stuple!(void*))._0; }); } 16 19 // this is a template so it's not compiled into static library code 17 20 // thus allowing the debug segments to depend on the application's debug mode, not the library's … … 111 114 if (!_loaded) return; 112 115 (*new_ptr)(); 113 //if (rptr!=regs.ptr) { writefln("Invalid end ptr, ", rptr, " vs ", regs.ptr, " loaded: ", _loaded); asm { int 3; } }114 116 } 115 117 } … … 136 138 } else vstack=(vstack_file=new MmFile(null, MmFile.Mode.ReadWriteNew, size, null))[]; 137 139 static if (patchedPhobos) me = addStack(vstack.ptr, vstack.ptr + vstack.length); 140 addRange(vstack.ptr, vstack.ptr + vstack.length); 138 141 } 139 142 static if (patchedPhobos) Stack* me, caller;
