Strip block comments: Difference between revisions

Add Swift
m (→‎{{header|Nim}}: Pass delim argument to recursive call)
(Add Swift)
Line 2,265:
 
say strip_block_comments(ARGF.slurp);</lang>
 
=={{header|Swift}}==
 
<lang 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))</lang>
 
{{out}}
 
<pre>
function subroutine() {
a = b + c ;
}
 
 
 
function something() {
}
 
</pre>
 
=={{header|Tcl}}==