@@ -1272,6 +1272,19 @@ fn char_is_printable(c: char) -> bool {
1272
1272
!( cat. is_other ( ) || cat. is_separator ( ) )
1273
1273
}
1274
1274
1275
+ fn str_get_chars ( s : & str , range : std:: ops:: Range < usize > ) -> & str {
1276
+ let mut chars = s. chars ( ) ;
1277
+ for _ in 0 ..range. start {
1278
+ let _ = chars. next ( ) ;
1279
+ }
1280
+ let start = chars. as_str ( ) ;
1281
+ for _ in range {
1282
+ let _ = chars. next ( ) ;
1283
+ }
1284
+ let end = chars. as_str ( ) ;
1285
+ & start[ ..start. len ( ) - end. len ( ) ]
1286
+ }
1287
+
1275
1288
#[ cfg( test) ]
1276
1289
mod tests {
1277
1290
use super :: * ;
@@ -1350,6 +1363,19 @@ mod tests {
1350
1363
"TypeError" . to_owned( )
1351
1364
) ;
1352
1365
}
1366
+
1367
+ #[ test]
1368
+ fn test_get_chars ( ) {
1369
+ let s = "0123456789" ;
1370
+ assert_eq ! ( str_get_chars( s, 3 ..7 ) , "3456" ) ;
1371
+ assert_eq ! ( str_get_chars( s, 3 ..7 ) , & s[ 3 ..7 ] ) ;
1372
+
1373
+ let s = "0유니코드 문자열9" ;
1374
+ assert_eq ! ( str_get_chars( s, 3 ..7 ) , "코드 문" ) ;
1375
+
1376
+ let s = "0😀😃😄😁😆😅😂🤣9" ;
1377
+ assert_eq ! ( str_get_chars( s, 3 ..7 ) , "😄😁😆😅" ) ;
1378
+ }
1353
1379
}
1354
1380
1355
1381
impl PyCommonStringWrapper < str > for PyStringRef {
@@ -1406,16 +1432,7 @@ impl<'s> PyCommonString<'s, char> for str {
1406
1432
}
1407
1433
1408
1434
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 ( ) ]
1435
+ str_get_chars ( self , range)
1419
1436
}
1420
1437
1421
1438
fn is_empty ( & self ) -> bool {
0 commit comments