File: streaming.rs

package info (click to toggle)
rust-simdutf8 0.1.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 248 kB
  • sloc: makefile: 4
file content (41 lines) | stat: -rw-r--r-- 1,063 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#[allow(unused_imports)]
use std::io::{stdin, Read, Result};

#[cfg(all(
    feature = "public_imp",
    any(target_arch = "x86", target_arch = "x86_64")
))]
fn main() -> Result<()> {
    use simdutf8::basic::imp::Utf8Validator;

    unsafe {
        if !std::is_x86_feature_detected!("avx2") {
            panic!("This example only works with CPUs supporting AVX 2");
        }

        let mut validator = simdutf8::basic::imp::x86::avx2::Utf8ValidatorImp::new();
        let mut buf = vec![0; 8192];
        loop {
            let bytes_read = stdin().read(buf.as_mut())?;
            if bytes_read == 0 {
                break;
            }
            validator.update(&buf);
        }

        if validator.finalize().is_ok() {
            println!("Input is valid UTF-8");
        } else {
            println!("Input is not valid UTF-8");
        }
    }

    Ok(())
}

/// Dummy main. This example requires the crate feature `public_imp`.
#[cfg(not(all(
    feature = "public_imp",
    any(target_arch = "x86", target_arch = "x86_64")
)))]
fn main() {}