Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:52:46

0001 #ifndef BOOST_THREAD_DETAIL_STRING_TO_UNSIGNED_HPP_INCLUDED
0002 #define BOOST_THREAD_DETAIL_STRING_TO_UNSIGNED_HPP_INCLUDED
0003 
0004 // Copyright 2023 Peter Dimov
0005 // Distributed under the Boost Software License, Version 1.0.
0006 // https://www.boost.org/LICENSE_1_0.txt
0007 
0008 #include <string>
0009 #include <climits>
0010 
0011 namespace boost
0012 {
0013 namespace thread_detail
0014 {
0015 
0016 inline bool string_to_unsigned( std::string const& s, unsigned& v )
0017 {
0018     v = 0;
0019 
0020     if( s.empty() )
0021     {
0022         return false;
0023     }
0024 
0025     for( char const* p = s.c_str(); *p; ++p )
0026     {
0027         unsigned char ch = static_cast<unsigned char>( *p );
0028 
0029         if( ch < '0' || ch > '9' )
0030         {
0031             return false;
0032         }
0033 
0034         if( v > UINT_MAX / 10 )
0035         {
0036             return false;
0037         }
0038 
0039         unsigned q = static_cast<unsigned>( ch - '0' );
0040 
0041         if( v == UINT_MAX / 10 && q > UINT_MAX % 10 )
0042         {
0043             return false;
0044         }
0045 
0046         v = v * 10 + q;
0047     }
0048 
0049     return true;
0050 }
0051 
0052 } // namespace thread_detail
0053 } // namespace boost
0054 
0055 #endif // #ifndef BOOST_THREAD_DETAIL_STRING_TO_UNSIGNED_HPP_INCLUDED