digitalmars.D.learn - Why does File.byLine() return char[] and not string
- Shriramana Sharma (5/5) Oct 16 2015 Is there a particular reason that File.byLine() returns char[] and not
- Daniel Kozak (3/9) Oct 16 2015 You need to do dup or idup anyway. It reuses same buffer, so it is not
- Kagamin (1/1) Oct 16 2015 http://dlang.org/phobos/std_stdio.html#.File.byLineCopy
- Meta (6/10) Oct 16 2015 byLine reuses an internal buffer for each line which gets
- Shriramana Sharma (3/3) Oct 16 2015 Thanks people, for the replies. That's very clear now.
- Suliman (4/15) Oct 18 2015 Sorry, but could you explain more simply? I reread all
- novice2 (4/6) Oct 18 2015 just the fact for programmer, that result line can be changed by
- Suliman (7/13) Oct 18 2015 Am I right understand that byLine work like:
- Daniel Kozak via Digitalmars-d-learn (6/27) Oct 18 2015 V Sun, 18 Oct 2015 15:51:13 +0000
- Meta (30/34) Oct 18 2015 This is more or less how byLine works, simplified:
Is there a particular reason that File.byLine() returns char[] and not string i.e. immutable(char)[]? Is it just to avoid being overly restrictive? It seems that having to .idup it is inefficient... --
Oct 16 2015
Shriramana Sharma p=C3=AD=C5=A1e v P=C3=A1 16. 10. 2015 v 16:08 +0530:Is there a particular reason that File.byLine() returns char[] and not=20 string i.e. immutable(char)[]? Is it just to avoid being overly restrictive?=20 It seems that having to .idup it is inefficient... =20You need to do dup or idup anyway. It reuses same buffer, so it is not immutable.
Oct 16 2015
On Friday, 16 October 2015 at 10:38:52 UTC, Shriramana Sharma wrote:Is there a particular reason that File.byLine() returns char[] and not string i.e. immutable(char)[]? Is it just to avoid being overly restrictive? It seems that having to .idup it is inefficient...byLine reuses an internal buffer for each line which gets overwritten each iteration. The fact that it returns char instead of string is meant to signal this to the user, to tell them that the value they're getting back is mutable and subject to change.
Oct 16 2015
Thanks people, for the replies. That's very clear now. --
Oct 16 2015
On Friday, 16 October 2015 at 12:43:59 UTC, Meta wrote:On Friday, 16 October 2015 at 10:38:52 UTC, Shriramana Sharma wrote:Sorry, but could you explain more simply? I reread all information, bit can't understand about what buffer you are talking. And what is "signal"? How it's working?Is there a particular reason that File.byLine() returns char[] and not string i.e. immutable(char)[]? Is it just to avoid being overly restrictive? It seems that having to .idup it is inefficient...byLine reuses an internal buffer for each line which gets overwritten each iteration. The fact that it returns char instead of string is meant to signal this to the user, to tell them that the value they're getting back is mutable and subject to change.
Oct 18 2015
what buffer you are talking.internal buffer. where result line resides.And what is "signal"? How it's working?just the fact for programmer, that result line can be changed by other code (by phobos library code in this case). no any special programming "signal".
Oct 18 2015
On Sunday, 18 October 2015 at 15:40:09 UTC, novice2 wrote:Am I right understand that byLine work like: read string, put it's to internal buffer, then read new line, overwrite existent buffer etc... byLineCopy is create range that storage all strings in format of string, not char? What is size of this buffer, how it's calculate?what buffer you are talking.internal buffer. where result line resides.And what is "signal"? How it's working?just the fact for programmer, that result line can be changed by other code (by phobos library code in this case). no any special programming "signal".
Oct 18 2015
V Sun, 18 Oct 2015 15:51:13 +0000 Suliman via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> napsáno:On Sunday, 18 October 2015 at 15:40:09 UTC, novice2 wrote:YesAm I right understand that byLine work like: read string, put it's to internal buffer, then read new line, overwrite existent buffer etc...what buffer you are talking.internal buffer. where result line resides.And what is "signal"? How it's working?just the fact for programmer, that result line can be changed by other code (by phobos library code in this case). no any special programming "signal".byLineCopy is create range that storage all strings in format of string, not char?byLineCopy is same as byLine, but do dup for each lineWhat is size of this buffer, how it's calculate?it is dynamic it depends on line length
Oct 18 2015
On Sunday, 18 October 2015 at 15:03:22 UTC, Suliman wrote:Sorry, but could you explain more simply? I reread all information, bit can't understand about what buffer you are talking.This is more or less how byLine works, simplified: struct ByLine { File file; char[] line; char[] buffer; char terminator; bool empty() { return line is null; } char[] front() { return line; } void popFront() { line = buffer; file.readLine(line, terminator); //This overwrites the current contents of line if (line.length > buffer.length) { buffer = line; } if (line.empty) line = null; } } auto byLine(string fileName, char terminator = '\n') { return ByLine(File(fileName), terminator); }And what is "signal"? How it's working?It's just an expression that means "to convey information". So when ByLine.front returns char[], a mutable array of char, it's meant to convey to the programmer that since the return value is mutable, it could change and they should make a copy.
Oct 18 2015