tolf and detab

bearophile bearophileHUGS at lycos.com
Sun Aug 8 14:54:18 PDT 2010


Andrej Mitrovic:

> Andrei used to!string() in an early example in TDPL for some line-by-line
> processing. I'm not sure of the advantages/disadvantages of to!type vs .dup.

I have modified the code:

import std.stdio: File, writeln;
import std.conv: to;

int process(string fileName) {
    int total = 0;

    auto file = File(fileName);
    foreach (rawLine; file.byLine()) {
        string line = to!string(rawLine);
        total += line.length;
    }
    file.close();

    return total;
}

void main(string[] args) {
    if (args.length == 2)
        writeln("Total: ", process(args[1]));
}


The run time is 1.29 seconds, showing this is equivalent to the idup.

Bye,
bearophile


More information about the Digitalmars-d mailing list