summaryrefslogtreecommitdiff
path: root/rust/libnewsboat/src/utils.rs
diff options
context:
space:
mode:
authorGravatar Alexander Batischev <eual.jp@gmail.com> 2019-09-28 21:46:03 +0300
committerGravatar Alexander Batischev <eual.jp@gmail.com> 2019-09-28 23:17:58 +0300
commit1e08618a01d0521d1972de0a8cb1a100c857e660 (patch)
tree17a1a9b4e4ad6b43445862c0e275fc6d2204b6cd /rust/libnewsboat/src/utils.rs
parentc63da29f70de21f045a1e40d45bcbf332ef71a4c (diff)
downloadnewsboat-1e08618a01d0521d1972de0a8cb1a100c857e660.tar.gz
newsboat-1e08618a01d0521d1972de0a8cb1a100c857e660.tar.zst
newsboat-1e08618a01d0521d1972de0a8cb1a100c857e660.zip
Port utils::strip_comments to Rust
Diffstat (limited to 'rust/libnewsboat/src/utils.rs')
-rw-r--r--rust/libnewsboat/src/utils.rs32
1 files changed, 32 insertions, 0 deletions
diff --git a/rust/libnewsboat/src/utils.rs b/rust/libnewsboat/src/utils.rs
index 91ee9cfe..40f6ad39 100644
--- a/rust/libnewsboat/src/utils.rs
+++ b/rust/libnewsboat/src/utils.rs
@@ -16,6 +16,7 @@ use self::url::percent_encoding::*;
use self::url::Url;
use libc::c_ulong;
use logger::{self, Level};
+use std::borrow::Cow;
use std::fs::DirBuilder;
use std::io::{self, Write};
use std::os::unix::fs::DirBuilderExt;
@@ -545,6 +546,16 @@ pub fn newsboat_major_version() -> u32 {
env!("CARGO_PKG_VERSION_MAJOR").parse::<u32>().unwrap()
}
+/// Returns the part of the string before first # character (or the whole input string if there are
+/// no # character in it).
+pub fn strip_comments(line: &str) -> Cow<str> {
+ if let Some(index) = line.find('#') {
+ Cow::from(&line[0..index])
+ } else {
+ Cow::from(line)
+ }
+}
+
#[cfg(test)]
mod tests {
extern crate tempfile;
@@ -1042,4 +1053,25 @@ mod tests {
assert_eq!(strnaturalcmp("aa10", "aa2"), Ordering::Greater);
}
+
+ #[test]
+ fn t_strip_comments() {
+ // no comments in line
+ assert_eq!(strip_comments(""), "");
+ assert_eq!(strip_comments("\t\n"), "\t\n");
+ assert_eq!(strip_comments("some directive "), "some directive ");
+
+ // fully commented line
+ assert_eq!(strip_comments("#"), "");
+ assert_eq!(strip_comments("# #"), "");
+ assert_eq!(strip_comments("# comment"), "");
+
+ // partially commented line
+ assert_eq!(strip_comments("directive # comment"), "directive ");
+ assert_eq!(
+ strip_comments("directive # comment # another"),
+ "directive "
+ );
+ assert_eq!(strip_comments("directive#comment"), "directive");
+ }
}