Skip to content

Commit 6664805

Browse files
committed
remove hello world example and make first str_count
1 parent 9a8c75b commit 6664805

File tree

7 files changed

+94
-26
lines changed

7 files changed

+94
-26
lines changed

NAMESPACE

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
11
# Generated by roxygen2: do not edit by hand
22

3-
export(hello_world)
43
useDynLib(stringer, .registration = TRUE)

R/extendr-wrappers.R

+3-3
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
#' @useDynLib stringer, .registration = TRUE
99
NULL
1010

11-
#' Return string `"Hello world!"` to R.
12-
#' @export
13-
hello_world <- function() .Call(wrap__hello_world)
11+
str_regex_count <- function(strings, pattern) .Call(wrap__str_regex_count, strings, pattern)
12+
13+
str_text_count <- function(strings, sub) .Call(wrap__str_text_count, strings, sub)
1414

R/interface.R

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
setClass(
2+
"text",
3+
representation(value = "character")
4+
)
5+
6+
setClass(
7+
"regex",
8+
representation(value = "character")
9+
)
10+
11+
text <- function(x) {
12+
new("text", value = x)
13+
}
14+
15+
regex <- function(x) {
16+
new("regex", value = x)
17+
}
18+
19+
str_count <- function(l, pattern) {
20+
switch(
21+
class(pattern),
22+
text = str_text_count(l, pattern@value),
23+
regex = str_regex_count(l, pattern@value),
24+
character = str_regex_count(l, pattern)
25+
)
26+
}

man/hello_world.Rd

-11
This file was deleted.

src/rust/Cargo.lock

+33
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/rust/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ name = 'stringer'
99

1010
[dependencies]
1111
extendr-api = '*'
12+
regex = "1.5"

src/rust/src/lib.rs

+31-11
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,36 @@
11
use extendr_api::prelude::*;
2+
use regex::Regex;
23

3-
/// Return string `"Hello world!"` to R.
4-
/// @export
5-
#[extendr]
6-
fn hello_world() -> &'static str {
7-
"Hello world!"
8-
}
9-
10-
// Macro to generate exports.
11-
// This ensures exported functions are registered with R.
12-
// See corresponding C code in `entrypoint.c`.
134
extendr_module! {
145
mod stringer;
15-
fn hello_world;
6+
fn str_regex_count;
7+
fn str_text_count;
8+
}
9+
10+
#[extendr]
11+
fn str_regex_count(strings: Vec<String>, pattern: String) -> Vec<i64> {
12+
let re = match Regex::new(pattern.as_str()) {
13+
Ok(re) => re,
14+
Err(err) => panic!("{}", err),
15+
};
16+
strings.iter().map(|x| {
17+
let c = re.split(x.as_str()).count();
18+
if c > 0 {
19+
(c - 1) as i64
20+
} else {
21+
0 as i64
22+
}
23+
}).collect()
1624
}
25+
26+
#[extendr]
27+
fn str_text_count(strings: Vec<String>, sub: String) -> Vec<i64> {
28+
strings.iter().map(|x| {
29+
let c = x.split(&sub).count();
30+
if c > 0 {
31+
(c - 1) as i64
32+
} else {
33+
0 as i64
34+
}
35+
}).collect()
36+
}

0 commit comments

Comments
 (0)