Chromium Code Reviews| Index: sdk/lib/io/stdio.dart |
| diff --git a/sdk/lib/io/stdio.dart b/sdk/lib/io/stdio.dart |
| index 9c034f437adbca9db958341dea7552cd361eaf79..d0fe2c88ca349aa16520400c5ea5fedf16a4d161 100644 |
| --- a/sdk/lib/io/stdio.dart |
| +++ b/sdk/lib/io/stdio.dart |
| @@ -51,73 +51,58 @@ class Stdin extends _StdStream implements Stream<List<int>> { |
| bool retainNewlines: false}) { |
|
Lasse Reichstein Nielsen
2014/04/22 08:07:01
The retainNewlines parameter isn't documented.
It
Anders Johnsen
2014/04/22 10:45:42
Done.
|
| const CR = 13; |
| const LF = 10; |
| - var line = new StringBuffer(); |
| - bool end = false; |
| + final List line = []; |
| + // On Windows, if lineMode is disabled, only CR is received. |
| + bool crIsNewline = Platform.isWindows && !lineMode; |
| bool lastCharWasCR = false; |
| - var error; |
| - |
| - StreamController<List<int>> controller = |
| - new StreamController<List<int>>(sync: true); |
| - Stream stream = controller.stream.transform(encoding.decoder); |
| - stream.listen((String str) { |
| - line.write(str); |
| - }, onError: (e) { |
| - error = e; |
| - }, onDone: () { |
| - end = true; |
| - }); |
| - |
| bool empty = true; |
| - while (!end) { |
| + while (true) { |
| int b = readByteSync(); |
|
Lasse Reichstein Nielsen
2014/04/22 08:07:01
b -> byte
Anders Johnsen
2014/04/22 10:45:42
Done.
|
| - |
| if (b < 0) { |
| // We didn't write the carriage return in case a line feed would be |
| // the next character. Add it now. |
| - if (lastCharWasCR && !retainNewlines) controller.add([CR]); |
| - controller.close(); |
| + if (lastCharWasCR && !retainNewlines) line.add(CR); |
| + break; |
| } else { |
| empty = false; |
| // We consider \r\n and \n as new lines. |
|
Lasse Reichstein Nielsen
2014/04/22 08:07:01
Both \r\n and \n are considered line terminators.
Anders Johnsen
2014/04/22 10:45:42
Done.
|
| // A \r on its own is treated like a normal character. |
|
Lasse Reichstein Nielsen
2014/04/22 08:07:01
treated like -> treated as
Anders Johnsen
2014/04/22 10:45:42
Done.
|
| if (b == CR) { |
| + if (crIsNewline) { |
| + if (retainNewlines) line.add(b); |
| + break; |
| + } |
| if (lastCharWasCR && !retainNewlines) { |
|
Lasse Reichstein Nielsen
2014/04/22 08:07:01
You are testing retainNewlines twice.
How about:
Anders Johnsen
2014/04/22 10:45:42
Done.
|
| // We didn't write the carriage return in case a line feed would be |
| // the next character. |
| // Add it now (since we treat it like a normal character now). |
| - controller.add([CR]); |
| + line.add(CR); |
| } |
| // We add the carriage return only if we keep new lines. |
| // Otherwise we need to wait for the next character (in case it is |
| // a line feed). |
| - if (retainNewlines) controller.add([b]); |
| + if (retainNewlines) line.add(b); |
| lastCharWasCR = true; |
| } else if (b == LF) { |
| - end = true; |
| // We don't care if there was a carriage return before. If we keep |
| // the line separators it has already been added to the controller. |
| // Otherwise we don't want it anyway. |
| - if (retainNewlines) controller.add([b]); |
| - controller.close(); |
| + if (retainNewlines) line.add(b); |
| + break; |
| } else { |
| // Since the current character is not a line feed we flush the |
| // carriage return we didn't write last iteration. |
| if (lastCharWasCR) { |
|
Lasse Reichstein Nielsen
2014/04/22 08:07:01
&& !retainNewLines
?
Anders Johnsen
2014/04/22 10:45:42
Done.
|
| - controller.add([CR]); |
| + line.add(CR); |
| lastCharWasCR = false; |
| } |
| - controller.add([b]); |
| + line.add(b); |
| } |
| } |
| - if (error != null) { |
| - // Error during decoding. |
| - throw error; |
| - } |
| } |
|
Lasse Reichstein Nielsen
2014/04/22 08:07:01
This seems complicated. You are handling two separ
Anders Johnsen
2014/04/22 10:45:42
Done.
|
| - |
| if (empty) return null; |
| - return line.toString(); |
| + return encoding.decode(line); |
| } |
| /** |