MCPcopy Create free account
hub / github.com/WasmEdge/WasmEdge / stringToInteger

Function stringToInteger

include/po/parser.h:28–59  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

26
27template <typename ConvResultT, typename ResultT = ConvResultT>
28inline cxx20::expected<ResultT, Error>
29stringToInteger(ConvResultT (&Conv)(const char *, char **, int),
30 std::string Value) noexcept {
31 using namespace std::literals;
32 char *EndPtr;
33 const char *CStr = Value.c_str();
34 auto SavedErrNo = std::exchange(errno, 0);
35 const auto Result = Conv(CStr, &EndPtr, 10);
36 std::swap(SavedErrNo, errno);
37 if (Value.empty() || *EndPtr != '\0') {
38 return cxx20::unexpected<Error>(
39 std::in_place, ErrCode::InvalidArgument,
40 fmt::format("invalid integer value: {}", Value));
41 }
42 auto InsideRange = [](auto WiderResult) constexpr noexcept {
43 using WiderResultT = decltype(WiderResult);
44 if constexpr (std::is_same_v<ResultT, WiderResultT>) {
45 return true;
46 } else {
47 return static_cast<WiderResultT>(std::numeric_limits<ResultT>::min()) <=
48 WiderResult &&
49 WiderResult <=
50 static_cast<WiderResultT>(std::numeric_limits<ResultT>::max());
51 }
52 };
53 if (SavedErrNo == ERANGE || !InsideRange(Result)) {
54 return cxx20::unexpected<Error>(
55 std::in_place, ErrCode::OutOfRange,
56 fmt::format("integer value out of range: {}", Value));
57 }
58 return static_cast<ResultT>(Result);
59}
60
61template <typename ConvResultT, typename ResultT = ConvResultT>
62inline cxx20::expected<ResultT, Error>

Callers 4

parseMethod · 0.85
parseMethod · 0.85
parseMethod · 0.85
parseMethod · 0.85

Calls 3

swapFunction · 0.85
c_strMethod · 0.80
emptyMethod · 0.45

Tested by

no test coverage detected