From 61321013e142cad9d3dd171e218c0b9898b855cd Mon Sep 17 00:00:00 2001 From: Jakub Olejnik Date: Wed, 4 Nov 2020 00:06:12 +0100 Subject: [PATCH 1/4] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20Refactor=20escaping=20?= =?UTF-8?q?in=20`LocRow`?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Sources/ACKLocalizationCore/Model/LocRow.swift | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/Sources/ACKLocalizationCore/Model/LocRow.swift b/Sources/ACKLocalizationCore/Model/LocRow.swift index 79c588f..e9a2cde 100644 --- a/Sources/ACKLocalizationCore/Model/LocRow.swift +++ b/Sources/ACKLocalizationCore/Model/LocRow.swift @@ -29,16 +29,14 @@ public struct LocRow { /// Key with replaced quotes private var normalizedKey: String { - return key - .replacingOccurrences(of: "\"", with: "\\\"") + key.replacingOccurrences(of: "\"", with: #"\""#) } /// Value with replaced placeholder arguments private var normalizedValue: String { - return value - .replacingOccurrences(of: "\"", with: "\\\"") - .replacingOccurrences(of: "\n", with: "\\n") - .replacingOccurrences(of: "\\u", with: "\\U") + value.replacingOccurrences(of: "\"", with: #"\""#) + .replacingOccurrences(of: "\n", with: #"\n"#) + .replacingOccurrences(of: "\\u", with: #"\U"#) .replacingOccurrences(of: "%s", with: "%@") .replacingOccurrences(of: "%", with: "%%") .replacingOccurrences(of: "%%@", with: "%@") From 8b7bc26b40167187cc10628896e3b4b6186e6ae6 Mon Sep 17 00:00:00 2001 From: Jakub Olejnik Date: Wed, 4 Nov 2020 00:09:14 +0100 Subject: [PATCH 2/4] =?UTF-8?q?=E2=9C=85=20Add=20new=20tests=20for=20liter?= =?UTF-8?q?al=20escaping?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Tests/ACKLocalizationCoreTests/LocRowTests.swift | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/Tests/ACKLocalizationCoreTests/LocRowTests.swift b/Tests/ACKLocalizationCoreTests/LocRowTests.swift index 51940ec..6ad5e33 100644 --- a/Tests/ACKLocalizationCoreTests/LocRowTests.swift +++ b/Tests/ACKLocalizationCoreTests/LocRowTests.swift @@ -52,16 +52,31 @@ final class LocRowTests: XCTestCase { XCTAssertEqual("\"abc\\\"abc\" = \"quotes_value\";", locRow.localizableRow) } + func testKeyQuotesAreEscaped2() { + let locRow = LocRow(key: "abc\"abc", value: "quotes_value") + XCTAssertEqual(#""abc\"abc" = "quotes_value";"#, locRow.localizableRow) + } + func testValueQuotesAreEscaped() { let locRow = LocRow(key: "quotes_key", value: "abc\"abc") XCTAssertEqual("\"quotes_key\" = \"abc\\\"abc\";", locRow.localizableRow) } + func testValueQuotesAreEscaped2() { + let locRow = LocRow(key: "quotes_key", value: "abc\"abc") + XCTAssertEqual(#""quotes_key" = "abc\"abc";"#, locRow.localizableRow) + } + func testNewLineIsEscaped() { let locRow = LocRow(key: "nl_key", value: "abc\nabc") XCTAssertEqual("\"nl_key\" = \"abc\\nabc\";", locRow.localizableRow) } + func testNewLineIsEscaped2() { + let locRow = LocRow(key: "nl_key", value: "abc\nabc") + XCTAssertEqual(#""nl_key" = "abc\nabc";"#, locRow.localizableRow) + } + func testIntPositionArgumentsAreReplaced() { let locRow = LocRow(key: "pos_arg_key", value: "%1$d people will arrive in %2$d minutes") XCTAssertEqual("\"pos_arg_key\" = \"%1$d people will arrive in %2$d minutes\";", locRow.localizableRow) From d4d6803955f16703662dce6ebff23f86319cddab Mon Sep 17 00:00:00 2001 From: Jakub Olejnik Date: Wed, 4 Nov 2020 00:11:23 +0100 Subject: [PATCH 3/4] =?UTF-8?q?=F0=9F=93=9D=20Update=20changelog?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CHANGELOG.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b271c47..d66933e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,11 +8,12 @@ ## master -## 1.1.3 - ### Fixed - Fix creating invalid keys, if contain quote marks ([#22](https://github.com/AckeeCZ/ACKLocalization/pull/22), kudos to @mateuszjablonski) +### Changed +- Internal refactoring regarding string literal escaping ([#24](https://github.com/AckeeCZ/ACKLocalization/pull/24), kudos to @olejnjak) + ## 1.1.2 ### Fixed From dc869ea20e8d9d2e5be15a4f48e0de1deda09779 Mon Sep 17 00:00:00 2001 From: Jakub Olejnik Date: Fri, 6 Nov 2020 12:53:21 +0100 Subject: [PATCH 4/4] =?UTF-8?q?=E2=9C=85=20Simplify=20escaping=20in=20test?= =?UTF-8?q?s?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../LocRowTests.swift | 37 ++++++------------- 1 file changed, 11 insertions(+), 26 deletions(-) diff --git a/Tests/ACKLocalizationCoreTests/LocRowTests.swift b/Tests/ACKLocalizationCoreTests/LocRowTests.swift index 6ad5e33..49600d8 100644 --- a/Tests/ACKLocalizationCoreTests/LocRowTests.swift +++ b/Tests/ACKLocalizationCoreTests/LocRowTests.swift @@ -4,22 +4,22 @@ import XCTest final class LocRowTests: XCTestCase { func testBasicRow() { let locRow = LocRow(key: "key", value: "value") - XCTAssertEqual("\"key\" = \"value\";", locRow.localizableRow) + XCTAssertEqual(#""key" = "value";"#, locRow.localizableRow) } func testIntegerRow() { let locRow = LocRow(key: "int_key", value: "int value %d") - XCTAssertEqual("\"int_key\" = \"int value %d\";", locRow.localizableRow) + XCTAssertEqual(#""int_key" = "int value %d";"#, locRow.localizableRow) } func testAlternativeIntegerRow() { let locRow = LocRow(key: "int_key", value: "int value %i") - XCTAssertEqual("\"int_key\" = \"int value %i\";", locRow.localizableRow) + XCTAssertEqual(#""int_key" = "int value %i";"#, locRow.localizableRow) } func testFloatRow() { let locRow = LocRow(key: "float_key", value: "float value %f") - XCTAssertEqual("\"float_key\" = \"float value %f\";", locRow.localizableRow) + XCTAssertEqual(#""float_key" = "float value %f";"#, locRow.localizableRow) } func testOneDecimalFloatRow() { @@ -34,66 +34,51 @@ final class LocRowTests: XCTestCase { func testStringRow() { let locRow = LocRow(key: "string_key", value: "string value %s") - XCTAssertEqual("\"string_key\" = \"string value %@\";", locRow.localizableRow) + XCTAssertEqual(#""string_key" = "string value %@";"#, locRow.localizableRow) } func testCocoaStringRow() { let locRow = LocRow(key: "string_key", value: "string value %@") - XCTAssertEqual("\"string_key\" = \"string value %@\";", locRow.localizableRow) + XCTAssertEqual(#""string_key" = "string value %@";"#, locRow.localizableRow) } func testPercentIsEscaped() { let locRow = LocRow(key: "percent_key", value: "%d % percent") - XCTAssertEqual("\"percent_key\" = \"%d %% percent\";", locRow.localizableRow) + XCTAssertEqual(#""percent_key" = "%d %% percent";"#, locRow.localizableRow) } func testKeyQuotesAreEscaped() { - let locRow = LocRow(key: "abc\"abc", value: "quotes_value") - XCTAssertEqual("\"abc\\\"abc\" = \"quotes_value\";", locRow.localizableRow) - } - - func testKeyQuotesAreEscaped2() { let locRow = LocRow(key: "abc\"abc", value: "quotes_value") XCTAssertEqual(#""abc\"abc" = "quotes_value";"#, locRow.localizableRow) } func testValueQuotesAreEscaped() { - let locRow = LocRow(key: "quotes_key", value: "abc\"abc") - XCTAssertEqual("\"quotes_key\" = \"abc\\\"abc\";", locRow.localizableRow) - } - - func testValueQuotesAreEscaped2() { let locRow = LocRow(key: "quotes_key", value: "abc\"abc") XCTAssertEqual(#""quotes_key" = "abc\"abc";"#, locRow.localizableRow) } func testNewLineIsEscaped() { - let locRow = LocRow(key: "nl_key", value: "abc\nabc") - XCTAssertEqual("\"nl_key\" = \"abc\\nabc\";", locRow.localizableRow) - } - - func testNewLineIsEscaped2() { let locRow = LocRow(key: "nl_key", value: "abc\nabc") XCTAssertEqual(#""nl_key" = "abc\nabc";"#, locRow.localizableRow) } func testIntPositionArgumentsAreReplaced() { let locRow = LocRow(key: "pos_arg_key", value: "%1$d people will arrive in %2$d minutes") - XCTAssertEqual("\"pos_arg_key\" = \"%1$d people will arrive in %2$d minutes\";", locRow.localizableRow) + XCTAssertEqual(#""pos_arg_key" = "%1$d people will arrive in %2$d minutes";"#, locRow.localizableRow) } func testFloatPositionArgumentsAreReplaced() { let locRow = LocRow(key: "pos_arg_key", value: "%1$f people will arrive in %2$f minutes") - XCTAssertEqual("\"pos_arg_key\" = \"%1$f people will arrive in %2$f minutes\";", locRow.localizableRow) + XCTAssertEqual(#""pos_arg_key" = "%1$f people will arrive in %2$f minutes";"#, locRow.localizableRow) } func testStringPositionArgumentsAreReplaced() { let locRow = LocRow(key: "pos_arg_key", value: "%1$s people will arrive in %2$s minutes") - XCTAssertEqual("\"pos_arg_key\" = \"%1$@ people will arrive in %2$@ minutes\";", locRow.localizableRow) + XCTAssertEqual(#""pos_arg_key" = "%1$@ people will arrive in %2$@ minutes";"#, locRow.localizableRow) } func testCocoaStringPositionArgumentsAreReplaced() { let locRow = LocRow(key: "pos_arg_key", value: "%1$@ people will arrive in %2$@ minutes") - XCTAssertEqual("\"pos_arg_key\" = \"%1$@ people will arrive in %2$@ minutes\";", locRow.localizableRow) + XCTAssertEqual(#""pos_arg_key" = "%1$@ people will arrive in %2$@ minutes";"#, locRow.localizableRow) } }