]> git.lyx.org Git - features.git/commitdiff
Improve support for empty lengths
authorJean-Marc Lasgouttes <lasgouttes@lyx.org>
Mon, 15 Apr 2013 10:35:11 +0000 (12:35 +0200)
committerJean-Marc Lasgouttes <lasgouttes@lyx.org>
Fri, 24 May 2013 13:44:58 +0000 (15:44 +0200)
Parse empty string as empty length
Output empty length as empty string when it makes sense (not for LaTeX strings, for example).

src/Length.cpp
src/lengthcommon.cpp

index f8eb920c0ff48e5b58bd0065d0aad38d00c9009a..bb9ec6a3b90b8096d46af014a537cdd869a9527f 100644 (file)
@@ -19,6 +19,7 @@
 #include "LyXRC.h"
 
 #include "support/docstream.h"
+#include "support/lassert.h"
 
 #include <sstream>
 #include <iomanip>
@@ -67,7 +68,8 @@ void Length::swap(Length & rhs)
 string const Length::asString() const
 {
        ostringstream os;
-       os << val_ << unit_name[unit_]; // setw?
+       if (unit_ != UNIT_NONE)
+               os << val_ << unit_name[unit_]; // setw?
        return os.str();
 }
 
@@ -75,7 +77,8 @@ string const Length::asString() const
 docstring const Length::asDocstring() const
 {
        odocstringstream os;
-       os << val_ << unit_name[unit_]; // setw?
+       if (unit_ != UNIT_NONE)
+               os << val_ << unit_name[unit_]; // setw?
        return os.str();
 }
 
@@ -102,6 +105,9 @@ string const Length::asLatexString() const
        case PPH:
                os << val_ / 100.0 << "\\paperheight";
                break;
+       case UNIT_NONE:
+               // One should not try to ouput latex code for an empty length
+               LASSERT(false, break);
        default:
                os << val_ << unit_name[unit_];
          break;
@@ -363,6 +369,9 @@ GlueLength::GlueLength(string const & data)
 
 string const GlueLength::asString() const
 {
+       if (len_.empty())
+               return string();
+
        ostringstream buffer;
 
        buffer << len_.value();
index 217c22b6048c48e393d8ddfe8309c01ec49950d9..72f5bbf88faf4274800b11edd344152b9ae79e4b 100644 (file)
@@ -236,8 +236,11 @@ bool isValidGlueLength(string const & data, GlueLength * result)
        // forward approach leads to very long, tedious code that would be
        // much harder to understand and maintain. (AS)
 
-       if (data.empty())
+       if (data.empty()) {
+               if (result)
+                       *result = GlueLength();
                return true;
+       }
        string buffer = ltrim(data);
 
        // To make isValidGlueLength recognize negative values as
@@ -306,8 +309,11 @@ bool isValidLength(string const & data, Length * result)
        // The parser may seem overkill for lengths without
        // glue, but since we already have it, using it is
        // easier than writing something from scratch.
-       if (data.empty())
+       if (data.empty()) {
+               if (result)
+                       *result = Length();
                return true;
+       }
 
        string   buffer = data;
        int      pattern_index = 0;