Thread overview | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
|
October 16, 2015 Why does File.byLine() return char[] and not string | ||||
---|---|---|---|---|
| ||||
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...
--
Shriramana Sharma, Penguin #395953
|
October 16, 2015 Re: Why does File.byLine() return char[] and not string | ||||
---|---|---|---|---|
| ||||
Posted in reply to Shriramana Sharma | Shriramana Sharma píše v Pá 16. 10. 2015 v 16:08 +0530:
> 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...
>
You need to do dup or idup anyway. It reuses same buffer, so it is not immutable.
|
October 16, 2015 Re: Why does File.byLine() return char[] and not string | ||||
---|---|---|---|---|
| ||||
Posted in reply to Shriramana Sharma | http://dlang.org/phobos/std_stdio.html#.File.byLineCopy |
October 16, 2015 Re: Why does File.byLine() return char[] and not string | ||||
---|---|---|---|---|
| ||||
Posted in reply to Shriramana Sharma | 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.
|
October 16, 2015 Re: Why does File.byLine() return char[] and not string | ||||
---|---|---|---|---|
| ||||
Posted in reply to Meta | Thanks people, for the replies. That's very clear now.
--
Shriramana Sharma, Penguin #395953
|
October 18, 2015 Re: Why does File.byLine() return char[] and not string | ||||
---|---|---|---|---|
| ||||
Posted in reply to Meta | 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?
|
October 18, 2015 Re: Why does File.byLine() return char[] and not string | ||||
---|---|---|---|---|
| ||||
Posted in reply to Suliman | > 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". |
October 18, 2015 Re: Why does File.byLine() return char[] and not string | ||||
---|---|---|---|---|
| ||||
Posted in reply to novice2 | 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?
|
October 18, 2015 Re: Why does File.byLine() return char[] and not string | ||||
---|---|---|---|---|
| ||||
Posted in reply to Suliman | 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 |
October 18, 2015 Re: Why does File.byLine() return char[] and not string | ||||
---|---|---|---|---|
| ||||
Posted in reply to Suliman | 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. |
Copyright © 1999-2021 by the D Language Foundation