Skip to content

Commit

Permalink
add: a new method String::rev() (#866)
Browse files Browse the repository at this point in the history
* add: a new method String::reverse()

* rename String::reverse() -> String::rev()

---------

Co-authored-by: Yorkin <[email protected]>
  • Loading branch information
iamzhz and Yoorkin authored Aug 19, 2024
1 parent 71de79a commit 0bccc94
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 0 deletions.
11 changes: 11 additions & 0 deletions string/string.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -441,3 +441,14 @@ pub fn to_upper(self : String) -> String {
}
buf.to_string()
}

/// Reverse string
pub fn rev(self : String) -> String {
// TODO: deal with non-ascii characters
let mut result : String = ""
let len = self.length()
for i = len - 1; i >= 0; i = i - 1 {
result = result + self[i].to_string()
}
result
}
1 change: 1 addition & 0 deletions string/string.mbti
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ impl String {
last_index_of(String, String, ~from : Int = ..) -> Int
replace(String, ~old : String, ~new : String) -> String
replace_all(String, ~old : String, ~new : String) -> String
rev(String) -> String
split(String, String) -> Iter[String]
starts_with(String, String) -> Bool
substring(String, ~start : Int = .., ~end : Int = ..) -> String
Expand Down
6 changes: 6 additions & 0 deletions string/string_test.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,12 @@ test "replace_all" {
)
}

test "reverse" {
inspect!("hello".rev(), content="olleh")
inspect!("Nothing".rev(), content="gnihtoN")
inspect!("< >".rev(), content="> <")
}

test "to_lower" {
inspect!("ABCXYZ".to_lower(), content="abcxyz")
inspect!("aBcXyZ".to_lower(), content="abcxyz")
Expand Down

0 comments on commit 0bccc94

Please sign in to comment.