pub struct Vocabulary { /* private fields */ }Expand description
Parsed personal entries. Region declarations and flags are validated but not kept, so lookup is locale-neutral. Entries are grouped by lowercase form, so a lookup compares case rules within one group.
Implementations§
Source§impl Vocabulary
impl Vocabulary
Sourcepub fn parse(source: &[u8]) -> Result<Self, ParseError>
pub fn parse(source: &[u8]) -> Result<Self, ParseError>
Parse a Vim word list line by line, including BOM, CRLF, case and forbidden flags.
A line whose first byte is # is a comment whatever follows it, because Vim’s zw, zug
and zuw comment out an entry by overwriting its first byte. A line that isn’t UTF-8, an
unknown / directive, an unsupported flag or another invalid line is skipped and counted
in Vocabulary::skipped.
use keyrill_core::vocabulary::{Membership, Vocabulary};
let words = Vocabulary::parse("# personal\nrustc\nNeovim/=\nteh/!\nbad/*\n".as_bytes())?;
let units = |s: &str| s.encode_utf16().collect::<Vec<u16>>();
assert_eq!(words.lookup(&units("RUSTC")), Membership::Recognized);
assert_eq!(words.lookup(&units("neovim")), Membership::Absent);
assert_eq!(words.lookup(&units("teh")), Membership::Forbidden);
assert_eq!((words.len(), words.skipped()), (3, 1));§Errors
A ParseError for oversized input, NUL anywhere or a leading non-UTF-8 /encoding=;
nothing is partially kept.
Sourcepub fn parse_words(
source: &[u8],
membership: Membership,
) -> Result<Self, ParseError>
pub fn parse_words( source: &[u8], membership: Membership, ) -> Result<Self, ParseError>
Parse a plain word list, one word per line, as OS spell checkers keep learned words: UTF-8
with an optional BOM, or UTF-16 LE with its BOM. Every word gets membership and the case
rule of an unflagged Vim entry; # and / are ordinary characters. A line that doesn’t
decode or holds a control character is skipped and counted.
use keyrill_core::vocabulary::{Membership, Vocabulary};
let units = |s: &str| s.encode_utf16().collect::<Vec<u16>>();
let learned = Vocabulary::parse_words(b"km/h\r\nrustc\n", Membership::Recognized)?;
assert_eq!(learned.lookup(&units("km/h")), Membership::Recognized);
assert_eq!(learned.lookup(&units("Rustc")), Membership::Recognized);
let mut utf16 = vec![0xff, 0xfe];
utf16.extend("teh\r\n".encode_utf16().flat_map(u16::to_le_bytes));
let excluded = Vocabulary::parse_words(&utf16, Membership::Forbidden)?;
assert_eq!(excluded.lookup(&units("teh")), Membership::Forbidden);§Errors
A ParseError for oversized input, NUL anywhere, a UTF-16 LE source of odd length or a
big-endian UTF-16 BOM; nothing is partially kept.
Sourcepub fn skipped(&self) -> usize
pub fn skipped(&self) -> usize
Number of skipped lines that weren’t comments, blank or accepted entries and directives.
Sourcepub fn lookup(&self, word: &[u16]) -> Membership
pub fn lookup(&self, word: &[u16]) -> Membership
Membership of word under the case rule of each entry that matches it; across matching
entries, forbidden wins, then recognized, then rare.