Separators and Escaping in Text Formats

A delimiter needs a rule to distinguish separators from literal data bearing the same spelling.

Source
Anbloq
Language
English
Created
Updated
Chapters
4
Reading time
1 min
In this article4 chapters
  1. Trace two fields
  2. Test the escape rule
  3. Apply an explicit rule for escape characters themselves
  4. Validate malformed escape sequences deliberately

Trace two fields

A record with the values A, B and 7 cannot be faithfully parsed by splitting every comma indiscriminately. A format can quote the first field so its internal comma remains data. Finding and splitting delimiters works well only within its stated assumptions.

Test the escape rule

If a field may itself contain the quotation character, the format must describe how to represent it. Include a value with both a separator and a quote in a round-trip test.[1]

Apply an explicit rule for escape characters themselves

In a line format, suppose unescaped pipe separates fields and backslash escapes either pipe or backslash. A record containing a\|b|c has fields a|b and c. Splitting on every pipe would create three fields. The reader must track whether the next character is escaped. See string finding and splitting.

Read a, then escaped pipe, then b → first content "a|b"
Next unescaped pipe terminates first field
Remaining c forms second field

Validate malformed escape sequences deliberately

Decide whether a final backslash is an error or a literal. Encode backslash itself so arbitrary permitted data remains round-trippable. Test a literal delimiter, a literal escape character followed by a delimiter, and two adjacent separators.

References

  1. IETF, RFC 3629: UTF-8, a Transformation Format of ISO 10646 ↩