Skip to content

Commit d2c2066

Browse files
Zhenming-Linpeter-jerry-ye
authored andcommitted
Add data and start_offset methods for StringView
1 parent babd609 commit d2c2066

File tree

3 files changed

+33
-0
lines changed

3 files changed

+33
-0
lines changed

string/string.mbti

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ fn contains(String, StringView) -> Bool
88

99
fn contains_char(String, Char) -> Bool
1010

11+
fn data(StringView) -> String
12+
1113
fn default() -> String
1214

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

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

64+
fn start_offset(StringView) -> Int
65+
6266
#deprecated
6367
fn starts_with(String, String) -> Bool
6468

@@ -92,6 +96,7 @@ fn StringView::charcode_at(Self, Int) -> Int
9296
fn StringView::charcodes(Self, start~ : Int = .., end~ : Int = ..) -> Self
9397
fn StringView::contains(Self, Self) -> Bool
9498
fn StringView::contains_char(Self, Char) -> Bool
99+
fn StringView::data(Self) -> String
95100
fn StringView::find(Self, Self) -> Int?
96101
fn StringView::find_by(Self, (Char) -> Bool) -> Int?
97102
fn[A] StringView::fold(Self, init~ : A, (A, Char) -> A) -> A
@@ -119,6 +124,7 @@ fn StringView::rev_find(Self, Self) -> Int?
119124
fn[A] StringView::rev_fold(Self, init~ : A, (A, Char) -> A) -> A
120125
fn StringView::rev_iter(Self) -> Iter[Char]
121126
fn StringView::split(Self, Self) -> Iter[Self]
127+
fn StringView::start_offset(Self) -> Int
122128
fn StringView::to_lower(Self) -> Self
123129
fn StringView::to_upper(Self) -> Self
124130
fn StringView::trim(Self, Self) -> Self

string/view.mbt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,19 @@ pub fn View::op_get(self : View, index : Int) -> Int {
5858
self.str.unsafe_charcode_at(self.start + index)
5959
}
6060
61+
///|
62+
/// Returns the original string that is being viewed.
63+
pub fn data(self : View) -> String {
64+
self.str
65+
}
66+
67+
///|
68+
/// Returns the starting offset (in UTF-16 code units) of this view into its
69+
/// underlying string.
70+
pub fn start_offset(self : View) -> Int {
71+
self.start
72+
}
73+
6174
///|
6275
/// Returns the length of the view.
6376
///

string/view_test.mbt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,20 @@ test "stringview rev_get" {
6565
inspect(view.rev_iter().nth(4).unwrap(), content="e")
6666
}
6767

68+
///|
69+
test "stringview data" {
70+
let str = "Hello🤣🤣🤣"
71+
let view = str.view(start_offset=1, end_offset=7)
72+
inspect!(view.data(), content="Hello🤣🤣🤣")
73+
}
74+
75+
///|
76+
test "stringview start_offset" {
77+
let str = "Hello🤣🤣🤣"
78+
let view = str.view(start_offset=1, end_offset=7)
79+
inspect!(view.start_offset(), content="1")
80+
}
81+
6882
///|
6983
test "stringview length" {
7084
let str = "Hello🤣🤣🤣"

0 commit comments

Comments
 (0)