Skip to content

Add data and start_offset methods for StringView #2090

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

Merged
merged 1 commit into from
Jun 5, 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
6 changes: 6 additions & 0 deletions string/string.mbti
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ fn contains(String, StringView) -> Bool

fn contains_char(String, Char) -> Bool

fn data(StringView) -> String

fn default() -> String

#deprecated
Expand Down Expand Up @@ -59,6 +61,8 @@ fn rev_iter(String) -> Iter[Char]

fn split(String, StringView) -> Iter[StringView]

fn start_offset(StringView) -> Int

#deprecated
fn starts_with(String, String) -> Bool

Expand Down Expand Up @@ -92,6 +96,7 @@ fn StringView::charcode_at(Self, Int) -> Int
fn StringView::charcodes(Self, start~ : Int = .., end~ : Int = ..) -> Self
fn StringView::contains(Self, Self) -> Bool
fn StringView::contains_char(Self, Char) -> Bool
fn StringView::data(Self) -> String
fn StringView::find(Self, Self) -> Int?
fn StringView::find_by(Self, (Char) -> Bool) -> Int?
fn[A] StringView::fold(Self, init~ : A, (A, Char) -> A) -> A
Expand Down Expand Up @@ -119,6 +124,7 @@ fn StringView::rev_find(Self, Self) -> Int?
fn[A] StringView::rev_fold(Self, init~ : A, (A, Char) -> A) -> A
fn StringView::rev_iter(Self) -> Iter[Char]
fn StringView::split(Self, Self) -> Iter[Self]
fn StringView::start_offset(Self) -> Int
fn StringView::to_lower(Self) -> Self
fn StringView::to_upper(Self) -> Self
fn StringView::trim(Self, Self) -> Self
Expand Down
13 changes: 13 additions & 0 deletions string/view.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,19 @@ pub fn View::op_get(self : View, index : Int) -> Int {
self.str.unsafe_charcode_at(self.start + index)
}

///|
/// Returns the original string that is being viewed.
pub fn data(self : View) -> String {
self.str
}

///|
/// Returns the starting offset (in UTF-16 code units) of this view into its
/// underlying string.
pub fn start_offset(self : View) -> Int {
self.start
}

///|
/// Returns the length of the view.
///
Expand Down
14 changes: 14 additions & 0 deletions string/view_test.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,20 @@ test "stringview rev_get" {
inspect(view.rev_iter().nth(4).unwrap(), content="e")
}

///|
test "stringview data" {
let str = "Hello🤣🤣🤣"
let view = str.view(start_offset=1, end_offset=7)
inspect!(view.data(), content="Hello🤣🤣🤣")
}

///|
test "stringview start_offset" {
let str = "Hello🤣🤣🤣"
let view = str.view(start_offset=1, end_offset=7)
inspect!(view.start_offset(), content="1")
}

///|
test "stringview length" {
let str = "Hello🤣🤣🤣"
Expand Down
Loading