Skip to content

Commit dbe9eb2

Browse files
committed
Refactoring; Reuse function in simplecpp
1 parent 636387b commit dbe9eb2

3 files changed

Lines changed: 4 additions & 69 deletions

File tree

lib/mathlib.cpp

Lines changed: 4 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
#include "errortypes.h"
2222
#include "utils.h"
2323

24+
#include <simplecpp.h>
25+
2426
#include <cctype>
2527
#include <cmath>
2628
#include <cstdlib>
@@ -355,35 +357,6 @@ unsigned int MathLib::encodeMultiChar(const std::string& str)
355357
return retval;
356358
}
357359

358-
static bool isoctal(int c)
359-
{
360-
return c>='0' && c<='7';
361-
}
362-
363-
MathLib::bigint MathLib::characterLiteralToLongNumber(const std::string& str)
364-
{
365-
if (str.empty())
366-
return 0; // <- only possible in unit testing
367-
368-
// '\xF6'
369-
if (str.size() == 4 && str.compare(0,2,"\\x")==0 && std::isxdigit(str[2]) && std::isxdigit(str[3])) {
370-
return std::strtoul(str.substr(2).c_str(), nullptr, 16);
371-
}
372-
373-
// '\123'
374-
if (str.size() == 4 && str[0] == '\\' && isoctal(str[1]) && isoctal(str[2]) && isoctal(str[3])) {
375-
return (char)std::strtoul(str.substr(1).c_str(), nullptr, 8);
376-
}
377-
378-
// C99 6.4.4.4
379-
// The value of an integer character constant containing more than one character (e.g., 'ab'),
380-
// or containing a character or escape sequence that does not map to a single-byte execution character,
381-
// is implementation-defined.
382-
// clang and gcc seem to use the following encoding: 'AB' as (('A' << 8) | 'B')
383-
const std::string& normStr = normalizeCharacterLiteral(str);
384-
return encodeMultiChar(normStr);
385-
}
386-
387360
std::string MathLib::normalizeCharacterLiteral(const std::string& iLiteral)
388361
{
389362
std::string normalizedLiteral;
@@ -533,7 +506,7 @@ MathLib::bigint MathLib::toLongNumber(const std::string & str)
533506
}
534507

535508
if (isCharLiteral(str)) {
536-
return characterLiteralToLongNumber(getCharLiteral(str));
509+
return simplecpp::characterLiteralToLL(str);
537510
}
538511

539512
try {
@@ -597,7 +570,7 @@ static double floatHexToDoubleNumber(const std::string& str)
597570
double MathLib::toDoubleNumber(const std::string &str)
598571
{
599572
if (isCharLiteral(str))
600-
return characterLiteralToLongNumber(getCharLiteral(str));
573+
return simplecpp::characterLiteralToLL(str);
601574
if (isIntHex(str))
602575
return static_cast<double>(toLongNumber(str));
603576
// nullcheck

lib/mathlib.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -127,12 +127,6 @@ class CPPCHECKLIB MathLib {
127127

128128
static unsigned int encodeMultiChar(const std::string& str);
129129

130-
/**
131-
* \param[in] str character literal
132-
* @return Number of internal representation of the character literal
133-
* */
134-
static MathLib::bigint characterLiteralToLongNumber(const std::string& str);
135-
136130
/**
137131
* \param[in] iCode Code being considered
138132
* \param[in] iPos A posision within iCode

test/testmathlib.cpp

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -304,38 +304,6 @@ class TestMathLib : public TestFixture {
304304
ASSERT_EQUALS((int)('\100'), MathLib::toLongNumber("'\\100'"));
305305
ASSERT_EQUALS((int)('\200'), MathLib::toLongNumber("'\\200'"));
306306
ASSERT_EQUALS((int)(L'A'), MathLib::toLongNumber("L'A'"));
307-
#ifdef __GNUC__
308-
// BEGIN Implementation-specific results
309-
ASSERT_EQUALS((int)('AB'), MathLib::toLongNumber("'AB'"));
310-
ASSERT_EQUALS((int)('ABC'), MathLib::toLongNumber("'ABC'"));
311-
ASSERT_EQUALS((int)('ABCD'), MathLib::toLongNumber("'ABCD'"));
312-
ASSERT_EQUALS((int)('ABCDE'), MathLib::toLongNumber("'ABCDE'"));
313-
// END Implementation-specific results
314-
#endif
315-
ASSERT_EQUALS((int)('\0'), MathLib::toLongNumber("'\\0'"));
316-
ASSERT_EQUALS(0x1B, MathLib::toLongNumber("'\\e'"));
317-
ASSERT_EQUALS((int)('\r'), MathLib::toLongNumber("'\\r'"));
318-
ASSERT_EQUALS((int)('\x12'), MathLib::toLongNumber("'\\x12'"));
319-
// may cause some compile problems: ASSERT_EQUALS((int)('\x123'), MathLib::toLongNumber("'\\x123'"));
320-
// may cause some compile problems: ASSERT_EQUALS((int)('\x1234'), MathLib::toLongNumber("'\\x1234'"));
321-
ASSERT_EQUALS((int)('\3'), MathLib::toLongNumber("'\\3'"));
322-
ASSERT_EQUALS((int)('\34'), MathLib::toLongNumber("'\\34'"));
323-
ASSERT_EQUALS((int)('\034'), MathLib::toLongNumber("'\\034'"));
324-
ASSERT_EQUALS((int)('\x34'), MathLib::toLongNumber("'\\x34'"));
325-
ASSERT_EQUALS((int)('\134'), MathLib::toLongNumber("'\\134'"));
326-
ASSERT_EQUALS((int)('\134t'), MathLib::toLongNumber("'\\134t'")); // Ticket #7452
327-
ASSERT_THROW(MathLib::toLongNumber("'\\9'"), InternalError);
328-
ASSERT_THROW(MathLib::toLongNumber("'\\934'"), InternalError);
329-
// that is not gcc/clang encoding
330-
ASSERT_EQUALS(959657011, MathLib::toLongNumber("'\\u9343'"));
331-
ASSERT_EQUALS(1714631779, MathLib::toLongNumber("'\\U0001f34c'"));
332-
{
333-
// some unit-testing for a utility function
334-
ASSERT_EQUALS(0, MathLib::characterLiteralToLongNumber(std::string()));
335-
ASSERT_EQUALS(32, MathLib::characterLiteralToLongNumber(std::string(" ")));
336-
ASSERT_EQUALS(538976288, MathLib::characterLiteralToLongNumber(std::string(" ")));
337-
ASSERT_THROW(MathLib::characterLiteralToLongNumber(std::string("\\u")), InternalError);
338-
}
339307

340308
ASSERT_EQUALS(-8552249625308161526, MathLib::toLongNumber("0x89504e470d0a1a0a"));
341309
ASSERT_EQUALS(-8481036456200365558, MathLib::toLongNumber("0x8a4d4e470d0a1a0a"));

0 commit comments

Comments
 (0)