oops broke text

This commit is contained in:
Jack Mac 2021-10-19 19:16:07 -04:00
parent daea06108f
commit 6faa7f090a
2 changed files with 59 additions and 36 deletions

View File

@ -0,0 +1,27 @@
use imgui::*;
mod support;
fn main() {
let system = support::init(file!());
let mut stable_str = String::new();
system.main_loop(move |_, ui| {
if let Some(_window) = ui
.window("Input text callbacks")
.size([500.0, 300.0], Condition::FirstUseEver)
.begin()
{
if ui.input_text("input stable", &mut stable_str).build() {
dbg!(&stable_str);
}
let mut per_frame_buf = String::new();
ui.input_text("input per frame", &mut per_frame_buf).build();
if ui.is_item_deactivated_after_edit() {
dbg!(&per_frame_buf);
}
}
});
}

View File

@ -320,26 +320,24 @@ where
}
};
if o {
let cap = self.buf.capacity();
let cap = self.buf.capacity();
// SAFETY: this slice is simply a view into the underlying buffer
// of a String. We MAY be holding onto a view of uninitialized memory,
// however, since we're holding this as a u8 slice, I think it should be
// alright...
// additionally, we can go over the bytes directly, rather than char indices,
// because NUL will never appear in any UTF8 outside the NUL character (ie, within
// a char).
let buf = unsafe { std::slice::from_raw_parts(self.buf.as_ptr(), cap) };
if let Some(len) = buf.iter().position(|x| *x == b'\0') {
// `len` is the position of the first `\0` byte in the String
unsafe {
self.buf.as_mut_vec().set_len(len);
}
} else {
// There is no null terminator, the best we can do is to not
// update the string length.
// SAFETY: this slice is simply a view into the underlying buffer
// of a String. We MAY be holding onto a view of uninitialized memory,
// however, since we're holding this as a u8 slice, I think it should be
// alright...
// additionally, we can go over the bytes directly, rather than char indices,
// because NUL will never appear in any UTF8 outside the NUL character (ie, within
// a char).
let buf = unsafe { std::slice::from_raw_parts(self.buf.as_ptr(), cap) };
if let Some(len) = buf.iter().position(|x| *x == b'\0') {
// `len` is the position of the first `\0` byte in the String
unsafe {
self.buf.as_mut_vec().set_len(len);
}
} else {
// There is no null terminator, the best we can do is to not
// update the string length.
}
o
@ -468,26 +466,24 @@ impl<'ui, 'p, T: InputTextCallbackHandler, L: AsRef<str>> InputTextMultiline<'ui
)
};
if o {
let cap = self.buf.capacity();
let cap = self.buf.capacity();
// SAFETY: this slice is simply a view into the underlying buffer
// of a String. We MAY be holding onto a view of uninitialized memory,
// however, since we're holding this as a u8 slice, I think it should be
// alright...
// additionally, we can go over the bytes directly, rather than char indices,
// because NUL will never appear in any UTF8 outside the NUL character (ie, within
// a char).
let buf = unsafe { std::slice::from_raw_parts(self.buf.as_ptr(), cap) };
if let Some(len) = buf.iter().position(|x| *x == b'\0') {
// `len` is the position of the first `\0` byte in the String
unsafe {
self.buf.as_mut_vec().set_len(len);
}
} else {
// There is no null terminator, the best we can do is to not
// update the string length.
// SAFETY: this slice is simply a view into the underlying buffer
// of a String. We MAY be holding onto a view of uninitialized memory,
// however, since we're holding this as a u8 slice, I think it should be
// alright...
// additionally, we can go over the bytes directly, rather than char indices,
// because NUL will never appear in any UTF8 outside the NUL character (ie, within
// a char).
let buf = unsafe { std::slice::from_raw_parts(self.buf.as_ptr(), cap) };
if let Some(len) = buf.iter().position(|x| *x == b'\0') {
// `len` is the position of the first `\0` byte in the String
unsafe {
self.buf.as_mut_vec().set_len(len);
}
} else {
// There is no null terminator, the best we can do is to not
// update the string length.
}
o