Skip to content

Commit

Permalink
Refactor|libdeng2: String pattern formatting moved to its own method
Browse files Browse the repository at this point in the history
Pattern formatting does not belong in LogEntry::asText(). Also
fixed a bug in detecting an escaped format character (%%).
  • Loading branch information
skyjake committed Mar 22, 2013
1 parent c35d942 commit ce33ba0
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 37 deletions.
3 changes: 0 additions & 3 deletions doomsday/libdeng2/include/de/core/log.h
Expand Up @@ -310,9 +310,6 @@ class DENG2_PUBLIC LogEntry : public Lockable, public ISerializable
};
Q_DECLARE_FLAGS(Flags, Flag)

/// The format string has incorrect syntax. @ingroup errors
DENG2_ERROR(IllegalFormatError);

typedef QList<Arg *> Args;

public:
Expand Down
13 changes: 13 additions & 0 deletions doomsday/libdeng2/include/de/data/string.h
Expand Up @@ -21,6 +21,7 @@
#define LIBDENG2_STRING_H

#include <QString>
#include <QList>

#include "../libdeng2.h"
#include "../Block"
Expand Down Expand Up @@ -68,6 +69,8 @@ class DENG2_PUBLIC String : public QString
virtual ddouble asNumber() const = 0;
};

typedef QList<IPatternArg const *> PatternArgs;

typedef dint size_type;

public:
Expand Down Expand Up @@ -130,6 +133,16 @@ class DENG2_PUBLIC String : public QString
*/
String operator / (String const &path) const;

/**
* Applies pattern formatting using the string as a format string.
*
* @param args List of arguments.
*
* @return String with placeholders replaced using the arguments.
* @see patternFormat()
*/
String operator % (PatternArgs args) const;

/**
* Does a record member concatenation on a variable name. Record members
* use '.' as the separator character.
Expand Down
29 changes: 3 additions & 26 deletions doomsday/libdeng2/src/core/log.cpp
Expand Up @@ -372,32 +372,9 @@ String LogEntry::asText(Flags const &formattingFlags, int shortenSection) const
}
else
{
Args::const_iterator arg = _args.begin();

DENG2_FOR_EACH_CONST(String, i, _format)
{
if(*i == '%')
{
if(arg == _args.end())
{
// Out of args.
throw IllegalFormatError("LogEntry::asText", "Ran out of arguments");
}

output << String::patternFormat(i, _format.end(), **arg);
++arg;
}
else
{
output << *i;
}
}

// Just append the rest of the arguments without special instructions.
for(; arg != _args.end(); ++arg)
{
output << **arg;
}
String::PatternArgs patArgs;
DENG2_FOR_EACH_CONST(Args, i, _args) patArgs << *i;
output << _format % patArgs;
}

if(flags & Styled)
Expand Down
58 changes: 50 additions & 8 deletions doomsday/libdeng2/src/data/string.cpp
Expand Up @@ -98,6 +98,51 @@ String String::operator / (String const &path) const
return concatenatePath(path);
}

String String::operator % (PatternArgs args) const
{
String result;
QTextStream output(&result);

PatternArgs::const_iterator arg = args.begin();

DENG2_FOR_EACH_CONST(String, i, *this)
{
if(*i == '%')
{
String::const_iterator next = i;
advanceFormat(next, end());
if(*next == '%')
{
// Escaped.
output << *next;
++i;
continue;
}

if(arg == args.end())
{
// Out of args.
throw IllegalPatternError("String::operator %", "Ran out of arguments");
}

output << patternFormat(i, end(), **arg);
++arg;
}
else
{
output << *i;
}
}

// Just append the rest of the arguments without special instructions.
for(; arg != args.end(); ++arg)
{
output << (*arg)->asText();
}

return result;
}

String String::concatenatePath(String const &other, QChar dirChar) const
{
if(QDir::isAbsolutePath(other))
Expand Down Expand Up @@ -425,16 +470,16 @@ String String::patternFormat(String::const_iterator &formatIter,
{
advanceFormat(formatIter, formatEnd);

QString result;
QTextStream output(&result);

// An argument comes here.
bool rightAlign = true;
dint maxWidth = 0;
dint minWidth = 0;

if(*formatIter == '%')
{
// Escaped.
return String(1, *formatIter);
}
DENG2_ASSERT(*formatIter != '%');

if(*formatIter == '-')
{
// Left aligned.
Expand Down Expand Up @@ -464,9 +509,6 @@ String String::patternFormat(String::const_iterator &formatIter,
}

// Finally, the type formatting.
QString result;
QTextStream output(&result);

switch((*formatIter).toLatin1())
{
case 's':
Expand Down

0 comments on commit ce33ba0

Please sign in to comment.