Skip to content

Commit

Permalink
stri_length
Browse files Browse the repository at this point in the history
  • Loading branch information
Marcin Bujarski authored and Marcin Bujarski committed Jan 24, 2013
1 parent 7eb62a5 commit 7728af4
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 3 deletions.
7 changes: 4 additions & 3 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
export("%+%")
export("%==%")
export("%+%")
export(stri_charcategories)
export(stri_char_getcategoryid)
export(stri_char_getpropertyid)
export(stri_charcategories)
export(stri_chartype)
export(stri_dup)
export(stri_encinfo)
Expand All @@ -12,6 +12,7 @@ export(stri_encset)
export(stri_flatten)
export(stri_info)
export(stri_join)
export(stri_length)
export(stri_localeinfo)
export(stri_localelist)
export(stri_localeset)
Expand All @@ -20,8 +21,8 @@ export(stri_locate_first_class)
export(stri_locate_last_class)
export(stri_nfc)
export(stri_nfd)
export(stri_nfkc_casefold)
export(stri_nfkc)
export(stri_nfkc_casefold)
export(stri_nfkd)
export(stri_numbytes)
export(stri_split)
Expand Down
10 changes: 10 additions & 0 deletions R/length.R
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,13 @@ stri_numbytes <- function(str) {
# prepare_arg done internally
.Call("stri_numbytes", str, PACKAGE="stringi")
}


#' The number of characters in a string
#'
#' @param str character vector
#' @return integer vector giving the number of characters in each element of character vector
#' @export
stri_length <- function(str) {
.Call("stri_length", str, PACKAGE="stringi")
}
25 changes: 25 additions & 0 deletions src/length.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,28 @@ SEXP stri_numbytes(SEXP s)
UNPROTECT(1);
return ret;
}

SEXP stri_length(SEXP s)
{
s = stri_prepare_arg_string(s);
int ns = LENGTH(s);
UChar32 c;

SEXP ret;
PROTECT(ret = allocVector(INTSXP, ns));

for (int k = 0; k < ns; k++) {
SEXP q = STRING_ELT(s, k);
if (q == NA_STRING)
INTEGER(ret)[k] = NA_INTEGER;
else {
int j = 0; // number of code points
int nq = LENGTH(q);
for (int i = 0; i < nq; j++)
U8_NEXT(CHAR(q), i, nq, c);
INTEGER(ret)[k] = j;
}
}
UNPROTECT(1);
return ret;
}
1 change: 1 addition & 0 deletions src/stringi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ static const R_CallMethodDef cCallMethods[] = {
{"stri_info", (DL_FUNC)&stri_info, 0},
{"stri_join", (DL_FUNC)&stri_join, 1},
{"stri_join2", (DL_FUNC)&stri_join2, 2},
{"stri_length", (DL_FUNC)&stri_length, 1},
{"stri_localeinfo", (DL_FUNC)&stri_localeinfo, 1},
{"stri_localelist", (DL_FUNC)&stri_localelist, 0},
{"stri_localeset", (DL_FUNC)&stri_localeset, 1},
Expand Down
1 change: 1 addition & 0 deletions src/stringi.h
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ SEXP stri_char_getpropertyid(SEXP x);
// length.cpp
SEXP stri_numbytes(SEXP s);
R_len_t stri__numbytes_max(SEXP s);
SEXP stri_length(SEXP s);

// wrap.cpp
SEXP stri_wrap_greedy(SEXP count, SEXP width, SEXP spacecost);
Expand Down

0 comments on commit 7728af4

Please sign in to comment.