Created
February 8, 2021 22:38
-
-
Save JayKickliter/288832b32e60a06cee3cb26bdd2ddba5 to your computer and use it in GitHub Desktop.
Remove line and block comments from a &str in Rust
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fn main() { | |
let decommented = decomment(INPUT); | |
println!("{}", decommented); | |
} | |
/// Removes both c-style block comments and c++-style line comments from a str. | |
pub fn decomment(src: &str) -> String { | |
let mut in_line_comment = false; | |
let mut in_block_comment = false; | |
let mut decommented = String::with_capacity(src.len()); | |
let mut itr = src.chars().peekable(); | |
while let Some(ch) = itr.next() { | |
match (ch, itr.peek()) { | |
('/', Some('*')) => { | |
assert!(!in_line_comment); | |
assert!(!in_block_comment); | |
let _ = itr.next(); | |
in_block_comment = true; | |
} | |
('*', Some('/')) => { | |
assert!(in_block_comment); | |
let _ = itr.next(); | |
in_block_comment = false; | |
} | |
('/', Some('/')) => { | |
assert!(!in_line_comment); | |
assert!(!in_block_comment); | |
let _ = itr.next(); | |
in_line_comment = true; | |
} | |
('\r', Some('\n')) if in_line_comment => { | |
let _ = itr.next(); | |
in_line_comment = false; | |
} | |
('\n', _) if in_line_comment => { | |
in_line_comment = false; | |
} | |
_ if in_block_comment || in_line_comment || ch.is_whitespace() => (), | |
_ => decommented.push(ch), | |
} | |
} | |
assert!(!in_block_comment); | |
decommented | |
} | |
static INPUT: &str = r#" | |
{ | |
"SX130x_conf": { /* multiline | |
* comment */ "spidev_path": "/dev/spidev0.0", | |
"lorawan_public": true, | |
"clksrc": 0, | |
"antenna_gain": 0, /* antenna gain, in dBi */ | |
"full_duplex": false, | |
"precision_timestamp": { | |
"enable": false, | |
"max_ts_metrics": 255, | |
"nb_symbols": 1 | |
}, | |
"radio_0": { | |
"enable": true, | |
"type": "SX1250", | |
"freq": 868100000, | |
"rssi_offset": -215.4, | |
"rssi_tcomp": {"coeff_a": 0, "coeff_b": 0, "coeff_c": 20.41, "coeff_d": 2162.56, "coeff_e": 0}, | |
"tx_enable": true, | |
"tx_freq_min": 863000000, | |
"tx_freq_max": 870000000, | |
"tx_gain_lut":[ | |
{"rf_power": 12, "pa_gain": 1, "pwr_idx": 4}, | |
{"rf_power": 13, "pa_gain": 1, "pwr_idx": 5}, | |
{"rf_power": 14, "pa_gain": 1, "pwr_idx": 6}, | |
{"rf_power": 15, "pa_gain": 1, "pwr_idx": 7}, | |
{"rf_power": 16, "pa_gain": 1, "pwr_idx": 8}, | |
{"rf_power": 17, "pa_gain": 1, "pwr_idx": 9}, | |
{"rf_power": 18, "pa_gain": 1, "pwr_idx": 10}, | |
{"rf_power": 19, "pa_gain": 1, "pwr_idx": 11}, | |
{"rf_power": 20, "pa_gain": 1, "pwr_idx": 12}, | |
{"rf_power": 21, "pa_gain": 1, "pwr_idx": 13}, | |
{"rf_power": 22, "pa_gain": 1, "pwr_idx": 14}, | |
{"rf_power": 23, "pa_gain": 1, "pwr_idx": 15}, | |
{"rf_power": 24, "pa_gain": 1, "pwr_idx": 16}, | |
{"rf_power": 25, "pa_gain": 1, "pwr_idx": 17}, | |
{"rf_power": 26, "pa_gain": 1, "pwr_idx": 19}, | |
{"rf_power": 27, "pa_gain": 1, "pwr_idx": 20} | |
] | |
}, | |
"radio_1": { | |
"enable": true, | |
"type": "SX1250", | |
"freq": 869400000, | |
"rssi_offset": -215.4, | |
"rssi_tcomp": {"coeff_a": 0, "coeff_b": 0, "coeff_c": 20.41, "coeff_d": 2162.56, "coeff_e": 0}, | |
"tx_enable": false | |
}, | |
"chan_multiSF_0": { //868.9, 25mW, 0.1% DC | |
"enable": true, | |
"radio": 1, | |
"if": -500000 | |
}, | |
"chan_multiSF_1": { //869.1, 25mW, 0.1% DC | |
"enable": true, | |
"radio": 1, | |
"if": -300000 | |
}, | |
"chan_multiSF_2": { //869.4, 500mW, 10% DC | |
"enable": true, | |
"radio": 1, | |
"if": 0 | |
}, | |
"chan_multiSF_3": { //867.7, 25mW, 1% DC | |
"enable": true, | |
"radio": 0, | |
"if": -400000 | |
}, | |
"chan_multiSF_4": { //867.9, 25mW, 1% DC | |
"enable": true, | |
"radio": 0, | |
"if": -200000 | |
}, | |
"chan_multiSF_5": { //868.1, 25mW, 1% DC | |
"enable": true, | |
"radio": 0, | |
"if": 0 | |
}, | |
"chan_multiSF_6": { //868.3, 25mW, 1% DC | |
"enable": true, | |
"radio": 0, | |
"if": 200000 | |
}, | |
"chan_multiSF_7": { //868.5, 25mW, 1% DC | |
"enable": true, | |
"radio": 0, | |
"if": 400000 | |
}, | |
"chan_Lora_std": { | |
"enable": true, | |
"radio": 1, | |
"if": -200000, | |
"bandwidth": 250000, | |
"spread_factor": 7 | |
}, | |
"chan_FSK": { | |
/* disabled */ | |
"enable": false, | |
"radio": 0, | |
"if": 300000, | |
"bandwidth": 250000, | |
"datarate": 100000 | |
} | |
}, | |
"gateway_conf": { | |
"gps_i2c_path": "/dev/i2c-1", | |
"gateway_ID": "AA555A0000000000", | |
/* change with default server address/ports */ | |
"server_address": "localhost", | |
"serv_port_up": 1680, | |
"serv_port_down": 1680, | |
/* adjust the following parameters for your network */ | |
"keepalive_interval": 10, | |
"stat_interval": 30, | |
"push_timeout_ms": 100, | |
/* forward only valid packets */ | |
"forward_crc_valid": true, | |
"forward_crc_error": false, | |
"forward_crc_disabled": false | |
} | |
} | |
"#; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment