MCPcopy Create free account
hub / github.com/argotorg/solidity / isValidLiteral

Method isValidLiteral

libsolidity/ast/Types.cpp:909–1008  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

907}
908
909std::tuple<bool, rational> RationalNumberType::isValidLiteral(Literal const& _literal)
910{
911 rational value;
912 try
913 {
914 ASTString valueString = _literal.valueWithoutUnderscores();
915
916 auto expPoint = find(valueString.begin(), valueString.end(), 'e');
917 if (expPoint == valueString.end())
918 expPoint = find(valueString.begin(), valueString.end(), 'E');
919
920 if (boost::starts_with(valueString, "0x"))
921 {
922 // process as hex
923 value = bigint(valueString);
924 }
925 else if (expPoint != valueString.end())
926 {
927 // Parse mantissa and exponent. Checks numeric limit.
928 std::tuple<bool, rational> mantissa = parseRational(std::string(valueString.begin(), expPoint));
929
930 if (!std::get<0>(mantissa))
931 return std::make_tuple(false, rational(0));
932 value = std::get<1>(mantissa);
933
934 // 0E... is always zero.
935 if (value == 0)
936 return std::make_tuple(true, rational(0));
937
938 bigint exp = bigint(std::string(expPoint + 1, valueString.end()));
939
940 if (exp > std::numeric_limits<int32_t>::max() || exp < std::numeric_limits<int32_t>::min())
941 return std::make_tuple(false, rational(0));
942
943 uint32_t expAbs = bigint(abs(exp)).convert_to<uint32_t>();
944
945 if (exp < 0)
946 {
947 if (!fitsPrecisionBase10(abs(value.denominator()), expAbs))
948 return std::make_tuple(false, rational(0));
949 value /= boost::multiprecision::pow(
950 bigint(10),
951 expAbs
952 );
953 }
954 else if (exp > 0)
955 {
956 if (!fitsPrecisionBase10(abs(value.numerator()), expAbs))
957 return std::make_tuple(false, rational(0));
958 value *= boost::multiprecision::pow(
959 bigint(10),
960 expAbs
961 );
962 }
963 }
964 else
965 {
966 // parse as rational number

Callers

nothing calls this directly

Calls 7

parseRationalFunction · 0.85
absFunction · 0.85
subDenominationMethod · 0.80
fitsPrecisionBase10Function · 0.70
beginMethod · 0.45
endMethod · 0.45

Tested by

no test coverage detected