feat: 优化默认字符集与字符串构建
- 引入默认字符集常量并优化字符串构建及生成功能以提升效率
This commit is contained in:
@@ -1,5 +1,8 @@
|
|||||||
use rand::Rng;
|
use rand::Rng;
|
||||||
|
|
||||||
|
const DEFAULT_CHARSET: &[u8] =
|
||||||
|
b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
|
||||||
|
|
||||||
/// 随机Token生成器结构体
|
/// 随机Token生成器结构体
|
||||||
pub struct TokenGenerator {
|
pub struct TokenGenerator {
|
||||||
uppercase: bool,
|
uppercase: bool,
|
||||||
@@ -38,48 +41,47 @@ impl TokenGenerator {
|
|||||||
/// 生成随机token
|
/// 生成随机token
|
||||||
pub fn generate(&self, length: usize) -> String {
|
pub fn generate(&self, length: usize) -> String {
|
||||||
let charset = self.build_charset();
|
let charset = self.build_charset();
|
||||||
|
let mut result = String::with_capacity(length);
|
||||||
|
|
||||||
if charset.is_empty() {
|
if charset.is_empty() {
|
||||||
// 如果没有选择任何字符类型,使用默认字符集
|
// 如果没有选择任何字符类型,使用默认字符集
|
||||||
return self.generate_with_default_charset(length);
|
return self.generate_with_default_charset(length);
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut rng = rand::rng();
|
let mut rng = rand::rng();
|
||||||
(0..length)
|
for _ in 0..length {
|
||||||
.map(|_| {
|
let idx = rng.random_range(0..charset.len());
|
||||||
let idx = rng.random_range(0..charset.len());
|
result.push(charset[idx] as char);
|
||||||
charset[idx] as char
|
}
|
||||||
})
|
result
|
||||||
.collect()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// 构建字符集
|
/// 构建字符集
|
||||||
fn build_charset(&self) -> Vec<u8> {
|
fn build_charset(&self) -> Vec<u8> {
|
||||||
let mut charset = Vec::new();
|
let mut charset_string = String::new();
|
||||||
|
|
||||||
if self.uppercase {
|
if self.uppercase {
|
||||||
charset.extend_from_slice(b"ABCDEFGHIJKLMNOPQRSTUVWXYZ");
|
charset_string.push_str("ABCDEFGHIJKLMNOPQRSTUVWXYZ");
|
||||||
}
|
}
|
||||||
if self.lowercase {
|
if self.lowercase {
|
||||||
charset.extend_from_slice(b"abcdefghijklmnopqrstuvwxyz");
|
charset_string.push_str("abcdefghijklmnopqrstuvwxyz");
|
||||||
}
|
}
|
||||||
if self.numbers {
|
if self.numbers {
|
||||||
charset.extend_from_slice(b"0123456789");
|
charset_string.push_str("0123456789");
|
||||||
}
|
}
|
||||||
|
|
||||||
charset
|
charset_string.into_bytes()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// 使用默认字符集生成token
|
/// 使用默认字符集生成token
|
||||||
fn generate_with_default_charset(&self, length: usize) -> String {
|
fn generate_with_default_charset(&self, length: usize) -> String {
|
||||||
let charset =
|
|
||||||
b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
|
|
||||||
let mut rng = rand::rng();
|
let mut rng = rand::rng();
|
||||||
|
let mut result = String::with_capacity(length);
|
||||||
|
|
||||||
(0..length)
|
for _ in 0..length {
|
||||||
.map(|_| {
|
let idx = rng.random_range(0..DEFAULT_CHARSET.len());
|
||||||
let idx = rng.random_range(0..charset.len());
|
result.push(DEFAULT_CHARSET[idx] as char);
|
||||||
charset[idx] as char
|
}
|
||||||
})
|
result
|
||||||
.collect()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user