]> git.tdb.fi Git - libs/core.git/commitdiff
Recognize CRLF as line terminator in getline
authorMikko Rasa <tdb@tdb.fi>
Sat, 16 Sep 2023 09:16:39 +0000 (12:16 +0300)
committerMikko Rasa <tdb@tdb.fi>
Sat, 16 Sep 2023 09:16:39 +0000 (12:16 +0300)
source/io/base.cpp
source/io/buffered.cpp
source/io/memory.cpp

index b8c13d2193060f94f957178f8f01052c62593d41..bf23ea2ac26ea3e8d25dd07bf2012713add715a9 100644 (file)
@@ -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;
index 9f001fbe9ae94b944611f368416799ceab836a04..7ba3c33190f45e3826bb1dd490799436cca0e960 100644 (file)
@@ -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;
                }
index 0539fdbc4245f3c9c9ada441bf2aefdbcf0c0707..2dc94e0e28ca89e47a676352f8b3d2ea66d16ce1 100644 (file)
@@ -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;
 }