diff --git a/pglablib/FormatToStream.cpp b/pglablib/FormatToStream.cpp new file mode 100644 index 0000000..f8615bc --- /dev/null +++ b/pglablib/FormatToStream.cpp @@ -0,0 +1,29 @@ +#include "FormatToStream.h" +#include +#include + +void FormatToStream(QTextStream &stream, QString format, std::function field_callback) +{ + // Use static to optimize only once + static QRegularExpression cached_find_var_re("(?:[^\\\\]|^)(\\/%([a-zA-Z0-9_-]+)%\\/)", QRegularExpression::OptimizeOnFirstUsageOption); + + int from = 0; + QRegularExpressionMatch match; + while (format.indexOf(cached_find_var_re, from, &match) >= 0) { + if (from > 0) { + // Because the regex has to check for backslash in front we have the from position + // one position before where we actually should continue for the second match and later ie when from > 0 + // Therefor increase from by 1 to make the substring (midRef) calculation work + ++from; + } + // copy code before the var to the stream + stream << format.midRef(from, match.capturedStart(1) - from); + field_callback(stream, match.captured(2)); + from = match.capturedEnd()-1; // -1 because it wants to match one character before or start of line to make sure there is no backslash + } + if (from > 0) { + // same reason as at the start of the loop + ++from; + } + stream << format.midRef(from); +} diff --git a/pglablib/FormatToStream.h b/pglablib/FormatToStream.h new file mode 100644 index 0000000..aabc1f3 --- /dev/null +++ b/pglablib/FormatToStream.h @@ -0,0 +1,17 @@ +#ifndef FORMATTOSTREAM_H +#define FORMATTOSTREAM_H + +#include +#include + +class QTextStream; + +/** + * @brief FormatToStream replaces /%var%/ variables in a string with the values returned by the callback + * @param stream The stream to which to write the result of the formatting operation + * @param format The format string containing the vars. + * @param field_callback A callable which returns the values for var. + */ +void FormatToStream(QTextStream &stream, QString format, std::function field_callback); + +#endif // FORMATTOSTREAM_H diff --git a/pglablib/codebuilder/CodeBuilder.cpp b/pglablib/codebuilder/CodeBuilder.cpp index baed498..9671311 100644 --- a/pglablib/codebuilder/CodeBuilder.cpp +++ b/pglablib/codebuilder/CodeBuilder.cpp @@ -6,32 +6,6 @@ #include "util.h" #include -void FormatToStream(QTextStream &stream, QString format, std::function field_callback) -{ - // Use static to optimize only once - static QRegularExpression cached_find_var_re("(?:[^\\\\]|^)(\\/%([a-zA-Z0-9_-]+)%\\/)", QRegularExpression::OptimizeOnFirstUsageOption); - - int from = 0; - QRegularExpressionMatch match; - while (format.indexOf(cached_find_var_re, from, &match) >= 0) { - if (from > 0) { - // Because the regex has to check for backslash in front we have the from position - // one position before where we actually should continue for the second match and later ie when from > 0 - // Therefor increase from by 1 to make the substring (midRef) calculation work - ++from; - } - // copy code before the var to the stream - stream << format.midRef(from, match.capturedStart(1) - from); - field_callback(stream, match.captured(2)); - from = match.capturedEnd()-1; // -1 because it wants to match one character before or start of line to make sure there is no backslash - } - if (from > 0) { - // same reason as at the start of the loop - ++from; - } - stream << format.midRef(from); -} - void CodeBuilder::GenCodeForExecutingQuery(QTextStream &q, const QString &query, const Pgsql::Result &result) { // %1 query string diff --git a/pglablib/pglablib.pro b/pglablib/pglablib.pro index 29b5c40..4441836 100644 --- a/pglablib/pglablib.pro +++ b/pglablib/pglablib.pro @@ -64,7 +64,8 @@ codebuilder/NameManglingRules.cpp \ codebuilder/DefaultConfigs.cpp \ codebuilder/TypeMappings.cpp \ codebuilder/IndentationConfig.cpp \ -codebuilder/StructureTemplate.cpp +codebuilder/StructureTemplate.cpp \ + FormatToStream.cpp HEADERS += \ Pglablib.h \ @@ -106,7 +107,8 @@ codebuilder/NameManglingRules.h \ codebuilder/DefaultConfigs.h \ codebuilder/TypeMappings.h \ codebuilder/IndentationConfig.h \ -codebuilder/StructureTemplate.h +codebuilder/StructureTemplate.h \ + FormatToStream.h unix { target.path = /usr/lib