pub fn find_shebang_end(content: &str) -> (usize, bool) {
if !content.starts_with("#!") {
return (0, false);
}
match memchr::memchr(b'\n', content.as_bytes()) {
Some(pos) => (pos + 1, true),
None => (content.len(), true),
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_find_shebang_end_with_lf() {
let content = "#!/usr/bin/env node\nconsole.log('hello');";
let (end, has_shebang) = find_shebang_end(content);
assert!(has_shebang);
assert_eq!(end, 20); assert_eq!(&content[..end], "#!/usr/bin/env node\n");
assert_eq!(&content[end..], "console.log('hello');");
}
#[test]
fn test_find_shebang_end_with_crlf() {
let content = "#!/usr/bin/env node\r\nconsole.log('hello');";
let (end, has_shebang) = find_shebang_end(content);
assert!(has_shebang);
assert_eq!(end, 21); assert_eq!(&content[..end], "#!/usr/bin/env node\r\n");
assert_eq!(&content[end..], "console.log('hello');");
}
#[test]
fn test_find_shebang_end_no_shebang() {
let content = "console.log('hello');";
let (end, has_shebang) = find_shebang_end(content);
assert!(!has_shebang);
assert_eq!(end, 0);
}
#[test]
fn test_find_shebang_end_no_newline() {
let content = "#!/usr/bin/env node";
let (end, has_shebang) = find_shebang_end(content);
assert!(has_shebang);
assert_eq!(end, content.len());
assert_eq!(&content[..end], "#!/usr/bin/env node");
}
}