-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathio.d
More file actions
82 lines (72 loc) · 1.69 KB
/
io.d
File metadata and controls
82 lines (72 loc) · 1.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
module stdex.io;
import std.stdio;
import std.algorithm;
struct Lines
{
this(File file, size_t bufferSize)
{
m_file = file;
m_chunks = file.byChunk(bufferSize);
peek();
}
@property bool empty() const
{
return m_empty;
}
@property char[] front()
{
return m_front;
}
void popFront()
{
peek();
}
void peek()
{
if (m_chunks.empty)
{
m_empty = true;
}
else
{
size_t i = 0;
bool done = false;
do
{
char[] chunk = cast(char[]) m_chunks.front[m_chunkOffset..$];
size_t n = 0;
while (n < chunk.length && chunk[n] != '\n')
++n;
m_frontBuffer.length = max(m_frontBuffer.length, i + n);
m_frontBuffer[i..i+n] = chunk[0..n];
i += n;
if (n < chunk.length)
{
++n;
done = true;
}
m_chunkOffset += n;
if (n == chunk.length)
{
m_chunks.popFront();
m_chunkOffset = 0;
if (m_chunks.empty)
break;
}
}
while (!done);
m_front = m_frontBuffer[0..i];
}
}
bool m_empty = false;
char[] m_front;
char[] m_frontBuffer;
char[] m_buffer;
private File m_file;
File.ByChunk m_chunks;
size_t m_chunkOffset = 0;
}
Lines byLineBuffered(File file, size_t bufferSize = 4096)
{
return Lines(file, bufferSize);
}