@@ -20,18 +20,18 @@ use core::ops::{self, Range, RangeBounds};
20
20
use core:: ops:: { Add , AddAssign } ;
21
21
use core:: ptr;
22
22
use core:: slice;
23
- use core:: str:: pattern:: Pattern ;
23
+ use core:: str:: pattern:: { Pattern , Utf8Pattern } ;
24
24
25
25
use crate :: alloc:: { Allocator , Global } ;
26
26
#[ cfg( not( no_global_oom_handling) ) ]
27
27
use crate :: borrow:: { Cow , ToOwned } ;
28
28
use crate :: boxed:: Box ;
29
29
use crate :: collections:: TryReserveError ;
30
- use crate :: str:: { self , from_utf8_unchecked_mut, Chars , Utf8Error } ;
30
+ use crate :: str:: { self , from_utf8_unchecked_mut, CharIndices , Chars , Utf8Error } ;
31
31
#[ cfg( not( no_global_oom_handling) ) ]
32
32
use crate :: str:: { from_boxed_utf8_unchecked, FromStr } ;
33
33
use crate :: string:: ToString ;
34
- use crate :: vec:: Vec ;
34
+ use crate :: vec:: { self , Vec } ;
35
35
36
36
/// A UTF-8–encoded, growable string, with allocator support.
37
37
///
@@ -491,6 +491,37 @@ impl<A: Allocator> String<A> {
491
491
pub fn allocator ( & self ) -> & A {
492
492
self . vec . allocator ( )
493
493
}
494
+
495
+ fn from_utf8_lossy_in ( v : & [ u8 ] , alloc : A ) -> Result < & str , String < A > > {
496
+ let mut iter = v. utf8_chunks ( ) ;
497
+
498
+ let first_valid = if let Some ( chunk) = iter. next ( ) {
499
+ let valid = chunk. valid ( ) ;
500
+ if chunk. invalid ( ) . is_empty ( ) {
501
+ debug_assert_eq ! ( valid. len( ) , v. len( ) ) ;
502
+ return Ok ( valid) ;
503
+ }
504
+ valid
505
+ } else {
506
+ return Ok ( "" ) ;
507
+ } ;
508
+
509
+ const REPLACEMENT : & str = "\u{FFFD} " ;
510
+
511
+ let mut res = String :: with_capacity_in ( v. len ( ) , alloc) ;
512
+ res. push_str ( first_valid) ;
513
+ res. push_str ( REPLACEMENT ) ;
514
+
515
+ for chunk in iter {
516
+ res. push_str ( chunk. valid ( ) ) ;
517
+ if !chunk. invalid ( ) . is_empty ( ) {
518
+ res. push_str ( REPLACEMENT ) ;
519
+ }
520
+ }
521
+
522
+ Err ( res)
523
+ }
524
+
494
525
}
495
526
496
527
impl String {
@@ -547,42 +578,12 @@ impl String {
547
578
#[ cfg( not( no_global_oom_handling) ) ]
548
579
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
549
580
pub fn from_utf8_lossy ( v : & [ u8 ] ) -> Cow < ' _ , str > {
550
- match String :: from_utf8_lossy_in ( Global ) {
581
+ match String :: from_utf8_lossy_in ( v , Global ) {
551
582
Ok ( s) => Cow :: Borrowed ( s) ,
552
583
Err ( s) => Cow :: Owned ( s) ,
553
584
}
554
585
}
555
586
556
- fn from_utf8_lossy_in ( v : & [ u8 ] , alloc : A ) -> Result < & str , String < A > > {
557
- let mut iter = v. utf8_chunks ( ) ;
558
-
559
- let first_valid = if let Some ( chunk) = iter. next ( ) {
560
- let valid = chunk. valid ( ) ;
561
- if chunk. invalid ( ) . is_empty ( ) {
562
- debug_assert_eq ! ( valid. len( ) , v. len( ) ) ;
563
- return Ok ( valid) ;
564
- }
565
- valid
566
- } else {
567
- return Ok ( "" ) ;
568
- } ;
569
-
570
- const REPLACEMENT : & str = "\u{FFFD} " ;
571
-
572
- let mut res = String :: with_capacity_in ( v. len ( ) , alloc) ;
573
- res. push_str ( first_valid) ;
574
- res. push_str ( REPLACEMENT ) ;
575
-
576
- for chunk in iter {
577
- res. push_str ( chunk. valid ( ) ) ;
578
- if !chunk. invalid ( ) . is_empty ( ) {
579
- res. push_str ( REPLACEMENT ) ;
580
- }
581
- }
582
-
583
- Err ( res)
584
- }
585
-
586
587
/// Converts a [`Vec<u8>`] to a `String`, substituting invalid UTF-8
587
588
/// sequences with replacement characters.
588
589
///
0 commit comments