Some checks failed
gitea-openium/resgen.swift/pipeline/head There was a failure building this commit
113 lines
3.1 KiB
Swift
113 lines
3.1 KiB
Swift
//
|
|
// Extensions.swift
|
|
//
|
|
//
|
|
// Created by Thibaut Schmitt on 13/12/2021.
|
|
//
|
|
|
|
import Foundation
|
|
|
|
extension String {
|
|
|
|
public func removeCharacters(from forbiddenChars: CharacterSet) -> String {
|
|
let passed = self.unicodeScalars.filter { !forbiddenChars.contains($0) }
|
|
return String(String.UnicodeScalarView(passed))
|
|
}
|
|
|
|
public func removeCharacters(from: String) -> String {
|
|
removeCharacters(from: CharacterSet(charactersIn: from))
|
|
}
|
|
|
|
public func replacingOccurrences(of: [String], with: String) -> Self {
|
|
var tmp = self
|
|
for ofValue in of {
|
|
tmp = tmp.replacingOccurrences(of: ofValue, with: with)
|
|
}
|
|
return tmp
|
|
}
|
|
|
|
public func removeTrailingWhitespace() -> Self {
|
|
var newString = self
|
|
|
|
while newString.last?.isWhitespace == true {
|
|
newString = String(newString.dropLast())
|
|
}
|
|
|
|
return newString
|
|
}
|
|
|
|
public func removeLeadingWhitespace() -> Self {
|
|
var newString = self
|
|
|
|
while newString.first?.isWhitespace == true {
|
|
newString = String(newString.dropFirst())
|
|
}
|
|
|
|
return newString
|
|
}
|
|
|
|
public func removeLeadingTrailingWhitespace() -> Self {
|
|
var newString = self
|
|
|
|
newString = newString.removeLeadingWhitespace()
|
|
newString = newString.removeTrailingWhitespace()
|
|
|
|
return newString
|
|
}
|
|
|
|
public func escapeDoubleQuote() -> Self {
|
|
replacingOccurrences(of: "\"", with: "\\\"")
|
|
}
|
|
|
|
public func replaceTiltWithHomeDirectoryPath() -> Self {
|
|
// See NSString.expandingTildeInPath
|
|
#if os(macOS)
|
|
replacingOccurrences(of: "~", with: "\(FileManager.default.homeDirectoryForCurrentUser.relativePath)")
|
|
#else
|
|
fatalError("This command should run on Mac only")
|
|
#endif
|
|
}
|
|
|
|
public func colorComponent() -> (alpha: String, red: String, green: String, blue: String) { // swiftlint:disable:this large_tuple
|
|
var alpha: String = "FF"
|
|
var red: String
|
|
var green: String
|
|
var blue: String
|
|
|
|
var colorClean = self
|
|
.replacingOccurrences(of: "#", with: "")
|
|
.replacingOccurrences(of: "0x", with: "")
|
|
|
|
if colorClean.count == 8 {
|
|
alpha = String(colorClean.prefix(2))
|
|
colorClean = String(colorClean.dropFirst(2))
|
|
}
|
|
|
|
red = String(colorClean.prefix(2))
|
|
colorClean = String(colorClean.dropFirst(2))
|
|
green = String(colorClean.prefix(2))
|
|
colorClean = String(colorClean.dropFirst(2))
|
|
blue = String(colorClean.prefix(2))
|
|
return (alpha: alpha, red: red, green: green, blue: blue)
|
|
}
|
|
|
|
public func uppercasedFirst() -> String {
|
|
prefix(1).uppercased() + dropFirst()
|
|
}
|
|
|
|
public func replacingFirstOccurrence(of: String, with: String) -> Self {
|
|
if let range = self.range(of: of) {
|
|
let tmp = self.replacingOccurrences(
|
|
of: of,
|
|
with: with,
|
|
options: .literal,
|
|
range: range
|
|
)
|
|
|
|
return tmp
|
|
}
|
|
|
|
return self
|
|
}
|
|
}
|