Skip to content
Open
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
2 changes: 0 additions & 2 deletions passKit/Parser/Constants.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,6 @@ public enum Constants {
static let MULTILINE_WITHOUT_LINE_BREAK_INDICATOR = ">"
static let MULTILINE_WITHOUT_LINE_BREAK_SEPARATOR = BLANK

static let OTPAUTH_URL_START = "\(OTPAUTH)://"

public static let PASSWORD_KEYWORD = "password"
public static let USERNAME_KEYWORD = "username"
public static let USER_KEYWORD = "user"
Expand Down
24 changes: 6 additions & 18 deletions passKit/Parser/Parser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -65,27 +65,15 @@ class Parser {
return result.trimmed
}

/// Split line from password file in to a key-value pair separted by `: `.
/// Split line from password file in to a key-value pair separted by `:`.
///
/// - Parameter line: Line from a password file
/// - Returns: Pair of two `String`s of which the first one can be 'nil'
/// - Returns: Pair of two `String`s of which the first one can be 'nil'. Both strings are already trimmed from whitespaces.
static func getKeyValuePair(from line: String) -> (key: String?, value: String) {
let items = line.components(separatedBy: ": ").map { String($0).trimmingCharacters(in: .whitespaces) }
var key: String?
var value = ""
if items.count == 1 || (items[0].isEmpty && items[1].isEmpty) {
// No ': ' found, or empty on both sides of ': '.
value = line
// "otpauth" special case
if value.hasPrefix(Constants.OTPAUTH_URL_START) {
key = Constants.OTPAUTH
}
} else {
if !items[0].isEmpty {
key = items[0]
}
value = items[1]
if let separatorIdx = line.firstIndex(of: ":") {
let key = String(line[..<separatorIdx]).trimmingCharacters(in: .whitespaces)
return (key.isEmpty ? nil : key, String(line[line.index(after: separatorIdx)...]).trimmingCharacters(in: .whitespaces))
}
return (key, value)
return (nil, line.trimmingCharacters(in: .whitespaces))
}
}
6 changes: 4 additions & 2 deletions passKitTests/Parser/ParserTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,12 @@ final class ParserTest: XCTestCase {
func testGetKeyValuePair() {
XCTAssert(Parser.getKeyValuePair(from: "key: value") == ("key", "value"))
XCTAssert(Parser.getKeyValuePair(from: "a key: a value") == ("a key", "a value"))
XCTAssert(Parser.getKeyValuePair(from: "key:value") == (nil, "key:value"))
XCTAssert(Parser.getKeyValuePair(from: "key:value") == ("key", "value"))
XCTAssert(Parser.getKeyValuePair(from: ": value") == (nil, "value"))
XCTAssert(Parser.getKeyValuePair(from: " : value ") == (nil, "value"))
XCTAssert(Parser.getKeyValuePair(from: " : ") == (nil, ""))
XCTAssert(Parser.getKeyValuePair(from: "key: ") == ("key", ""))
XCTAssert(Parser.getKeyValuePair(from: "otpauth://value") == ("otpauth", "otpauth://value"))
XCTAssert(Parser.getKeyValuePair(from: "otpauth://value") == ("otpauth", "//value"))
}

func testEmptyFiles() {
Expand Down
Loading