mirror of
https://github.com/saymrwulf/xous-core.git
synced 2026-11-18 08:52:45 +00:00
regex xous_ipc::String to String + cleanup idioms
some idioms are improved with String, this cleans up a bit of code in addition to the search/replace
This commit is contained in:
parent
15722a314a
commit
d2f13d92fe
1 changed files with 13 additions and 17 deletions
|
|
@ -7,7 +7,7 @@ use ime_plugin_api::*;
|
||||||
use log::{error, info};
|
use log::{error, info};
|
||||||
use num_traits::FromPrimitive;
|
use num_traits::FromPrimitive;
|
||||||
use xous::msg_scalar_unpack;
|
use xous::msg_scalar_unpack;
|
||||||
use xous_ipc::{Buffer, String};
|
use xous_ipc::Buffer;
|
||||||
|
|
||||||
fn main() -> ! {
|
fn main() -> ! {
|
||||||
log_server::init_wait().unwrap();
|
log_server::init_wait().unwrap();
|
||||||
|
|
@ -21,19 +21,19 @@ fn main() -> ! {
|
||||||
.expect("can't register server");
|
.expect("can't register server");
|
||||||
log::trace!("registered with NS -- {:?}", ime_sh_sid);
|
log::trace!("registered with NS -- {:?}", ime_sh_sid);
|
||||||
|
|
||||||
let mut history_store: HashMap<[u32; 4], Vec<String<64>>> = HashMap::new();
|
let mut history_store: HashMap<[u32; 4], Vec<String>> = HashMap::new();
|
||||||
let mut active_history: Option<([u32; 4], Vec<String<64>>)> = None;
|
let mut active_history: Option<([u32; 4], Vec<String>)> = None;
|
||||||
let history_max = 4;
|
let history_max = 4;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
use core::fmt::Write as CoreWriter;
|
use core::fmt::Write as CoreWriter;
|
||||||
let mut test1: String::<64> = String::new();
|
let mut test1: String = String::new();
|
||||||
write!(test1, "This〰should overflow the box").unwrap();
|
write!(test1, "This〰should overflow the box").unwrap();
|
||||||
history.push(test1);
|
history.push(test1);
|
||||||
let mut test2: String::<64> = String::new();
|
let mut test2: String = String::new();
|
||||||
write!(test2, "Another string too long").unwrap();
|
write!(test2, "Another string too long").unwrap();
|
||||||
history.push(test2);
|
history.push(test2);
|
||||||
let mut test3: String::<64> = String::new();
|
let mut test3: String = String::new();
|
||||||
write!(test3, "未雨绸缪").unwrap();
|
write!(test3, "未雨绸缪").unwrap();
|
||||||
history.push(test3);
|
history.push(test3);
|
||||||
*/
|
*/
|
||||||
|
|
@ -87,10 +87,10 @@ fn main() -> ! {
|
||||||
Some(Opcode::Picked) => {
|
Some(Opcode::Picked) => {
|
||||||
if let Some((_token, history)) = &mut active_history {
|
if let Some((_token, history)) = &mut active_history {
|
||||||
let buffer = unsafe { Buffer::from_memory_message(msg.body.memory_message().unwrap()) };
|
let buffer = unsafe { Buffer::from_memory_message(msg.body.memory_message().unwrap()) };
|
||||||
let s = buffer.as_flat::<String<4000>, _>().unwrap();
|
let s = buffer.as_flat::<String, _>().unwrap();
|
||||||
// the API allows for large picked feedback, but this implementation only keeps the first
|
// the API allows for large picked feedback, but this implementation only keeps the first
|
||||||
// 64 characters
|
// 64 characters
|
||||||
let mut local_s: String<64> = String::new();
|
let mut local_s: String = String::new();
|
||||||
use core::fmt::Write;
|
use core::fmt::Write;
|
||||||
write!(local_s, "{}", s.as_str()).expect("overflowed history variable");
|
write!(local_s, "{}", s.as_str()).expect("overflowed history variable");
|
||||||
log::trace!("storing history value | {}", s.as_str());
|
log::trace!("storing history value | {}", s.as_str());
|
||||||
|
|
@ -117,21 +117,17 @@ fn main() -> ! {
|
||||||
index = history.len() as u32 - 1;
|
index = history.len() as u32 - 1;
|
||||||
}
|
}
|
||||||
let mut i = 1;
|
let mut i = 1;
|
||||||
for &s in history.iter() {
|
let histlen = history.len();
|
||||||
|
for s in history.into_iter() {
|
||||||
// iterator is from oldest to newest, so do some math to go from newest to
|
// iterator is from oldest to newest, so do some math to go from newest to
|
||||||
// oldest TIL: there is a .rev() feature in
|
// oldest TIL: there is a .rev() feature in
|
||||||
// iterators. Next time maybe use that instead.
|
// iterators. Next time maybe use that instead.
|
||||||
if (history.len() as u32 - i) == index {
|
if (histlen as u32 - i) == index {
|
||||||
// decompose the string into a character-by-character sequence
|
// decompose the string into a character-by-character sequence
|
||||||
// and then stuff byte-by-byte, as fits, into the return array
|
// and then stuff byte-by-byte, as fits, into the return array
|
||||||
prediction.string.clear();
|
prediction.string.clear();
|
||||||
for ch in s.as_str().unwrap().chars() {
|
for ch in s.as_str().chars() {
|
||||||
if let Ok(_) = prediction.string.push(ch) {
|
prediction.string.push(ch);
|
||||||
// it's ok, carry on.
|
|
||||||
} else {
|
|
||||||
// we ran out of space, stop copying
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
prediction.valid = true;
|
prediction.valid = true;
|
||||||
break;
|
break;
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue