Refactoring; Reuse function in simplecpp

This commit is contained in:
Daniel Marjamäki 2021-05-01 18:13:40 +02:00
parent 636387b31c
commit dbe9eb2a27
3 changed files with 4 additions and 69 deletions

View File

@ -21,6 +21,8 @@
#include "errortypes.h"
#include "utils.h"
#include <simplecpp.h>
#include <cctype>
#include <cmath>
#include <cstdlib>
@ -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<double>(toLongNumber(str));
// nullcheck

View File

@ -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

View File

@ -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"));