Adapt quote_bytes test so it return QByteArray which was not yet tested

master
Jos van den Oever 2018-08-05 19:52:17 +02:00
parent 73b9c89b30
commit 5b458fddde
5 changed files with 23 additions and 10 deletions

View File

@ -50,10 +50,10 @@ impl PersonTrait for Person {
fn quote(&self, prefix: String, suffix: String) -> String {
format!("{}{}{}", prefix, self.user_name, suffix)
}
fn quote_bytes(&self, prefix: &[u8], suffix: &[u8]) -> String {
fn quote_bytes(&self, prefix: &[u8], suffix: &[u8]) -> Vec<u8> {
let prefix = String::from_utf8_lossy(prefix);
let suffix = String::from_utf8_lossy(suffix);
format!("{}{}{}", prefix, self.user_name, suffix)
format!("{}{}{}", prefix, self.user_name, suffix).into()
}
}

View File

@ -24,6 +24,9 @@ fn set_string_from_utf16(s: &mut String, str: *const c_ushort, len: c_int) {
pub enum QByteArray {}
fn to_usize(n: c_int) -> usize {
if n < 0 {
panic!("Cannot cast {} to usize", n);
@ -71,7 +74,7 @@ pub trait PersonTrait {
fn double_name(&mut self) -> ();
fn greet(&self, name: String) -> String;
fn quote(&self, prefix: String, suffix: String) -> String;
fn quote_bytes(&self, prefix: &[u8], suffix: &[u8]) -> String;
fn quote_bytes(&self, prefix: &[u8], suffix: &[u8]) -> Vec<u8>;
fn vowels_in_name(&self) -> u8;
}
@ -152,7 +155,7 @@ pub extern "C" fn person_quote(ptr: *const Person, prefix_str: *const c_ushort,
}
#[no_mangle]
pub extern "C" fn person_quote_bytes(ptr: *const Person, prefix_str: *const c_char, prefix_len: c_int, suffix_str: *const c_char, suffix_len: c_int, d: *mut QString, set: fn(*mut QString, str: *const c_char, len: c_int)) {
pub extern "C" fn person_quote_bytes(ptr: *const Person, prefix_str: *const c_char, prefix_len: c_int, suffix_str: *const c_char, suffix_len: c_int, d: *mut QByteArray, set: fn(*mut QByteArray, str: *const c_char, len: c_int)) {
let prefix = unsafe { slice::from_raw_parts(prefix_str as *const u8, to_usize(prefix_len)) };
let suffix = unsafe { slice::from_raw_parts(suffix_str as *const u8, to_usize(suffix_len)) };
let o = unsafe { &*ptr };

View File

@ -61,7 +61,7 @@
]
},
"quoteBytes": {
"return": "QString",
"return": "QByteArray",
"arguments": [
{
"name": "prefix",

View File

@ -7,6 +7,16 @@ namespace {
void set_qstring(QString* val, const char* utf8, int nbytes) {
*val = QString::fromUtf8(utf8, nbytes);
}
typedef void (*qbytearray_set)(QByteArray* val, const char* bytes, int nbytes);
void set_qbytearray(QByteArray* v, const char* bytes, int nbytes) {
if (v->isNull() && nbytes == 0) {
*v = QByteArray(bytes, nbytes);
} else {
v->truncate(0);
v->append(bytes, nbytes);
}
}
inline void personUserNameChanged(Person* o)
{
emit o->userNameChanged();
@ -21,7 +31,7 @@ extern "C" {
void person_double_name(Person::Private*);
void person_greet(const Person::Private*, const ushort*, int, QString*, qstring_set);
void person_quote(const Person::Private*, const ushort*, int, const ushort*, int, QString*, qstring_set);
void person_quote_bytes(const Person::Private*, const char*, int, const char*, int, QString*, qstring_set);
void person_quote_bytes(const Person::Private*, const char*, int, const char*, int, QByteArray*, qbytearray_set);
quint8 person_vowels_in_name(const Person::Private*);
};
@ -74,10 +84,10 @@ QString Person::quote(const QString& prefix, const QString& suffix) const
person_quote(m_d, prefix.utf16(), prefix.size(), suffix.utf16(), suffix.size(), &s, set_qstring);
return s;
}
QString Person::quoteBytes(const QByteArray& prefix, const QByteArray& suffix) const
QByteArray Person::quoteBytes(const QByteArray& prefix, const QByteArray& suffix) const
{
QString s;
person_quote_bytes(m_d, prefix.data(), prefix.size(), suffix.data(), suffix.size(), &s, set_qstring);
QByteArray s;
person_quote_bytes(m_d, prefix.data(), prefix.size(), suffix.data(), suffix.size(), &s, set_qbytearray);
return s;
}
quint8 Person::vowelsInName() const

View File

@ -26,7 +26,7 @@ public:
Q_INVOKABLE void doubleName();
Q_INVOKABLE QString greet(const QString& name) const;
Q_INVOKABLE QString quote(const QString& prefix, const QString& suffix) const;
Q_INVOKABLE QString quoteBytes(const QByteArray& prefix, const QByteArray& suffix) const;
Q_INVOKABLE QByteArray quoteBytes(const QByteArray& prefix, const QByteArray& suffix) const;
Q_INVOKABLE quint8 vowelsInName() const;
signals:
void userNameChanged();