Skip to content

Commit

Permalink
Add better countLeadingSpacesUpTo
Browse files Browse the repository at this point in the history
  • Loading branch information
tom-ludwig committed Oct 10, 2024
1 parent 63e644a commit 57daf76
Showing 1 changed file with 15 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,20 @@ extension TextViewController {
}
}

private func countLeadingSpacesUpTo(line: String, maxCount: Int) -> Int {
// Count leading spaces using prefix and `filter`
return line.prefix(maxCount).filter { $0 == " " }.count
func countLeadingSpacesUpTo(line: String, maxCount: Int) -> Int {
var count = 0
for char in line {
if char == " " {
count += 1
} else {
break // Stop as soon as a non-space character is encountered
}
// Stop early if we've counted the max number of spaces
if count == maxCount {
break
}
}

return count
}
}

0 comments on commit 57daf76

Please sign in to comment.