MCPcopy Create free account
hub / github.com/BYVoid/OpenCC / ParseInt32

Function ParseInt32

deps/google-benchmark/src/commandlineflags.cc:34–64  ·  view source on GitHub ↗

Parses 'str' for a 32-bit signed integer. If successful, writes the result to *value and returns true; otherwise leaves *value unchanged and returns false.

Source from the content-addressed store, hash-verified

32// the result to *value and returns true; otherwise leaves *value
33// unchanged and returns false.
34bool ParseInt32(const std::string& src_text, const char* str, int32_t* value) {
35 // Parses the environment variable as a decimal integer.
36 char* end = nullptr;
37 const long long_value = strtol(str, &end, 10); // NOLINT
38
39 // Has strtol() consumed all characters in the string?
40 if (*end != '\0') {
41 // No - an invalid character was encountered.
42 std::cerr << src_text << " is expected to be a 32-bit integer, "
43 << "but actually has value \"" << str << "\".\n";
44 return false;
45 }
46
47 // Is the parsed value in the range of an Int32?
48 const int32_t result = static_cast<int32_t>(long_value);
49 if (long_value == std::numeric_limits<long>::max() ||
50 long_value == std::numeric_limits<long>::min() ||
51 // The parsed value overflows as a long. (strtol() returns
52 // LONG_MAX or LONG_MIN when the input overflows.)
53 result != long_value
54 // The parsed value overflows as an Int32.
55 ) {
56 std::cerr << src_text << " is expected to be a 32-bit integer, "
57 << "but actually has value \"" << str << "\", "
58 << "which overflows.\n";
59 return false;
60 }
61
62 *value = result;
63 return true;
64}
65
66// Parses 'str' for a double. If successful, writes the result to *value and
67// returns true; otherwise leaves *value unchanged and returns false.

Callers 2

Int32FromEnvFunction · 0.70
ParseInt32FlagFunction · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected