diff --git a/src/Task.cpp b/src/Task.cpp index b07af1e8a..9d4aa6f0a 100644 --- a/src/Task.cpp +++ b/src/Task.cpp @@ -1617,6 +1617,53 @@ void Task::validate_before (const std::string& left, const std::string& right) #endif } +//////////////////////////////////////////////////////////////////////////////// +// Encode values prior to serialization. +// [ -> &open; +// ] -> &close; +const std::string Task::encode (const std::string& value) const +{ + std::string modified = value; + + str_replace (modified, "[", "&open;"); + str_replace (modified, "]", "&close;"); + + return modified; +} + +//////////////////////////////////////////////////////////////////////////////// +// Decode values after parse. +// " <- &dquot; +// ' <- &squot; or " +// , <- , +// [ <- &open; +// ] <- &close; +// : <- : +const std::string Task::decode (const std::string& value) const +{ + if (value.find ('&') != std::string::npos) + { + std::string modified = value; + + // Supported encodings. + str_replace (modified, "&open;", "["); + str_replace (modified, "&close;", "]"); + + // Support for deprecated encodings. These cannot be removed or old files + // will not be parsable. Not just old files - completed.data can contain + // tasks formatted/encoded using these. + str_replace (modified, "&dquot;", "\""); + str_replace (modified, """, "'"); + str_replace (modified, "&squot;", "'"); // Deprecated 2.0 + str_replace (modified, ",", ","); // Deprecated 2.0 + str_replace (modified, ":", ":"); // Deprecated 2.0 + + return modified; + } + + return value; +} + //////////////////////////////////////////////////////////////////////////////// int Task::determineVersion (const std::string& line) { diff --git a/src/Task.h b/src/Task.h index 23620c663..ca0796b34 100644 --- a/src/Task.h +++ b/src/Task.h @@ -164,6 +164,8 @@ private: void parseJSON (const std::string&); void parseLegacy (const std::string&); void validate_before (const std::string&, const std::string&); + const std::string encode (const std::string&) const; + const std::string decode (const std::string&) const; public: float urgency_priority () const;