wireguard-apple/WireGuard/Shared/Model/String+ArrayConversion.swift

33 lines
887 B
Swift
Raw Normal View History

2018-12-20 16:46:26 +00:00
// SPDX-License-Identifier: MIT
2019-01-02 00:56:33 +00:00
// Copyright © 2018-2019 WireGuard LLC. All Rights Reserved.
2018-12-20 16:46:26 +00:00
import Foundation
extension String {
2018-12-21 22:34:56 +00:00
2018-12-20 16:46:26 +00:00
func splitToArray(separator: Character = ",", trimmingCharacters: CharacterSet? = nil) -> [String] {
return split(separator: separator)
.map {
if let charSet = trimmingCharacters {
return $0.trimmingCharacters(in: charSet)
} else {
return String($0)
}
}
}
2018-12-21 22:34:56 +00:00
2018-12-20 16:46:26 +00:00
}
extension Optional where Wrapped == String {
2018-12-21 22:34:56 +00:00
2018-12-20 16:46:26 +00:00
func splitToArray(separator: Character = ",", trimmingCharacters: CharacterSet? = nil) -> [String] {
switch self {
case .none:
return []
case .some(let wrapped):
return wrapped.splitToArray(separator: separator, trimmingCharacters: trimmingCharacters)
}
}
2018-12-21 22:34:56 +00:00
2018-12-20 16:46:26 +00:00
}