mattrguerra/cortexmainPrivate on GitHub, published here in full

search.rs

Language Rust691 B19 lines

crates/cortex-core/src/storage/search.rs

  1. /// Turn what a person typed into a safe FTS5 query: every word quoted (so
  2. /// operators and punctuation cannot cause syntax errors) and the last word a
  3. /// prefix match, so results appear while typing.
  4. pub fn fts_query(input: &str) -> Option<String> {
  5. let words: Vec<String> = input
  6. .split(|c: char| !(c.is_alphanumeric() || c == '_' || c == '\''))
  7. .map(|w| w.trim_matches('\''))
  8. .filter(|w| !w.is_empty())
  9. .map(|w| format!("\"{}\"", w.replace('"', "")))
  10. .collect();
  11. let (last, rest) = words.split_last()?;
  12. let mut q = rest.join(" ");
  13. if !q.is_empty() {
  14. q.push(' ');
  15. }
  16. q.push_str(last);
  17. q.push('*');
  18. Some(q)
  19. }