import shortid from 'https://cdn.skypack.dev/shortid';
console.log(shortid()); //=> "lXN1aGba2"
import net from 'net';
const server = net.createServer(async (socket) => {
console.log('Got new connection!');
await socket.write('Hello! 👋\n');
await socket.destroy();
});
server.listen(3000, '127.0.0.1', () => {
console.log('Server is listening on port 3000...');
});
$ mkdir badutf8
$ cd badutf8
$ touch main.rs
$ cargo init --bin
$ cargo add anyhow bstr termcolor
/// Write each byte in the slice in its hexadecimal form,
/// and with bold coloring.
fn write_invalid_utf8<W: WriteColor>(
mut wtr: W,
slice: &[u8],
) -> io::Result<()> {
use termcolor::{Color, ColorSpec};
let mut color = ColorSpec::new();
color.set_fg(Some(Color::Red)).set_bold(true);
wtr.set_color(&color)?;
for &byte in slice.iter() {
write!(wtr, r"\x{:X}", byte)?;
}
wtr.reset()?;
Ok(())
}
mainuse std::io::{self, Write};
use bstr::{io::BufReadExt, ByteSlice};
use termcolor::{ColorChoice, WriteColor};
/// Usage:
/// badutf8 < stdin
/// foo ... | badutf8
fn main() -> anyhow::Result<()> {
let mut bufrdr = io::BufReader::new(io::stdin().lock());
let mut wtr = termcolor::StandardStream::stdout(ColorChoice::Auto);
let mut lineno = 0;
bufrdr.for_byte_line(|mut line| {
lineno += 1;
if line.is_utf8() {
return Ok(true);
}
write!(wtr, "{}:", lineno)?;
loop {
let (ch, size) = bstr::decode_utf8(line);
if size == 0 {
break;
} else if ch.is_some() {
wtr.write_all(&line[..size])?;
} else {
write_invalid_utf8(&mut wtr, &line[..size])?;
}
line = &line[size..];
}
write!(wtr, "\n")?;
Ok(true)
})?;
Ok(())
}
$ echo 'foo\xFFbar\xE2\x98quux' | badutf8
1:foo\xFFbar\xE2\x98quux
$ badutf8 < gecko-dev/third_party/aom/PATENTS
60:2.1. Affiliate. \x93Affiliate\x94 means an entity that directly or indirectly
63:2.2. Control. \x93Control\x94 means direct or indirect control of more than 50% of
73:2.5. Final Deliverable. \x93Final Deliverable\x94 means the final version of a
82:2.7. License. \x93License\x94 means this license.
84:2.8. Licensee. \x93Licensee\x94 means any person or entity who exercises patent
101:2.11. Reference Implementation. \x93Reference Implementation\x94 means an Encoder
105:2.12. Specification. \x93Specification\x94 means the specification designated by
原文作者:Rust语言中文社区;如有侵权,请联系管理员删除。