lexical-cast mereged from trunk r72347 (allow "C" locale grouping for other locales)

[SVN r72348]
This commit is contained in:
Antony Polukhin
2011-06-02 16:20:36 +00:00
parent 1f7147d24b
commit 6c5f31e7a5
3 changed files with 28 additions and 5 deletions

View File

@@ -654,6 +654,7 @@ namespace boost
unsigned char current_grouping = 0; unsigned char current_grouping = 0;
CharT const thousands_sep = np.thousands_sep(); CharT const thousands_sep = np.thousands_sep();
char remained = grouping[current_grouping] - 1; char remained = grouping[current_grouping] - 1;
bool shall_we_return = true;
for(;end>=begin; --end) for(;end>=begin; --end)
{ {
@@ -671,12 +672,31 @@ namespace boost
multiplier *= 10; multiplier *= 10;
--remained; --remained;
} else { } else {
if ( !Traits::eq(*end, thousands_sep) || begin == end ) return false; if ( !Traits::eq(*end, thousands_sep) ) //|| begin == end ) return false;
if (current_grouping < grouping_size-1 ) ++current_grouping; {
remained = grouping[current_grouping]; /*
* According to Programming languages - C++
* Digit grouping is checked. That is, the positions of discarded
* separators is examined for consistency with
* use_facet<numpunct<charT> >(loc ).grouping()
*
* BUT what if there is no separators at all and grouping()
* is not empty? Well, we have no extraced separators, so we
* won`t check them for consistency. This will allow us to
* work with "C" locale from other locales
*/
shall_we_return = false;
break;
} else {
if ( begin == end ) return false;
if (current_grouping < grouping_size-1 ) ++current_grouping;
remained = grouping[current_grouping];
}
} }
} }
} else
if (shall_we_return) return true;
}
#endif #endif
{ {
while ( begin <= end ) while ( begin <= end )

View File

@@ -677,6 +677,9 @@ void test_conversion_from_to_integral_for_locale()
, bad_lexical_cast); , bad_lexical_cast);
BOOST_CHECK_THROW(lexical_cast<T>( std::string("100") + np.thousands_sep() ), bad_lexical_cast); BOOST_CHECK_THROW(lexical_cast<T>( std::string("100") + np.thousands_sep() ), bad_lexical_cast);
BOOST_CHECK_THROW(lexical_cast<T>( np.thousands_sep() + std::string("100") ), bad_lexical_cast); BOOST_CHECK_THROW(lexical_cast<T>( np.thousands_sep() + std::string("100") ), bad_lexical_cast);
// Exception must not be thrown, when we are using no separators at all
BOOST_CHECK( lexical_cast<T>("10000") == static_cast<T>(10000) );
} }
test_conversion_from_integral_to_integral<T>(); test_conversion_from_integral_to_integral<T>();

View File

@@ -51,7 +51,7 @@ std::ostream &operator<<(std::ostream &O, const A &a)
{ {
a.out(O); a.out(O);
return O; return O;
}; }
void test_abstract() void test_abstract()
{ {