passforios/passKit/Extensions/String+Utilities.swift

31 lines
816 B
Swift
Raw Normal View History

2018-09-23 20:02:07 +08:00
//
// String+Utilities.swift
2018-09-23 20:02:07 +08:00
// passKit
//
// Created by Yishi Lin on 2018/9/23.
// Copyright © 2018 Bob Sun. All rights reserved.
//
2020-11-07 12:06:28 +01:00
public extension String {
var trimmed: String {
trimmingCharacters(in: .whitespacesAndNewlines)
}
2020-11-07 12:06:28 +01:00
func stringByAddingPercentEncodingForRFC3986() -> String? {
let unreserved = "-._~/"
2018-09-23 20:02:07 +08:00
var allowed = CharacterSet.alphanumerics
allowed.insert(charactersIn: unreserved)
return addingPercentEncoding(withAllowedCharacters: allowed)
}
2020-11-07 12:06:28 +01:00
func splitByNewline() -> [String] {
split(omittingEmptySubsequences: false) { $0 == "\n" || $0 == "\r\n" }.map(String.init)
}
2018-09-23 20:02:07 +08:00
}
2020-11-07 12:06:28 +01:00
public extension String {
static func | (left: String, right: String) -> String {
right.isEmpty ? left : left + "\n" + right
}
}