From cd0167d6b8bc3cadab3b674e7f5304d8322c77d8 Mon Sep 17 00:00:00 2001 From: Antony Polukhin Date: Tue, 31 May 2011 20:46:37 +0000 Subject: [PATCH] Fixes #5585 and adds test on it [SVN r72315] --- include/boost/lexical_cast.hpp | 28 ++++++++++++++++++++++++---- lexical_cast_test.cpp | 3 +++ 2 files changed, 27 insertions(+), 4 deletions(-) diff --git a/include/boost/lexical_cast.hpp b/include/boost/lexical_cast.hpp index c1b083b..ddd7398 100644 --- a/include/boost/lexical_cast.hpp +++ b/include/boost/lexical_cast.hpp @@ -654,6 +654,7 @@ namespace boost unsigned char current_grouping = 0; CharT const thousands_sep = np.thousands_sep(); char remained = grouping[current_grouping] - 1; + bool shall_we_return = true; for(;end>=begin; --end) { @@ -671,12 +672,31 @@ namespace boost multiplier *= 10; --remained; } else { - if ( !Traits::eq(*end, thousands_sep) || begin == end ) return false; - if (current_grouping < grouping_size-1 ) ++current_grouping; - remained = grouping[current_grouping]; + if ( !Traits::eq(*end, thousands_sep) ) //|| begin == end ) return false; + { + /* + * According to Programming languages - C++ + * Digit grouping is checked. That is, the positions of discarded + * separators is examined for consistency with + * use_facet >(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 { while ( begin <= end ) diff --git a/lexical_cast_test.cpp b/lexical_cast_test.cpp index f88017f..f20994e 100644 --- a/lexical_cast_test.cpp +++ b/lexical_cast_test.cpp @@ -677,6 +677,9 @@ void test_conversion_from_to_integral_for_locale() , bad_lexical_cast); BOOST_CHECK_THROW(lexical_cast( std::string("100") + np.thousands_sep() ), bad_lexical_cast); BOOST_CHECK_THROW(lexical_cast( 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("10000") == static_cast(10000) ); } test_conversion_from_integral_to_integral();