common: Add the fmt module.
[gps-watch.git] / src / common / fmt.rs
diff --git a/src/common/fmt.rs b/src/common/fmt.rs
new file mode 100644 (file)
index 0000000..836d1b9
--- /dev/null
@@ -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
+}