Files
Stephen Celis f48c096f85 Swift Language Support: Drop <5.9, Add 6.0 (#121)
* Swift Language Support: Drop <5.9, Add 6.0

* wip

* wip

* wip

* wip

* wip
2024-06-18 08:03:05 -07:00

31 lines
962 B
Swift

import Foundation
extension String {
init?(stringProtocol value: Any) {
guard let value = value as? any StringProtocol else { return nil }
self.init(value)
}
func indenting(by count: Int) -> String {
self.indenting(with: String(repeating: " ", count: count))
}
func indenting(with prefix: String) -> String {
guard !prefix.isEmpty else { return self }
return "\(prefix)\(self.replacingOccurrences(of: "\n", with: "\n\(prefix)"))"
}
func hashCount(isMultiline: Bool) -> Int {
let (quote, offset) = isMultiline ? ("\"\"\"", 2) : ("\"", 0)
var substring = self[...]
var hashCount = 0
let pattern = "(\(quote)[#]*)"
while let range = substring.range(of: pattern, options: .regularExpression) {
let count = substring.distance(from: range.lowerBound, to: range.upperBound) - offset
hashCount = max(count, hashCount)
substring = substring[range.upperBound...]
}
return hashCount
}
}