3 releases

0.0.3 Apr 7, 2020
0.0.2 Apr 6, 2020
0.0.1 Mar 28, 2020

#837 in Text processing

Download history 723/week @ 2024-04-08 417/week @ 2024-04-15 468/week @ 2024-04-22 433/week @ 2024-04-29 172/week @ 2024-05-06 687/week @ 2024-05-13 159/week @ 2024-05-20 713/week @ 2024-05-27 901/week @ 2024-06-03 1256/week @ 2024-06-10 1537/week @ 2024-06-17 1425/week @ 2024-06-24 237/week @ 2024-07-01 1428/week @ 2024-07-08 1302/week @ 2024-07-15 1144/week @ 2024-07-22

4,169 downloads per month
Used in flex-rs

Apache-2.0

31KB
739 lines

No Comment

Version Minimum Rust version: 1.36

Remove comments from a char iterator.

This crate provides the WithoutComments iterator and the IntoWithoutComments trait implemented for all Iterator<Item=char> providing the without_comments method. Comment specifications are available for rust-style, c-style, python-style, and haskell-style line and block comments, a way to specify custom comment specifications is planned. This crate is intended to be used for removing comments from text, not from code, for this reason, "\*" will still open a block comment in rust mode because string literals have no semantic significance.

Usage

Add this to your Cargo.toml:

[dependencies]
without-comments = "0.0.3"

main.rs:

fn main() {
        use std::fs::read_to_string;
        use no_comment::{IntoWithoutComments as _, languages};

        let without_comments = read_to_string("tests.txt")
            .unwrap()
            .chars()
            .without_comments(languages::rust())
            .collect::<String>();

        println!("{}", without_comments);
}

test.txt:

This is text // this is a (rust) line comment
This is more text /* this is a (rust) block comment
/* this one is nested */ */
This is text again
/* If a comment is left open, it keeps
going until the end.

output:

This is text 
This is more text 
This is text again

Note that trailing spaces and newlines are preserved, showing whitespace the output looks like this:

This·is·text·¶
This·is·more·text·¶
This·is·text·again¶

Dependencies

~315–770KB
~18K SLoC