1
1
use std:: ops:: Range ;
2
2
3
+ use crate :: wtf8:: { Wtf8 , Wtf8Buf } ;
4
+
3
5
pub type EncodeErrorResult < S , B , E > = Result < ( EncodeReplace < S , B > , usize ) , E > ;
4
6
5
7
pub type DecodeErrorResult < S , B , E > = Result < ( S , Option < B > , usize ) , E > ;
6
8
7
- pub trait StrBuffer : AsRef < str > {
9
+ pub trait StrBuffer : AsRef < Wtf8 > {
8
10
fn is_ascii ( & self ) -> bool {
9
11
self . as_ref ( ) . is_ascii ( )
10
12
}
@@ -63,19 +65,19 @@ fn decode_utf8_compatible<E: ErrorHandler, DecodeF, ErrF>(
63
65
errors : & E ,
64
66
decode : DecodeF ,
65
67
handle_error : ErrF ,
66
- ) -> Result < ( String , usize ) , E :: Error >
68
+ ) -> Result < ( Wtf8Buf , usize ) , E :: Error >
67
69
where
68
70
DecodeF : Fn ( & [ u8 ] ) -> Result < & str , DecodeError < ' _ > > ,
69
71
ErrF : Fn ( & [ u8 ] , Option < usize > ) -> HandleResult < ' _ > ,
70
72
{
71
73
if data. is_empty ( ) {
72
- return Ok ( ( String :: new ( ) , 0 ) ) ;
74
+ return Ok ( ( Wtf8Buf :: new ( ) , 0 ) ) ;
73
75
}
74
76
// we need to coerce the lifetime to that of the function body rather than the
75
77
// anonymous input lifetime, so that we can assign it data borrowed from data_from_err
76
78
let mut data = data;
77
79
let mut data_from_err: E :: BytesBuf ;
78
- let mut out = String :: with_capacity ( data. len ( ) ) ;
80
+ let mut out = Wtf8Buf :: with_capacity ( data. len ( ) ) ;
79
81
let mut remaining_index = 0 ;
80
82
let mut remaining_data = data;
81
83
loop {
98
100
err_idx..err_len. map_or_else ( || data. len ( ) , |len| err_idx + len) ;
99
101
let ( replace, new_data, restart) =
100
102
errors. handle_decode_error ( data, err_range, reason) ?;
101
- out. push_str ( replace. as_ref ( ) ) ;
103
+ out. push_wtf8 ( replace. as_ref ( ) ) ;
102
104
if let Some ( new_data) = new_data {
103
105
data_from_err = new_data;
104
106
data = data_from_err. as_ref ( ) ;
@@ -130,7 +132,7 @@ pub mod utf8 {
130
132
data : & [ u8 ] ,
131
133
errors : & E ,
132
134
final_decode : bool ,
133
- ) -> Result < ( String , usize ) , E :: Error > {
135
+ ) -> Result < ( Wtf8Buf , usize ) , E :: Error > {
134
136
decode_utf8_compatible (
135
137
data,
136
138
errors,
@@ -218,7 +220,7 @@ pub mod latin_1 {
218
220
) ?;
219
221
match replace {
220
222
EncodeReplace :: Str ( s) => {
221
- if s. as_ref ( ) . chars ( ) . any ( |c| ( c as u32 ) > 255 ) {
223
+ if s. as_ref ( ) . code_points ( ) . any ( |c| c . to_u32 ( ) > 255 ) {
222
224
return Err (
223
225
errors. error_encoding ( full_data, char_range, ERR_REASON )
224
226
) ;
@@ -240,10 +242,10 @@ pub mod latin_1 {
240
242
Ok ( out)
241
243
}
242
244
243
- pub fn decode < E : ErrorHandler > ( data : & [ u8 ] , _errors : & E ) -> Result < ( String , usize ) , E :: Error > {
245
+ pub fn decode < E : ErrorHandler > ( data : & [ u8 ] , _errors : & E ) -> Result < ( Wtf8Buf , usize ) , E :: Error > {
244
246
let out: String = data. iter ( ) . map ( |c| * c as char ) . collect ( ) ;
245
247
let out_len = out. len ( ) ;
246
- Ok ( ( out, out_len) )
248
+ Ok ( ( out. into ( ) , out_len) )
247
249
}
248
250
}
249
251
@@ -303,7 +305,7 @@ pub mod ascii {
303
305
Ok ( out)
304
306
}
305
307
306
- pub fn decode < E : ErrorHandler > ( data : & [ u8 ] , errors : & E ) -> Result < ( String , usize ) , E :: Error > {
308
+ pub fn decode < E : ErrorHandler > ( data : & [ u8 ] , errors : & E ) -> Result < ( Wtf8Buf , usize ) , E :: Error > {
307
309
decode_utf8_compatible (
308
310
data,
309
311
errors,
0 commit comments