Merge pull request #46446 from zhuzilin:optimize-readline

PiperOrigin-RevId: 360960162
Change-Id: I7d9aff7d7ce0e3b033032819f5ed7b74befc86a1
This commit is contained in:
TensorFlower Gardener 2021-03-04 11:23:21 -08:00
commit 33b48dbbea

View File

@ -60,25 +60,32 @@ Status BufferedInputStream::ReadLineHelper(StringType* result,
bool include_eol) { bool include_eol) {
result->clear(); result->clear();
Status s; Status s;
size_t start_pos = pos_;
while (true) { while (true) {
if (pos_ == limit_) { if (pos_ == limit_) {
result->append(buf_.data() + start_pos, pos_ - start_pos);
// Get more data into buffer // Get more data into buffer
s = FillBuffer(); s = FillBuffer();
if (limit_ == 0) { if (limit_ == 0) {
break; break;
} }
start_pos = pos_;
} }
char c = buf_[pos_++]; char c = buf_[pos_];
if (c == '\n') { if (c == '\n') {
result->append(buf_.data() + start_pos, pos_ - start_pos);
if (include_eol) { if (include_eol) {
result->append(1, c); result->append(1, c);
} }
pos_++;
return Status::OK(); return Status::OK();
} }
// We don't append '\r' to *result // We don't append '\r' to *result
if (c != '\r') { if (c == '\r') {
result->append(1, c); result->append(buf_.data() + start_pos, pos_ - start_pos);
start_pos = pos_ + 1;
} }
pos_++;
} }
if (errors::IsOutOfRange(s) && !result->empty()) { if (errors::IsOutOfRange(s) && !result->empty()) {
return Status::OK(); return Status::OK();