Skip to content

Commit

Permalink
perf!(builtin/string): prevent calculating .length() twice in `subs…
Browse files Browse the repository at this point in the history
…tring()`
  • Loading branch information
rami3l authored and bobzhang committed Dec 26, 2024
1 parent eeff5ad commit 8341d4d
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 7 deletions.
2 changes: 1 addition & 1 deletion builtin/builtin.mbti
Original file line number Diff line number Diff line change
Expand Up @@ -623,7 +623,7 @@ impl String {
op_add(String, String) -> String
op_equal(String, String) -> Bool
op_get(String, Int) -> Char
substring(String, start~ : Int = .., end~ : Int = ..) -> String
substring(String, start~ : Int = .., end? : Int) -> String
to_json(String) -> Json
to_string(String) -> String
}
Expand Down
13 changes: 7 additions & 6 deletions builtin/string.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,13 @@ fn unsafe_substring(str : String, start : Int, end : Int) -> String {
/// ignore(s.substring(start=3, end=2))
/// }
/// ```
pub fn substring(
self : String,
start~ : Int = 0,
end~ : Int = self.length()
) -> String {
guard start >= 0 && start <= end && end <= self.length()
pub fn substring(self : String, start~ : Int = 0, end? : Int) -> String {
let len = self.length()
let end = match end {
Some(end) => end
None => len
}
guard start >= 0 && start <= end && end <= len
unsafe_substring(self, start, end)
}

Expand Down

0 comments on commit 8341d4d

Please sign in to comment.