Changeset 361

Show
Ignore:
Timestamp:
06/17/08 20:44:12 (6 months ago)
Author:
FeepingCreature
Message:
Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/tools/tools/base.d

    r360 r361  
    361361} 
    362362 
    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
     363template 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 .. $]
    366366  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; 
    368369    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 changes 
    370       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; 
    371372    } else { 
    372373      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; 
    374375      // 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; 
    376377    } 
    377378  } 
     
    379380 
    380381static assert(CarefulReplace!("foobar b", "b", "xx").repl == "foobar xx"); 
     382static assert(CarefulReplace!("a.foobaa()", "a", "#").repl == "#.foobaa()"); 
    381383 
    382384// V is the array/tuple used to store values, 
     
    672674} 
    673675 
    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); 
     676string 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; 
    678682} 
    679683 
     
    707711 
    708712string cut_off(string what, string where) { 
     713  if (what.length < where.length) return what; 
    709714  for (int i = 0; i < what.length-where.length; ++i) { 
    710715    if (what[i .. i+where.length] == where) return what[0 .. i]; 
     
    753758      } else if (ch == ')') { // super done 
    754759        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 = ""; 
    756764        sup = false; 
    757765      } else if (ch == ',' || ch == ';') { // assign closing elements 
     
    9941002      } 
    9951003      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(); 
    9961008    } else static if (is(typeof(elem.toString()): string)) { 
    9971009      res ~= elem.toString(); 
  • trunk/tools/tools/downloader.d

    r348 r361  
    246246      ubyte digit=0; 
    247247      foreach (ch; hex.tolower()) { 
    248         if (ch>='a' && ch<='f') digit=ch+10-'a'; else 
    249         if (ch>='0' && ch<='9') digit=ch-'0'; else 
     248        if (ch>='a' && ch<='f') digit=cast(ubyte) (ch+10-'a'); else 
     249        if (ch>='0' && ch<='9') digit=cast(ubyte) (ch-'0'); else 
    250250        throw new Exception("Invalid character '"~ch~"' in hex string"); 
    251251        res=res*16+digit; 
     
    262262      } 
    263263      received=received[line_end+2 .. $]; 
    264       chunks ~= received[0..chars]; 
    265       received=received[chars+2 .. $]; 
     264      chunks ~= received[0..cast(uint) chars]; 
     265      received=received[cast(uint) chars+2 .. $]; 
    266266      return receiveChunked; 
    267267    } else { 
  • trunk/tools/tools/ini.d

    r360 r361  
    8181    } 
    8282  } 
     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  } 
    83103  void set(T)(string section, string label, T what) { 
    84104    synchronized(this) { 
     
    117137  //    :) 
    118138  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    } 
    131153  } 
    132154  //~this() { save; } 
  • trunk/tools/tools/log.d

    r321 r361  
    3434    else static if (is(typeof(part)==void *)) { _printf("(%p)", cast(ulong)part); } 
    3535    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))) { 
    3737      auto c=cast(Object) part; 
    3838      if (c) _log(c.toString()); 
  • trunk/tools/tools/mersenne.d

    r359 r361  
    44const MATRIX_A=0x9908b0dfUL, UPPER_MASK=0x80000000UL, LOWER_MASK=0x7fffffffUL; 
    55 
     6import tools.downloader, tools.base; 
     7ubyte[] 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 
    616final class Mersenne { 
    7   size_t mt[N]
     17  size_t[N] mt
    818  int mti=N; 
    919  void seed(uint s) { 
     
    1424    } 
    1525  } 
     26  void seed_hotbits() { mt[] = cast(size_t[]) get_hotbits(N*4); } 
    1627  void init_by_array(size_t init_key[], size_t key_length) { 
    1728    int i=1, j=0, k=(N>key_length ? N : key_length); 
  • trunk/tools/tools/stackthreads_impl.d

    r287 r361  
    1414    pragma(msg, "This may, in certain not-so-corner-cases, cause memory leaks. Blame the lack of Phobos maintenance."); 
    1515  } 
     16   
     17  //TLS!(void*) original_stack_start; 
     18  //static this() { New(original_stack_start, { return &(new Stuple!(void*))._0; }); } 
    1619  // this is a template so it's not compiled into static library code 
    1720  // thus allowing the debug segments to depend on the application's debug mode, not the library's 
     
    111114      if (!_loaded) return; 
    112115      (*new_ptr)(); 
    113       //if (rptr!=regs.ptr) { writefln("Invalid end ptr, ", rptr, " vs ", regs.ptr, " loaded: ", _loaded); asm { int 3; } } 
    114116    } 
    115117  } 
     
    136138      } else vstack=(vstack_file=new MmFile(null, MmFile.Mode.ReadWriteNew, size, null))[]; 
    137139      static if (patchedPhobos) me = addStack(vstack.ptr, vstack.ptr + vstack.length); 
     140      addRange(vstack.ptr, vstack.ptr + vstack.length); 
    138141    } 
    139142    static if (patchedPhobos) Stack* me, caller;