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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
use std::io;
use std::io::Write;
const APPEND : &'static str = "!!!1!";
pub struct Stdshout<W: Write>(W);
impl<W: Write> Stdshout<W> {
pub fn new(w: W) -> Stdshout<W> {
Stdshout(w)
}
}
impl<W: Write> Write for Stdshout<W> {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
let lossy = String::from_utf8_lossy(buf.as_ref()).to_uppercase();
for line in lossy.split('\n') {
if line.len() > 0 {
self.0.write(line.as_bytes())?;
self.0.write(APPEND.as_bytes())?;
self.0.write(b"\n")?;
}
}
Ok(buf.len())
}
fn flush(&mut self) -> io::Result<()> {
self.0.flush()
}
}
pub fn shout<D: AsRef<[u8]>>(data: D) -> String {
let mut lossy = String::from_utf8_lossy(data.as_ref()).to_uppercase();
lossy.push_str(APPEND);
lossy
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn it_works() {
assert_eq!("HELLO!!!1!", shout("hello"));
}
#[test]
fn binary_data() {
assert_eq!("�!!!1!", shout(b"\xF0\x90\x80"));
}
}