CHGenerator/Sources/Generator/Extensions/String+Extensions.swift

107 lines
3.0 KiB
Swift
Raw Normal View History

2022-08-16 10:39:05 +02:00
import Foundation
extension String {
var nonEmpty: String? {
self.isEmpty ? nil : self
}
var trimmed: String {
trimmingCharacters(in: .whitespacesAndNewlines)
}
func indented(by indentation: String) -> String {
components(separatedBy: "\n").joined(separator: "\n" + indentation)
}
var withoutEmptyLines: String {
components(separatedBy: "\n")
.filter { !$0.trimmed.isEmpty }
.joined(separator: "\n")
}
2022-11-27 20:31:56 +01:00
/**
Remove the part after the last occurence of the separator (including the separator itself).
The string is left unchanges, if it does not contain the separator.
*/
2022-08-16 10:39:05 +02:00
func dropAfterLast(_ separator: String) -> String {
guard contains(separator) else {
return self
}
return components(separatedBy: separator).dropLast().joined(separator: separator)
2022-08-16 10:39:05 +02:00
}
func dropBeforeFirst(_ separator: String) -> String {
guard contains(separator) else {
return self
}
return components(separatedBy: separator).dropFirst().joined(separator: separator)
2022-08-16 10:39:05 +02:00
}
func lastComponentAfter(_ separator: String) -> String {
components(separatedBy: separator).last!
}
/**
Insert the new content before the last occurence of the specified separator.
If the separator does not appear in the string, then the new content is simply appended.
*/
2022-08-16 10:39:05 +02:00
func insert(_ content: String, beforeLast separator: String) -> String {
let parts = components(separatedBy: separator)
guard parts.count > 1 else {
return self + content
}
2022-08-16 10:39:05 +02:00
return parts.dropLast().joined(separator: separator) + content + separator + parts.last!
}
2022-08-17 10:34:14 +02:00
2023-07-28 13:59:08 +02:00
/**
Remove everything behind the first separator.
Also removes the separator itself. If the separator is not contained in the string, then the full string is returned.
*/
2022-08-17 10:34:14 +02:00
func dropAfterFirst<T>(_ separator: T) -> String where T: StringProtocol {
components(separatedBy: separator).first!
}
func between(_ start: String, and end: String) -> String {
dropBeforeFirst(start).dropAfterFirst(end)
}
}
extension Substring {
2023-02-20 15:34:26 +01:00
func dropBeforeFirst(_ separator: String) -> String {
guard contains(separator) else {
return String(self)
}
return components(separatedBy: separator).dropFirst().joined(separator: separator)
}
2022-08-17 10:34:14 +02:00
func between(_ start: String, and end: String) -> String {
components(separatedBy: start).last!
.components(separatedBy: end).first!
}
2023-02-20 15:34:26 +01:00
func between(first: String, andLast last: String) -> String {
dropBeforeFirst(first).dropAfterLast(last)
}
2023-02-28 09:31:44 +01:00
func last(after: String) -> String {
components(separatedBy: after).last!
}
2022-08-16 10:39:05 +02:00
}
extension String {
func createFolderAndWrite(to url: URL) throws {
try data(using: .utf8)!.createFolderAndWrite(to: url)
}
}
extension String: DefaultValueProvider {
static var defaultValue: String { "" }
}