Oregami
Repositories/oxedyne/fe2o3

oxedyne/fe2o3/fe2o3_core/src/macros/string.rs

2.3 KiB, 13 runs

created by r1870400018:102, which is this file's identity for as long as the history lasts, whatever it is later renamed to

download · who wrote it · its history

1#[macro_export]
2/// Print a line to the console including the source file and line info.
3///
4///```
5/// use oxedyne_fe2o3_core::prelude::*;
6///
7/// let trooper = 421;
8/// msg!("TK{} why aren't you at your post?", trooper);
9///```
10macro_rules! msg {
11 () => (println!("{}:{}\n",file!(),line!()));
12 ($($arg:tt)*) => ({
13 print!("{}:{}: ",file!(),line!());
14 println!($($arg)*);
15 })
16}
17
18#[macro_export]
19/// A three letter alias for `std::format!`.
20///
21///```
22/// use oxedyne_fe2o3_core::prelude::*;
23///
24/// let s = fmt!("The meaning is {}", 42);
25///```
26macro_rules! fmt {
27 () => (String::from(""));
28 ($($arg:tt)*) => (format!($($arg)*));
29}
30
31#[macro_export]
32/// Return the type of the given expression as a `String`.
33///
34///```
35/// use oxedyne_fe2o3_core::prelude::*;
36///
37/// let typ = fmt_typ!(42u8);
38///```
39macro_rules! fmt_typ {
40 () => (String::from(""));
41 ($v:expr) => (oxedyne_fe2o3_core::format_type($v));
42}
43
44#[macro_export]
45/// A three letter alias for wrapping a string in a `Stringer`.
46///
47///```
48/// use oxedyne_fe2o3_core::prelude::*;
49///
50/// let s = str!("The meaning is {}", 42);
51///```
52macro_rules! str {
53 () => (Stringer::from(""));
54 ($($arg:tt)*) => (Stringer::new($($arg)*));
55}
56
57#[macro_export]
58/// Dump of a byte slice to `String`s.
59///
60/// Convert a byte slice to text values in a vector of `String` lines. The format string is
61/// applied without modiciation to each byte, with no change to the first and last lines.
62///
63/// Arguments:
64///
65/// * format string including placeholder,
66/// * byte slice,
67/// * maximum number of bytes per line,
68///
69/// # Example
70///```
71/// use oxedyne_fe2o3_core::dump;
72///
73/// let b = [0_u16; 10];
74/// let lines: Vec<String> = dump!(" {:04x}", &b, 4);
75///```
76macro_rules! dump {
77 ($f:tt, $b:expr, $c:expr) => {
78 {
79 let mut lines: Vec<String> = Vec::new();
80 let mut line = String::new();
81 let mut i: usize = 1;
82 for e in $b {
83 line.push_str(&format!($f, e));
84 if i % $c == 0 {
85 lines.push(line);
86 line = String::new();
87 }
88 i += 1;
89 }
90 if line.len() > 0 {
91 lines.push(line);
92 }
93 lines
94 }
95 };
96 ($f:tt, $b:expr) => {
97 dump!($f, $b, 8);
98 };
99}