Changeset 438

Show
Ignore:
Timestamp:
10/16/07 02:37:10 (1 year ago)
Author:
andrei
Message:

Got rid of a ton of duplication that seemed innocuous at first.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • candidate/phobos/std/stdio.d

    r430 r438  
    205205} 
    206206 
    207  
    208207/*********************************** 
    209208 * If the first argument $(D_PARAM args[0]) is a $(D_PARAM FILE*), for 
     
    223222void write(T...)(T args) 
    224223{ 
    225     // duplicate code for speed; no passing data around 
    226     static if (!is(typeof(args[0]) : FILE*)) 
    227     { 
    228         write(stdout, args)
     224    static if (is(typeof(args[0]) : FILE*)) 
     225    { 
     226        alias args[0] target; 
     227        static const first = 1
    229228    } 
    230229    else 
    231230    { 
    232         FLOCK(args[0]); 
    233         scope(exit) FUNLOCK(args[0]); 
    234         foreach (arg; args[1 .. $]) 
    235         { 
    236             final s = to!(string)(arg); 
    237             size_t written = void; 
    238             version(linux) 
    239             { 
    240                 written = fwrite_unlocked(s.ptr, 
    241                     s[0].sizeof, s.length, args[0]); 
    242             } 
    243             else 
    244             { 
    245                 // TODO: figure out unlocked bulk writes on Windows 
    246                 written = std.c.stdio.fwrite(s.ptr, 
    247                     s[0].sizeof, s.length, args[0]); 
    248             } 
    249             if (written != s.length) 
    250             { 
    251                 StdioException(); 
    252             } 
    253         } 
    254     } 
     231        alias stdout target; 
     232        static const first = 0; 
     233    } 
     234    writef(target, "", args[first .. $]); 
    255235} 
    256236 
     
    343323        " argument to writef. If no formatting is needed," 
    344324        " you may want to use write."; 
    345     static if (!is(typeof(args[0]) : FILE*)) 
    346     { 
    347         static if (!isSomeString!(T[0])) 
    348         { 
    349             // compatibility hack 
    350             return writef(stdio, "", args); 
    351         } 
    352         else 
    353         { 
    354             w.backend = stdout; 
    355             FLOCK(w.backend); 
    356             scope(exit) FUNLOCK(w.backend); 
    357             std.format.formattedWrite(w, args); 
    358         } 
     325    static if (is(typeof(args[0]) : FILE*)) 
     326    { 
     327        alias args[0] target; 
     328        static const first = 1; 
    359329    } 
    360330    else 
    361331    { 
    362         static if (!isSomeString!(T[1])) 
    363         { 
    364             // compatibility hack 
    365             return writef(args[0], "", args[1 .. $]); 
    366         } 
    367         else 
    368         { 
    369             w.backend = args[0]; 
    370             FLOCK(w.backend); 
    371             scope(exit) FUNLOCK(w.backend); 
    372             std.format.formattedWrite(w, args[1 .. $]); 
    373         } 
     332        alias stdout target; 
     333        static const first = 0; 
     334    } 
     335    w.backend = target; 
     336    FLOCK(w.backend); 
     337    scope(exit) FUNLOCK(w.backend); 
     338    static if (!isSomeString!(T[first])) 
     339    { 
     340        // compatibility hack 
     341        std.format.formattedWrite(w, "", args[first .. $]); 
     342    } 
     343    else 
     344    { 
     345        std.format.formattedWrite(w, args[first .. $]); 
    374346    } 
    375347} 
     
    399371void writefln(T...)(T args) 
    400372{ 
    401     //writef(args, '\n'); 
    402     // Duplicate code so we don't duplicate the stack; replace with macro l8r 
    403     FileWriter!(char) w; 
    404     static const errorMessage = 
    405         "You must pass a formatting string as the first" 
    406         " argument to writefln. If no formatting is needed," 
    407         " you may want to use writeln."; 
    408     static if (!is(typeof(args[0]) : FILE*)) 
    409     { 
    410         static if (!isSomeString!(T[0])) 
    411         { 
    412             // compatibility hack 
    413             return writef(stdout, "", args, '\n'); 
    414         } 
    415         else 
    416         { 
    417             w.backend = stdout; 
    418             FLOCK(w.backend); 
    419             scope(exit) FUNLOCK(w.backend); 
    420             std.format.formattedWrite(w, args, '\n'); 
    421         } 
    422     } 
    423     else 
    424     { 
    425         static if (!isSomeString!(T[1])) 
    426         { 
    427             // compatibility hack 
    428             return writef(args[0], "", args[1 .. $], '\n'); 
    429         } 
    430         else 
    431         { 
    432             w.backend = args[0]; 
    433             FLOCK(w.backend); 
    434             scope(exit) FUNLOCK(w.backend); 
    435             std.format.formattedWrite(w, args[1 .. $], '\n'); 
    436         } 
    437     } 
     373    writef(args, '\n'); 
    438374} 
    439375