Fixed a bug in unified string handling (issue 7)
diff --git a/TODO.txt b/TODO.txt
index 572a15c..51519ac 100644
--- a/TODO.txt
+++ b/TODO.txt
@@ -2,7 +2,7 @@
------------------
* bugfix: correct handling of do{} while statements in some cases
-
+* bugfix: Issue 6 (concatenation of string literals)
Version Update
--------------
diff --git a/pycparser/c_parser.py b/pycparser/c_parser.py
index 1b4be48..bef2176 100644
--- a/pycparser/c_parser.py
+++ b/pycparser/c_parser.py
@@ -1239,7 +1239,7 @@
p[0] = c_ast.Constant(
'string', p[1], self._coord(p.lineno(1)))
else:
- p[1].value = p[1].value.rstrip('"') + p[2].lstrip('"')
+ p[1].value = p[1].value[:-1] + p[2][1:]
p[0] = p[1]
def p_unified_wstring_literal(self, p):
@@ -1250,7 +1250,7 @@
p[0] = c_ast.Constant(
'string', p[1], self._coord(p.lineno(1)))
else:
- p[1].value = p[1].value.rstrip('"') + p[2].lstrip('"')
+ p[1].value = p[1].value.rstrip[:-1] + p[2][1:]
p[0] = p[1]
def p_empty(self, p):
diff --git a/tests/test_c_parser.py b/tests/test_c_parser.py
index 3e32cb6..62f5327 100644
--- a/tests/test_c_parser.py
+++ b/tests/test_c_parser.py
@@ -830,6 +830,12 @@
self.assertEqual(
d3.ext[0].body.stmts[0].args.exprs[1].value,
r'"Wrong Params?\nUsage:\n%s <binary_file_path>\n"')
+
+ d4 = self.get_decl_init('char* s = "" "foobar";')
+ self.assertEqual(d4, ['Constant', 'string', '"foobar"'])
+
+ d5 = self.get_decl_init(r'char* s = "foo\"" "bar";')
+ self.assertEqual(d5, ['Constant', 'string', r'"foo\"bar"'])
class TestCParser_whole_code(unittest.TestCase):