3 ms·
Could you explain your point about line numbering?
by renox 10y ago
Could you explain your point about line numbering?
- MaulingMonkey 10y agoTake, for example, displaying the context source code for an error when you know the file and lineNo of the error. A naive approach would be to read the entire file into an array and then to output the lines [lineNo-context..lineNo+context], possibly with line numbers prefixed. This is O(N) memory with regards to the size of the file being processed. A 8GB source file will crash your program when built for 32-bit. Another approach is to read and discard until you've discarded lineNo-context '\n' characters, then copy chunks from your source directly to the output until you've read another 2*context+1 characters. This is O(1) memory (strictly speaking, you could do it byte-by-byte with an integer counter or two - practically you might have a fixed size buffer to read/write faster) and would allow you to handle even 1TB files sanely. To be fair, I'm often guilty of the naive approach myself :)