From 63b770066064b9bffedfe0158192b08061e9ecf6 Mon Sep 17 00:00:00 2001 From: Alexander Mai Date: Sun, 22 Nov 2015 14:20:36 +0100 Subject: [PATCH] Use clang/gcc-conforming encoding of multicharacter literals --- lib/mathlib.cpp | 7 ++++++- test/testmathlib.cpp | 7 ++++++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/lib/mathlib.cpp b/lib/mathlib.cpp index efd72721e..4e5dd414b 100644 --- a/lib/mathlib.cpp +++ b/lib/mathlib.cpp @@ -325,7 +325,12 @@ MathLib::bigint MathLib::characterLiteralToLongNumber(const std::string& str) // 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. - return str[0] & 0xff; + // clang and gcc seem to use the following encoding: 'AB' as (('A' << 8) | 'B') + int retval(str.front()); + for (std::string::const_iterator it=str.begin()+1; it!=str.end(); ++it) { + retval = retval<<8 | *it; + } + return retval; // str[0] & 0xff; } switch (str[1]) { diff --git a/test/testmathlib.cpp b/test/testmathlib.cpp index 380534a33..94b615b78 100644 --- a/test/testmathlib.cpp +++ b/test/testmathlib.cpp @@ -271,7 +271,12 @@ private: // from char ASSERT_EQUALS((int)('A'), MathLib::toLongNumber("'A'")); - ASSERT_EQUALS((int)('A'), MathLib::toLongNumber("'ABC'")); + // 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 ASSERT_EQUALS((int)('\0'), MathLib::toLongNumber("'\\0'")); ASSERT_EQUALS((int)('\r'), MathLib::toLongNumber("'\\r'")); ASSERT_EQUALS((int)('\x12'), MathLib::toLongNumber("'\\x12'"));