mirror of
https://github.com/boostorg/thread.git
synced 2026-01-19 04:42:13 +00:00
Add boost/thread/detail/string_to_unsigned.hpp
This commit is contained in:
55
include/boost/thread/detail/string_to_unsigned.hpp
Normal file
55
include/boost/thread/detail/string_to_unsigned.hpp
Normal file
@@ -0,0 +1,55 @@
|
||||
#ifndef BOOST_THREAD_DETAIL_STRING_TO_UNSIGNED_HPP_INCLUDED
|
||||
#define BOOST_THREAD_DETAIL_STRING_TO_UNSIGNED_HPP_INCLUDED
|
||||
|
||||
// Copyright 2023 Peter Dimov
|
||||
// Distributed under the Boost Software License, Version 1.0.
|
||||
// https://www.boost.org/LICENSE_1_0.txt
|
||||
|
||||
#include <string>
|
||||
#include <climits>
|
||||
|
||||
namespace boost
|
||||
{
|
||||
namespace thread_detail
|
||||
{
|
||||
|
||||
inline bool string_to_unsigned( std::string const& s, unsigned& v )
|
||||
{
|
||||
v = 0;
|
||||
|
||||
if( s.empty() )
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
for( char const* p = s.c_str(); *p; ++p )
|
||||
{
|
||||
unsigned char ch = static_cast<unsigned char>( *p );
|
||||
|
||||
if( ch < '0' || ch > '9' )
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if( v > UINT_MAX / 10 )
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
unsigned q = static_cast<unsigned>( ch - '0' );
|
||||
|
||||
if( v == UINT_MAX / 10 && q > UINT_MAX % 10 )
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
v = v * 10 + q;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
} // namespace thread_detail
|
||||
} // namespace boost
|
||||
|
||||
#endif // #ifndef BOOST_THREAD_DETAIL_STRING_TO_UNSIGNED_HPP_INCLUDED
|
||||
@@ -1103,5 +1103,6 @@ rule generate_self_contained_header_tests
|
||||
test-suite detail
|
||||
:
|
||||
[ run detail/string_trim_test.cpp ]
|
||||
[ run detail/string_to_unsigned_test.cpp ]
|
||||
;
|
||||
}
|
||||
|
||||
55
test/detail/string_to_unsigned_test.cpp
Normal file
55
test/detail/string_to_unsigned_test.cpp
Normal file
@@ -0,0 +1,55 @@
|
||||
// Copyright 2023 Peter Dimov
|
||||
// Distributed under the Boost Software License, Version 1.0.
|
||||
// https://www.boost.org/LICENSE_1_0.txt
|
||||
|
||||
#include <boost/thread/detail/string_to_unsigned.hpp>
|
||||
#include <boost/core/lightweight_test.hpp>
|
||||
|
||||
int main()
|
||||
{
|
||||
using boost::thread_detail::string_to_unsigned;
|
||||
|
||||
unsigned v;
|
||||
|
||||
BOOST_TEST_NOT( string_to_unsigned( "", v ) ) && BOOST_TEST_EQ( v, 0 );
|
||||
BOOST_TEST_NOT( string_to_unsigned( " ", v ) ) && BOOST_TEST_EQ( v, 0 );
|
||||
BOOST_TEST_NOT( string_to_unsigned( "+1", v ) ) && BOOST_TEST_EQ( v, 0 );
|
||||
BOOST_TEST_NOT( string_to_unsigned( "-1", v ) ) && BOOST_TEST_EQ( v, 0 );
|
||||
BOOST_TEST_NOT( string_to_unsigned( "abc", v ) ) && BOOST_TEST_EQ( v, 0 );
|
||||
|
||||
BOOST_TEST( string_to_unsigned( "0", v ) ) && BOOST_TEST_EQ( v, 0 );
|
||||
BOOST_TEST( string_to_unsigned( "1", v ) ) && BOOST_TEST_EQ( v, 1 );
|
||||
BOOST_TEST( string_to_unsigned( "12", v ) ) && BOOST_TEST_EQ( v, 12 );
|
||||
BOOST_TEST( string_to_unsigned( "123", v ) ) && BOOST_TEST_EQ( v, 123 );
|
||||
BOOST_TEST( string_to_unsigned( "1234", v ) ) && BOOST_TEST_EQ( v, 1234 );
|
||||
BOOST_TEST( string_to_unsigned( "12345", v ) ) && BOOST_TEST_EQ( v, 12345 );
|
||||
BOOST_TEST( string_to_unsigned( "123456", v ) ) && BOOST_TEST_EQ( v, 123456 );
|
||||
BOOST_TEST( string_to_unsigned( "1234567", v ) ) && BOOST_TEST_EQ( v, 1234567 );
|
||||
BOOST_TEST( string_to_unsigned( "12345678", v ) ) && BOOST_TEST_EQ( v, 12345678 );
|
||||
BOOST_TEST( string_to_unsigned( "123456789", v ) ) && BOOST_TEST_EQ( v, 123456789 );
|
||||
BOOST_TEST( string_to_unsigned( "1234567890", v ) ) && BOOST_TEST_EQ( v, 1234567890 );
|
||||
BOOST_TEST_NOT( string_to_unsigned( "12345678901", v ) ) && BOOST_TEST_EQ( v, 1234567890 );
|
||||
BOOST_TEST_NOT( string_to_unsigned( "123456789012", v ) ) && BOOST_TEST_EQ( v, 1234567890 );
|
||||
|
||||
BOOST_TEST( string_to_unsigned( "4294967295", v ) ) && BOOST_TEST_EQ( v, 4294967295 );
|
||||
BOOST_TEST_NOT( string_to_unsigned( "4294967296", v ) ) && BOOST_TEST_EQ( v, 429496729 );
|
||||
|
||||
BOOST_TEST( string_to_unsigned( "01", v ) ) && BOOST_TEST_EQ( v, 1 );
|
||||
BOOST_TEST( string_to_unsigned( "001", v ) ) && BOOST_TEST_EQ( v, 1 );
|
||||
BOOST_TEST( string_to_unsigned( "0001", v ) ) && BOOST_TEST_EQ( v, 1 );
|
||||
BOOST_TEST( string_to_unsigned( "00001", v ) ) && BOOST_TEST_EQ( v, 1 );
|
||||
BOOST_TEST( string_to_unsigned( "000001", v ) ) && BOOST_TEST_EQ( v, 1 );
|
||||
BOOST_TEST( string_to_unsigned( "0000001", v ) ) && BOOST_TEST_EQ( v, 1 );
|
||||
BOOST_TEST( string_to_unsigned( "00000001", v ) ) && BOOST_TEST_EQ( v, 1 );
|
||||
BOOST_TEST( string_to_unsigned( "000000001", v ) ) && BOOST_TEST_EQ( v, 1 );
|
||||
BOOST_TEST( string_to_unsigned( "0000000001", v ) ) && BOOST_TEST_EQ( v, 1 );
|
||||
BOOST_TEST( string_to_unsigned( "00000000001", v ) ) && BOOST_TEST_EQ( v, 1 );
|
||||
BOOST_TEST( string_to_unsigned( "000000000001", v ) ) && BOOST_TEST_EQ( v, 1 );
|
||||
BOOST_TEST( string_to_unsigned( "0000000000001", v ) ) && BOOST_TEST_EQ( v, 1 );
|
||||
BOOST_TEST( string_to_unsigned( "00000000000001", v ) ) && BOOST_TEST_EQ( v, 1 );
|
||||
|
||||
BOOST_TEST_NOT( string_to_unsigned( "1a", v ) ) && BOOST_TEST_EQ( v, 1 );
|
||||
BOOST_TEST_NOT( string_to_unsigned( "2 ", v ) ) && BOOST_TEST_EQ( v, 2 );
|
||||
|
||||
return boost::report_errors();
|
||||
}
|
||||
Reference in New Issue
Block a user