]> granicus.if.org Git - icinga2/commitdiff
Stream#ReadLine(): simplify algorithm 6393/head
authorAlexander A. Klimov <alexander.klimov@icinga.com>
Wed, 20 Jun 2018 15:28:52 +0000 (17:28 +0200)
committerAlexander A. Klimov <alexander.klimov@icinga.com>
Wed, 20 Jun 2018 15:28:52 +0000 (17:28 +0200)
refs #6354

lib/base/stream.cpp

index a7ac2067ce745f3f08bc70bce2e2a1c50fda523b..0b847060f471cacb91337abe34e8864c7749f895 100644 (file)
@@ -129,40 +129,19 @@ StreamReadStatus Stream::ReadLine(String *line, StreamReadContext& context, bool
                }
        }
 
-       int count = 0;
-       size_t first_newline;
-
        for (size_t i = 0; i < context.Size; i++) {
                if (context.Buffer[i] == '\n') {
-                       count++;
-
-                       if (count == 1)
-                               first_newline = i;
-                       else if (count > 1)
-                               break;
-               }
-       }
-
-       switch (count) {
-               case 0:
-                       context.MustRead = true;
-                       break;
-               case 1:
-                       context.MustRead = first_newline == (context.Size - 1u);
-                       break;
-               default:
-                       context.MustRead = false;
-       }
-
-       if (count > 0) {
-               *line = String(context.Buffer, &(context.Buffer[first_newline]));
-               boost::algorithm::trim_right(*line);
+                       *line = String(context.Buffer, context.Buffer + i);
+                       boost::algorithm::trim_right(*line);
 
-               context.DropData(first_newline + 1);
+                       context.DropData(i + 1u);
 
-               return StatusNewItem;
+                       context.MustRead = !context.Size;
+                       return StatusNewItem;
+               }
        }
 
+       context.MustRead = true;
        return StatusNeedData;
 }