datetime_string/str.rs
1//! String-related utilities.
2//!
3//! Serialization function takes `&mut [u8]` or similar types rather than
4//! `&mut W where W: std::io::Write`, because `core` does not have an alternative
5//! of `std::io::Write`, and `core::fmt::Write` is not implemented for `&mut [u8]`.
6
7/// Writes 0-padded 4 digits string.
8///
9/// It is guaranteed that this does not panic and `buf` is filled with ASCII digits.
10///
11/// # Failures
12///
13/// Note that this function writes meaning less content for integer which is not
14/// representable with 4 digits.
15/// This is safe in a sense that this never cause UB for any integer input, but
16/// callers should ensure that the integer value is less than 10000.
17#[inline]
18pub(crate) fn write_digit4(buf: &mut [u8; 4], v: u16) {
19 buf[3] = (v % 10) as u8 + b'0';
20 let v = v / 10;
21 buf[2] = (v % 10) as u8 + b'0';
22 let v = v / 10;
23 buf[1] = (v % 10) as u8 + b'0';
24 let v = v / 10;
25 buf[0] = v as u8 + b'0';
26}
27
28/// Writes 0-padded 2 digits string.
29///
30/// It is guaranteed that this does not panic and `buf` is filled with ASCII digits.
31///
32/// # Failures
33///
34/// Note that this function writes meaning less content for integer which is not
35/// representable with 4 digits.
36/// This is safe in a sense that this never cause UB for any integer input, but
37/// callers should ensure that the integer value is less than 10000.
38#[inline]
39pub(crate) fn write_digit2(buf: &mut [u8; 2], v: u8) {
40 buf[0] = (v / 10) as u8 + b'0';
41 buf[1] = (v % 10) as u8 + b'0';
42}