From dbe9eb2a27b1e64558a42c36f1461872d8cc1d05 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sat, 1 May 2021 18:13:40 +0200 Subject: [PATCH] Refactoring; Reuse function in simplecpp --- lib/mathlib.cpp | 35 ++++------------------------------- lib/mathlib.h | 6 ------ test/testmathlib.cpp | 32 -------------------------------- 3 files changed, 4 insertions(+), 69 deletions(-) diff --git a/lib/mathlib.cpp b/lib/mathlib.cpp index 8f04f8b8b..2fe7699c2 100644 --- a/lib/mathlib.cpp +++ b/lib/mathlib.cpp @@ -21,6 +21,8 @@ #include "errortypes.h" #include "utils.h" +#include + #include #include #include @@ -355,35 +357,6 @@ unsigned int MathLib::encodeMultiChar(const std::string& str) return retval; } -static bool isoctal(int c) -{ - return c>='0' && c<='7'; -} - -MathLib::bigint MathLib::characterLiteralToLongNumber(const std::string& str) -{ - if (str.empty()) - return 0; // <- only possible in unit testing - - // '\xF6' - if (str.size() == 4 && str.compare(0,2,"\\x")==0 && std::isxdigit(str[2]) && std::isxdigit(str[3])) { - return std::strtoul(str.substr(2).c_str(), nullptr, 16); - } - - // '\123' - if (str.size() == 4 && str[0] == '\\' && isoctal(str[1]) && isoctal(str[2]) && isoctal(str[3])) { - return (char)std::strtoul(str.substr(1).c_str(), nullptr, 8); - } - - // C99 6.4.4.4 - // The value of an integer character constant containing more than one character (e.g., 'ab'), - // or containing a character or escape sequence that does not map to a single-byte execution character, - // is implementation-defined. - // clang and gcc seem to use the following encoding: 'AB' as (('A' << 8) | 'B') - const std::string& normStr = normalizeCharacterLiteral(str); - return encodeMultiChar(normStr); -} - std::string MathLib::normalizeCharacterLiteral(const std::string& iLiteral) { std::string normalizedLiteral; @@ -533,7 +506,7 @@ MathLib::bigint MathLib::toLongNumber(const std::string & str) } if (isCharLiteral(str)) { - return characterLiteralToLongNumber(getCharLiteral(str)); + return simplecpp::characterLiteralToLL(str); } try { @@ -597,7 +570,7 @@ static double floatHexToDoubleNumber(const std::string& str) double MathLib::toDoubleNumber(const std::string &str) { if (isCharLiteral(str)) - return characterLiteralToLongNumber(getCharLiteral(str)); + return simplecpp::characterLiteralToLL(str); if (isIntHex(str)) return static_cast(toLongNumber(str)); // nullcheck diff --git a/lib/mathlib.h b/lib/mathlib.h index dfc4c6587..e37ac3819 100644 --- a/lib/mathlib.h +++ b/lib/mathlib.h @@ -127,12 +127,6 @@ public: static unsigned int encodeMultiChar(const std::string& str); - /** - * \param[in] str character literal - * @return Number of internal representation of the character literal - * */ - static MathLib::bigint characterLiteralToLongNumber(const std::string& str); - /** * \param[in] iCode Code being considered * \param[in] iPos A posision within iCode diff --git a/test/testmathlib.cpp b/test/testmathlib.cpp index f3079533a..34cc02679 100644 --- a/test/testmathlib.cpp +++ b/test/testmathlib.cpp @@ -304,38 +304,6 @@ private: ASSERT_EQUALS((int)('\100'), MathLib::toLongNumber("'\\100'")); ASSERT_EQUALS((int)('\200'), MathLib::toLongNumber("'\\200'")); ASSERT_EQUALS((int)(L'A'), MathLib::toLongNumber("L'A'")); -#ifdef __GNUC__ - // BEGIN Implementation-specific results - ASSERT_EQUALS((int)('AB'), MathLib::toLongNumber("'AB'")); - ASSERT_EQUALS((int)('ABC'), MathLib::toLongNumber("'ABC'")); - ASSERT_EQUALS((int)('ABCD'), MathLib::toLongNumber("'ABCD'")); - ASSERT_EQUALS((int)('ABCDE'), MathLib::toLongNumber("'ABCDE'")); - // END Implementation-specific results -#endif - ASSERT_EQUALS((int)('\0'), MathLib::toLongNumber("'\\0'")); - ASSERT_EQUALS(0x1B, MathLib::toLongNumber("'\\e'")); - ASSERT_EQUALS((int)('\r'), MathLib::toLongNumber("'\\r'")); - ASSERT_EQUALS((int)('\x12'), MathLib::toLongNumber("'\\x12'")); - // may cause some compile problems: ASSERT_EQUALS((int)('\x123'), MathLib::toLongNumber("'\\x123'")); - // may cause some compile problems: ASSERT_EQUALS((int)('\x1234'), MathLib::toLongNumber("'\\x1234'")); - ASSERT_EQUALS((int)('\3'), MathLib::toLongNumber("'\\3'")); - ASSERT_EQUALS((int)('\34'), MathLib::toLongNumber("'\\34'")); - ASSERT_EQUALS((int)('\034'), MathLib::toLongNumber("'\\034'")); - ASSERT_EQUALS((int)('\x34'), MathLib::toLongNumber("'\\x34'")); - ASSERT_EQUALS((int)('\134'), MathLib::toLongNumber("'\\134'")); - ASSERT_EQUALS((int)('\134t'), MathLib::toLongNumber("'\\134t'")); // Ticket #7452 - ASSERT_THROW(MathLib::toLongNumber("'\\9'"), InternalError); - ASSERT_THROW(MathLib::toLongNumber("'\\934'"), InternalError); - // that is not gcc/clang encoding - ASSERT_EQUALS(959657011, MathLib::toLongNumber("'\\u9343'")); - ASSERT_EQUALS(1714631779, MathLib::toLongNumber("'\\U0001f34c'")); - { - // some unit-testing for a utility function - ASSERT_EQUALS(0, MathLib::characterLiteralToLongNumber(std::string())); - ASSERT_EQUALS(32, MathLib::characterLiteralToLongNumber(std::string(" "))); - ASSERT_EQUALS(538976288, MathLib::characterLiteralToLongNumber(std::string(" "))); - ASSERT_THROW(MathLib::characterLiteralToLongNumber(std::string("\\u")), InternalError); - } ASSERT_EQUALS(-8552249625308161526, MathLib::toLongNumber("0x89504e470d0a1a0a")); ASSERT_EQUALS(-8481036456200365558, MathLib::toLongNumber("0x8a4d4e470d0a1a0a"));