/usr/include/cxxtools/convert.h is in libcxxtools-dev 2.2.1-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 | /*
* Copyright (C) 2004-2007 by Marc Boris Duerner
* Copyright (C) 2004-2007 by Stepan Beal
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* As a special exception, you may use this file as part of a free
* software library without restriction. Specifically, if other files
* instantiate templates or use macros or inline functions from this
* file, or you compile this file and link it with other files to
* produce an executable, this file does not by itself cause the
* resulting executable to be covered by the GNU General Public
* License. This exception does not however invalidate any other
* reasons why the executable file might be covered by the GNU Library
* General Public License.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef CXXTOOLS_CONVERT_H
#define CXXTOOLS_CONVERT_H
#include <cxxtools/api.h>
#include <cxxtools/config.h>
#include <cxxtools/string.h>
#include <cxxtools/stringstream.h>
#include <cxxtools/conversionerror.h>
#include <cxxtools/typetraits.h>
#include <sstream>
#include <string>
#include <typeinfo>
#include <limits>
#include <iterator>
#include <cctype>
#include <cmath>
#include <cxxtools/config.h>
namespace cxxtools
{
//
// Conversions to cxxtools::String
//
inline void convert(String& s, const String& str)
{
s = str;
}
CXXTOOLS_API void convert(String& s, const std::string& value);
CXXTOOLS_API void convert(String& s, bool value);
CXXTOOLS_API void convert(String& s, char value);
CXXTOOLS_API void convert(String& s, wchar_t value);
CXXTOOLS_API void convert(String& s, Char value);
CXXTOOLS_API void convert(String& s, unsigned char value);
CXXTOOLS_API void convert(String& s, signed char value);
CXXTOOLS_API void convert(String& s, short value);
CXXTOOLS_API void convert(String& s, unsigned short value);
CXXTOOLS_API void convert(String& s, int value);
CXXTOOLS_API void convert(String& s, unsigned int value);
CXXTOOLS_API void convert(String& s, long value);
CXXTOOLS_API void convert(String& s, unsigned long value);
CXXTOOLS_API void convert(String& s, float value);
CXXTOOLS_API void convert(String& s, double value);
CXXTOOLS_API void convert(String& s, long double value);
template <typename T>
inline void convert(String& s, const T& value)
{
OStringStream os;
os << value;
s = os.str();
}
//
// Conversions from cxxtools::String
//
CXXTOOLS_API void convert(bool& n, const String& str);
CXXTOOLS_API void convert(char& n, const String& str);
CXXTOOLS_API void convert(wchar_t& n, const String& str);
CXXTOOLS_API void convert(Char& n, const String& str);
CXXTOOLS_API void convert(unsigned char& n, const String& str);
CXXTOOLS_API void convert(signed char& n, const String& str);
CXXTOOLS_API void convert(short& n, const String& str);
CXXTOOLS_API void convert(unsigned short& n, const String& str);
CXXTOOLS_API void convert(int& n, const String& str);
CXXTOOLS_API void convert(unsigned int& n, const String& str);
CXXTOOLS_API void convert(long& n, const String& str);
CXXTOOLS_API void convert(unsigned long& n, const String& str);
#ifdef HAVE_LONG_LONG
CXXTOOLS_API void convert(long long& n, const String& str);
#endif
#ifdef HAVE_UNSIGNED_LONG_LONG
CXXTOOLS_API void convert(unsigned long long& n, const String& str);
#endif
CXXTOOLS_API void convert(float& n, const String& str);
CXXTOOLS_API void convert(double& n, const String& str);
CXXTOOLS_API void convert(long double& n, const String& str);
template <typename T>
inline void convert(T& t, const String& str)
{
IStringStream is(str);
Char ch;
is >> t;
if (is.fail() || !(is >> ch).eof())
ConversionError::doThrow(typeid(T).name(), "String");
}
//
// Conversions to std::string
//
inline void convert(std::string& s, const std::string& str)
{
s = str;
}
CXXTOOLS_API void convert(std::string& s, const String& str);
CXXTOOLS_API void convert(std::string& s, bool value);
CXXTOOLS_API void convert(std::string& s, char value);
CXXTOOLS_API void convert(std::string& s, signed char value);
CXXTOOLS_API void convert(std::string& s, unsigned char value);
CXXTOOLS_API void convert(std::string& s, short value);
CXXTOOLS_API void convert(std::string& s, unsigned short value);
CXXTOOLS_API void convert(std::string& s, int value);
CXXTOOLS_API void convert(std::string& s, unsigned int value);
CXXTOOLS_API void convert(std::string& s, long value);
CXXTOOLS_API void convert(std::string& s, unsigned long value);
CXXTOOLS_API void convert(std::string& s, float value);
CXXTOOLS_API void convert(std::string& s, double value);
CXXTOOLS_API void convert(std::string& s, long double value);
template <typename T>
inline void convert(std::string& s, const T& value)
{
std::ostringstream os;
os << value;
s = os.str();
}
//
// Conversions from std::string
//
CXXTOOLS_API void convert(bool& n, const std::string& str);
CXXTOOLS_API void convert(char& n, const std::string& str);
CXXTOOLS_API void convert(signed char& n, const std::string& str);
CXXTOOLS_API void convert(unsigned char& n, const std::string& str);
CXXTOOLS_API void convert(short& n, const std::string& str);
CXXTOOLS_API void convert(unsigned short& n, const std::string& str);
CXXTOOLS_API void convert(int& n, const std::string& str);
CXXTOOLS_API void convert(unsigned int& n, const std::string& str);
CXXTOOLS_API void convert(long& n, const std::string& str);
CXXTOOLS_API void convert(unsigned long& n, const std::string& str);
#ifdef HAVE_LONG_LONG
CXXTOOLS_API void convert(long long& n, const std::string& str);
#endif
#ifdef HAVE_UNSIGNED_LONG_LONG
CXXTOOLS_API void convert(unsigned long long& n, const std::string& str);
#endif
CXXTOOLS_API void convert(float& n, const std::string& str);
CXXTOOLS_API void convert(double& n, const std::string& str);
CXXTOOLS_API void convert(long double& n, const std::string& str);
template <typename T>
inline void convert(T& t, const std::string& str)
{
std::istringstream is(str);
char ch;
is >> t;
if (is.fail() || !(is >> ch).eof())
ConversionError::doThrow(typeid(T).name(), "string");
}
//
// Conversions from const char* (null-terminated)
//
CXXTOOLS_API void convert(bool& n, const char* str);
CXXTOOLS_API void convert(char& n, const char* str);
CXXTOOLS_API void convert(signed char& n, const char* str);
CXXTOOLS_API void convert(unsigned char& n, const char* str);
CXXTOOLS_API void convert(short& n, const char* str);
CXXTOOLS_API void convert(unsigned short& n, const char* str);
CXXTOOLS_API void convert(int& n, const char* str);
CXXTOOLS_API void convert(unsigned int& n, const char* str);
CXXTOOLS_API void convert(long& n, const char* str);
CXXTOOLS_API void convert(unsigned long& n, const char* str);
#ifdef HAVE_LONG_LONG
CXXTOOLS_API void convert(long long& n, const char* str);
#endif
#ifdef HAVE_UNSIGNED_LONG_LONG
CXXTOOLS_API void convert(unsigned long long& n, const char* str);
#endif
CXXTOOLS_API void convert(float& n, const char* str);
CXXTOOLS_API void convert(double& n, const char* str);
CXXTOOLS_API void convert(long double& n, const char* str);
//
// Generic stream-based conversions
//
template<typename T, typename S>
void convert(T& to, const S& from)
{
StringStream ss;
if( !(ss << from && ss >> to) )
throw ConversionError("conversion between streamable types failed");
}
template<typename T, typename S>
struct Convert
{
T operator()(const S& from) const
{
T value = T();
convert(value, from);
return value;
}
};
template<typename T, typename S>
T convert(const S& from)
{
T value = T();
convert(value, from);
return value;
}
//
// number formatting
//
/** @brief Formats an integer in a given format format.
*/
template <typename OutIterT, typename T, typename FormatT>
inline OutIterT putInt(OutIterT it, T i, const FormatT& fmt);
/** @brief Formats an integer in a decimal format.
*/
template <typename OutIterT, typename T>
inline OutIterT putInt(OutIterT it, T i);
/** @brief Formats a floating point value in a given format.
*/
template <typename OutIterT, typename T, typename FormatT>
OutIterT putFloat(OutIterT it, T d, const FormatT& fmt, int precision);
/** @brief Formats a floating point value in default format.
*/
template <typename OutIterT, typename T>
OutIterT putFloat(OutIterT it, T d);
//
// number parsing
//
/** @brief Parses an integer value in a given format.
*/
template <typename InIterT, typename T, typename FormatT>
InIterT getInt(InIterT it, InIterT end, bool& ok, T& n, const FormatT& fmt);
/** @brief Parses an integer value in decimal format.
*/
template <typename InIterT, typename T>
InIterT getInt(InIterT it, InIterT end, bool& ok, T& n);
/** @brief Parses a floating point value in a given format.
*/
template <typename InIterT, typename T, typename FormatT>
InIterT getFloat(InIterT it, InIterT end, bool& ok, T& n, const FormatT& fmt);
/** @brief Parses a floating point value.
*/
template <typename InIterT, typename T>
InIterT getFloat(InIterT it, InIterT end, bool& ok, T& n);
template <typename CharType>
struct NumberFormat
{
typedef CharType CharT;
static CharT plus()
{ return CharT('+'); }
static CharT minus()
{ return CharT('-'); }
};
template <typename CharType>
struct DecimalFormat : public NumberFormat<CharType>
{
typedef CharType CharT;
static const unsigned base = 10;
static CharT toChar(unsigned char n)
{
return CharT(static_cast<char>('0' + n));
}
/** @brief Converts a character to a digit.
Returns a number equal or less than the base on success or a number
greater than base on failure.
*/
static unsigned char toDigit(CharT ch)
{
int cc = std::char_traits<CharT>::to_int_type(ch) - 48;
// let negatives overrun
return static_cast<unsigned>(cc);
}
};
template <typename CharType>
struct OctalFormat : public NumberFormat<CharType>
{
typedef CharType CharT;
static const unsigned base = 8;
static CharT toChar(unsigned char n)
{
return CharT(static_cast<char>('0' + n));
}
/** @brief Converts a character to a digit.
Returns a number equal or less than the base on success or a number
greater than base on failure.
*/
static unsigned char toDigit(CharT ch)
{
int cc = std::char_traits<CharT>::to_int_type(ch) - 48;
// let negatives overrun
return static_cast<unsigned>(cc);
}
};
template <typename CharType>
struct HexFormat : public NumberFormat<CharType>
{
typedef CharType CharT;
static const unsigned base = 16;
static CharT toChar(unsigned char n)
{
n &= 0x1F; // prevent overrun
static const char* digtab = "0123456789abcdef";
return digtab[n];
}
/** @brief Converts a character to a digit.
Returns a number equal or less than the base on success or a number
greater than base on failure.
*/
static unsigned char toDigit(CharT ch)
{
static const unsigned char chartab[64] =
{
0, 1, 2, 3, 4, 5, 6, 7, 8, 9,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
0xFF,10,11,12,13,14,15,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
0xFF,10,11,12,13,14,15,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
};
int cc = ch - 48;
// let negatives overrun
unsigned uc = static_cast<unsigned>(cc);
if(uc > 64)
return 0xFF;
unsigned char idx = static_cast<unsigned char>(uc);
return chartab[ idx ];
}
};
template <typename CharType>
struct BinaryFormat : public NumberFormat<CharType>
{
typedef CharType CharT;
static const unsigned base = 2;
static CharT toChar(unsigned char n)
{
return CharT(static_cast<char>('0' + n));
}
/** @brief Converts a character to a digit.
Returns a number equal or less than the base on success or a number
greater than base on failure.
*/
static unsigned char toDigit(CharT ch)
{
int cc = std::char_traits<CharT>::to_int_type(ch) - 48;
// let negatives overrun
return static_cast<unsigned>(cc);
}
};
template <typename CharType>
struct FloatFormat : public DecimalFormat<CharType>
{
typedef CharType CharT;
static CharT point()
{ return CharT('.'); }
static CharT e()
{ return CharT('e'); }
static CharT E()
{ return CharT('E'); }
static const CharT* nan()
{
static const CharT nanstr[] = { CharT('n'), CharT('a'), CharT('n'), 0 };
return nanstr;
}
static const CharT* inf()
{
static const CharT nanstr[] = { CharT('i'), CharT('n'), CharT('f'), 0 };
return nanstr;
}
};
//! @internal @brief Returns the absolute value of \a i
inline unsigned char formatAbs(char i, bool& isNeg)
{
isNeg = i < 0;
unsigned char u = isNeg ? -i : static_cast<unsigned char>(i);
return u;
}
//! @internal @brief Returns the absolute value of \a i
inline unsigned char formatAbs(unsigned char i, bool& isNeg)
{
isNeg = false;
return i;
}
//! @internal @brief Returns the absolute value of \a i
inline unsigned short formatAbs(short i, bool& isNeg)
{
isNeg = i < 0;
unsigned short u = isNeg ? -i : static_cast<unsigned short>(i);
return u;
}
//! @internal @brief Returns the absolute value of \a i
inline unsigned short formatAbs(unsigned short i, bool& isNeg)
{
isNeg = false;
return i;
}
//! @internal @brief Returns the absolute value of \a i
inline unsigned int formatAbs(int i, bool& isNeg)
{
isNeg = i < 0;
unsigned int u = isNeg ? -i : static_cast<unsigned int>(i);
return u;
}
//! @internal @brief Returns the absolute value of \a i
inline unsigned int formatAbs(unsigned int i, bool& isNeg)
{
isNeg = false;
return i;
}
//! @internal @brief Returns the absolute value of \a i
inline unsigned long formatAbs(long i, bool& isNeg)
{
isNeg = i < 0;
unsigned long u = isNeg ? -i : static_cast<unsigned long>(i);
return u;
}
//! @internal @brief Returns the absolute value of \a i
inline unsigned long formatAbs(unsigned long i, bool& isNeg)
{
isNeg = false;
return i;
}
#ifdef HAVE_LONG_LONG
//! @internal @brief Returns the absolute value of \a i
inline unsigned long long formatAbs(long long i, bool& isNeg)
{
isNeg = i < 0;
unsigned long long u = isNeg ? -i : static_cast<unsigned long long>(i);
return u;
}
#endif
#ifdef HAVE_UNSIGNEDLONG_LONG
//! @internal @brief Returns the absolute value of \a i
inline unsigned long long formatAbs(unsigned long long i, bool& isNeg)
{
isNeg = false;
return i;
}
#endif
/** @brief Formats an integer in a given format.
*/
template <typename CharT, typename T, typename FormatT>
inline CharT* formatInt(CharT* buf, std::size_t buflen, T si, const FormatT& fmt)
{
typedef typename IntTraits<T>::Unsigned UnsignedInt;
CharT* end = buf + buflen;
CharT* cur = end;
const unsigned base = fmt.base;
bool isNeg = false;
UnsignedInt u = formatAbs(si, isNeg);
do
{
T lsd = u % base;
u /= base;
--cur;
*cur = fmt.toChar( unsigned(lsd) );
}
while(u != 0 && cur != buf);
if(cur == buf)
return buf;
if(isNeg)
{
--cur;
*cur = fmt.minus();
}
return cur;
}
/** @brief Formats an integer in binary format.
*/
template <typename CharT, typename T>
inline CharT* formatInt(CharT* buf, std::size_t buflen, T i, const BinaryFormat<CharT>& fmt)
{
CharT* end = buf + buflen;
CharT* cur = end;
T mask = 1;
do
{
--cur;
*cur = fmt.toChar( unsigned(i & mask));
i = i >> 1;
}
while(i != 0 && cur != buf);
if(cur == buf)
return buf;
return cur;
}
template <typename OutIterT, typename T, typename FormatT>
inline OutIterT putInt(OutIterT it, T i, const FormatT& fmt)
{
// large enough even for binary and a sign
const std::size_t buflen = (sizeof(T) * 8) + 1;
typename FormatT::CharT buf[buflen];
typename FormatT::CharT* p = formatInt(buf, buflen, i, fmt);
typename FormatT::CharT* end = buf + buflen;
for(; p != end; ++p)
*it++ = *p;
return it;
}
template <typename OutIterT, typename T>
inline OutIterT putInt(OutIterT it, T i)
{
DecimalFormat<char> fmt;
return putInt(it, i, fmt);
}
template <typename OutIterT, typename T, typename FormatT>
inline OutIterT putFloat(OutIterT it, T d, const FormatT& fmt, int precision)
{
typedef typename FormatT::CharT CharT;
// 1. Test for not-a-number with d != d
if( d != d )
{
for(const CharT* nanstr = fmt.nan(); *nanstr != 0; ++nanstr)
{
*it = *nanstr;
++it;
}
return it;
}
// 2. check sign
if(d < 0.0)
{
*it = fmt.minus();
++it;
}
T num = std::fabs(d);
// 3. Test for infinity
if( num == std::numeric_limits<T>::infinity() )
{
for(const CharT* infstr = fmt.inf(); *infstr != 0; ++infstr)
{
*it = *infstr;
++it;
}
return it;
}
const int bufsize = std::numeric_limits<T>::digits10 + 1;
if (precision > bufsize)
precision = bufsize;
CharT fract[bufsize + 1];
fract[bufsize] = CharT('\0');
int exp = static_cast<int>(std::floor(std::log10(num))) + 1;
num *= std::pow(T(10.0), static_cast<int>(precision) - exp);
num += .5;
bool notZero = false;
for (unsigned short d = precision; d > 0; --d)
{
T n = num / 10.0;
T fl = std::floor(n) * 10.0;
unsigned char v = static_cast<unsigned char>(num - fl);
notZero |= (v != 0);
fract[d - 1] = notZero ? fmt.toChar(v) : CharT('\0');
num = n;
}
if (fract[0] == CharT('\0'))
{
*it = '0'; ++it;
return it;
}
if (exp <= 0)
{
*it = '0'; ++it;
*it = '.'; ++it;
while (exp < 0)
{
*it = '0'; ++it;
++exp;
}
for (int d = 0; fract[d]; ++d)
{
*it = fract[d]; ++it;
}
}
else
{
for (int d = 0; fract[d]; ++d)
{
if (exp-- == 0)
{
*it = '.';
++it;
}
*it = fract[d]; ++it;
}
while (exp-- > 0)
{
*it = '0'; ++it;
}
}
return it;
}
template <typename OutIterT, typename T>
inline OutIterT putFloat(OutIterT it, T d)
{
const int precision = std::numeric_limits<T>::digits10 + 1;
FloatFormat<char> fmt;
return putFloat(it, d, fmt, precision);
}
template <typename InIterT, typename FormatT>
InIterT getSign(InIterT it, InIterT end, bool& pos, const FormatT& fmt)
{
pos = true;
// strip leading whitespace, parse sign
while (it != end && isspace(*it))
++it;
if(*it == fmt.minus())
{
pos = false;
++it;
}
else if( *it == fmt.plus() )
{
++it;
}
return it;
}
template <typename InIterT, typename T, typename FormatT>
InIterT getInt(InIterT it, InIterT end, bool& ok, T& n, const FormatT& fmt)
{
typedef typename IntTraits<T>::Unsigned UnsignedInt;
typedef typename IntTraits<T>::Signed SignedInt;
n = 0;
ok = false;
UnsignedInt max = std::numeric_limits<T>::max();
bool pos = false;
it = getSign(it, end, pos, fmt);
if (it == end)
return it;
bool isNeg = ! pos;
if( isNeg )
{
// return if minus sign was parsed for unsigned type
if( isNeg != std::numeric_limits<T>::is_signed)
return it;
// abs(min) is max for negative signed types
SignedInt smin = std::numeric_limits<T>::min();
max = static_cast<UnsignedInt>(-smin);
}
// parse number
UnsignedInt u = 0;
const UnsignedInt base = fmt.base;
unsigned char d = 0;
while(it != end)
{
d = fmt.toDigit(*it);
if(d >= base)
break;
if ( u != 0u && base > (max/u) )
return it;
u *= base;
if(d > max - u)
return it;
u += d;
++it;
}
if( isNeg )
n = static_cast<T>(u * -1);
else
n = static_cast<T>(u);
ok = true;
return it;
}
template <typename InIterT, typename T>
InIterT getInt(InIterT it, InIterT end, bool& ok, T& n)
{
return getInt(it, end, ok, n, DecimalFormat<char>() );
}
template <typename InIterT, typename T, typename FormatT>
InIterT getFloat(InIterT it, InIterT end, bool& ok, T& n, const FormatT& fmt)
{
typedef typename FormatT::CharT CharT;
CharT zero = fmt.toChar(0);
n = 0.0;
ok = false;
bool pos = false;
it = getSign(it, end, pos, fmt);
if (it == end)
return it;
// NaN, -inf, +inf
bool done = false;
while(it != end)
{
switch(std::char_traits<CharT>::to_int_type(*it))
{
case 'n':
case 'N':
if(++it == end)
return it;
if(*it != CharT('a') && *it != CharT('A'))
return it;
if(++it == end)
return it;
if(*it != CharT('n') && *it != CharT('N'))
return it;
// NaNQ, NaNS (seen on AIX/xlC)
{
InIterT nit = it;
++nit;
if (*nit == CharT('q') || *nit == CharT('Q'))
{
n = std::numeric_limits<T>::quiet_NaN();
++it;
}
else if (*nit == CharT('s') || *nit == CharT('S'))
{
n = std::numeric_limits<T>::signaling_NaN();
++it;
}
else
{
n = std::numeric_limits<T>::quiet_NaN();
}
}
ok = true;
return ++it;
break;
case 'i':
case 'I':
if(++it == end)
return it;
if(*it != CharT('n') && *it != CharT('N'))
return it;
if(++it == end)
return it;
if(*it != CharT('f') && *it != CharT('F'))
return it;
if( ++it != end )
{
if(*it != CharT('i') && *it != CharT('I'))
return it;
if(++it == end)
return it;
if(*it != CharT('n') && *it != CharT('N'))
return it;
if(++it == end)
return it;
if(*it != CharT('i') && *it != CharT('I'))
return it;
if(++it == end)
return it;
if(*it != CharT('t') && *it != CharT('T'))
return it;
if(++it == end)
return it;
if(*it != CharT('y') && *it != CharT('Y'))
return it;
++it;
}
n = std::numeric_limits<T>::infinity();
if(!pos)
n *= -1;
ok = true;
return it;
break;
default:
done = true;
break;
}
if(done)
break;
++it;
}
// integral part
bool withFractional = false;
for( ; it != end; ++it)
{
if( *it == fmt.point() || *it == fmt.e() || *it == fmt.E() )
{
if( *it == fmt.point())
{
withFractional = true;
++it;
}
break;
}
unsigned digit = fmt.toDigit(*it);
if(digit >= fmt.base)
return it;
n *= 10;
n += digit;
}
// it is ok, if fraction is missing
if(it == end)
{
if( ! pos )
n *= -1;
ok = true;
return it;
}
T base = 10.0;
if( withFractional)
{
// fractional part, ignore 0 digits after dot
unsigned short fractDigits = 0;
size_t maxDigits = std::numeric_limits<unsigned short>::max() - std::numeric_limits<T>::digits10;
while(it != end && *it == zero)
{
if( fractDigits > maxDigits )
return it;
++fractDigits;
++it;
}
// fractional part, parse like integer, skip insignificant digits
unsigned short significants = 0;
T fraction = 0.0;
for( ; it != end; ++it)
{
unsigned digit = fmt.toDigit(*it);
if(digit >= fmt.base)
break;
if( significants <= std::numeric_limits<T>::digits10 )
{
fraction *= 10;
fraction += digit;
++fractDigits;
++significants;
}
}
// fractional part, scale down
fraction /= std::pow(base, T(fractDigits));
n += fraction;
}
// exponent [e|E][+|-][0-9]*
if(it != end && (*it == fmt.e() || *it == fmt.E()) )
{
if(++it == end)
return it;
long exp = 0;
it = getInt(it, end, ok, exp, fmt);
if( ! ok )
return it;
n *= std::pow(base, T(exp));
}
if( ! pos )
n *= -1;
ok = true;
return it;
}
template <typename InIterT, typename T>
InIterT getFloat(InIterT it, InIterT end, bool& ok, T& n)
{
return getFloat( it, end, ok, n, FloatFormat<char>() );
}
} // namespace cxxtools
#endif
|