From 355104d002bbe132cc213f274bfc3e3e2ec2eb35 Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Sat, 16 Sep 2023 12:16:39 +0300 Subject: [PATCH] Recognize CRLF as line terminator in getline --- source/io/base.cpp | 7 +++++++ source/io/buffered.cpp | 5 ++++- source/io/memory.cpp | 5 ++++- 3 files changed, 15 insertions(+), 2 deletions(-) diff --git a/source/io/base.cpp b/source/io/base.cpp index b8c13d2..bf23ea2 100644 --- a/source/io/base.cpp +++ b/source/io/base.cpp @@ -31,6 +31,13 @@ bool Base::getline(string &line) while(!eof()) { int c = get(); + if(c=='\r') + { + c = get(); + if(c=='\n') + break; + line += '\r'; + } if(c==-1 || c=='\n') break; line += c; diff --git a/source/io/buffered.cpp b/source/io/buffered.cpp index 9f001fb..7ba3c33 100644 --- a/source/io/buffered.cpp +++ b/source/io/buffered.cpp @@ -156,7 +156,10 @@ bool Buffered::getline(string &line) for(char *i=begin; i!=end; ++i) if(*i=='\n') { - line.assign(begin, i-begin); + size_t length = i-begin; + if(i>begin && *(i-1)=='\r') + --length; + line.assign(begin, length); begin = i+1; return true; } diff --git a/source/io/memory.cpp b/source/io/memory.cpp index 0539fdb..2dc94e0 100644 --- a/source/io/memory.cpp +++ b/source/io/memory.cpp @@ -87,7 +87,10 @@ bool Memory::getline(string &line) } char *nl = find(pos, end, '\n'); - line.assign(pos, nl); + size_t len = nl-pos; + if(nl!=end && nl>pos && *(nl-1)=='\r') + --len; + line.assign(pos, len); pos = (nl==end ? end : nl+1); return true; } -- 2.45.2