From 3535830c98c1d77dd8dfc018267cdfd74bad3773 Mon Sep 17 00:00:00 2001 From: Nathan Burles Date: Mon, 1 Aug 2016 09:54:10 +0100 Subject: [PATCH] Fixes ATN serialization --- runtime/Cpp/runtime/src/atn/ATNSerializer.cpp | 21 +++++++++++++++---- runtime/Cpp/runtime/src/support/guid.h | 3 +++ 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/runtime/Cpp/runtime/src/atn/ATNSerializer.cpp b/runtime/Cpp/runtime/src/atn/ATNSerializer.cpp index defa33950..e779fa023 100755 --- a/runtime/Cpp/runtime/src/atn/ATNSerializer.cpp +++ b/runtime/Cpp/runtime/src/atn/ATNSerializer.cpp @@ -135,7 +135,7 @@ std::vector ATNSerializer::serialize() { int edgeType = t->getSerializationType(); if (edgeType == Transition::SET || edgeType == Transition::NOT_SET) { SetTransition *st = static_cast(t); - if (setIndices.find(st->set) != setIndices.end()) { + if (setIndices.find(st->set) == setIndices.end()) { sets.push_back(st->set); setIndices.insert({ st->set, (int)sets.size() - 1 }); } @@ -383,7 +383,7 @@ std::vector ATNSerializer::serialize() { } size_t value = (data.at(i) + 2) & 0xFFFF; - data.assign(i, value); + data.at(i) = value; } return data; @@ -625,6 +625,19 @@ std::string ATNSerializer::getDecoded(ATN *atn, std::vector &tokenN } void ATNSerializer::serializeUUID(std::vector &data, Guid uuid) { - for (auto &entry : uuid) - data.push_back(entry); + unsigned int twoBytes = 0; + bool firstByte = true; + for( std::vector::const_reverse_iterator rit = uuid.rbegin(); rit != uuid.rend(); ++rit ) + { + if (firstByte) { + twoBytes = *rit; + firstByte = false; + } else { + twoBytes |= (*rit << 8); + data.push_back(twoBytes); + firstByte = true; + } + } + if (!firstByte) + throw IllegalArgumentException( "The UUID provided is not valid (odd number of bytes)." ); } diff --git a/runtime/Cpp/runtime/src/support/guid.h b/runtime/Cpp/runtime/src/support/guid.h index 3520d0abb..0be26f525 100755 --- a/runtime/Cpp/runtime/src/support/guid.h +++ b/runtime/Cpp/runtime/src/support/guid.h @@ -70,6 +70,9 @@ public: const std::string toString() const; std::vector::const_iterator begin() { return _bytes.begin(); }; std::vector::const_iterator end() { return _bytes.end(); }; + std::vector::const_reverse_iterator rbegin() { return _bytes.rbegin(); }; + std::vector::const_reverse_iterator rend() { return _bytes.rend(); }; + private: