summaryrefslogtreecommitdiff
path: root/src/utils.rs
diff options
context:
space:
mode:
authorMaxVerevkin <maxxverrr@gmail.com>2023-05-31 16:00:07 +0300
committerMaxVerevkin <maxxverrr@gmail.com>2023-05-31 16:00:07 +0300
commitad4f237bbae26af795fcb5250d3fbe155b67eff3 (patch)
tree1f2a2554af1ded51a4ad6a991bec4de37436da71 /src/utils.rs
parent01deca357331fc9a2a4be96a23fd06d374a85c70 (diff)
downloadi3bar-mango-ad4f237bbae26af795fcb5250d3fbe155b67eff3.tar.gz
refactor i3bar_protocol.rs
Diffstat (limited to 'src/utils.rs')
-rw-r--r--src/utils.rs29
1 files changed, 29 insertions, 0 deletions
diff --git a/src/utils.rs b/src/utils.rs
index 2ed1868..e45785f 100644
--- a/src/utils.rs
+++ b/src/utils.rs
@@ -49,6 +49,20 @@ pub fn de_first_json<'a, T: Deserialize<'a>>(
}
}
+/// Returns a byte slice with leading ASCII whitespace bytes removed.
+///
+/// TODO: remove if/when slice::trim_ascii_start is stabilized.
+pub fn trim_ascii_start(mut bytes: &[u8]) -> &[u8] {
+ while let [first, rest @ ..] = bytes {
+ if first.is_ascii_whitespace() {
+ bytes = rest;
+ } else {
+ break;
+ }
+ }
+ bytes
+}
+
#[cfg(test)]
mod tests {
use super::*;
@@ -102,4 +116,19 @@ mod tests {
let s = b"hello\nworld\n...";
assert_eq!(last_line(s), Some((str!("world"), str!("..."))));
}
+
+ #[test]
+ fn test_trim_start() {
+ let s = b" ";
+ assert_eq!(trim_ascii_start(s), b"");
+
+ let s = b"hello";
+ assert_eq!(trim_ascii_start(s), b"hello");
+
+ let s = b"\t \nhello";
+ assert_eq!(trim_ascii_start(s), b"hello");
+
+ let s = b" \t \nhello\n";
+ assert_eq!(trim_ascii_start(s), b"hello\n");
+ }
}