File tree Expand file tree Collapse file tree 3 files changed +32
-10
lines changed Expand file tree Collapse file tree 3 files changed +32
-10
lines changed Original file line number Diff line number Diff line change @@ -4,3 +4,4 @@ pub mod cell;
4
4
pub mod float_ops;
5
5
pub mod hash;
6
6
pub mod rc;
7
+ pub mod str;
Original file line number Diff line number Diff line change
1
+ pub fn get_chars ( s : & str , range : std:: ops:: Range < usize > ) -> & str {
2
+ let mut chars = s. chars ( ) ;
3
+ for _ in 0 ..range. start {
4
+ let _ = chars. next ( ) ;
5
+ }
6
+ let start = chars. as_str ( ) ;
7
+ for _ in range {
8
+ let _ = chars. next ( ) ;
9
+ }
10
+ let end = chars. as_str ( ) ;
11
+ & start[ ..start. len ( ) - end. len ( ) ]
12
+ }
13
+
14
+ #[ cfg( test) ]
15
+ mod tests {
16
+ use super :: * ;
17
+
18
+ #[ test]
19
+ fn test_get_chars ( ) {
20
+ let s = "0123456789" ;
21
+ assert_eq ! ( get_chars( s, 3 ..7 ) , "3456" ) ;
22
+ assert_eq ! ( get_chars( s, 3 ..7 ) , & s[ 3 ..7 ] ) ;
23
+
24
+ let s = "0유니코드 문자열9" ;
25
+ assert_eq ! ( get_chars( s, 3 ..7 ) , "코드 문" ) ;
26
+
27
+ let s = "0😀😃😄😁😆😅😂🤣9" ;
28
+ assert_eq ! ( get_chars( s, 3 ..7 ) , "😄😁😆😅" ) ;
29
+ }
30
+ }
Original file line number Diff line number Diff line change @@ -1406,16 +1406,7 @@ impl<'s> PyCommonString<'s, char> for str {
1406
1406
}
1407
1407
1408
1408
fn get_chars < ' a > ( & ' a self , range : std:: ops:: Range < usize > ) -> & ' a Self {
1409
- let mut chars = self . chars ( ) ;
1410
- for _ in 0 ..range. start {
1411
- let _ = chars. next ( ) ;
1412
- }
1413
- let start = chars. as_str ( ) ;
1414
- for _ in range {
1415
- let _ = chars. next ( ) ;
1416
- }
1417
- let end = chars. as_str ( ) ;
1418
- & start[ ..start. len ( ) - end. len ( ) ]
1409
+ rustpython_common:: str:: get_chars ( self , range)
1419
1410
}
1420
1411
1421
1412
fn is_empty ( & self ) -> bool {
You can’t perform that action at this time.
0 commit comments