#comments #line-comment #iterator #block #string #specification #rust-style

no-comment

从字符迭代器中移除 Rust 风格的行和块注释

3 个版本

0.0.3 2020年4月7日
0.0.2 2020年4月6日
0.0.1 2020年3月28日

文本处理 中排名 #837

Download history 789/week @ 2024-04-05 377/week @ 2024-04-12 520/week @ 2024-04-19 376/week @ 2024-04-26 278/week @ 2024-05-03 551/week @ 2024-05-10 294/week @ 2024-05-17 584/week @ 2024-05-24 853/week @ 2024-05-31 1095/week @ 2024-06-07 1502/week @ 2024-06-14 1587/week @ 2024-06-21 367/week @ 2024-06-28 1189/week @ 2024-07-05 1427/week @ 2024-07-12 1099/week @ 2024-07-19

每月下载量 4,530
用于 flex-rs

Apache-2.0 许可

31KB
739

无注释

Version Minimum Rust version: 1.36

char 迭代器中移除注释。

此 crate 提供了 WithoutComments 迭代器和为所有 Iterator<Item=char> 实现的 IntoWithoutComments trait,提供 without_comments 方法。支持 Rust 风格、C 风格、Python 风格和 Haskell 风格的行和块注释,计划提供自定义注释规范的方法。此 crate 旨在用于从文本中移除注释,而不是从代码中移除,因此,在 Rust 模式下,"\*" 仍会打开块注释,因为字符串字面量没有语义意义。

用法

将此添加到您的 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.

输出

This is text 
This is more text 
This is text again

请注意,保留尾随空格和新行,显示空白,输出看起来像这样

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

依赖项

~0.4–0.8MB
~19K SLoC