Merge pull request #1789 from davidhewitt/example-word-count

examples: make `word-count` example comparison fairer
This commit is contained in:
David Hewitt 2021-08-13 15:08:16 +01:00 committed by GitHub
commit 254ea53f3f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 5 additions and 27 deletions

View File

@ -1,6 +1,3 @@
// Source adopted from
// https://github.com/tildeio/helix-website/blob/master/crates/word_count/src/lib.rs
use pyo3::prelude::*; use pyo3::prelude::*;
use rayon::prelude::*; use rayon::prelude::*;
@ -24,28 +21,11 @@ fn search_sequential_allow_threads(py: Python, contents: &str, needle: &str) ->
py.allow_threads(|| search_sequential(contents, needle)) py.allow_threads(|| search_sequential(contents, needle))
} }
fn matches(word: &str, needle: &str) -> bool { /// Count the occurrences of needle in line, case insensitive
let mut needle = needle.chars();
for ch in word.chars().skip_while(|ch| !ch.is_alphabetic()) {
match needle.next() {
None => {
return !ch.is_alphabetic();
}
Some(expect) => {
if ch.to_lowercase().next() != Some(expect) {
return false;
}
}
}
}
needle.next().is_none()
}
/// Count the occurences of needle in line, case insensitive
fn count_line(line: &str, needle: &str) -> usize { fn count_line(line: &str, needle: &str) -> usize {
let mut total = 0; let mut total = 0;
for word in line.split(' ') { for word in line.split(' ') {
if matches(word, needle) { if word == needle {
total += 1; total += 1;
} }
} }

View File

@ -1,7 +1,6 @@
from concurrent.futures import ThreadPoolExecutor from concurrent.futures import ThreadPoolExecutor
import pytest import pytest
import word_count import word_count

View File

@ -8,11 +8,10 @@ __all__ = [
] ]
def search_py(contents, needle): def search_py(contents: str, needle: str) -> int:
total = 0 total = 0
for line in contents.split(): for line in contents.splitlines():
words = line.split(" ") for word in line.split(" "):
for word in words:
if word == needle: if word == needle:
total += 1 total += 1
return total return total