trunk/branch integration: using Poco::Ascii

This commit is contained in:
Marian Krivos 2011-08-23 06:50:30 +00:00
parent 7331a2cf49
commit 560be487dd

View File

@ -1,7 +1,7 @@
//
// Format.cpp
//
// $Id: //poco/svn/Foundation/src/Format.cpp#2 $
// $Id: //poco/1.4/Foundation/src/Format.cpp#2 $
//
// Library: Foundation
// Package: Core
@ -16,14 +16,14 @@
// execute, and transmit the Software, and to prepare derivative works of the
// Software, and to permit third-parties to whom the Software is furnished to
// do so, all subject to the following:
//
//
// The copyright notices in the Software and this entire statement, including
// the above license grant, this restriction and the following disclaimer,
// must be included in all copies of the Software, in whole or in part, and
// all derivative works of the Software, unless such copies or derivative
// works are solely in the form of machine-executable object code generated by
// a source language processor.
//
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
@ -36,9 +36,8 @@
#include "Poco/Format.h"
#include "Poco/Exception.h"
#include <ios>
#include "Poco/Ascii.h"
#include <sstream>
#include <cctype>
#include <cstddef>
@ -64,33 +63,33 @@ namespace
}
void parseWidth(std::ostream& str, std::string::const_iterator& itFmt, const std::string::const_iterator& endFmt)
{
int width = 0;
while (itFmt != endFmt && std::isdigit(*itFmt))
{
width = 10*width + *itFmt - '0';
++itFmt;
void parseWidth(std::ostream& str, std::string::const_iterator& itFmt, const std::string::const_iterator& endFmt)
{
int width = 0;
while (itFmt != endFmt && Ascii::isDigit(*itFmt))
{
width = 10*width + *itFmt - '0';
++itFmt;
}
if (width != 0) str.width(width);
}
void parsePrec(std::ostream& str, std::string::const_iterator& itFmt, const std::string::const_iterator& endFmt)
{
if (itFmt != endFmt && *itFmt == '.')
{
++itFmt;
int prec = 0;
while (itFmt != endFmt && std::isdigit(*itFmt))
{
prec = 10*prec + *itFmt - '0';
++itFmt;
{
++itFmt;
int prec = 0;
while (itFmt != endFmt && Ascii::isDigit(*itFmt))
{
prec = 10*prec + *itFmt - '0';
++itFmt;
}
if (prec >= 0) str.precision(prec);
}
}
char parseMod(std::string::const_iterator& itFmt, const std::string::const_iterator& endFmt)
{
char mod = 0;
@ -100,16 +99,27 @@ namespace
{
case 'l':
case 'h':
case 'L':
case 'L':
case '?': mod = *itFmt++; break;
}
}
return mod;
}
return mod;
}
std::size_t parseIndex(std::string::const_iterator& itFmt, const std::string::const_iterator& endFmt)
{
int index = 0;
while (itFmt != endFmt && Ascii::isDigit(*itFmt))
{
index = 10*index + *itFmt - '0';
++itFmt;
}
if (itFmt != endFmt && *itFmt == ']') ++itFmt;
return index;
}
void prepareFormat(std::ostream& str, char type)
{
void prepareFormat(std::ostream& str, char type)
{
switch (type)
{
case 'd':
@ -122,8 +132,8 @@ namespace
case 'f': str << std::fixed; break;
}
}
void writeAnyInt(std::ostream& str, const Any& any)
{
if (any.type() == typeid(char))
@ -211,7 +221,7 @@ namespace
str << RefAnyCast<std::string>(*itVal++);
break;
case 'z':
str << AnyCast<std::size_t>(*itVal++);
str << AnyCast<std::size_t>(*itVal++);
break;
case 'I':
case 'D':
@ -340,20 +350,38 @@ void format(std::string& result, const std::string& fmt, const std::vector<Any>&
std::string::const_iterator itFmt = fmt.begin();
std::string::const_iterator endFmt = fmt.end();
std::vector<Any>::const_iterator itVal = values.begin();
std::vector<Any>::const_iterator endVal = values.end();
std::vector<Any>::const_iterator endVal = values.end();
while (itFmt != endFmt)
{
switch (*itFmt)
{
case '%':
++itFmt;
if (itFmt != endFmt && itVal != endVal)
formatOne(result, itFmt, endFmt, itVal);
else if (itFmt != endFmt)
result += *itFmt++;
break;
default:
result += *itFmt;
case '%':
++itFmt;
if (itFmt != endFmt && itVal != endVal)
{
if (*itFmt == '[')
{
++itFmt;
std::size_t index = parseIndex(itFmt, endFmt);
if (index < values.size())
{
std::vector<Any>::const_iterator it = values.begin() + index;
formatOne(result, itFmt, endFmt, it);
}
else throw InvalidArgumentException("format argument index out of range", fmt);
}
else
{
formatOne(result, itFmt, endFmt, itVal);
}
}
else if (itFmt != endFmt)
{
result += *itFmt++;
}
break;
default:
result += *itFmt;
++itFmt;
}
}