RosettaCodeData/Task/Strip-block-comments/Swift/strip-block-comments.swift
2023-07-01 13:44:08 -04:00

36 lines
850 B
Swift

import Foundation
func stripBlocks(from str: String, open: String = "/*", close: String = "*/") -> String {
guard !open.isEmpty && !close.isEmpty else {
return str
}
var ret = str
while let begin = ret.range(of: open), let end = ret[begin.upperBound...].range(of: close) {
ret.replaceSubrange(Range(uncheckedBounds: (begin.lowerBound, end.upperBound)), with: "")
}
return ret
}
let test = """
/**
* Some comments
* longer comments here that we can parse.
*
* Rahoo
*/
function subroutine() {
a = /* inline comment */ b + c ;
}
/*/ <-- tricky comments */
/**
* Another comment.
*/
function something() {
}
"""
print(stripBlocks(from: test))