Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add String::concat_view #1249

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 21 additions & 5 deletions string/string.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,16 @@ pub fn String::from_array(chars : Array[Char]) -> String {
}

///|
/// Concatenate strings.
/// Concatenate strings by a string array view.
///
/// ```
/// let s = @string.concat(["Hello", ", ", "world!"])
/// let s = @string.concat_view(["Hello", ", ", "world!"][:])
/// assert_eq!(s, "Hello, world!")
/// let s = @string.concat(["a", "b", "c"], separator=",")
/// let s = @string.concat_view(["a", "b", "c"][:], separator=",")
/// assert_eq!(s, "a,b,c")
/// ```
pub fn String::concat(
strings : Array[String],
pub fn String::concat_view(
strings : ArrayView[String],
separator~ : String = ""
) -> String {
let buf = StringBuilder::new()
Expand All @@ -64,6 +64,22 @@ pub fn String::concat(
buf.to_string()
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what's the rationale to make it a plain function istead of a method?


///|
/// Concatenate strings by a string array.
///
/// ```
/// let s = @string.concat(["Hello", ", ", "world!"])
/// assert_eq!(s, "Hello, world!")
/// let s = @string.concat(["a", "b", "c"], separator=",")
/// assert_eq!(s, "a,b,c")
/// ```
pub fn String::concat(
strings : Array[String],
separator~ : String = ""
) -> String {
String::concat_view(strings[:], separator~)
}

///|
/// Compare two strings.
/// String with longer length is bigger.
Expand Down
1 change: 1 addition & 0 deletions string/string.mbti
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package moonbitlang/core/string
impl String {
compare(String, String) -> Int
concat(Array[String], separator~ : String = ..) -> String
concat_view(ArrayView[String], separator~ : String = ..) -> String
contains(String, String) -> Bool
contains_char(String, Char) -> Bool
default() -> String
Expand Down
9 changes: 9 additions & 0 deletions string/string_test.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,15 @@ test "String::concat" {
inspect!(String::concat([], separator=" "), content="")
}

test "String::concat_view" {
inspect!(String::concat_view(["123", "456"][:]), content="123456")
inspect!(
String::concat_view(["aaa", "bbb", "ccc"][1:3], separator=" "),
content="bbb ccc",
)
inspect!(String::concat_view([][:], separator=" "), content="")
}

test "default" {
inspect!(@string.default())
}
Expand Down