Revert "Refactoring; Reuse function in simplecpp"

This reverts commit dbe9eb2a27.
This commit is contained in:
Daniel Marjamäki 2021-05-01 19:54:14 +02:00
parent e1dd04c536
commit 6f60c6d965
3 changed files with 69 additions and 4 deletions

View File

@ -21,8 +21,6 @@
#include "errortypes.h"
#include "utils.h"
#include <simplecpp.h>
#include <cctype>
#include <cmath>
#include <cstdlib>
@ -357,6 +355,35 @@ 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;
@ -506,7 +533,7 @@ MathLib::bigint MathLib::toLongNumber(const std::string & str)
}
if (isCharLiteral(str)) {
return simplecpp::characterLiteralToLL(str);
return characterLiteralToLongNumber(getCharLiteral(str));
}
try {
@ -570,7 +597,7 @@ static double floatHexToDoubleNumber(const std::string& str)
double MathLib::toDoubleNumber(const std::string &str)
{
if (isCharLiteral(str))
return simplecpp::characterLiteralToLL(str);
return characterLiteralToLongNumber(getCharLiteral(str));
if (isIntHex(str))
return static_cast<double>(toLongNumber(str));
// nullcheck

View File

@ -127,6 +127,12 @@ 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

View File

@ -304,6 +304,38 @@ 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"));