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 3 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
15 changes: 15 additions & 0 deletions Tests/ACKLocalizationCoreTests/LocRowTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down