Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

line.isEmpty doesn't seem to work when copy&pasting #1

Open
vmendi opened this issue Feb 2, 2015 · 1 comment
Open

line.isEmpty doesn't seem to work when copy&pasting #1

vmendi opened this issue Feb 2, 2015 · 1 comment

Comments

@vmendi
Copy link

vmendi commented Feb 2, 2015

I don't know why. I have tried to copy the text both directly from the browser console and from a text editor. Adding an empty line at the end of the buffer doesn't work.

If I copy line by line, it works. It may be something related to the end of line/carriage return character?

@bjornm
Copy link
Owner

bjornm commented Feb 2, 2015

Sounds like a potential bug in Stdin.readLineSync() - I'm on Mac and it's working fine. What os are you using? Could you please try the following dart test file and see what we get?

import 'dart:io';
import 'dart:convert';

main() {
  print("Paste something:");
  String line = "";
  do {
    String line = readLineSync();
    print("line.isEmpty=${line.isEmpty}");
  } while(line.isNotEmpty);
  print("DONE");
}

String readLineSync({Encoding encoding: SYSTEM_ENCODING,
                    bool retainNewlines: false}) {
  const CR = 13;
  const LF = 10;
  final List line = [];
  // On Windows, if lineMode is disabled, only CR is received.
  bool crIsNewline = Platform.isWindows &&
  (stdioType(stdin) == StdioType.TERMINAL) &&
  !stdin.lineMode;
  print("Platform.isWindows=${Platform.isWindows} stdioType=${stdioType(stdin)} stin.lineMode=${stdin.lineMode} crIsNewLine=$crIsNewline");
  if (retainNewlines) {
    int byte;
    do {
      byte = stdin.readByteSync();
      print("Got byte $byte");
      if (byte < 0) {
        break;
      }
      line.add(byte);
    } while (byte != LF && !(byte == CR && crIsNewline));
    if (line.isEmpty) {
      return null;
    }
  } else if (crIsNewline) {
    // CR and LF are both line terminators, neither is retained.
    while (true) {
      int byte = stdin.readByteSync();
      print("Got byte $byte");
      if (byte < 0) {
        if (line.isEmpty) return null;
        break;
      }
      if (byte == LF || byte == CR) break;
      line.add(byte);
    }
  } else {
    // Case having to hande CR LF as a single unretained line terminator.
    outer: while (true) {
      int byte = stdin.readByteSync();
      print("Got byte $byte");
      if (byte == LF) break;
      if (byte == CR) {
        do {
          byte = stdin.readByteSync();
          print("Got byte $byte");
          if (byte == LF) break outer;

          line.add(CR);
        } while (byte == CR);
        // Fall through and handle non-CR character.
      }
      if (byte < 0) {
        if (line.isEmpty) return null;
        break;
      }
      line.add(byte);
    }
  }
  return encoding.decode(line);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants