Consistent TextWriter trailing whitespace handling
authorTeemu Murtola <teemu.murtola@gmail.com>
Sat, 11 Mar 2017 08:06:11 +0000 (10:06 +0200)
committerDavid van der Spoel <davidvanderspoel@gmail.com>
Sun, 12 Mar 2017 10:37:59 +0000 (11:37 +0100)
Depending on how exactly the writer was called, trailing whitespace in
the input strings was either preserved or removed.  Now it behaves at
least more predictably.  Add tests.

The actual change is in the line wrapper.

Change-Id: I44d545cac53bc2c3aef363ec259ec82cbd1aec90

src/gromacs/utility/stringutil.cpp
src/gromacs/utility/tests/refdata/TextLineWrapperTest_WrapsCorrectlyWithExtraWhitespace.xml
src/gromacs/utility/tests/refdata/TextWriterTest_PreservesTrailingWhitespace.xml [new file with mode: 0644]
src/gromacs/utility/tests/stringutil.cpp
src/gromacs/utility/tests/textwriter.cpp

index 9b9cc1a2a12b5b4c2894939802ae0c6c0791ab5d..663982f7de6e6cc77ec1929be1848841502559ac 100644 (file)
@@ -386,8 +386,15 @@ TextLineWrapper::formatLine(const std::string &input,
     }
     int  indent        = (bFirstLine ? settings_.firstLineIndent() : settings_.indent());
     bool bContinuation = (lineEnd < inputLength && input[lineEnd - 1] != '\n');
-    // Strip trailing whitespace.
-    if (!settings_.bKeepFinalSpaces_ || lineEnd < inputLength || input[inputLength - 1] == '\n')
+    // Remove explicit line breaks in input
+    // (the returned line should not contain line breaks).
+    while (lineEnd > lineStart && input[lineEnd - 1] == '\n')
+    {
+        --lineEnd;
+    }
+    // Strip trailing whitespace, unless they are explicit in the input and it
+    // has been requested to keep them.
+    if (bContinuation || !settings_.bKeepFinalSpaces_)
     {
         while (lineEnd > lineStart && std::isspace(input[lineEnd - 1]))
         {
index 1bb9fb422799f30449e826cc210bca64490a9c0a..2a303ac9fc073eb7b97e1fe703964e1f167ad8b0 100644 (file)
@@ -5,5 +5,10 @@
  A quick brown
 fox jumps
  over the lazy
+dog]]></String>
+  <String Name="WrappedAt14WithTrailingWhitespace"><![CDATA[
+ A quick brown
+fox jumps  
+ over the lazy
 dog]]></String>
 </ReferenceData>
diff --git a/src/gromacs/utility/tests/refdata/TextWriterTest_PreservesTrailingWhitespace.xml b/src/gromacs/utility/tests/refdata/TextWriterTest_PreservesTrailingWhitespace.xml
new file mode 100644 (file)
index 0000000..b947fb7
--- /dev/null
@@ -0,0 +1,12 @@
+<?xml version="1.0"?>
+<?xml-stylesheet type="text/xsl" href="referencedata.xsl"?>
+<ReferenceData>
+  <String Name="Output"><![CDATA[
+Line   
+Line   
+Line   
+Line   
+Line   
+Line   
+]]></String>
+</ReferenceData>
index e3c5d87ed0e5aa511d83a245c336ecb149192cf9..bf974093d69fbd8520f1ea1397b5f75ab2c05c31 100644 (file)
@@ -284,7 +284,7 @@ TEST_F(TextLineWrapperTest, HandlesTrailingWhitespace)
 
     wrapper.settings().setKeepFinalSpaces(true);
     EXPECT_EQ("line   ", wrapper.wrapToString("line   "));
-    EXPECT_EQ("line\n", wrapper.wrapToString("line   \n"));
+    EXPECT_EQ("line   \n", wrapper.wrapToString("line   \n"));
 }
 
 TEST_F(TextLineWrapperTest, HandlesTrailingNewlines)
@@ -400,6 +400,10 @@ TEST_F(TextLineWrapperTest, WrapsCorrectlyWithExtraWhitespace)
 
     checkText(wrapper.wrapToString(g_wrapTextWhitespace),
               "WrappedAt14");
+
+    wrapper.settings().setKeepFinalSpaces(true);
+    checkText(wrapper.wrapToString(g_wrapTextWhitespace),
+              "WrappedAt14WithTrailingWhitespace");
 }
 
 } // namespace
index ae671eca9040678aebc8c481620ea46ae7b31df0..db870a26841d0421c4c4af48ba085ac6459ce86b 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * This file is part of the GROMACS molecular simulation package.
  *
- * Copyright (c) 2015, by the GROMACS development team, led by
+ * Copyright (c) 2015,2017, by the GROMACS development team, led by
  * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
  * and including many others, as listed in the AUTHORS file in the
  * top-level source directory and at http://www.gromacs.org.
@@ -138,4 +138,17 @@ TEST_F(TextWriterTest, TracksNewlines)
     checkOutput();
 }
 
+TEST_F(TextWriterTest, PreservesTrailingWhitespace)
+{
+    writer_.writeString("Line   ");
+    writer_.writeLine();
+    writer_.writeString(std::string("Line   "));
+    writer_.writeLine();
+    writer_.writeLine("Line   ");
+    writer_.writeLine(std::string("Line   "));
+    writer_.writeString("Line   \n");
+    writer_.writeString(std::string("Line   \n"));
+    checkOutput();
+}
+
 } // namespace