mirror of
https://github.com/trezor/trezor-firmware.git
synced 2025-01-03 12:00:59 +00:00
chore(core/rust): use format_i64 to convert numbers to strings
This commit is contained in:
parent
3bf3e4c38b
commit
e00af4f7f3
@ -1,13 +1,15 @@
|
|||||||
use crate::ui::{
|
use crate::{
|
||||||
component::{
|
strutil,
|
||||||
base::ComponentExt,
|
ui::{
|
||||||
paginated::Paginate,
|
component::{
|
||||||
text::paragraphs::{Paragraph, ParagraphStrType, Paragraphs},
|
base::ComponentExt,
|
||||||
Child, Component, Event, EventCtx, Pad,
|
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};
|
use super::{theme, Button, ButtonMsg};
|
||||||
@ -210,7 +212,7 @@ impl Component for NumberInput {
|
|||||||
|
|
||||||
fn paint(&mut self) {
|
fn paint(&mut self) {
|
||||||
let mut buf = [0u8; 10];
|
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 digit_font = Font::DEMIBOLD;
|
||||||
let y_offset = digit_font.text_height() / 2 + Button::<&str>::BASELINE_OFFSET;
|
let y_offset = digit_font.text_height() / 2 + Button::<&str>::BASELINE_OFFSET;
|
||||||
display::rect_fill(self.area, theme::BG);
|
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.
|
/// Constructs a string from a C string.
|
||||||
///
|
///
|
||||||
/// # Safety
|
/// # Safety
|
||||||
@ -140,7 +120,7 @@ pub fn icon_text_center(
|
|||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use crate::strutil;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn u32_to_str_valid() {
|
fn u32_to_str_valid() {
|
||||||
@ -148,7 +128,7 @@ mod tests {
|
|||||||
let mut b = [0; 10];
|
let mut b = [0; 10];
|
||||||
|
|
||||||
for test in testcases {
|
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();
|
let s = test.to_string();
|
||||||
assert_eq!(converted, s);
|
assert_eq!(converted, s);
|
||||||
}
|
}
|
||||||
@ -160,7 +140,7 @@ mod tests {
|
|||||||
let mut b = [0; 3];
|
let mut b = [0; 3];
|
||||||
|
|
||||||
for test in testcases {
|
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)
|
assert_eq!(converted, None)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user