Skip to content

fix: parse and compile \a, \b, \f and \v escapes like GNU gettext - #118

Open
zzoraemon043 wants to merge 1 commit into
smhg:mainfrom
zzoraemon043:fix/control-character-escapes
Open

zzoraemon043 wants to merge 1 commit into
smhg:mainfrom
zzoraemon043:fix/control-character-escapes

Conversation

@zzoraemon043

Copy link
Copy Markdown

GNU gettext writes the BEL, BS, FF and VT control characters as \a, \b, \f and \v in PO strings (msgcat output for a msgstr containing them is msgstr "x\a\b\f\vy"). po.parse only knows \t, \n and \r, so it reads these back as the letters a, b, f and v:

po.parse('msgid "test"\nmsgstr "a\\ab\\bf\\fv\\v"\n').translations[''].test.msgstr[0]
// before: 'aabbffvv'
// after:  'a\x07b\bf\fv\v' (same as msgfmt + mo.parse)

This change:

  • unescapes \a, \b, \f and \v in the PO parser
  • escapes them in the PO compiler when escapeCharacters is on, so po.compile output matches msgcat (before, the raw control characters were written)

Tests: a parser test and two compiler tests (escaped output, round trip). The parser test and the escaped-output test fail on master. npm test (72) and npm run lint pass.

Also checked against GNU gettext 0.21: msgcat output for 300 long entries mixing these characters with text parses and compiles back to the same file, with foldLength: 79 line breaks matching msgcat.

Not covered: octal (\101) and hex (\x41) escapes, which msgfmt also accepts. Those encode bytes in the file charset, which does not map directly onto the already decoded string the lexer works on.

GNU gettext writes the BEL, BS, FF and VT control characters as \a, \b, \f and \v
in PO strings. The parser read them back as the letters a, b, f and v, so a
file written by msgcat lost those characters. Unescape them when parsing and
escape them when compiling with escapeCharacters, so po.compile output matches
msgcat.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant