API
encode(b: &[u8]) -> Vec<u8>
Encodes a byte array into brainfuck code.
let code = encode(b"Hello");
let code_str = std::str::from_utf8(&code).unwrap();
- Input: arbitrary byte array (UTF-8 text, binary, etc.)
- Output: byte vector containing brainfuck code (ASCII range)
- Uses incremental encoding based on the difference from the previous byte, so consecutive similar values produce shorter code.
decode(s: &str) -> Vec<u8>
Executes brainfuck code and returns the resulting bytes.
let result = decode("+++++++++[>++++++++<-]>+.");
// result == [73] ('I')
- Input: brainfuck code string
- Output: vector of bytes output by
.commands - When a
,command is encountered, reads one byte from stdin into the current cell.
swap_chars(s: &mut String, config: &Config)
Replaces standard brainfuck symbols with custom strings defined in Config.
let config: Config = toml::from_str(toml_str).unwrap();
let mut code = "+++++.".to_string();
swap_chars(&mut code, &config);
- Apply to standard brainfuck code generated by
encode. - Panics if Config validation fails.
unswap_chars(s: &mut String, config: &Config)
Restores custom strings back to standard brainfuck symbols.
let config: Config = toml::from_str(toml_str).unwrap();
let mut code = custom_code.to_string();
unswap_chars(&mut code, &config);
let result = decode(&code);
- Call before
decodeto convert custom symbols back to standard. - Substitutes longer tokens first to minimize substring conflicts.
Config
#[derive(Debug, Deserialize)]
pub struct Config {
right: String, // >
left: String, // <
plus: String, // +
minus: String, // -
loop_start: String, // [
loop_end: String, // ]
print: String, // .
input: String, // ,
}
Deserialize from a TOML file using toml::from_str.