mirror of
https://github.com/pointfreeco/swift-custom-dump.git
synced 2026-04-07 19:17:35 +00:00
31 lines
962 B
Swift
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
|
|
}
|
|
}
|