From 90f4245565f6239454f69e2e9211dd0e76d83273 Mon Sep 17 00:00:00 2001 From: Tilman Sauerbeck Date: Tue, 31 Dec 2019 13:35:35 +0100 Subject: [PATCH] common: Add the fmt module. --- SConscript.libcommon | 1 + src/common/fmt.rs | 130 +++++++++++++++++++++++++++++++++++++++++++ src/common/lib.rs | 1 + 3 files changed, 132 insertions(+) create mode 100644 src/common/fmt.rs diff --git a/SConscript.libcommon b/SConscript.libcommon index f4abd4b..d4a3c2c 100644 --- a/SConscript.libcommon +++ b/SConscript.libcommon @@ -19,6 +19,7 @@ source_files_rs = [ 'src/common/display.rs', 'src/common/screen.rs', 'src/common/gps.rs', + 'src/common/fmt.rs', ] source_files_c = [ diff --git a/src/common/fmt.rs b/src/common/fmt.rs new file mode 100644 index 0000000..836d1b9 --- /dev/null +++ b/src/common/fmt.rs @@ -0,0 +1,130 @@ +/* + * Copyright (c) 2017 Tilman Sauerbeck (tilman at code-monkey de) + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +pub fn fmt_u32(s: &mut [u8], n: u32) -> usize { + let mut len = 1usize; + let mut tmp = n; + + // Count the number of bytes needed. + while tmp > 9 { + tmp /= 10; + len += 1; + } + + if len <= s.len() { + let mut tmp = n; + let mut offset = len; + let mut len2 = len + 1; + + while len2 > 1 { + len2 -= 1; + + offset -= 1; + s[offset] = b'0' + (tmp % 10) as u8; + + tmp /= 10; + } + } + + len +} + +pub fn fmt_u32_pad(s: &mut [u8], n: u32, pad: usize, pad_char: u8) -> usize { + let mut len = 1usize; + let mut tmp = n; + + // Count the number of bytes needed. + while tmp > 9 { + tmp /= 10; + len += 1; + } + + if s.len() == 0 { + return if len > pad { + len + } else { + pad + }; + } + + let mut offset = 0; + + while len < pad { + s[offset] = pad_char; + offset += 1; + len += 1; + } + + fmt_u32(&mut s[offset..], n); + + len +} + +fn to_hex(c: u8) -> u8 { + if c >= 10 { + c - 10 + b'a' + } else { + c + b'0' + } +} + +pub fn fmt_x32(s: &mut [u8], n: u32) -> usize { + let len = 8; + let mut tmp = n; + + if s.len() >= len { + for i in (0..8).rev() { + s[i] = to_hex((tmp & 0xf) as u8); + tmp >>= 4; + } + } + + len +} + +pub fn fmt_human(s: &mut [u8], _u: u32) -> usize { + let suffix; + let mut u = _u; + + if u < (1 << 10) { + suffix = None; + } else if u < (1 << 20) { + u >>= 10; + suffix = Some(b'K'); + } else { + u >>= 20; + suffix = Some(b'M'); + } + + let mut len = fmt_u32(s, u); + + if let Some(suffix_c) = suffix { + if len < s.len() { + s[len] = suffix_c; + } + + len += 1; + } + + len +} diff --git a/src/common/lib.rs b/src/common/lib.rs index 2cae469..dcca2f2 100644 --- a/src/common/lib.rs +++ b/src/common/lib.rs @@ -41,6 +41,7 @@ pub mod usb_serial; pub mod screen; pub mod display; pub mod gps; +pub mod fmt; use core::panic::PanicInfo; -- 2.30.2