5
5
// SPDX-License-Identifier: MIT OR Apache-2.0
6
6
use cxx:: { type_id, ExternType } ;
7
7
use std:: cmp:: Ordering ;
8
- use std:: fmt;
8
+ use std:: fmt:: { self , Write } ;
9
9
use std:: mem:: MaybeUninit ;
10
10
11
11
use crate :: { CaseSensitivity , QByteArray , QStringList , SplitBehaviorFlags } ;
@@ -141,8 +141,8 @@ mod ffi {
141
141
fn operatorCmp ( a : & QString , b : & QString ) -> i8 ;
142
142
143
143
#[ doc( hidden) ]
144
- #[ rust_name = "qstring_to_rust_string " ]
145
- fn qstringToRustString ( string : & QString ) -> String ;
144
+ #[ rust_name = "qstring_as_slice " ]
145
+ fn qstringAsSlice ( string : & QString ) -> & [ u16 ] ;
146
146
147
147
#[ doc( hidden) ]
148
148
#[ rust_name = "qstring_arg" ]
@@ -262,14 +262,20 @@ impl fmt::Display for QString {
262
262
/// Format the `QString` as a Rust string.
263
263
///
264
264
/// Note that this converts from UTF-16 to UTF-8.
265
- fn fmt ( & self , f : & mut std:: fmt:: Formatter ) -> std:: fmt:: Result {
266
- f. pad ( & String :: from ( self ) )
265
+ fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
266
+ if f. width ( ) . is_some ( ) || f. precision ( ) . is_some ( ) {
267
+ return f. pad ( & String :: from ( self ) ) ;
268
+ }
269
+ for c in char:: decode_utf16 ( self . as_slice ( ) . iter ( ) . copied ( ) ) {
270
+ f. write_char ( c. unwrap_or ( char:: REPLACEMENT_CHARACTER ) ) ?;
271
+ }
272
+ Ok ( ( ) )
267
273
}
268
274
}
269
275
270
276
impl fmt:: Debug for QString {
271
277
fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
272
- f . pad ( & String :: from ( self ) )
278
+ String :: from ( self ) . fmt ( f )
273
279
}
274
280
}
275
281
@@ -321,7 +327,7 @@ impl From<&QString> for String {
321
327
///
322
328
/// Note that this converts from UTF-16 to UTF-8.
323
329
fn from ( qstring : & QString ) -> Self {
324
- ffi :: qstring_to_rust_string ( qstring)
330
+ String :: from_utf16_lossy ( qstring. as_slice ( ) )
325
331
}
326
332
}
327
333
@@ -330,7 +336,7 @@ impl From<QString> for String {
330
336
///
331
337
/// Note that this converts from UTF-16 to UTF-8.
332
338
fn from ( qstring : QString ) -> Self {
333
- ffi :: qstring_to_rust_string ( & qstring)
339
+ String :: from_utf16_lossy ( qstring. as_slice ( ) )
334
340
}
335
341
}
336
342
@@ -342,6 +348,11 @@ impl QString {
342
348
ffi:: qstring_arg ( self , a)
343
349
}
344
350
351
+ /// Extracts a slice containing the entire UTF-16 array.
352
+ pub fn as_slice ( & self ) -> & [ u16 ] {
353
+ ffi:: qstring_as_slice ( self )
354
+ }
355
+
345
356
/// Lexically compares this string with the `other` string.
346
357
///
347
358
/// If `cs` is [`CaseSensitivity::CaseSensitive`], the comparison is case-sensitive; otherwise the comparison is case-insensitive.
0 commit comments