2012-09-30 05:17:56 +00:00
|
|
|
//
|
|
|
|
// NumericString.h
|
|
|
|
//
|
|
|
|
// $Id: //poco/1.4/Foundation/include/Poco/NumericString.h#1 $
|
|
|
|
//
|
|
|
|
// Library: Foundation
|
|
|
|
// Package: Core
|
|
|
|
// Module: NumericString
|
|
|
|
//
|
|
|
|
// Numeric string utility functions.
|
|
|
|
//
|
|
|
|
// Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
|
|
|
|
// and Contributors.
|
|
|
|
//
|
|
|
|
// Permission is hereby granted, free of charge, to any person or organization
|
|
|
|
// obtaining a copy of the software and accompanying documentation covered by
|
|
|
|
// this license (the "Software") to use, reproduce, display, distribute,
|
|
|
|
// 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
|
|
|
|
// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
|
|
|
|
// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
|
|
|
|
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
|
|
|
// DEALINGS IN THE SOFTWARE.
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
|
|
#ifndef Foundation_NumericString_INCLUDED
|
|
|
|
#define Foundation_NumericString_INCLUDED
|
|
|
|
|
|
|
|
|
|
|
|
#include "Poco/Foundation.h"
|
|
|
|
#include "Poco/FPEnvironment.h"
|
|
|
|
#ifdef min
|
|
|
|
#undef min
|
|
|
|
#endif
|
|
|
|
#ifdef max
|
|
|
|
#undef max
|
|
|
|
#endif
|
|
|
|
#include <limits>
|
|
|
|
#include <cmath>
|
2012-09-30 13:10:51 +00:00
|
|
|
#include <locale>
|
2012-09-30 05:17:56 +00:00
|
|
|
|
|
|
|
|
|
|
|
namespace Poco {
|
|
|
|
|
|
|
|
|
2012-09-30 13:10:51 +00:00
|
|
|
inline char decimalSeparator()
|
2012-10-01 00:52:53 +00:00
|
|
|
/// Returns decimal separator from global locale or
|
|
|
|
/// default '.' for platforms where locale is unavailable.
|
2012-09-30 13:10:51 +00:00
|
|
|
{
|
|
|
|
#if !defined(POCO_NO_LOCALE)
|
2012-10-01 00:52:53 +00:00
|
|
|
return std::use_facet<std::numpunct<char> >(std::locale()).decimal_point();
|
2012-09-30 13:10:51 +00:00
|
|
|
#else
|
2012-10-01 00:52:53 +00:00
|
|
|
return '.';
|
2012-09-30 13:10:51 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
inline char thousandSeparator()
|
2012-10-01 00:52:53 +00:00
|
|
|
/// Returns thousand separator from global locale or
|
|
|
|
/// default ',' for platforms where locale is unavailable.
|
2012-09-30 13:10:51 +00:00
|
|
|
{
|
|
|
|
#if !defined(POCO_NO_LOCALE)
|
2012-10-01 00:52:53 +00:00
|
|
|
return std::use_facet<std::numpunct<char> >(std::locale()).thousands_sep();
|
2012-09-30 13:10:51 +00:00
|
|
|
#else
|
2012-10-01 00:52:53 +00:00
|
|
|
return ',';
|
2012-09-30 13:10:51 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-09-30 05:17:56 +00:00
|
|
|
template <typename I>
|
2012-10-13 03:51:32 +00:00
|
|
|
bool strToInt(const char* pStr, I& result, short base, char thSep = ',')
|
2012-10-01 00:52:53 +00:00
|
|
|
/// Converts zero-terminated character array to integer number;
|
|
|
|
/// Thousand separators are recognized for base10 and current locale;
|
|
|
|
/// it is silently skipped but not verified for correct positioning.
|
|
|
|
/// Function returns true if succesful. If parsing was unsuccesful,
|
|
|
|
/// the return value is false with the result value undetermined.
|
2012-09-30 05:17:56 +00:00
|
|
|
{
|
2012-10-01 00:52:53 +00:00
|
|
|
if (!pStr) return false;
|
2012-10-13 03:51:32 +00:00
|
|
|
while (isspace(*pStr)) ++pStr;
|
2012-09-30 05:17:56 +00:00
|
|
|
if (*pStr == '\0') return false;
|
2012-10-13 05:10:49 +00:00
|
|
|
char sign = 1;
|
2012-10-01 00:52:53 +00:00
|
|
|
if ((base == 10) && (*pStr == '-'))
|
2012-09-30 05:17:56 +00:00
|
|
|
{
|
|
|
|
sign = -1;
|
|
|
|
++pStr;
|
|
|
|
}
|
2012-10-01 00:52:53 +00:00
|
|
|
else if (*pStr == '+') ++pStr;
|
2012-09-30 05:17:56 +00:00
|
|
|
|
2012-10-01 00:52:53 +00:00
|
|
|
// parser states:
|
|
|
|
const char STATE_SIGNIFICANT_DIGITS = 1;
|
|
|
|
char state = 0;
|
|
|
|
|
|
|
|
result = 0;
|
|
|
|
I limitCheck = std::numeric_limits<I>::max() / base;
|
2012-09-30 05:17:56 +00:00
|
|
|
for (; *pStr != '\0'; ++pStr)
|
|
|
|
{
|
|
|
|
switch (*pStr)
|
|
|
|
{
|
2012-10-01 00:52:53 +00:00
|
|
|
case 'x': case 'X':
|
|
|
|
if (base != 0x10) return false;
|
|
|
|
|
|
|
|
case '0':
|
|
|
|
if (state < STATE_SIGNIFICANT_DIGITS) break;
|
|
|
|
|
|
|
|
case '1': case '2': case '3': case '4':
|
2012-09-30 05:17:56 +00:00
|
|
|
case '5': case '6': case '7':
|
2012-10-01 00:52:53 +00:00
|
|
|
if (state < STATE_SIGNIFICANT_DIGITS) state = STATE_SIGNIFICANT_DIGITS;
|
|
|
|
if (result > limitCheck) return false;
|
|
|
|
result = result * base + (*pStr - '0');
|
|
|
|
|
2012-09-30 05:17:56 +00:00
|
|
|
break;
|
2012-09-30 13:10:51 +00:00
|
|
|
|
2012-09-30 05:17:56 +00:00
|
|
|
case '8': case '9':
|
2012-10-01 00:52:53 +00:00
|
|
|
if ((base == 10) || (base == 0x10))
|
2012-09-30 05:17:56 +00:00
|
|
|
{
|
2012-10-01 00:52:53 +00:00
|
|
|
if (state < STATE_SIGNIFICANT_DIGITS) state = STATE_SIGNIFICANT_DIGITS;
|
|
|
|
if (result > limitCheck) return false;
|
2012-09-30 05:17:56 +00:00
|
|
|
result = result * base + (*pStr - '0');
|
|
|
|
}
|
|
|
|
else return false;
|
2012-10-01 00:52:53 +00:00
|
|
|
|
2012-09-30 05:17:56 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
|
2012-10-01 00:52:53 +00:00
|
|
|
if (base != 0x10) return false;
|
|
|
|
if (state < STATE_SIGNIFICANT_DIGITS) state = STATE_SIGNIFICANT_DIGITS;
|
|
|
|
if (result > limitCheck) return false;
|
|
|
|
result = result * base + (10 + *pStr - 'a');
|
|
|
|
|
2012-09-30 05:17:56 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
|
2012-10-01 00:52:53 +00:00
|
|
|
if (base != 0x10) return false;
|
|
|
|
|
|
|
|
if (state < STATE_SIGNIFICANT_DIGITS) state = STATE_SIGNIFICANT_DIGITS;
|
|
|
|
if (result > limitCheck) return false;
|
|
|
|
result = result * base + (10 + *pStr - 'A');
|
|
|
|
|
2012-09-30 05:17:56 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'U':
|
|
|
|
case 'u':
|
|
|
|
case 'L':
|
|
|
|
case 'l':
|
2012-10-01 00:52:53 +00:00
|
|
|
goto done;
|
2012-09-30 05:17:56 +00:00
|
|
|
|
|
|
|
case '.':
|
|
|
|
if ((base == 10) && (thSep == '.')) break;
|
|
|
|
else return false;
|
|
|
|
|
|
|
|
case ',':
|
|
|
|
if ((base == 10) && (thSep == ',')) break;
|
|
|
|
else return false;
|
|
|
|
|
|
|
|
case ' ':
|
2012-10-01 00:52:53 +00:00
|
|
|
if ((base == 10) && (thSep == ' ')) break;
|
2012-10-13 03:51:32 +00:00
|
|
|
case '\t':
|
|
|
|
case '\n':
|
|
|
|
case '\v':
|
|
|
|
case '\f':
|
|
|
|
case '\r':
|
2012-10-01 00:52:53 +00:00
|
|
|
goto done;
|
2012-09-30 05:17:56 +00:00
|
|
|
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-01 00:52:53 +00:00
|
|
|
done:
|
|
|
|
if ((sign < 0) && (base == 10)) result *= sign;
|
2012-09-30 05:17:56 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template <typename I>
|
2012-10-13 03:51:32 +00:00
|
|
|
bool strToInt(const std::string& str, I& result, short base, char thSep = ',')
|
2012-09-30 05:17:56 +00:00
|
|
|
/// Converts string to integer number;
|
|
|
|
/// This is a wrapper function, for details see see the
|
2012-10-13 03:51:32 +00:00
|
|
|
/// bool strToInt(const char*, I&, short, char) implementation.
|
2012-09-30 05:17:56 +00:00
|
|
|
{
|
2012-10-13 03:51:32 +00:00
|
|
|
return strToInt(str.c_str(), result, base, thSep);
|
2012-09-30 05:17:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#ifndef POCO_NO_FPENVIRONMENT
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
static char DUMMY_EXP_UNDERFLOW = 0; // dummy default val
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename F>
|
2012-10-13 03:51:32 +00:00
|
|
|
bool strToFloat (const char* pStr, F& result, char& eu = DUMMY_EXP_UNDERFLOW, char decSep = '.', char thSep = ',')
|
2012-09-30 05:17:56 +00:00
|
|
|
/// Converts zero-terminated array to floating-point number;
|
|
|
|
/// Returns true if succesful. Exponent underflow (i.e. loss of precision)
|
|
|
|
/// is signalled in eu. Thousand separators are recognized for the locale
|
|
|
|
/// and silently skipped but not verified for correct positioning.
|
|
|
|
///
|
|
|
|
/// If parsing was unsuccesful, the return value is false with
|
|
|
|
/// result and eu values undetermined.
|
|
|
|
{
|
2012-10-13 03:51:32 +00:00
|
|
|
poco_assert (decSep != thSep);
|
|
|
|
|
|
|
|
if (pStr == 0 || *pStr == '\0') return false;
|
2012-09-30 05:17:56 +00:00
|
|
|
|
|
|
|
// parser states:
|
|
|
|
const char STATE_LEADING_SPACES = 0;
|
|
|
|
const char STATE_DIGITS_BEFORE_DEC_POINT = 1;
|
|
|
|
const char STATE_DIGITS_AFTER_DEC_POINT = 2;
|
|
|
|
const char STATE_EXP_CHAR = 3;
|
|
|
|
const char STATE_EXP_DIGITS = 4;
|
|
|
|
const char STATE_SUFFIX = 5; // 'f' suffix
|
|
|
|
|
|
|
|
char numSign = 1, expSign = 1;
|
|
|
|
char state = STATE_LEADING_SPACES;
|
|
|
|
F mantissa = 0.0, exponent = 0.0;
|
|
|
|
F pow10 = 1.;
|
|
|
|
result = 0.0;
|
|
|
|
eu = 0;
|
|
|
|
for (; *pStr != '\0'; ++pStr)
|
|
|
|
{
|
|
|
|
switch (*pStr)
|
|
|
|
{
|
|
|
|
case '.':
|
|
|
|
if (decSep == '.')
|
|
|
|
{
|
2012-10-13 03:51:32 +00:00
|
|
|
if (state >= STATE_DIGITS_AFTER_DEC_POINT) return false;
|
2012-09-30 05:17:56 +00:00
|
|
|
state = STATE_DIGITS_AFTER_DEC_POINT;
|
2012-10-13 03:51:32 +00:00
|
|
|
break;
|
2012-09-30 05:17:56 +00:00
|
|
|
}
|
2012-10-13 03:51:32 +00:00
|
|
|
else if ((thSep == '.') && (state == STATE_DIGITS_BEFORE_DEC_POINT))
|
|
|
|
break;
|
|
|
|
else
|
|
|
|
return false;
|
2012-09-30 05:17:56 +00:00
|
|
|
|
|
|
|
case ',':
|
|
|
|
if (decSep == ',')
|
|
|
|
{
|
2012-10-13 03:51:32 +00:00
|
|
|
if (state >= STATE_DIGITS_AFTER_DEC_POINT) return false;
|
2012-09-30 05:17:56 +00:00
|
|
|
state = STATE_DIGITS_AFTER_DEC_POINT;
|
2012-10-13 03:51:32 +00:00
|
|
|
break;
|
2012-09-30 05:17:56 +00:00
|
|
|
}
|
2012-10-13 03:51:32 +00:00
|
|
|
else if ((thSep == ',') && (state == STATE_DIGITS_BEFORE_DEC_POINT))
|
|
|
|
break;
|
|
|
|
else
|
|
|
|
return false;
|
2012-09-30 05:17:56 +00:00
|
|
|
|
2012-10-13 03:51:32 +00:00
|
|
|
case ' ': // space (SPC)
|
|
|
|
if ((thSep == ' ') && (state == STATE_DIGITS_BEFORE_DEC_POINT)) break;
|
|
|
|
case '\t': // horizontal tab (TAB)
|
|
|
|
case '\n': // line feed (LF)
|
|
|
|
case '\v': // vertical tab (VT)
|
|
|
|
case '\f': // form feed (FF)
|
|
|
|
case '\r': // carriage return (CR)
|
|
|
|
if ((state >= STATE_DIGITS_AFTER_DEC_POINT) || (state >= STATE_EXP_DIGITS))
|
|
|
|
break;
|
|
|
|
else if ((state > STATE_LEADING_SPACES) && (state < STATE_DIGITS_AFTER_DEC_POINT))
|
2012-09-30 05:17:56 +00:00
|
|
|
return false;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '-':
|
|
|
|
if (state == STATE_LEADING_SPACES)
|
|
|
|
numSign = -1;
|
|
|
|
else if (state == STATE_EXP_CHAR) // exponential char
|
|
|
|
expSign = -1;
|
|
|
|
else return false;
|
|
|
|
case '+':
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '0':
|
|
|
|
case '1':
|
|
|
|
case '2':
|
|
|
|
case '3':
|
|
|
|
case '4':
|
|
|
|
case '5':
|
|
|
|
case '6':
|
|
|
|
case '7':
|
|
|
|
case '8':
|
|
|
|
case '9':
|
|
|
|
if (state >= STATE_SUFFIX) return false; // constant suffix
|
|
|
|
if (state <= STATE_DIGITS_BEFORE_DEC_POINT) // integral part digits
|
|
|
|
{
|
|
|
|
result = result * 10 + (*pStr - '0');
|
|
|
|
state = STATE_DIGITS_BEFORE_DEC_POINT;
|
|
|
|
}
|
|
|
|
else if (state <= STATE_DIGITS_AFTER_DEC_POINT) // fractional part digits
|
|
|
|
{
|
|
|
|
mantissa += (*pStr - '0') / (pow10 *= 10.);
|
|
|
|
state = STATE_DIGITS_AFTER_DEC_POINT;
|
|
|
|
}
|
|
|
|
else if (state <= STATE_EXP_DIGITS) // exponent digits
|
|
|
|
{
|
|
|
|
exponent = exponent * 10 + (*pStr - '0');
|
|
|
|
state = STATE_EXP_DIGITS;
|
|
|
|
}
|
|
|
|
else return false;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'E':
|
|
|
|
case 'e':
|
|
|
|
if (state > STATE_DIGITS_AFTER_DEC_POINT) return false;
|
|
|
|
state = STATE_EXP_CHAR;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'F':
|
|
|
|
case 'f':
|
|
|
|
state = STATE_SUFFIX;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (exponent > std::numeric_limits<F>::max_exponent10)
|
|
|
|
{
|
|
|
|
eu = expSign;
|
|
|
|
exponent = std::numeric_limits<F>::max_exponent10;
|
|
|
|
}
|
|
|
|
|
|
|
|
result += mantissa;
|
|
|
|
if (numSign != 1) result *= numSign;
|
|
|
|
if (exponent > 1.0)
|
|
|
|
{
|
|
|
|
F scale = std::pow(10., exponent);
|
|
|
|
result = (expSign < 0) ? (result / scale) : (result * scale);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (state != STATE_LEADING_SPACES) && // empty/zero-length string
|
|
|
|
!FPEnvironment::isInfinite(result) &&
|
|
|
|
!FPEnvironment::isNaN(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template <typename F>
|
2012-10-13 03:51:32 +00:00
|
|
|
bool strToFloat (const std::string& s, F& result, char& eu = DUMMY_EXP_UNDERFLOW, char decSep = '.', char thSep = ',')
|
2012-09-30 05:17:56 +00:00
|
|
|
/// Converts string to floating-point number;
|
|
|
|
/// This is a wrapper function, for details see see the
|
2012-10-13 03:51:32 +00:00
|
|
|
/// bool strToFloat(const char*, F&, char&, char, char) implementation.
|
2012-09-30 05:17:56 +00:00
|
|
|
{
|
2012-10-13 03:51:32 +00:00
|
|
|
return strToFloat(s.c_str(), result, eu, decSep, thSep);
|
2012-09-30 05:17:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#endif // POCO_NO_FPENVIRONMENT
|
|
|
|
|
|
|
|
|
|
|
|
} // namespace Poco
|
|
|
|
|
|
|
|
|
|
|
|
#endif // Foundation_NumericString_INCLUDED
|