1 module nxt.file_ex;
2 
3 /** Read file $(D path) into raw array with one extra terminating zero byte.
4  *
5  * This extra terminating zero (`null`) byte at the end is typically used as a
6  * sentinel value to speed up textual parsers.
7  *
8  * TODO add or merge to Phobos?
9  *
10  * See_Also: https://en.wikipedia.org/wiki/Sentinel_value
11  * See_Also: http://forum.dlang.org/post/pdzxpkusvifelumkrtdb@forum.dlang.org
12  */
13 immutable(void)[] rawReadPath(string path) @trusted
14 {
15     import std.array : uninitializedArray;
16 
17     import std.stdio : File;
18     auto file = File(path, `rb`);
19 
20     alias Data = ubyte[];
21 
22     const bool appendTerminatingNull = true;
23     const totalSize = appendTerminatingNull ? file.size + 1 : file.size;
24 
25     Data data = uninitializedArray!(Data)(totalSize); // one extra for terminator
26 
27     file.rawRead(data);
28 
29     if (totalSize)
30     {
31         data[file.size] = 0;     // zero terminator for sentinel
32     }
33 
34     import std.exception : assumeUnique;
35     return assumeUnique(data);
36 }