From 714fd4d90b07684fc7ac10d0daf6b11296b93a5b Mon Sep 17 00:00:00 2001 From: dnwpark Date: Fri, 15 Mar 2024 18:57:54 -0400 Subject: [PATCH] Remove separate escape_string function. --- edb/common/string.py | 37 ---------------------------- edb/edgeql/quote.py | 20 +++++++++++++++- tests/common/test_string.py | 48 ------------------------------------- tests/edgeql/test_quote.py | 21 +++++++++++++++- 4 files changed, 39 insertions(+), 87 deletions(-) delete mode 100644 edb/common/string.py delete mode 100644 tests/common/test_string.py diff --git a/edb/common/string.py b/edb/common/string.py deleted file mode 100644 index 6cb56cdb134..00000000000 --- a/edb/common/string.py +++ /dev/null @@ -1,37 +0,0 @@ -# -# This source file is part of the EdgeDB open source project. -# -# Copyright 2009-present MagicStack Inc. and the EdgeDB authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# - - -def escape_string(s: str) -> str: - # characters escaped according to - # https://www.postgresql.org/docs/current/sql-syntax-lexical.html - # 4.1.2.2 String Constants with C-Style Escapes - result = s - - # escape backslash first - result = result.replace('\\', '\\\\') - - result = result.replace('\'', '\\\'') - result = result.replace('\"', '\\\"') - result = result.replace('\b', '\\b') - result = result.replace('\f', '\\f') - result = result.replace('\n', '\\n') - result = result.replace('\r', '\\r') - result = result.replace('\t', '\\t') - - return result diff --git a/edb/edgeql/quote.py b/edb/edgeql/quote.py index e21d5c69538..91795fd5e79 100644 --- a/edb/edgeql/quote.py +++ b/edb/edgeql/quote.py @@ -21,7 +21,6 @@ import re -from edb.common.string import escape_string from .parser.grammar import keywords @@ -37,6 +36,25 @@ def quote_literal(string: str) -> str: + + def escape_string(s: str) -> str: + # characters escaped according to + # https://www.edgedb.com/docs/reference/edgeql/lexical#strings + result = s + + # escape backslash first + result = result.replace('\\', '\\\\') + + result = result.replace('\'', '\\\'') + result = result.replace('\"', '\\\"') + result = result.replace('\b', '\\b') + result = result.replace('\f', '\\f') + result = result.replace('\n', '\\n') + result = result.replace('\r', '\\r') + result = result.replace('\t', '\\t') + + return result + return "'" + escape_string(string) + "'" diff --git a/tests/common/test_string.py b/tests/common/test_string.py deleted file mode 100644 index df8986630fe..00000000000 --- a/tests/common/test_string.py +++ /dev/null @@ -1,48 +0,0 @@ -# -# This source file is part of the EdgeDB open source project. -# -# Copyright 2018-present MagicStack Inc. and the EdgeDB authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# - -import unittest - -import edb.common.string as string - -class StringTests(unittest.TestCase): - unescaped_escaped_strings = [ - ('', ''), - ('abc', 'abc'), - ('\b', '\\b'), - ('\f', '\\f'), - ('\n', '\\n'), - ('\r', '\\r'), - ('\t', '\\t'), - ('\'', '\\\''), - ('\"', '\\\"'), - ('\\', '\\\\'), - ('\\b', '\\\\b'), - ('\\f', '\\\\f'), - ('\\n', '\\\\n'), - ('\\r', '\\\\r'), - ('\\t', '\\\\t'), - ('\\\'', '\\\\\\\''), - ('\\\"', '\\\\\\\"'), - ('\\\\', '\\\\\\\\'), - ('abc"efg\nhij\'klm\\nop', 'abc\\"efg\\nhij\\\'klm\\\\nop'), - ] - - def test_escape_string(self): - for unescaped, escaped in StringTests.unescaped_escaped_strings: - self.assertEqual(string.escape_string(unescaped), escaped) diff --git a/tests/edgeql/test_quote.py b/tests/edgeql/test_quote.py index 0c762c2a962..13306328855 100644 --- a/tests/edgeql/test_quote.py +++ b/tests/edgeql/test_quote.py @@ -23,5 +23,24 @@ class QuoteTests(unittest.TestCase): def test_quote_string(self): + self.assertEqual(qlquote.quote_literal(""), "''"), self.assertEqual(qlquote.quote_literal("abc"), "'abc'") - self.assertEqual(qlquote.quote_literal("abc\\\n"), "'abc\\\\\\n'") + self.assertEqual(qlquote.quote_literal("\b"), "'\\b'") + self.assertEqual(qlquote.quote_literal("\f"), "'\\f'") + self.assertEqual(qlquote.quote_literal("\n"), "'\\n'") + self.assertEqual(qlquote.quote_literal("\r"), "'\\r'") + self.assertEqual(qlquote.quote_literal("\t"), "'\\t'") + self.assertEqual(qlquote.quote_literal("\'"), "'\\\''") + self.assertEqual(qlquote.quote_literal("\""), "'\\\"'") + self.assertEqual(qlquote.quote_literal("\\"), "'\\\\'") + self.assertEqual(qlquote.quote_literal("\\b"), "'\\\\b'") + self.assertEqual(qlquote.quote_literal("\\f"), "'\\\\f'") + self.assertEqual(qlquote.quote_literal("\\n"), "'\\\\n'") + self.assertEqual(qlquote.quote_literal("\\r"), "'\\\\r'") + self.assertEqual(qlquote.quote_literal("\\t"), "'\\\\t'") + self.assertEqual(qlquote.quote_literal("\\\'"), "'\\\\\\\''") + self.assertEqual(qlquote.quote_literal("\\\""), "'\\\\\\\"'") + self.assertEqual(qlquote.quote_literal("\\\\"), "'\\\\\\\\'") + self.assertEqual(qlquote.quote_literal( + "abc\"efg\nhij\'klm\\nop"), + "'abc\\\"efg\\nhij\\\'klm\\\\nop'")