structure/caps: cleaner nested serialization
This is a suggestion for tidying up the serialization of nested GstStructures/GstCaps (or any type that needs to include the serialization of other types as part of its own serialization) to make them more readable.
E.g. currently, if you have three nested structures si
, for i=1,2,3
, where si
contains the GstStructure value ni=s(i+1)
when i=1,2
and the string value vi="i "
, then calling gst_structure_to_string(s1)
will produce the string:
s1, v1=(string)"1\ ", n1=(structure)"s2\,\ v2\=\(string\)\"2\\\ \"\,\ n2\=\(structure\)\"s3\\\,\\\ v3\\\=\\\(string\\\)\\\"3\\\\\\\ \\\"\\\;\"\;";
So, s(i+1)
's string is escaped before inserting it into si
, which means that s3
ends up being doubly escaped.
It seems that the primary reason all these levels of escaping are needed are because the character "
is used as both the opening and closing delimiter for the value, so each "
needs to be escaped several times so we don't confuse it with the end delimiter, but it also results in all escape sequences becoming very long!
I would suggest that the serialization process should use different delimiters for opening and closing a value which may well contain other serialized values. This because you can read in such a string and count every time you reach an unescaped open delimiter, and every time you reach an unescaped closing delimiter, and once they cancel out you have the end of the field. And this would only require that any other appearances of the delimiter characters are escaped once, regardless of how many levels of nesting are present.
One choice would be to use backtick and apostrophe. As far as I'm aware, the only basic type that will serialize to include a backtick or apostrophe would be a string, but these characters are already escaped (there is a potential issue with apostrophes and backticks being used in field names, but that is a separate bigger issue #433). I'm used to using backtick and apostrophe from writing in LaTeX, where they are used to open and close quotes, but I know people can be averse to the backtick. Note, all the bracket delimiters are already used for types, ranges, arrays and lists. The other option would be to use some sequence of characters.
So, for example, the above structure would instead become
s1, v1=(string)"1\ ", n1=(structure)`s2, v2=(string)"2\ ", n2=(structure)`s3, v3=(string)"3\ ";';';
the nested structure can easily be picked out as
s2, v2=(string)"2\ ", n2=(structure)`s3, v3=(string)"3\ ";';
and the next as
s3, v3=(string)"3\ ";
and no unescaping process is required.