Skip to content

Commit 73b544a

Browse files
committed
add count_doubles_slice
1 parent e690d68 commit 73b544a

2 files changed

Lines changed: 8 additions & 0 deletions

File tree

doubles_with_rust.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@ def test_rust_memreplace(benchmark):
7979
def test_rust_fold(benchmark):
8080
print(benchmark(myrustlib.count_doubles_fold, val))
8181

82+
def test_rust_slice(benchmark):
83+
print(benchmark(myrustlib.count_doubles_slice, val))
8284

8385
# def test_rust_regex(benchmark):
8486
# print(benchmark(myrustlib.count_doubles_regex, val))

pyext-myrustlib/src/lib.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,11 @@ fn count_doubles_once_bytes(_py: Python, val: &str) -> PyResult<u64> {
9595
Ok(total)
9696
}
9797

98+
fn count_doubles_slice(_py: Python, val: &str) -> PyResult<u64> {
99+
let count = val.as_bytes().windows(2).filter(|slice| slice[0] == slice[1]).count();
100+
Ok(count as u64)
101+
}
102+
98103

99104
// Rust Gegex crate does not support lokkaround/backreference
100105
// https://github.com/rust-lang/regex/issues/302
@@ -113,6 +118,7 @@ py_module_initializer!(libmyrustlib, initlibmyrustlib, PyInit_myrustlib, |py, m
113118
try!(m.add(py, "count_doubles_peek", py_fn!(py, count_doubles_peek(val: &str))));
114119
try!(m.add(py, "count_doubles_memreplace", py_fn!(py, count_doubles_memreplace(val: &str))));
115120
try!(m.add(py, "count_doubles_fold", py_fn!(py, count_doubles_fold(val: &str))));
121+
try!(m.add(py, "count_doubles_slice", py_fn!(py, count_doubles_slice(val: &str))));
116122
// try!(m.add(py, "count_doubles_regex", py_fn!(py, count_doubles_regex(val: &str))));
117123
Ok(())
118124
});

0 commit comments

Comments
 (0)