diff --git a/string/string.mbti b/string/string.mbti index f5ff66cb8..c633f3444 100644 --- a/string/string.mbti +++ b/string/string.mbti @@ -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 diff --git a/string/view.mbt b/string/view.mbt index 04f394162..ab6eef211 100644 --- a/string/view.mbt +++ b/string/view.mbt @@ -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. ///