Spec-Zone.ru › Dart 2

dart:io

Метод readLineSync

String? readLineSync(
  1. {Encoding encoding = systemEncoding,
  2. bool retainNewlines = false}
)

Считывает строку из stdin.

Ожидает, пока не будет доступна полная строка.

Строки могут быть завершены либо <CR><LF>, либо <LF>. В Windows, в случаях, когда stdioType stdin равен StdioType.terminal, разделитель может также быть одиночным <CR>.

Байты входных данных преобразуются в строку с помощью encoding. Если encoding опущено, по умолчанию используется systemEncoding.

Если retainNewlines равно false, возвращаемая строка не будет включать конечный разделитель строки. Если true, возвращаемая строка будет включать разделитель строки. По умолчанию false.

Если конец файла достигнут после того, как были прочитаны какие-либо байты из stdin, эти данные возвращаются без разделителя строки. Возвращает null, если байты не предшествовали концу ввода.

Реализация

String? readLineSync(
    {Encoding encoding = systemEncoding, bool retainNewlines = false}) {
  const CR = 13;
  const LF = 10;
  final List<int> line = <int>[];
  // On Windows, if lineMode is disabled, only CR is received.
  bool crIsNewline = Platform.isWindows &&
      (stdioType(stdin) == StdioType.terminal) &&
      !lineMode;
  if (retainNewlines) {
    int byte;
    do {
      byte = readByteSync();
      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 = readByteSync();
      if (byte < 0) {
        if (line.isEmpty) return null;
        break;
      }
      if (byte == LF || byte == CR) break;
      line.add(byte);
    }
  } else {
    // Case having to handle CR LF as a single unretained line terminator.
    outer:
    while (true) {
      int byte = readByteSync();
      if (byte == LF) break;
      if (byte == CR) {
        do {
          byte = readByteSync();
          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);
}

© 2012 the Dart project authors
Licensed under the BSD 3-Clause "New" or "Revised" License.
https://api.dart.dev/stable/2.18.5/dart-io/Stdin/readLineSync.html

Spec-Zone.ru

Настройки Оффлайн Что нового Помощь О нас
Spec-Zone .ru
спецификации, руководства, описания, API