Skip to content

Commit 5937256

Browse files
committed
add StringView::unsafe_make
1 parent 320c854 commit 5937256

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

string/string.mbti

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ impl StringView {
2323
op_as_view(Self, start~ : Int = .., end? : Int) -> Self
2424
op_get(Self, Int) -> Char
2525
rev_get(Self, Int) -> Char
26+
unsafe_make(String, Int, Int) -> Self
2627
}
2728
impl Show for StringView
2829

string/view.mbt

+33
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,39 @@ pub fn View::op_as_view(self : View, start~ : Int = 0, end? : Int) -> View {
292292
{ str: self.str, start, end }
293293
}
294294
295+
///|
296+
/// Creates a new `StringView` without checking if the indices are valid UTF-16
297+
/// boundaries.
298+
///
299+
/// Parameters:
300+
///
301+
/// * `string` : The source string to create a view from.
302+
/// * `start` : The starting UTF-16 code unit index into the string.
303+
/// * `end` : The ending UTF-16 code unit index into the string (exclusive).
304+
///
305+
/// Returns a new `StringView` instance that provides a view into the specified
306+
/// portion of the string.
307+
///
308+
/// Example:
309+
///
310+
/// ```moonbit
311+
/// ///|
312+
/// test "StringView::unsafe_make" {
313+
/// let str = "Hello🌍"
314+
/// let view = StringView::unsafe_make(str, 0, 5)
315+
/// inspect!(view, content="Hello")
316+
/// }
317+
/// ```
318+
///
319+
/// @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."
320+
pub fn View::unsafe_make(
321+
str : String,
322+
start : Int,
323+
end : Int
324+
) -> StringView {
325+
{ str, start, end }
326+
}
327+
295328
///|
296329
/// Return the character at the given index.
297330
///

0 commit comments

Comments
 (0)