Skip to content

Commit 5881415

Browse files
authored
Use QString::as_slice for String conversions (#1323)
* cxx-qt-lib: QString::as_slice * fix lints
1 parent 6147bdf commit 5881415

File tree

3 files changed

+27
-15
lines changed

3 files changed

+27
-15
lines changed

crates/cxx-qt-lib/include/core/qstring.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,9 @@ namespace cxxqtlib1 {
2727

2828
QString
2929
qstringInitFromRustString(::rust::Str string);
30-
::rust::String
31-
qstringToRustString(const QString& string);
30+
31+
::rust::Slice<const ::std::uint16_t>
32+
qstringAsSlice(const QString& string);
3233

3334
QString
3435
qstringArg(const QString& string, const QString& a);

crates/cxx-qt-lib/src/core/qstring.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,12 @@ qstringInitFromRustString(::rust::Str string)
4646
return QString::fromUtf8(string.data(), string.size());
4747
}
4848

49-
::rust::String
50-
qstringToRustString(const QString& string)
49+
::rust::Slice<const ::std::uint16_t>
50+
qstringAsSlice(const QString& string)
5151
{
52-
// Note that this changes UTF-16 to UTF-8
53-
const auto byteArray = string.toUtf8();
54-
return ::rust::String(byteArray.constData(), byteArray.size());
52+
return ::rust::Slice<const ::std::uint16_t>(
53+
reinterpret_cast<const std::uint16_t*>(string.data()),
54+
static_cast<::std::size_t>(string.size()));
5555
}
5656

5757
QString

crates/cxx-qt-lib/src/core/qstring.rs

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
// SPDX-License-Identifier: MIT OR Apache-2.0
66
use cxx::{type_id, ExternType};
77
use std::cmp::Ordering;
8-
use std::fmt;
8+
use std::fmt::{self, Write};
99
use std::mem::MaybeUninit;
1010

1111
use 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

270276
impl 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

Comments
 (0)