SHARE
TWEET

nbuff

a guest Sep 19th, 2019 70 in 3 days
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. mport std.stdio;
  2. import nbuff;
  3. import std.string;
  4.  
  5. void main()
  6. {
  7.     Buffer  b;
  8.     size_t  position;
  9.     auto    NL = "\n".representation;
  10.  
  11.     File    f = File("text.txt", "r");
  12.     foreach(chunk;f.byChunk(5)) {
  13.         b.append(chunk.idup); // you have to use idup here as File.byChunk reuse it
  14.         // can we find NewLine? we use "position" so we newer scan input twice
  15.         long p = b.countUntil(position, NL);
  16.         if ( p >= 0 ) {
  17.             writef("%s\n",b[0..p]);
  18.             // throw away processed line (data will be GC-ed)
  19.             b = b[p+1..$];
  20.             // reset "position"
  21.             position = 0;
  22.         } else {
  23.             // we scanned all input, do not look in it twice
  24.             position = b.length;
  25.         }
  26.     }
  27.     if ( b.length > 0 ) {
  28.         // last line without NL
  29.         write(b);
  30.     }
  31. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top