Skip to content

add StringView::unsafe_make #1748

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions string/string.mbti
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ impl StringView {
op_as_view(Self, start~ : Int = .., end? : Int) -> Self
op_get(Self, Int) -> Char
rev_get(Self, Int) -> Char
unsafe_make(String, Int, Int) -> Self
}
impl Show for StringView

Expand Down
28 changes: 28 additions & 0 deletions string/view.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,34 @@ pub fn View::op_as_view(self : View, start~ : Int = 0, end? : Int) -> View {
{ str: self.str, start, end }
}

///|
/// Creates a new `StringView` without checking if the indices are valid UTF-16
/// boundaries.
///
/// Parameters:
///
/// * `string` : The source string to create a view from.
/// * `start` : The starting UTF-16 code unit index into the string.
/// * `end` : The ending UTF-16 code unit index into the string (exclusive).
///
/// Returns a new `StringView` instance that provides a view into the specified
/// portion of the string.
///
/// Example:
///
/// ```moonbit
/// test "StringView::unsafe_make" {
/// let str = "Hello🌍"
/// let view = StringView::unsafe_make(str, 0, 5)
/// inspect!(view, content="Hello")
/// }
/// ```
///
/// @alert unsafe "This function does not check if the indices are valid UTF-16 boundaries. Use String::op_as\_view for safe string view creation."
pub fn View::unsafe_make(str : String, start : Int, end : Int) -> StringView {
{ str, start, end }
}

///|
/// Return the character at the given index.
///
Expand Down