From kde-commits Sun Dec 04 18:52:11 2016 From: Andreas Hartmetz Date: Sun, 04 Dec 2016 18:52:11 +0000 To: kde-commits Subject: [dferry] serialization: Add Arguments::Reader::isDictKey() and fix pretty-printing. Message-Id: X-MARC-Message: https://marc.info/?l=kde-commits&m=148087754228826 Git commit eb1418add5a83a80adbe3c44925907d765828d80 by Andreas Hartmetz. Committed on 04/12/2016 at 18:40. Pushed by ahartmetz into branch 'master'. Add Arguments::Reader::isDictKey() and fix pretty-printing. The fixes are: - use isDictKey() - there was still a TODO after removing NextDictEntry - make it work when WITH_DICT_ENTRY is defined Also make the pretty-printer output a bit easier to understand. The one char indentation for dict keys and values was ugly, and the "v " for variant was hard to distinguish from "V " for dict entry value, so switch to "* " for variant. M +28 -11 serialization/arguments.cpp M +2 -0 serialization/arguments.h https://commits.kde.org/dferry/eb1418add5a83a80adbe3c44925907d765828d80 diff --git a/serialization/arguments.cpp b/serialization/arguments.cpp index 5ad3148..88ab65e 100644 --- a/serialization/arguments.cpp +++ b/serialization/arguments.cpp @@ -649,7 +649,13 @@ std::string Arguments::prettyPrint() const while (!isDone) { // HACK use nestingPrefix to determine when we're switching from k= ey to value - this can be done // more cleanly with an aggregate stack if translation or sim= ilar makes this approach too ugly - if (strEndsWith(nestingPrefix, "{")) { + if (reader.isDictKey()) { + if (strEndsWith(nestingPrefix, "V ")) { + nestingPrefix.resize(nestingPrefix.size() - strlen("V ")); + assert(strEndsWith(nestingPrefix, "{ ")); + } + } + if (strEndsWith(nestingPrefix, "{ ")) { nestingPrefix +=3D "K "; } else if (strEndsWith(nestingPrefix, "K ")) { nestingPrefix.replace(nestingPrefix.size() - strlen("K "), str= len("V "), "V "); @@ -672,7 +678,7 @@ std::string Arguments::prettyPrint() const case Arguments::BeginVariant: reader.beginVariant(); ret << nestingPrefix << "begin variant\n"; - nestingPrefix +=3D "v "; + nestingPrefix +=3D "* "; break; case Arguments::EndVariant: reader.endVariant(); @@ -706,21 +712,22 @@ std::string Arguments::prettyPrint() const case Arguments::BeginDict: { inEmptyArray =3D !reader.beginDict(Arguments::Reader::ReadType= sOnlyIfEmpty); ret << nestingPrefix << "begin dict\n"; - nestingPrefix +=3D "{K "; + nestingPrefix +=3D "{ "; break; } -#if 0 // TODO - case Arguments::NextDictEntry: - reader.nextDictEntry(); - if (strEndsWith(nestingPrefix, "V ")) { - nestingPrefix.resize(nestingPrefix.size() - strlen("V ")); - assert(strEndsWith(nestingPrefix, "{")); - } +#ifdef WITH_DICT_ENTRY + // We *could* use those states to be a bit more efficient than wit= h calling isDictKey() all + // the time, but let's keep it simple, and WITH_DICT_ENTRY as a no= n-default configuration. + case Arguments::BeginDictEntry: + reader.beginDictEntry(); + break; + case Arguments::EndDictEntry: + reader.endDictEntry(); break; #endif case Arguments::EndDict: reader.endDict(); inEmptyArray =3D reader.isInsideEmptyArray(); - nestingPrefix.resize(nestingPrefix.size() - strlen("{V ")); + nestingPrefix.resize(nestingPrefix.size() - strlen("{ V ")); ret << nestingPrefix << "end dict\n"; break; case Arguments::Boolean: { @@ -1726,6 +1733,16 @@ void Arguments::Reader::skipDict() } } = +bool Arguments::Reader::isDictKey() const +{ + if (!d->m_aggregateStack.empty()) { + const Private::AggregateInfo &aggregateInfo =3D d->m_aggregateStac= k.back(); + return aggregateInfo.aggregateType =3D=3D BeginDict && + d->m_signaturePosition =3D=3D aggregateInfo.arr.containedTy= peBegin; + } + return false; +} + void Arguments::Reader::endDict() { VALID_IF(m_state =3D=3D EndDict, Error::ReadWrongType); diff --git a/serialization/arguments.h b/serialization/arguments.h index faa8c1e..0f39252 100644 --- a/serialization/arguments.h +++ b/serialization/arguments.h @@ -217,6 +217,8 @@ public: = bool beginDict(EmptyArrayOption option =3D SkipIfEmpty); void skipDict(); // like skipArray() + bool isDictKey() const; // this can be used to track whether the c= urrent value is a dict key or value, e.g. + // for pretty-printing purposes (it is usu= ally clear in marshalling code). void endDict(); // like endArray() = void beginStruct();