forked from CleverRaven/Cataclysm-DDA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
try_parse_integer.cpp
45 lines (41 loc) · 1.62 KB
/
try_parse_integer.cpp
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
#include "try_parse_integer.h"
#include <sstream>
#include "translations.h"
template<typename T>
ret_val<T> try_parse_integer( const std::string &s, bool use_locale )
{
// Using stringstream-based parsing because that's the only one in the
// standard library for which it's possible to turn off the
// locale-dependency. Once we're using C++17 we could use a combination of
// std::from_chars and std::strto* functions, but this should be fine so
// long as the code is not performance-critical.
std::istringstream buffer( s );
#ifdef __APPLE__
// On Apple platforms we always use the classic locale, because the other
// locales seem to behave strangely. See
// https://github.com/CleverRaven/Cataclysm-DDA/pull/48431 for more
// discussion.
static_cast<void>( use_locale );
buffer.imbue( std::locale::classic() );
#else
if( !use_locale ) {
buffer.imbue( std::locale::classic() );
}
#endif
T result;
buffer >> result;
if( !buffer ) {
return ret_val<T>::make_failure(
0, string_format( _( "Could not convert '%s' to an integer" ), s ) );
}
char c;
buffer >> c;
if( buffer ) {
return ret_val<T>::make_failure(
0, string_format( _( "Stray characters after integer in '%s'" ), s ) );
}
return ret_val<T>::make_success( result );
}
template ret_val<int> try_parse_integer<int>( const std::string &, bool use_locale );
template ret_val<long> try_parse_integer<long>( const std::string &, bool use_locale );
template ret_val<long long> try_parse_integer<long long>( const std::string &, bool use_locale );