1 module pry.stream;
2 
3 import pry.traits;
4 import std.range.primitives, std.traits, std.utf;
5 
6 struct SimpleStream(S)
7 if(isSomeString!S) {
8 private:
9 	size_t _index;
10 	S _range;
11 public:
12 	alias Range = S;
13 	static struct Error {
14 		size_t location;
15 		string reason;
16 	}
17 
18 	dchar front(){
19 		size_t t = _index;
20 		return decode(_range, t);
21 	}
22 
23 	this(S source){
24 		_range = source;
25 		_index = 0;
26 	}
27 
28 	void popFront(){  _index += stride(_range, _index); }
29 
30 	@property bool empty(){ return _index == _range.length; }
31 
32 	S slice(size_t i){ return _range[i .. _index]; }
33 
34 	size_t mark(){ return _index; }
35 
36 	void restore(size_t i){ _index = i; }
37 
38 	@property size_t location() {
39 		return _index;
40 	}
41 }
42 
43 static assert(isStream!(SimpleStream!string));
44 
45 /// Wrap a given slicable random access range into a SimpleStream.
46 auto stream(Range)(Range range)
47 if(isRandomAccessRange!Range || isSomeString!Range) {
48 	return SimpleStream!Range(range);
49 }