From 9618f0ebbf4b88045247c01ce8c8f58203508ebf Mon Sep 17 00:00:00 2001 From: Unit 193 Date: Tue, 12 May 2015 16:19:40 -0400 Subject: Imported Upstream version 3.0.6 --- SQLiteStudio3/coreSQLiteStudio/csvserializer.cpp | 54 +++++++++++++++++++++--- 1 file changed, 48 insertions(+), 6 deletions(-) (limited to 'SQLiteStudio3/coreSQLiteStudio/csvserializer.cpp') diff --git a/SQLiteStudio3/coreSQLiteStudio/csvserializer.cpp b/SQLiteStudio3/coreSQLiteStudio/csvserializer.cpp index 7d7d20b..c89074f 100644 --- a/SQLiteStudio3/coreSQLiteStudio/csvserializer.cpp +++ b/SQLiteStudio3/coreSQLiteStudio/csvserializer.cpp @@ -35,6 +35,38 @@ QString CsvSerializer::serialize(const QStringList& data, const CsvFormat& forma return outputCells.join(format.columnSeparator); } +template +bool isCsvColumnSeparator(const T& data, int pos, const CsvFormat& format) +{ + if (!format.strictColumnSeparator && format.columnSeparator.contains(data[pos])) + return true; + + for (const QChar& c : format.columnSeparator) + { + if (c != data[pos++]) + return false; + } + + return true; +} + +template +bool isCsvRowSeparator(const T& data, int& pos, const CsvFormat& format) +{ + if (!format.strictRowSeparator && format.rowSeparator.contains(data[pos])) + return true; + + int localPos = pos; + for (const QChar& c : format.rowSeparator) + { + if (localPos >= data.size() || c != data[localPos++]) + return false; + } + + pos = localPos - 1; + return true; +} + template QList> typedDeserialize(const T& data, const CsvFormat& format) { @@ -58,23 +90,33 @@ QList> typedDeserialize(const T& data, const CsvFormat& format) } else if (quotes && c == '"' ) { - if (pos + 1 < data.length() && data[pos+1] == '"' ) + if (pos + 1 < data.length()) { - field += c; - pos++; + if (data[pos+1] == '"' ) + { + field += c; + pos++; + } + else + { + quotes = false; + } } else { - quotes = false; + if (field.length() == 0) + cells << field; + + quotes = false; } } - else if (!quotes && format.columnSeparator.contains(c)) + else if (!quotes && isCsvColumnSeparator(data, pos, format)) { cells << field; field.clear(); sepAsLast = true; } - else if (!quotes && format.rowSeparator.contains(c)) + else if (!quotes && isCsvRowSeparator(data, pos, format)) { cells << field; rows << cells; -- cgit v1.2.3