From 3e0784b9a9dd8c7bb188d8dd9aa282c55ca6bf81 Mon Sep 17 00:00:00 2001 From: Andrey Vlasovskikh Date: Wed, 9 Jan 2013 19:46:45 +0400 Subject: [PATCH] Fixed braces escaping for new-style formatted strings (PY-8372) --- .../python/inspections/PyStringFormatParser.java | 8 ++++---- .../com/jetbrains/python/PyStringFormatParserTest.java | 7 +++++++ 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/python/src/com/jetbrains/python/inspections/PyStringFormatParser.java b/python/src/com/jetbrains/python/inspections/PyStringFormatParser.java index d479b9e83b10..f759981c3bfb 100644 --- a/python/src/com/jetbrains/python/inspections/PyStringFormatParser.java +++ b/python/src/com/jetbrains/python/inspections/PyStringFormatParser.java @@ -145,8 +145,8 @@ public class PyStringFormatParser { final int n = s.length(); while (pos < n) { int next = s.indexOf('{', pos); - while (next > 0 && s.charAt(next - 1) == '\\') { - next = s.indexOf('{', next + 1); + while (next > 0 && next < n - 1 && s.charAt(next + 1) == '{') { + next = s.indexOf('{', next + 2); } if (next < 0) { break; @@ -156,8 +156,8 @@ public class PyStringFormatParser { } pos = next; next = s.indexOf('}', pos); - while (next > 0 && s.charAt(next - 1) == '\\') { - next = s.indexOf('}', next + 1); + while (next > 0 && next < n - 1 && s.charAt(next + 1) == '}') { + next = s.indexOf('}', next + 2); } if (next > pos) { // TODO: Parse substitution details diff --git a/python/testSrc/com/jetbrains/python/PyStringFormatParserTest.java b/python/testSrc/com/jetbrains/python/PyStringFormatParserTest.java index bdce2229f4d0..b64593674850 100644 --- a/python/testSrc/com/jetbrains/python/PyStringFormatParserTest.java +++ b/python/testSrc/com/jetbrains/python/PyStringFormatParserTest.java @@ -119,4 +119,11 @@ public class PyStringFormatParserTest extends TestCase { assertEquals(TextRange.create(1, 6), chunks.get(0).getTextRange()); assertEquals(TextRange.create(6, 11), chunks.get(1).getTextRange()); } + + // PY-8372 + public void testBracesEscaping() { + final List chunks = filterSubstitutions(parseNewStyleFormat("\\{\\}, {{}}")); + assertEquals(1, chunks.size()); + assertEquals(TextRange.create(1, 4), chunks.get(0).getTextRange()); + } }