55// SPDX-License-Identifier: MIT OR Apache-2.0
66use cxx:: { type_id, ExternType } ;
77use std:: cmp:: Ordering ;
8- use std:: fmt;
8+ use std:: fmt:: { self , Write } ;
99use std:: mem:: MaybeUninit ;
1010
1111use crate :: { CaseSensitivity , QByteArray , QStringList , SplitBehaviorFlags } ;
@@ -141,8 +141,8 @@ mod ffi {
141141 fn operatorCmp ( a : & QString , b : & QString ) -> i8 ;
142142
143143 #[ 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 ] ;
146146
147147 #[ doc( hidden) ]
148148 #[ rust_name = "qstring_arg" ]
@@ -262,14 +262,20 @@ impl fmt::Display for QString {
262262 /// Format the `QString` as a Rust string.
263263 ///
264264 /// 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 ( ( ) )
267273 }
268274}
269275
270276impl fmt:: Debug for QString {
271277 fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
272- f . pad ( & String :: from ( self ) )
278+ String :: from ( self ) . fmt ( f )
273279 }
274280}
275281
@@ -321,7 +327,7 @@ impl From<&QString> for String {
321327 ///
322328 /// Note that this converts from UTF-16 to UTF-8.
323329 fn from ( qstring : & QString ) -> Self {
324- ffi :: qstring_to_rust_string ( qstring)
330+ String :: from_utf16_lossy ( qstring. as_slice ( ) )
325331 }
326332}
327333
@@ -330,7 +336,7 @@ impl From<QString> for String {
330336 ///
331337 /// Note that this converts from UTF-16 to UTF-8.
332338 fn from ( qstring : QString ) -> Self {
333- ffi :: qstring_to_rust_string ( & qstring)
339+ String :: from_utf16_lossy ( qstring. as_slice ( ) )
334340 }
335341}
336342
@@ -342,6 +348,11 @@ impl QString {
342348 ffi:: qstring_arg ( self , a)
343349 }
344350
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+
345356 /// Lexically compares this string with the `other` string.
346357 ///
347358 /// If `cs` is [`CaseSensitivity::CaseSensitive`], the comparison is case-sensitive; otherwise the comparison is case-insensitive.
0 commit comments