refactor: Use nanorand crate instead of custom xorshift implementation

This commit is contained in:
Kogia-sima 2020-12-16 16:52:07 +09:00
parent 9b733f973f
commit d0ec00fb29
3 changed files with 15 additions and 8 deletions

7
Cargo.lock generated
View File

@ -81,6 +81,12 @@ version = "2.3.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3728d817d99e5ac407411fa471ff9800a778d88a24685968b36824eaf4bee400"
[[package]]
name = "nanorand"
version = "0.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3173d7bb904c5a3a2f9167eb936916a39e97124846b8316223323aed9a34d1e7"
[[package]]
name = "output_vt100"
version = "0.1.2"
@ -131,6 +137,7 @@ name = "sailfish"
version = "0.2.3"
dependencies = [
"itoap",
"nanorand",
"ryu",
"sailfish-macros",
"version_check",

View File

@ -28,3 +28,8 @@ optional = true
[build-dependencies]
version_check = "0.9.2"
[dev-dependencies.nanorand]
version = "0.5.1"
default-features = false
features = ["std", "wyrand"]

View File

@ -106,6 +106,7 @@ pub fn escape_to_string(feed: &str, s: &mut String) {
#[cfg(test)]
mod tests {
use super::*;
use nanorand::RNG;
fn escape(feed: &str) -> String {
let mut s = String::new();
@ -155,7 +156,7 @@ mod tests {
#[test]
fn random() {
const ASCII_CHARS: &'static [u8] = br##"abcdefghijklmnopqrstuvwxyz0123456789-^\@[;:],./\!"#$%&'()~=~|`{+*}<>?_"##;
let mut state = 88172645463325252u64;
let mut gen = nanorand::WyRand::new();
let mut data = Vec::with_capacity(100);
let mut buf_naive = Buffer::new();
@ -165,12 +166,7 @@ mod tests {
for _ in 0..5 {
data.clear();
for _ in 0..len {
// xorshift
state ^= state << 13;
state ^= state >> 7;
state ^= state << 17;
let idx = state as usize % ASCII_CHARS.len();
let idx = gen.generate_range(0, ASCII_CHARS.len());
data.push(ASCII_CHARS[idx]);
}
@ -184,7 +180,6 @@ mod tests {
s.as_ptr().add(s.len()),
);
dbg!(s);
fallback::escape(s, &mut buf);
assert_eq!(buf.as_str(), buf_naive.as_str());
buf.clear();