DFortune

Unix fortune-cookie parser written in D
git clone git://xatko.vsos.ethz.ch/DFortune.git
Log | Files | Refs

commit 2c63f3e9e0c2fc640d996d1519a3f06e8da89f0f
parent 28860abd2f58cc39bac3a723eec071f2c7620220
Author: Dominik Schmidt <das1993@hotmail.com>
Date:   Sun,  9 Aug 2015 15:55:47 +0200

Use CookieResult-struct for Fortunes-aggregate.

CookieResult additionally saves the Fortune-file.

Diffstat:
dfortune.d | 27+++++++++++++++++----------
1 file changed, 17 insertions(+), 10 deletions(-)

diff --git a/dfortune.d b/dfortune.d @@ -211,7 +211,14 @@ class Fortune : RandomAccessFinite!(Cookie){ static assert(isRandomAccessRange!(Fortune)); } } - +struct CookieResult{ + Cookie c; + Fortune f; + alias c this; + string toString(){ + return f.read(c); + } +} struct Fortunes{ Fortune fortunes[]; size_t pos,end; @@ -232,13 +239,13 @@ struct Fortunes{ r.popFront(); } } - Cookie opIndex(size_t i){ + CookieResult opIndex(size_t i){ foreach(Fortune f; fortunes.save){ if(i>=f.length){ i-=f.length; } else{ - return f.opIndex(i); + return CookieResult(f.opIndex(i),f); } } assert(0); @@ -249,13 +256,13 @@ struct Fortunes{ size+=f.length; return size; } - @property Cookie front(){ + @property CookieResult front(){ return opIndex(pos); } - @property Cookie back(){ + @property CookieResult back(){ return opIndex(end); } - Cookie moveAt(size_t i){ + CookieResult moveAt(size_t i){ pos=i; return opIndex(pos); } @@ -265,16 +272,16 @@ struct Fortunes{ @property Fortunes save(){ return Fortunes(fortunes, pos, end); } - Cookie moveFront(){ + CookieResult moveFront(){ return front(); } - Cookie moveBack(){ + CookieResult moveBack(){ return back(); } void popBack(){ end--; } - int opApply(int delegate(Cookie)f){ + int opApply(int delegate(CookieResult)f){ int ret; while(!empty()){ ret=f(moveFront()); @@ -285,7 +292,7 @@ struct Fortunes{ } return ret; } - int opApply(int delegate(size_t,Cookie)f){ + int opApply(int delegate(size_t,CookieResult)f){ int ret; size_t i; while(!empty()){