www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Why does File.byLine() return char[] and not string

reply Shriramana Sharma <samjnaa_dont_spam_me gmail.com> writes:
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
next sibling parent Daniel Kozak <kozzi dlang.cz> writes:
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...
=20
You need to do dup or idup anyway. It reuses same buffer, so it is not immutable.
Oct 16 2015
prev sibling next sibling parent Kagamin <spam here.lot> writes:

Oct 16 2015
prev sibling parent reply Meta <jared771 gmail.com> writes:
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
next sibling parent Shriramana Sharma <samjnaa_dont_spam_me gmail.com> writes:
Thanks people, for the replies. That's very clear now.

-- 

Oct 16 2015
prev sibling parent reply Suliman <evermind live.ru> writes:
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:
 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.
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?
Oct 18 2015
next sibling parent reply novice2 <sorry noem.ail> writes:
 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
parent reply Suliman <evermind live.ru> writes:
On Sunday, 18 October 2015 at 15:40:09 UTC, novice2 wrote:
 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".
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?
Oct 18 2015
parent Daniel Kozak via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> writes:
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:
 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".
Am I right understand that byLine work like: read string, put it's to internal buffer, then read new line, overwrite existent buffer etc...
Yes
 
 byLineCopy is create range that storage all strings in format of 
 string, not char?
 
byLineCopy is same as byLine, but do dup for each line
 What is size of this buffer, how it's calculate?
it is dynamic it depends on line length
Oct 18 2015
prev sibling parent Meta <jared771 gmail.com> writes:
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