[JavaScript] 纯文本查看 复制代码
// Copyright (c) 2024 Hantong Chen(cxw620). MIT license.
// RE from `https://s1.hdslb.com/bfs/seed/log/report/log-reporter.js`
// Last Updated: 2024/1/12 16:33, js content md5: a6fa378028e0cce7ea7202dda4783781
fn main() {
println!("{}", UuidInfoc::gen());
}
#[allow(dead_code)]
struct UuidInfoc;
#[allow(dead_code)]
impl UuidInfoc {
pub fn gen() -> String {
static DIGHT_MAP: [&'static str; 16] = [
// Math.random() === 0 is really rare that probability is unlimited close to 0
"1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F", "10",
];
let t = now!().as_millis() % 100_000;
str_concat!(
&random_choice!(8, 4, 4, 4, 12; "-"; DIGHT_MAP),
&format!("{:0>5}", t),
"infoc"
)
}
}
#[macro_export]
/// + Generates a random string by choosing ones from given candidates.
///
/// Candidates should be `Vec<&str>` or `[&'a str]`.
///
/// # Examples
///
/// ```
/// use crate::random_choice;
///
/// static DIGHT_MAP: [&'static str; 17] = [
/// "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F", "10",
/// ];
///
/// let rc_1 = random_choice!(32, DIGHT_MAP);
/// let rc_2 = random_choice!(8, 4, 4, 4, 12; "-"; DIGHT_MAP); // like `8310B0E0A-40105-9EC3-8298-36C75D10FEA59`
/// ```
macro_rules! random_choice {
($range: expr, $choice_set: expr) => {{
let mut rng = rand::thread_rng();
let mut result = String::with_capacity(32);
(0..$range).for_each(|_| {
result.push_str($choice_set[rand::Rng::gen_range(&mut rng, 0..$choice_set.len())]);
});
result
}};
($($range: expr),+; $split: expr; $choice_set: expr) => {{
let mut rng = rand::thread_rng();
let mut result = String::with_capacity(32);
$(
(0..$range).for_each(|_| {
result.push_str($choice_set[rand::Rng::gen_range(&mut rng, 0..$choice_set.len())]);
});
result.push_str($split);
)+
result.truncate(result.len() - $split.len());
result
}};
}
#[macro_export]
macro_rules! str_concat {
($($x:expr),*) => {
{
let mut string_final = String::with_capacity(512);
$(
string_final.push_str($x);
)*
string_final
}
};
}
#[macro_export]
/// Faster way to get current timestamp other than `chrono::Local::now().timestamp()`,
/// 12x faster on my machine.
///
/// See [`Duration`](https://doc.rust-lang.org/std/time/struct.Duration.html) for more details.
macro_rules! now {
() => {{
match std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH) {
Ok(t) => t,
Err(_) => panic!("SystemTime before UNIX EPOCH!"),
}
}};
}