Offline, privacy-first grammar checker. Fast, open-source, Rust-powered
refactor(core)!: `Lrc<[char]>` instead of `Lrc<Vec<char>>` (#3060)
Steve Kurch2b5c8cd352a1
diff --git a/harper-core/src/document.rs b/harper-core/src/document.rsindex 34adf59e5..f9bc348fe 100644--- a/harper-core/src/document.rs+++ b/harper-core/src/document.rs@@ -17,7 +17,7 @@ use crate::{OrdinalSuffix, Span};/// A document containing some amount of lexed and parsed English text.#[derive(Debug, Clone)]pub struct Document {- source: Lrc<Vec<char>>,+ source: Lrc<[char]>,tokens: Vec<Token>,}@@ -53,23 +53,23 @@ impl Document {/// Lexes and parses text to produce a document using a provided language/// parser and dictionary.pub fn new(text: &str, parser: &impl Parser, dictionary: &impl Dictionary) -> Self {- let source: Vec<_> = text.chars().collect();+ let source: Lrc<_> = text.chars().collect();- Self::new_from_vec(Lrc::new(source), parser, dictionary)+ Self::new_from_chars(source, parser, dictionary)}/// Lexes and parses text to produce a document using a provided language/// parser and the included curated dictionary.pub fn new_curated(text: &str, parser: &impl Parser) -> Self {- let source: Vec<_> = text.chars().collect();+ let source: Lrc<_> = text.chars().collect();- Self::new_from_vec(Lrc::new(source), parser, &FstDictionary::curated())+ Self::new_from_chars(source, parser, &FstDictionary::curated())}/// Lexes and parses text to produce a document using a provided language/// parser and dictionary.- pub fn new_from_vec(- source: Lrc<Vec<char>>,+ pub fn new_from_chars(+ source: Lrc<[char]>,parser: &impl Parser,dictionary: &impl Dictionary,) -> Self {@@ -84,11 +84,7 @@ impl Document {/// Create a new document from character data using the built-in [`PlainEnglish`]/// parser and curated dictionary. This avoids string-to-char conversions.pub fn new_plain_english_curated_chars(source: &[char]) -> Self {- Self::new_from_vec(- Lrc::new(source.to_vec()),- &PlainEnglish,- &FstDictionary::curated(),- )+ Self::new_from_chars(Lrc::from(source), &PlainEnglish, &FstDictionary::curated())}/// Parse text to produce a document using the built-in [`PlainEnglish`]@@ -103,7 +99,7 @@ impl Document {/// This avoids running potentially expensive metadata generation code, so this is more/// efficient if you don't need that information.pub(crate) fn new_basic_tokenize(text: &str, parser: &impl Parser) -> Self {- let source = Lrc::new(text.chars().collect_vec());+ let source: Lrc<_> = text.chars().collect();let tokens = parser.parse(&source);let mut document = Self { source, tokens };document.apply_fixups();@@ -129,7 +125,7 @@ impl Document {/// Create a new document from character data using the built-in [`Markdown`] parser/// and curated dictionary. This avoids string-to-char conversions.pub fn new_markdown_default_curated_chars(chars: &[char]) -> Self {- Self::new_from_vec(+ Self::new_from_chars(chars.to_vec().into(),&Markdown::default(),&FstDictionary::curated(),diff --git a/harper-core/src/title_case.rs b/harper-core/src/title_case.rsindex 925800803..0db00b039 100644--- a/harper-core/src/title_case.rs+++ b/harper-core/src/title_case.rs@@ -12,20 +12,20 @@ use crate::{CharStringExt, Document, TokenStringExt, parsers::Parser};/// A helper function for [`make_title_case`] that uses Strings instead of char buffers.pub fn make_title_case_str(source: &str, parser: &impl Parser, dict: &impl Dictionary) -> String {- let source: Vec<char> = source.chars().collect();+ let source: Lrc<_> = source.chars().collect();- make_title_case_chars(Lrc::new(source), parser, dict).to_string()+ make_title_case_chars(source, parser, dict).to_string()}// Make a given string [title case](https://en.wikipedia.org/wiki/Title_case) following the Chicago Manual of Style.pub fn make_title_case_chars(- source: Lrc<Vec<char>>,+ source: Lrc<[char]>,parser: &impl Parser,dict: &impl Dictionary,) -> Vec<char> {- let document = Document::new_from_vec(source.clone(), parser, dict);+ let document = Document::new_from_chars(source.clone(), parser, dict);- make_title_case(document.get_tokens(), source.as_slice(), dict)+ make_title_case(document.get_tokens(), &source, dict)}pub fn try_make_title_case(diff --git a/harper-core/src/weir/mod.rs b/harper-core/src/weir/mod.rsindex fb4caf682..a3e0a6a7a 100644--- a/harper-core/src/weir/mod.rs+++ b/harper-core/src/weir/mod.rs@@ -171,7 +171,7 @@ impl WeirLinter {continue;}- let doc = Document::new_from_vec(+ let doc = Document::new_from_chars(current.chars().collect::<Vec<_>>().into(),&Markdown::default(),&FstDictionary::curated(),@@ -197,7 +197,7 @@ impl WeirLinter {let mut iter_count = 0;loop {- let test = Document::new_from_vec(+ let test = Document::new_from_chars(text_chars.clone().into(),&Markdown::default(),&FstDictionary::curated(),@@ -224,7 +224,7 @@ impl WeirLinter {}fn lint_count(text: &str, linter: &mut impl Linter) -> usize {- let document = Document::new_from_vec(+ let document = Document::new_from_chars(text.chars().collect::<Vec<_>>().into(),&Markdown::default(),&FstDictionary::curated(),diff --git a/harper-wasm/src/lib.rs b/harper-wasm/src/lib.rsindex fb4b3bb6a..6e5350f87 100644--- a/harper-wasm/src/lib.rs+++ b/harper-wasm/src/lib.rs@@ -247,13 +247,10 @@ impl Linter {}pub fn ignore_lint(&mut self, source_text: String, lint: Lint) {- let source: Vec<_> = source_text.chars().collect();+ let source: Lrc<_> = source_text.chars().collect();- let document = Document::new_from_vec(- source.into(),- &lint.language.create_parser(),- &self.dictionary,- );+ let document =+ Document::new_from_chars(source, &lint.language.create_parser(), &self.dictionary);self.ignored_lints.ignore_lint(&lint.inner, &document);}@@ -267,7 +264,7 @@ impl Linter {pub fn context_hash(&self, source_text: String, lint: &Lint) -> u64 {let source: Vec<_> = source_text.chars().collect();- let document = Document::new_from_vec(+ let document = Document::new_from_chars(source.into(),&lint.language.create_parser(),&self.dictionary,@@ -284,8 +281,7 @@ impl Linter {all_headings: bool,regex_mask: Option<String>,) -> Vec<OrganizedGroup> {- let source: Vec<_> = text.chars().collect();- let source = Lrc::new(source);+ let source: Lrc<_> = text.chars().collect();let mut parser = language.create_parser();@@ -302,7 +298,7 @@ impl Linter {parser = Box::new(OopsAllHeadings::new(parser));}- let document = Document::new_from_vec(source.clone(), &parser, &self.dictionary);+ let document = Document::new_from_chars(source.clone(), &parser, &self.dictionary);let temp = self.lint_group.config.clone();self.lint_group.config.fill_with_curated();@@ -325,7 +321,7 @@ impl Linter {.into_iter().map(|l| {let problem_text = l.get_str(&source);- let span = Into::<Span>::into(l.span).to_js_indices(source.as_slice());+ let span = Into::<Span>::into(l.span).to_js_indices(&source);Lint::new(l, span, problem_text, language)})@@ -344,8 +340,7 @@ impl Linter {all_headings: bool,regex_mask: Option<String>,) -> Vec<Lint> {- let source: Vec<_> = text.chars().collect();- let source = Lrc::new(source);+ let source: Lrc<_> = text.chars().collect();let mut parser = language.create_parser();@@ -362,7 +357,7 @@ impl Linter {parser = Box::new(OopsAllHeadings::new(parser));}- let document = Document::new_from_vec(source.clone(), &parser, &self.dictionary);+ let document = Document::new_from_chars(source.clone(), &parser, &self.dictionary);let temp = self.lint_group.config.clone();self.lint_group.config.fill_with_curated();@@ -378,7 +373,7 @@ impl Linter {.into_iter().map(|l| {let problem_text = l.get_str(&source);- let span = Into::<Span>::into(l.span).to_js_indices(source.as_slice());+ let span = Into::<Span>::into(l.span).to_js_indices(&source);Lint::new(l, span, problem_text, language)}).collect()@@ -453,7 +448,7 @@ impl Linter {) -> Result<String, String> {let mut source: Vec<_> = source_text.chars().collect();- let doc = Document::new_from_vec(+ let doc = Document::new_from_chars(source.clone().into(),&lint.language.create_parser(),&self.dictionary,