Skip to content

Commit b034710

Browse files
Separate additional fields by ":" instead of ": "
1 parent 1a92996 commit b034710

File tree

3 files changed

+10
-22
lines changed

3 files changed

+10
-22
lines changed

passKit/Parser/Constants.swift

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,6 @@ public enum Constants {
4444
static let MULTILINE_WITHOUT_LINE_BREAK_INDICATOR = ">"
4545
static let MULTILINE_WITHOUT_LINE_BREAK_SEPARATOR = BLANK
4646

47-
static let OTPAUTH_URL_START = "\(OTPAUTH)://"
48-
4947
public static let PASSWORD_KEYWORD = "password"
5048
public static let USERNAME_KEYWORD = "username"
5149
public static let USER_KEYWORD = "user"

passKit/Parser/Parser.swift

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -65,27 +65,15 @@ class Parser {
6565
return result.trimmed
6666
}
6767

68-
/// Split line from password file in to a key-value pair separted by `: `.
68+
/// Split line from password file in to a key-value pair separted by `:`.
6969
///
7070
/// - Parameter line: Line from a password file
71-
/// - Returns: Pair of two `String`s of which the first one can be 'nil'
71+
/// - Returns: Pair of two `String`s of which the first one can be 'nil'. Both strings are already trimmed from whitespaces.
7272
static func getKeyValuePair(from line: String) -> (key: String?, value: String) {
73-
let items = line.components(separatedBy: ": ").map { String($0).trimmingCharacters(in: .whitespaces) }
74-
var key: String?
75-
var value = ""
76-
if items.count == 1 || (items[0].isEmpty && items[1].isEmpty) {
77-
// No ': ' found, or empty on both sides of ': '.
78-
value = line
79-
// "otpauth" special case
80-
if value.hasPrefix(Constants.OTPAUTH_URL_START) {
81-
key = Constants.OTPAUTH
82-
}
83-
} else {
84-
if !items[0].isEmpty {
85-
key = items[0]
86-
}
87-
value = items[1]
73+
if let separatorIdx = line.firstIndex(of: ":") {
74+
let key = String(line[..<separatorIdx]).trimmingCharacters(in: .whitespaces)
75+
return (key.isEmpty ? nil : key, String(line[line.index(after: separatorIdx)...]).trimmingCharacters(in: .whitespaces))
8876
}
89-
return (key, value)
77+
return (nil, line.trimmingCharacters(in: .whitespaces))
9078
}
9179
}

passKitTests/Parser/ParserTest.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,12 @@ final class ParserTest: XCTestCase {
2929
func testGetKeyValuePair() {
3030
XCTAssert(Parser.getKeyValuePair(from: "key: value") == ("key", "value"))
3131
XCTAssert(Parser.getKeyValuePair(from: "a key: a value") == ("a key", "a value"))
32-
XCTAssert(Parser.getKeyValuePair(from: "key:value") == (nil, "key:value"))
32+
XCTAssert(Parser.getKeyValuePair(from: "key:value") == ("key", "value"))
3333
XCTAssert(Parser.getKeyValuePair(from: ": value") == (nil, "value"))
34+
XCTAssert(Parser.getKeyValuePair(from: " : value ") == (nil, "value"))
35+
XCTAssert(Parser.getKeyValuePair(from: " : ") == (nil, ""))
3436
XCTAssert(Parser.getKeyValuePair(from: "key: ") == ("key", ""))
35-
XCTAssert(Parser.getKeyValuePair(from: "otpauth://value") == ("otpauth", "otpauth://value"))
37+
XCTAssert(Parser.getKeyValuePair(from: "otpauth://value") == ("otpauth", "//value"))
3638
}
3739

3840
func testEmptyFiles() {

0 commit comments

Comments
 (0)