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

Refactor string literal escaping #24

Merged
merged 4 commits into from
Nov 8, 2020
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 4 additions & 6 deletions Sources/ACKLocalizationCore/Model/LocRow.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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: "%@")
Expand Down
28 changes: 14 additions & 14 deletions Tests/ACKLocalizationCoreTests/LocRowTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -34,51 +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)
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)
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)
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)
}
}