Debian Codegraph @

src/util/span_substring.rs

raw symbols SCIP
1 use craterust-analyzer cargo ron 0.12.0 crate/::{
2 derust-analyzer cargo ron 0.12.0 de/::{Positionrust-analyzer cargo ron 0.12.0 error/Position#, Spanrust-analyzer cargo ron 0.12.0 error/Span#},
3 errorrust-analyzer cargo ron 0.12.0 error/::SpannedResultrust-analyzer cargo ron 0.12.0 error/SpannedResult#,
4 };
5 use allocrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc crate/::stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/::Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#;
6
7 impl Positionrust-analyzer cargo ron 0.12.0 error/Position# {
8 /// Given a Position and a string, return the 0-indexed grapheme index into the
9 /// string at that position, or [None] if the Position is out of bounds of the string.
10 #[must_use]
11 pub fn grapheme_indexrust-analyzer cargo ron 0.12.0 util/span_substring/impl#[Position]grapheme_index().(&self, s: &str) -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<usize> {
12 use unicode_segmentationrust-analyzer cargo unicode-segmentation 1.12.0 crate/::UnicodeSegmentationrust-analyzer cargo unicode-segmentation 1.12.0 UnicodeSegmentation#;
13 let mut line_no = 1;
14 let mut col_no = 1;
15
16 if (self.linerust-analyzer cargo ron 0.12.0 error/Position#line., self.colrust-analyzer cargo ron 0.12.0 error/Position#col.) rust-analyzer cargo core https://github.com/rust-lang/rust/library/core tuple/impl#[`(U, T)`][`PartialEq<Self>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core tuple/impl#[`(U, T)`][`PartialEq<Self>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core tuple/impl#[`(U, T)`][`PartialEq<Self>`]eq().(1, 1) {
17 return Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(0);
18 }
19
20 let mut i = 0;
21
22 // Slightly non-intuitive arithmetic: a zero-length string at line 1, col 1 -> 0
23
24 if (line_no, col_no) rust-analyzer cargo core https://github.com/rust-lang/rust/library/core tuple/impl#[`(U, T)`][`PartialEq<Self>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core tuple/impl#[`(U, T)`][`PartialEq<Self>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core tuple/impl#[`(U, T)`][`PartialEq<Self>`]eq().(self.linerust-analyzer cargo ron 0.12.0 error/Position#line., self.colrust-analyzer cargo ron 0.12.0 error/Position#col.) {
25 return Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(i);
26 }
27
28 for ch in s.graphemesrust-analyzer cargo unicode-segmentation 1.12.0 impl#[str][UnicodeSegmentation]graphemes().(true) {
29 if (line_no, col_no) rust-analyzer cargo core https://github.com/rust-lang/rust/library/core tuple/impl#[`(U, T)`][`PartialEq<Self>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core tuple/impl#[`(U, T)`][`PartialEq<Self>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core tuple/impl#[`(U, T)`][`PartialEq<Self>`]eq().(self.linerust-analyzer cargo ron 0.12.0 error/Position#line., self.colrust-analyzer cargo ron 0.12.0 error/Position#col.) {
30 return Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(i);
31 }
32
33 // "\n" and "\r\n" each come through the iterator as a single grapheme
34 if matchesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/matches!!(ch, "\n" | "\r\n") {
35 line_no rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign().+=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign().1;
36 col_no = 1;
37 } else {
38 col_no rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign().+=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign().1;
39 }
40
41 i rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign().+=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign().1;
42 }
43
44 // ...and a string of length 7 at line 1, col 8 -> 7
45 if (line_no, col_no) rust-analyzer cargo core https://github.com/rust-lang/rust/library/core tuple/impl#[`(U, T)`][`PartialEq<Self>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core tuple/impl#[`(U, T)`][`PartialEq<Self>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core tuple/impl#[`(U, T)`][`PartialEq<Self>`]eq().(self.linerust-analyzer cargo ron 0.12.0 error/Position#line., self.colrust-analyzer cargo ron 0.12.0 error/Position#col.) {
46 return Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(i);
47 }
48
49 Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#
50 }
51 }
52
53 impl Spanrust-analyzer cargo ron 0.12.0 error/Span# {
54 /// Given a `Span` and a string, form the resulting string selected exclusively (as in `[start..end`]) by the `Span`
55 /// or [`None`] if the span is out of bounds of the string at either end.
56 #[must_use]
57 pub fn substring_exclusiverust-analyzer cargo ron 0.12.0 util/span_substring/impl#[Span]substring_exclusive().(&self, s: &str) -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#> {
58 use allocrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc crate/::vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/::Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#;
59 use unicode_segmentationrust-analyzer cargo unicode-segmentation 1.12.0 crate/::UnicodeSegmentationrust-analyzer cargo unicode-segmentation 1.12.0 UnicodeSegmentation#;
60
61 if let (Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(start), Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(end)) = (self.startrust-analyzer cargo ron 0.12.0 error/Span#start..grapheme_indexrust-analyzer cargo ron 0.12.0 util/span_substring/impl#[Position]grapheme_index().(s), self.endrust-analyzer cargo ron 0.12.0 error/Span#end..grapheme_indexrust-analyzer cargo ron 0.12.0 util/span_substring/impl#[Position]grapheme_index().(s))
62 {
63 Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(s.graphemesrust-analyzer cargo unicode-segmentation 1.12.0 impl#[str][UnicodeSegmentation]graphemes().(true).collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().::<Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<&str>>()[rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Index<I>`]index().start..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#end]rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Index<I>`]index()..concatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc slice/impl#[`[T]`]concat().())
64 } else {
65 Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#
66 }
67 }
68
69 /// Given a `Span` and a string, form the resulting string selected inclusively (as in `[start..=end]`) by the `Span`
70 /// or [`None`] if the span is out of bounds of the string at either end.
71 #[must_use]
72 pub fn substring_inclusiverust-analyzer cargo ron 0.12.0 util/span_substring/impl#[Span]substring_inclusive().(&self, s: &str) -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#> {
73 use allocrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc crate/::vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/::Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#;
74 use unicode_segmentationrust-analyzer cargo unicode-segmentation 1.12.0 crate/::UnicodeSegmentationrust-analyzer cargo unicode-segmentation 1.12.0 UnicodeSegmentation#;
75
76 if let (Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(start), Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(end)) = (self.startrust-analyzer cargo ron 0.12.0 error/Span#start..grapheme_indexrust-analyzer cargo ron 0.12.0 util/span_substring/impl#[Position]grapheme_index().(s), self.endrust-analyzer cargo ron 0.12.0 error/Span#end..grapheme_indexrust-analyzer cargo ron 0.12.0 util/span_substring/impl#[Position]grapheme_index().(s))
77 {
78 Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(s.graphemesrust-analyzer cargo unicode-segmentation 1.12.0 impl#[str][UnicodeSegmentation]graphemes().(true).collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().::<Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<&str>>()[rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Index<I>`]index().start..=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeInclusive#end]rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Index<I>`]index()..concatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc slice/impl#[`[T]`]concat().())
79 } else {
80 Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#
81 }
82 }
83 }
84
85 /// Given a string `ron`, a [`SpannedResult`], and a substring, verify that trying to parse `ron` results in an error
86 /// equal to the [`SpannedResult`] with a Span that exclusively (as in `[start..end]`) selects that substring.
87 /// Note that there are two versions of this helper, inclusive and exclusive. This is because while the parser cursor
88 /// arithmetic that computes span positions always produces exclusive spans (as in `[start..end]`),
89 /// when doing validation against a target substring, the inclusive check including the final grapheme that triggered
90 /// the error is often a more intuitive target to check against.
91 /// Meanwhile, if the parser threw an EOF, for example, there is no final grapheme to check, and so
92 /// only the exclusive check would produce a meaningful result.
93 #[allow(clippy::unwrap_used)]
94 #[allow(clippy::missing_panics_doc)]
95 pub fn check_error_span_exclusiverust-analyzer cargo ron 0.12.0 util/span_substring/check_error_span_exclusive().<T: serderust-analyzer cargo serde 1.0.228 crate/::derust-analyzer cargo serde_core 1.0.228 de/::DeserializeOwnedrust-analyzer cargo serde_core 1.0.228 de/DeserializeOwned# + PartialEqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq# + corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::fmtrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/::Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Debug#>(
96 ron: &str,
97 check: SpannedResultrust-analyzer cargo ron 0.12.0 error/SpannedResult#<T>,
98 substr: &str,
99 ) {
100 let res_str = craterust-analyzer cargo ron 0.12.0 crate/::derust-analyzer cargo ron 0.12.0 de/::from_strrust-analyzer cargo ron 0.12.0 de/from_str().::<T>(ron);
101 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(res_str, check);
102
103 let res_bytes = craterust-analyzer cargo ron 0.12.0 crate/::derust-analyzer cargo ron 0.12.0 de/::from_bytesrust-analyzer cargo ron 0.12.0 de/from_bytes().::<T>(ron.as_bytesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]as_bytes().());
104 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(res_bytes, check);
105
106 #[cfg(feature = "std")]
107 {
108 let res_reader = craterust-analyzer cargo ron 0.12.0 crate/::derust-analyzer cargo ron 0.12.0 de/::from_readerrust-analyzer cargo ron 0.12.0 de/from_reader().::<&[u8], T>(ron.as_bytesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]as_bytes().());
109 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(res_reader, check);
110 }
111
112 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(
113 check.unwrap_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap_err().().spanrust-analyzer cargo ron 0.12.0 error/SpannedError#span..substring_exclusiverust-analyzer cargo ron 0.12.0 util/span_substring/impl#[Span]substring_exclusive().(ron).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().(),
114 substr
115 );
116 }
117
118 /// Given a string `ron`, a [`SpannedResult`], and a substring, verify that trying to parse `ron` results in an error
119 /// equal to the [`SpannedResult`] with a Span that inclusively (as in `[start..=end`]) selects that substring.
120 /// See [`check_error_span_exclusive`] for the rationale behind both versions of this helper.
121 #[allow(clippy::unwrap_used)]
122 #[allow(clippy::missing_panics_doc)]
123 pub fn check_error_span_inclusiverust-analyzer cargo ron 0.12.0 util/span_substring/check_error_span_inclusive().<T: serderust-analyzer cargo serde 1.0.228 crate/::derust-analyzer cargo serde_core 1.0.228 de/::DeserializeOwnedrust-analyzer cargo serde_core 1.0.228 de/DeserializeOwned# + PartialEqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq# + corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::fmtrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/::Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Debug#>(
124 ron: &str,
125 check: SpannedResultrust-analyzer cargo ron 0.12.0 error/SpannedResult#<T>,
126 substr: &str,
127 ) {
128 let res_str = craterust-analyzer cargo ron 0.12.0 crate/::derust-analyzer cargo ron 0.12.0 de/::from_strrust-analyzer cargo ron 0.12.0 de/from_str().::<T>(ron);
129 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(res_str, check);
130
131 let res_bytes = craterust-analyzer cargo ron 0.12.0 crate/::derust-analyzer cargo ron 0.12.0 de/::from_bytesrust-analyzer cargo ron 0.12.0 de/from_bytes().::<T>(ron.as_bytesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]as_bytes().());
132 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(res_bytes, check);
133
134 #[cfg(feature = "std")]
135 {
136 let res_reader = craterust-analyzer cargo ron 0.12.0 crate/::derust-analyzer cargo ron 0.12.0 de/::from_readerrust-analyzer cargo ron 0.12.0 de/from_reader().::<&[u8], T>(ron.as_bytesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]as_bytes().());
137 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(res_reader, check);
138 }
139
140 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(
141 check.unwrap_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap_err().().spanrust-analyzer cargo ron 0.12.0 error/SpannedError#span..substring_inclusiverust-analyzer cargo ron 0.12.0 util/span_substring/impl#[Span]substring_inclusive().(ron).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().(),
142 substr
143 );
144 }
145
146 #[cfg(test)]
147 mod testsrust-analyzer cargo ron 0.12.0 util/span_substring/tests/ {
148 use craterust-analyzer cargo ron 0.12.0 crate/::derust-analyzer cargo ron 0.12.0 de/::{Positionrust-analyzer cargo ron 0.12.0 error/Position#, Spanrust-analyzer cargo ron 0.12.0 error/Span#};
149
150 fn spanrust-analyzer cargo ron 0.12.0 util/span_substring/tests/span().(start: Positionrust-analyzer cargo ron 0.12.0 error/Position#, end: Positionrust-analyzer cargo ron 0.12.0 error/Position#) -> Spanrust-analyzer cargo ron 0.12.0 error/Span# {
151 Spanrust-analyzer cargo ron 0.12.0 error/Span# { startrust-analyzer cargo ron 0.12.0 error/Span#start., endrust-analyzer cargo ron 0.12.0 error/Span#end. }
152 }
153
154 fn posrust-analyzer cargo ron 0.12.0 util/span_substring/tests/pos().(line: usize, col: usize) -> Positionrust-analyzer cargo ron 0.12.0 error/Position# {
155 Positionrust-analyzer cargo ron 0.12.0 error/Position# { linerust-analyzer cargo ron 0.12.0 error/Position#line., colrust-analyzer cargo ron 0.12.0 error/Position#col. }
156 }
157
158 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
159 fn ascii_basicsrust-analyzer cargo ron 0.12.0 util/span_substring/tests/ascii_basics().() {
160 let text = "hello\nworld";
161
162 // first char / first col
163 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(posrust-analyzer cargo ron 0.12.0 util/span_substring/tests/pos().(1, 1).grapheme_indexrust-analyzer cargo ron 0.12.0 util/span_substring/impl#[Position]grapheme_index().(text), Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(0));
164
165 // last char on first line ('o')
166 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(posrust-analyzer cargo ron 0.12.0 util/span_substring/tests/pos().(1, 5).grapheme_indexrust-analyzer cargo ron 0.12.0 util/span_substring/impl#[Position]grapheme_index().(text), Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(4));
167
168 // start of second line ('w')
169 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(posrust-analyzer cargo ron 0.12.0 util/span_substring/tests/pos().(2, 1).grapheme_indexrust-analyzer cargo ron 0.12.0 util/span_substring/impl#[Position]grapheme_index().(text), Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(6));
170
171 // span across the `\n`
172 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(
173 spanrust-analyzer cargo ron 0.12.0 util/span_substring/tests/span().(posrust-analyzer cargo ron 0.12.0 util/span_substring/tests/pos().(1, 4), posrust-analyzer cargo ron 0.12.0 util/span_substring/tests/pos().(2, 2))
174 .substring_exclusiverust-analyzer cargo ron 0.12.0 util/span_substring/impl#[Span]substring_exclusive().(text)
175 .unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().(),
176 "lo\nw"
177 );
178 }
179
180 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
181 fn multibyte_greekrust-analyzer cargo ron 0.12.0 util/span_substring/tests/multibyte_greek().() {
182 let text = "Ξ±Ξ²Ξ³\ndeux\nδΈ‰ε››δΊ”\r\nend";
183
184 // Beta
185 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(posrust-analyzer cargo ron 0.12.0 util/span_substring/tests/pos().(1, 2).grapheme_indexrust-analyzer cargo ron 0.12.0 util/span_substring/impl#[Position]grapheme_index().(text), Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(1));
186
187 // δΈ‰
188 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(posrust-analyzer cargo ron 0.12.0 util/span_substring/tests/pos().(3, 1).grapheme_indexrust-analyzer cargo ron 0.12.0 util/span_substring/impl#[Position]grapheme_index().(text), Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(9));
189
190 // e
191 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(posrust-analyzer cargo ron 0.12.0 util/span_substring/tests/pos().(4, 1).grapheme_indexrust-analyzer cargo ron 0.12.0 util/span_substring/impl#[Position]grapheme_index().(text), Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(13));
192
193 // span from Ξ± to start of β€œdeux”
194 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(
195 spanrust-analyzer cargo ron 0.12.0 util/span_substring/tests/span().(posrust-analyzer cargo ron 0.12.0 util/span_substring/tests/pos().(1, 1), posrust-analyzer cargo ron 0.12.0 util/span_substring/tests/pos().(2, 1))
196 .substring_exclusiverust-analyzer cargo ron 0.12.0 util/span_substring/impl#[Span]substring_exclusive().(text)
197 .unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().(),
198 "Ξ±Ξ²Ξ³\n"
199 );
200 }
201
202 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
203 fn combining_mark_clusterrust-analyzer cargo ron 0.12.0 util/span_substring/tests/combining_mark_cluster().() {
204 // é == [0x65, 0xCC, 0x81] in UTF-8
205 let text = "e\u{0301}x\n";
206
207 // grapheme #1 (β€œé”)
208 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(posrust-analyzer cargo ron 0.12.0 util/span_substring/tests/pos().(1, 1).grapheme_indexrust-analyzer cargo ron 0.12.0 util/span_substring/impl#[Position]grapheme_index().(text), Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(0));
209
210 // grapheme #2 (β€œx”)
211 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(posrust-analyzer cargo ron 0.12.0 util/span_substring/tests/pos().(1, 2).grapheme_indexrust-analyzer cargo ron 0.12.0 util/span_substring/impl#[Position]grapheme_index().(text), Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(1));
212
213 // column 4 is past EOL
214 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(posrust-analyzer cargo ron 0.12.0 util/span_substring/tests/pos().(1, 4).grapheme_indexrust-analyzer cargo ron 0.12.0 util/span_substring/impl#[Position]grapheme_index().(text), Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#);
215
216 // full span
217 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(
218 spanrust-analyzer cargo ron 0.12.0 util/span_substring/tests/span().(posrust-analyzer cargo ron 0.12.0 util/span_substring/tests/pos().(1, 1), posrust-analyzer cargo ron 0.12.0 util/span_substring/tests/pos().(1, 2))
219 .substring_exclusiverust-analyzer cargo ron 0.12.0 util/span_substring/impl#[Span]substring_exclusive().(text)
220 .unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().(),
221 "e\u{0301}"
222 );
223 }
224
225 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
226 fn zwj_emoji_clusterrust-analyzer cargo ron 0.12.0 util/span_substring/tests/zwj_emoji_cluster().() {
227 let text = "πŸ‘©β€πŸ‘©β€πŸ‘§β€πŸ‘§ and πŸ‘¨β€πŸ‘©β€πŸ‘¦";
228
229 // The family emoji is the first grapheme on the line.
230 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(posrust-analyzer cargo ron 0.12.0 util/span_substring/tests/pos().(1, 1).grapheme_indexrust-analyzer cargo ron 0.12.0 util/span_substring/impl#[Position]grapheme_index().(text), Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(0));
231
232 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(posrust-analyzer cargo ron 0.12.0 util/span_substring/tests/pos().(1, 2).grapheme_indexrust-analyzer cargo ron 0.12.0 util/span_substring/impl#[Position]grapheme_index().(text), Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(1));
233
234 // Span selecting only the first emoji
235 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(
236 spanrust-analyzer cargo ron 0.12.0 util/span_substring/tests/span().(posrust-analyzer cargo ron 0.12.0 util/span_substring/tests/pos().(1, 1), posrust-analyzer cargo ron 0.12.0 util/span_substring/tests/pos().(1, 2))
237 .substring_exclusiverust-analyzer cargo ron 0.12.0 util/span_substring/impl#[Span]substring_exclusive().(text)
238 .unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().(),
239 "πŸ‘©β€πŸ‘©β€πŸ‘§β€πŸ‘§"
240 );
241
242 // Span selecting only the second emoji
243 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(
244 spanrust-analyzer cargo ron 0.12.0 util/span_substring/tests/span().(posrust-analyzer cargo ron 0.12.0 util/span_substring/tests/pos().(1, 7), posrust-analyzer cargo ron 0.12.0 util/span_substring/tests/pos().(1, 8))
245 .substring_exclusiverust-analyzer cargo ron 0.12.0 util/span_substring/impl#[Span]substring_exclusive().(text)
246 .unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().(),
247 "πŸ‘¨β€πŸ‘©β€πŸ‘¦"
248 );
249 }
250
251 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
252 fn mixed_newlinesrust-analyzer cargo ron 0.12.0 util/span_substring/tests/mixed_newlines().() {
253 let text = "one\r\ntwo\nthree\r\n";
254
255 // start of β€œtwo” (line numbers are 1-based)
256 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(posrust-analyzer cargo ron 0.12.0 util/span_substring/tests/pos().(2, 1).grapheme_indexrust-analyzer cargo ron 0.12.0 util/span_substring/impl#[Position]grapheme_index().(text), Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(4));
257
258 // β€œthree”
259 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(posrust-analyzer cargo ron 0.12.0 util/span_substring/tests/pos().(3, 1).grapheme_indexrust-analyzer cargo ron 0.12.0 util/span_substring/impl#[Position]grapheme_index().(text), Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(8));
260
261 // span β€œtwo\n”
262 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(
263 spanrust-analyzer cargo ron 0.12.0 util/span_substring/tests/span().(posrust-analyzer cargo ron 0.12.0 util/span_substring/tests/pos().(2, 1), posrust-analyzer cargo ron 0.12.0 util/span_substring/tests/pos().(3, 1))
264 .substring_exclusiverust-analyzer cargo ron 0.12.0 util/span_substring/impl#[Span]substring_exclusive().(text)
265 .unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().(),
266 "two\n"
267 );
268
269 // span β€œtwo\nthree”
270 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(
271 spanrust-analyzer cargo ron 0.12.0 util/span_substring/tests/span().(posrust-analyzer cargo ron 0.12.0 util/span_substring/tests/pos().(2, 1), posrust-analyzer cargo ron 0.12.0 util/span_substring/tests/pos().(3, 6))
272 .substring_exclusiverust-analyzer cargo ron 0.12.0 util/span_substring/impl#[Span]substring_exclusive().(text)
273 .unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().(),
274 "two\nthree"
275 );
276 }
277
278 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
279 fn oob_and_error_pathsrust-analyzer cargo ron 0.12.0 util/span_substring/tests/oob_and_error_paths().() {
280 let text = "short";
281
282 // line past EOF
283 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(posrust-analyzer cargo ron 0.12.0 util/span_substring/tests/pos().(2, 1).grapheme_indexrust-analyzer cargo ron 0.12.0 util/span_substring/impl#[Position]grapheme_index().(text), Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#);
284
285 // column past EOL
286 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(posrust-analyzer cargo ron 0.12.0 util/span_substring/tests/pos().(1, 10).grapheme_indexrust-analyzer cargo ron 0.12.0 util/span_substring/impl#[Position]grapheme_index().(text), Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#);
287
288 // span with either endpoint oob β†’ None
289 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(spanrust-analyzer cargo ron 0.12.0 util/span_substring/tests/span().(posrust-analyzer cargo ron 0.12.0 util/span_substring/tests/pos().(1, 1), posrust-analyzer cargo ron 0.12.0 util/span_substring/tests/pos().(2, 1)).substring_exclusiverust-analyzer cargo ron 0.12.0 util/span_substring/impl#[Span]substring_exclusive().(text), Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#);
290 }
291
292 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
293 fn whole_text_spanrust-analyzer cargo ron 0.12.0 util/span_substring/tests/whole_text_span().() {
294 let text = "αβγ\nδΡ΢";
295 let all = spanrust-analyzer cargo ron 0.12.0 util/span_substring/tests/span().(posrust-analyzer cargo ron 0.12.0 util/span_substring/tests/pos().(1, 1), posrust-analyzer cargo ron 0.12.0 util/span_substring/tests/pos().(2, 4));
296 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(&all.substring_exclusiverust-analyzer cargo ron 0.12.0 util/span_substring/impl#[Span]substring_exclusive().(text).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().(), text);
297 }
298
299 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
300 fn span_substring_helperrust-analyzer cargo ron 0.12.0 util/span_substring/tests/span_substring_helper().() {
301 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(
302 Spanrust-analyzer cargo ron 0.12.0 error/Span# {
303 startrust-analyzer cargo ron 0.12.0 error/Span#start.: Positionrust-analyzer cargo ron 0.12.0 error/Position# { linerust-analyzer cargo ron 0.12.0 error/Position#line.: 1, colrust-analyzer cargo ron 0.12.0 error/Position#col.: 1 },
304 endrust-analyzer cargo ron 0.12.0 error/Span#end.: Positionrust-analyzer cargo ron 0.12.0 error/Position# { linerust-analyzer cargo ron 0.12.0 error/Position#line.: 2, colrust-analyzer cargo ron 0.12.0 error/Position#col.: 1 },
305 }
306 .substring_exclusiverust-analyzer cargo ron 0.12.0 util/span_substring/impl#[Span]substring_exclusive().(
307 "In the first place, there are two sorts of bets, or toh.11 There is the
308 single axial bet in the center between the principals (toh ketengah), and
309 there is the cloud of peripheral ones around the ring between members
310 of the audience (toh kesasi). ",
311 )
312 .unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().(),
313 "In the first place, there are two sorts of bets, or toh.11 There is the\n"
314 );
315
316 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(
317 Spanrust-analyzer cargo ron 0.12.0 error/Span# {
318 startrust-analyzer cargo ron 0.12.0 error/Span#start.: Positionrust-analyzer cargo ron 0.12.0 error/Position# { linerust-analyzer cargo ron 0.12.0 error/Position#line.: 2, colrust-analyzer cargo ron 0.12.0 error/Position#col.: 1 },
319 endrust-analyzer cargo ron 0.12.0 error/Span#end.: Positionrust-analyzer cargo ron 0.12.0 error/Position# { linerust-analyzer cargo ron 0.12.0 error/Position#line.: 3, colrust-analyzer cargo ron 0.12.0 error/Position#col.: 1 },
320 }
321 .substring_exclusiverust-analyzer cargo ron 0.12.0 util/span_substring/impl#[Span]substring_exclusive().(
322 "In the first place, there are two sorts of bets, or toh.11 There is the
323 single axial bet in the center between the principals (toh ketengah), and
324 there is the cloud of peripheral ones around the ring between members
325 of the audience (toh kesasi). ",
326 )
327 .unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().(),
328 "single axial bet in the center between the principals (toh ketengah), and\n"
329 );
330 }
331 }