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

add to_string method for StringView #1609

Merged
merged 4 commits into from
Feb 7, 2025
Merged
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
18 changes: 16 additions & 2 deletions string/view.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ pub fn length_ge(self : StringView, len : Int) -> Bool {
/// let str = "Hello🤣🤣🤣"
/// let view = str[1:6]
/// inspect!(view, content=
/// #|"ello🤣"
/// "ello🤣"
///)
/// ```
pub fn String::op_as_view(
Expand Down Expand Up @@ -273,7 +273,7 @@ pub fn String::op_as_view(
/// let view = str[1:7]
/// let view2 = view[1:5]
/// inspect!(view2, content=
/// #|"llo🤣"
/// "llo🤣"
/// )
/// ```
pub fn StringView::op_as_view(
Expand Down Expand Up @@ -426,6 +426,20 @@ pub impl Show for StringView with output(self, logger) {
String::output(substr, logger)
}

///|
/// Returns a new String containing a copy of the characters in this view.
///
/// # Examples
///
/// ```
/// let str = "Hello World"
/// let view = str[0:5] // "Hello"
/// inspect!(view.to_string(), content="Hello")
/// ```
pub impl Show for StringView with to_string(self) {
self.str.substring(start=self.start, end=self.end)
}

///|
/// Returns an iterator over the Unicode characters in the string view.
pub fn StringView::iter(self : StringView) -> Iter[Char] {
Expand Down
280 changes: 46 additions & 234 deletions string/view_test.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -32,127 +32,42 @@ test "index_at with surrogate pairs" {
inspect!(offset, content="StringIndex(6)")
let offset = str.index_at(4)
inspect!(offset, content="None")
inspect!(
str[1:2],
content=
#|"🤣"
,
)
inspect!(
str[1:3],
content=
#|"🤣🤣"
,
)
inspect!(
str[1:-1],
content=
#|"🤣"
,
)
inspect!(
str[1:-2],
content=
#|""
,
)
inspect!(
str[:-1],
content=
#|"🤣🤣"
,
)
inspect!(str[1:2], content="🤣")
inspect!(str[1:3], content="🤣🤣")
inspect!(str[1:-1], content="🤣")
inspect!(str[1:-2], content="")
inspect!(str[:-1], content="🤣🤣")
}

test "stringview basic" {
let str = "Hello🤣🤣🤣"
let start = 1
let end = 6
inspect!(
str[start:],
content=
#|"ello🤣🤣🤣"
,
)
inspect!(
str[:end],
content=
#|"Hello🤣"
,
)
inspect!(
str[start:end],
content=
#|"ello🤣"
,
)
inspect!(
str[:],
content=
#|"Hello🤣🤣🤣"
,
)
inspect!(str[start:], content="ello🤣🤣🤣")
inspect!(str[:end], content="Hello🤣")
inspect!(str[start:end], content="ello🤣")
inspect!(str[:], content="Hello🤣🤣🤣")
}

test "stringview basic2" {
let str = "He🤣🤣🤣llo"
let start = 1
let end = 7
inspect!(
str[start:],
content=
#|"e🤣🤣🤣llo"
,
)
inspect!(
str[:end],
content=
#|"He🤣🤣🤣ll"
,
)
inspect!(
str[start:end],
content=
#|"e🤣🤣🤣ll"
,
)
inspect!(
str[:],
content=
#|"He🤣🤣🤣llo"
,
)
inspect!(str[start:], content="e🤣🤣🤣llo")
inspect!(str[:end], content="He🤣🤣🤣ll")
inspect!(str[start:end], content="e🤣🤣🤣ll")
inspect!(str[:], content="He🤣🤣🤣llo")
}

test "stringview subview" {
let str = "Hello🤣🤣🤣"
let start = 1
let end = 6
let view = str[start:end]
inspect!(
view[1:],
content=
#|"llo🤣"
,
)
inspect!(
view[1:4],
content=
#|"llo"
,
)
inspect!(
view[:4],
content=
#|"ello"
,
)
inspect!(
view[:],
content=
#|"ello🤣"
,
)
inspect!(view[1:], content="llo🤣")
inspect!(view[1:4], content="llo")
inspect!(view[:4], content="ello")
inspect!(view[:], content="ello🤣")
}

test "stringview op_get" {
Expand Down Expand Up @@ -252,103 +167,33 @@ test "stringview negative index" {
let str = "Hello🤣🤣🤣"
let str_view = str[:]
let view = str_view[-1:]
inspect!(
view,
content=
#|"🤣"
,
)
inspect!(view, content="🤣")
let view = str_view[-2:]
inspect!(
view,
content=
#|"🤣🤣"
,
)
inspect!(view, content="🤣🤣")
let view = str_view[-3:]
inspect!(
view,
content=
#|"🤣🤣🤣"
,
)
inspect!(view, content="🤣🤣🤣")
let view = str_view[-4:]
inspect!(
view,
content=
#|"o🤣🤣🤣"
,
)
inspect!(view, content="o🤣🤣🤣")
let view = str_view[:-1]
inspect!(
view,
content=
#|"Hello🤣🤣"
,
)
inspect!(view, content="Hello🤣🤣")
let view = str_view[:-2]
inspect!(
view,
content=
#|"Hello🤣"
,
)
inspect!(view, content="Hello🤣")
let view = str_view[:-3]
inspect!(
view,
content=
#|"Hello"
,
)
inspect!(view, content="Hello")
let view = str_view[:-4]
inspect!(
view,
content=
#|"Hell"
,
)
inspect!(view, content="Hell")
let view = str_view[-2:-1]
inspect!(
view,
content=
#|"🤣"
,
)
inspect!(view, content="🤣")
let view = str_view[-3:-1]
inspect!(
view,
content=
#|"🤣🤣"
,
)
inspect!(view, content="🤣🤣")
let view = str_view[-4:-1]
inspect!(
view,
content=
#|"o🤣🤣"
,
)
inspect!(view, content="o🤣🤣")
let view = str_view[-4:-2]
inspect!(
view,
content=
#|"o🤣"
,
)
inspect!(view, content="o🤣")
let view = str_view[-4:-4]
inspect!(
view,
content=
#|""
,
)
inspect!(view, content="")
let view = str_view[-3:-3]
inspect!(
view,
content=
#|""
,
)
inspect!(view, content="")
}

test "panic negative index 1" {
Expand Down Expand Up @@ -447,54 +292,14 @@ test "stringview iter" {

test "stringview negative index" {
let str = "hello🤣😭😂"
inspect!(
str[-2:-1],
content=
#|"😭"
,
)
inspect!(
str[-3:-1],
content=
#|"🤣😭"
,
)
inspect!(
str[-4:-1],
content=
#|"o🤣😭"
,
)
inspect!(
str[-4:],
content=
#|"o🤣😭😂"
,
)
inspect!(
str[:-2],
content=
#|"hello🤣"
,
)
inspect!(
str[-2:-2],
content=
#|""
,
)
inspect!(
str[-0:0],
content=
#|""
,
)
inspect!(
str[-8:],
content=
#|"hello🤣😭😂"
,
)
inspect!(str[-2:-1], content="😭")
inspect!(str[-3:-1], content="🤣😭")
inspect!(str[-4:-1], content="o🤣😭")
inspect!(str[-4:], content="o🤣😭😂")
inspect!(str[:-2], content="hello🤣")
inspect!(str[-2:-2], content="")
inspect!(str[-0:0], content="")
inspect!(str[-8:], content="hello🤣😭😂")
}

test "panic stringview negative index1" {
Expand All @@ -514,3 +319,10 @@ test "panic stringview negative index3" {
let _ = str[-1:-2]

}

test "to_string" {
let s = "Hello World"
let view = s[0:5]
let ss = "\{view} Moonbit"
inspect!(ss, content="Hello Moonbit")
}