Skip to content

Fast string seaching for node.js ( aho-corasick algorithm )

License

Notifications You must be signed in to change notification settings

xn-sakina/hoshino

Repository files navigation

hoshino

Fast string seaching powered by Rust. ( built upon aho-corasick )

Install

  pnpm i -D hoshino

Usage

findAllMatchSync / findAllMatch

import { findAllMatchSync } from 'hoshino'

const patterns = ['apple', 'maple', 'Snapple']
const haystack = 'Nobody likes maple in their apple flavored Snapple.'
//                             ^ 0 ^          ^ 1 ^          ^  2  ^
const matches = findAllMatchSync({ patterns, haystack })

// pattern
assert(patterns[matches[0].pattern], 'maple')
assert(patterns[matches[1].pattern], 'apple')
assert(patterns[matches[2].pattern], 'Snapple')

// index
const { start, end } = matches[0]
assert(haystack.slice(start, end), 'maple')

findLeftFirstMatchSync / findLeftFirstMatch

import { findLeftFirstMatchSync } from 'hoshino'

const patterns = ['apple', 'maple', 'Snapple']
const haystack = 'Nobody likes maple in their apple flavored Snapple.'
//                             ^^^^^ finding the leftmost first match
const { matched, pattern } = findLeftFirstMatchSync({ patterns, haystack })

if (matched) {
  assert(patterns[pattern], 'maple')
}

findLeftFirstLongestMatchSync / findLeftFirstLongestMatch

import { findLeftFirstLongestMatch } from 'hoshino'

const patterns = ['map', 'maple', 'Snapple']
const haystack = 'Nobody likes maple in their apple flavored Snapple.'
//                             ^^^^^ finding the leftmost-longest first match
const { matched, pattern } = findLeftFirstLongestMatch({ patterns, haystack })

if (matched) {
  assert(patterns[pattern], 'maple')
}

loadPatterns

import { loadPatterns } from 'hoshino'

loadPatterns(['map', 'maple', 'Snapple'])
// use findLeftFirstMatchSync({ haystack })
// use findAllMatchSync({ haystack })
// ...

Troubleshooting (wasm in browser)

SharedArrayBuffer is not defined

Add COOP and COEP response headers:

Cross-Origin-Opener-Policy: same-origin
Cross-Origin-Embedder-Policy: require-corp

Failed to execute 'decode' on 'TextDecoder': The provided ArrayBufferView value must not be shared.

Add COOP and COEP to enable the SharedArrayBuffer, or see stackoverflow#65743480 to patch @napi-rs/wasm-runtime.

License

MIT