Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixed: escape sequences handling #173

Merged
merged 2 commits into from Jul 9, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 10 additions & 4 deletions pyhocon/config_parser.py
Expand Up @@ -3,6 +3,7 @@
import socket
import contextlib
import codecs

from pyparsing import Forward, Keyword, QuotedString, Word, Literal, Suppress, Regex, Optional, SkipTo, ZeroOrMore, \
Group, lineno, col, TokenConverter, replaceWith, alphanums, alphas8bit, ParseSyntaxException, StringEnd
from pyparsing import ParserElement
Expand Down Expand Up @@ -156,14 +157,15 @@ class ConfigParser(object):
"""

REPLACEMENTS = {
'\\\\': '\\',
'\\\n': '\n',
'\\n': '\n',
'\\r': '\r',
'\\t': '\t',
'\\=': '=',
'\\#': '#',
'\\!': '!',
'\\"': '"'
'\\"': '"',
}

@classmethod
Expand All @@ -181,10 +183,14 @@ def parse(cls, content, basedir=None, resolve=True, unresolved_value=DEFAULT_SUB
:return: a ConfigTree or a list
"""

unescape_pattern = re.compile(r'\\.')

def replace_escape_sequence(match):
value = match.group(0)
return cls.REPLACEMENTS.get(value, value)

def norm_string(value):
for k, v in cls.REPLACEMENTS.items():
value = value.replace(k, v)
return value
return unescape_pattern.sub(replace_escape_sequence, value)

def unescape_string(tokens):
return ConfigUnquotedString(norm_string(tokens[0]))
Expand Down
32 changes: 32 additions & 0 deletions tests/test_config_parser.py
@@ -1,5 +1,6 @@
# -*- encoding: utf-8 -*-

import json
import os
import mock
import tempfile
Expand Down Expand Up @@ -2104,3 +2105,34 @@ def test_keys_with_slash(self):
assert 'abc' == config['/abc/cde1']
assert 'cde' == config['/abc/cde2']
assert 'fgh' == config['/abc/cde3']

def test_escape_sequences_json_equivalence(self):
"""
Quoted strings are in the same format as JSON strings,
See: https://github.com/lightbend/config/blob/master/HOCON.md#unchanged-from-json
"""
source = r"""
{
"plain-backslash": "\\",
"tab": "\t",
"no-tab": "\\t",
"newline": "\n",
"no-newline": "\\n",
"cr": "\r",
"no-cr": "\\r",
"windows": "c:\\temp"
}
"""
expected = {
'plain-backslash': '\\',
'tab': '\t',
'no-tab': '\\t',
'newline': '\n',
'no-newline': '\\n',
'cr': '\r',
'no-cr': '\\r',
'windows': 'c:\\temp',
}
config = ConfigFactory.parse_string(source)
assert config == expected
assert config == json.loads(source)