chore(core/rust): use format_i64 to convert numbers to strings

pull/3011/head
grdddj 1 year ago committed by Martin Milata
parent 3bf3e4c38b
commit e00af4f7f3

@ -1,13 +1,15 @@
use crate::ui::{
component::{
base::ComponentExt,
paginated::Paginate,
text::paragraphs::{Paragraph, ParagraphStrType, Paragraphs},
Child, Component, Event, EventCtx, Pad,
use crate::{
strutil,
ui::{
component::{
base::ComponentExt,
paginated::Paginate,
text::paragraphs::{Paragraph, ParagraphStrType, Paragraphs},
Child, Component, Event, EventCtx, Pad,
},
display::{self, Font},
geometry::{Grid, Insets, Offset, Rect},
},
display::{self, Font},
geometry::{Grid, Insets, Offset, Rect},
util,
};
use super::{theme, Button, ButtonMsg};
@ -210,7 +212,7 @@ impl Component for NumberInput {
fn paint(&mut self) {
let mut buf = [0u8; 10];
if let Some(text) = util::u32_to_str(self.value, &mut buf) {
if let Some(text) = strutil::format_i64(self.value as i64, &mut buf) {
let digit_font = Font::DEMIBOLD;
let y_offset = digit_font.text_height() / 2 + Button::<&str>::BASELINE_OFFSET;
display::rect_fill(self.area, theme::BG);

@ -20,26 +20,6 @@ impl<T, E> ResultExt for Result<T, E> {
}
}
pub fn u32_to_str(num: u32, buffer: &mut [u8]) -> Option<&str> {
let mut i = 0;
let mut num = num;
while num > 0 && i < buffer.len() {
buffer[i] = b'0' + ((num % 10) as u8);
num /= 10;
i += 1;
}
match i {
0 => Some("0"),
_ if num > 0 => None,
_ => {
let result = &mut buffer[..i];
result.reverse();
Some(core::str::from_utf8(result).unwrap())
}
}
}
/// Constructs a string from a C string.
///
/// # Safety
@ -140,7 +120,7 @@ pub fn icon_text_center(
#[cfg(test)]
mod tests {
use super::*;
use crate::strutil;
#[test]
fn u32_to_str_valid() {
@ -148,7 +128,7 @@ mod tests {
let mut b = [0; 10];
for test in testcases {
let converted = u32_to_str(test, &mut b).unwrap();
let converted = strutil::format_i64(test as i64, &mut b).unwrap();
let s = test.to_string();
assert_eq!(converted, s);
}
@ -160,7 +140,7 @@ mod tests {
let mut b = [0; 3];
for test in testcases {
let converted = u32_to_str(test, &mut b);
let converted = strutil::format_i64(test as i64, &mut b);
assert_eq!(converted, None)
}
}

Loading…
Cancel
Save