Here's a version of Shell::ReadLine that works for all my tests:
Handle<Value> Shell::ReadLine(const Arguments& args) {
size_t size = 256;
size_t len = 0;
i::SmartPointer<char> line(i::NewArray<char>(size));
if (fgets(*line + len, 256, stdin) != NULL) {
do {
if ((len = strlen(*line)) == 0) {
break;
} else if (line[len - 1] == '\n') {
line[--len] = 0;
break;
} else if (len > size - 256) {
// Expand storage to read the rest of the line.
size *= 2;
i::SmartPointer<char> new_line(i::NewArray<char>(size));
memcpy(*new_line, *line, len);
delete[] line.Detach();
line = new_line;
} // else try reading another 256 (really 255) chars
} while (fgets(*line + len, 256, stdin) != NULL);
return String::New(*line, len);
} else {
delete[] line.Detach();
return Null();
}
}
On Mon, Sep 7, 2009 at 7:54 AM, Matthew Wilson <[email protected]> wrote:
> This cannot use i::ReadLine, because users of readline() do not necessarily
> want linebreak-escapes as in JS syntax. Also, this patch causes it to
> return null instead of the final line. See the test below.
> C:\v8>type foo1.txt
> First Line\
> Non-Empty-Line
>
> After Empty-Line
> Final Line
> C:\v8>type foo1.txt|d8 -e "var l, x=0;while((l=readline())!==null)
> print(x++,l)"
> 0 First Line
> Non-Empty-Line
> 1
> 2 After Empty-Line
>
> C:\v8>
>
> -Matthew
>
> On Mon, Sep 7, 2009 at 7:39 AM, <[email protected]>wrote:
>
>> Landed in http://code.google.com/p/v8/source/detail?r=2838.
>>
>>
>> http://codereview.chromium.org/173262
>>
>
>
--~--~---------~--~----~------------~-------~--~----~
v8-dev mailing list
[email protected]
http://groups.google.com/group/v8-dev
-~----------~----~----~----~------~----~------~--~---