Skip to content

Commit 747287c

Browse files
authored
Merge pull request #268 from yutannihilation/stri-sub-replacement
"omit_na=TRUE" keeps the string as is if the replacement is NA
2 parents 5ca64f8 + 170c134 commit 747287c

File tree

4 files changed

+11
-10
lines changed

4 files changed

+11
-10
lines changed

R/sub.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,8 @@
7474
#' @param from integer vector or two-column matrix
7575
#' @param to integer vector; mutually exclusive with \code{length} and \code{from} being a matrix
7676
#' @param length integer vector; mutually exclusive with \code{to} and \code{from} being a matrix
77-
#' @param omit_na single logical value; if \code{TRUE}, missing values in \code{from},
78-
#' \code{to}, or \code{length} will result in an unchanged input; replacement function only
77+
#' @param omit_na single logical value; if \code{TRUE}, missing values in any of the arguments
78+
#' provided will result in an unchanged input; replacement function only
7979
#' @param value character vector to be substituted with; replacement function only
8080
#'
8181
#'

devel/testthat/test-sub.R

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,10 @@ test_that("stri_sub<-", {
7171
s <- c("a;b", "c:d"); stri_sub(s, stri_locate_first_fixed(s, ";"), omit_na=FALSE) <- "_"; expect_identical(s, c("a_b", NA))
7272
s <- c("a;b", "c:d"); stri_sub(s, stri_locate_first_fixed(s, ";")) <- "_"; expect_identical(s, c("a_b", NA))
7373

74+
s <- c("a;b", "c:d"); stri_sub(s, stri_locate_first_fixed(s, ";"), omit_na=TRUE) <- c("_", NA); expect_identical(s, c("a_b", "c:d"))
75+
s <- c("a;b", "c:d"); stri_sub(s, stri_locate_first_fixed(s, ";"), omit_na=FALSE) <- c("_", NA); expect_identical(s, c("a_b", NA))
76+
s <- c("a;b", "c:d"); stri_sub(s, stri_locate_first_fixed(s, ";")) <- c("_", NA); expect_identical(s, c("a_b", NA))
77+
7478
s <- "\u0106a\u0105"; stri_sub(s,0,to=0) <- "x"; expect_identical(s, "x\u0106a\u0105")
7579
s <- "\u0106a\u0105"; stri_sub(s,1,to=0) <- "x"; expect_identical(s, "x\u0106a\u0105")
7680
s <- "\u0106a\u0105"; stri_sub(s,2,to=0) <- "x"; expect_identical(s, "\u0106xa\u0105")

man/stri_sub.Rd

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/stri_sub.cpp

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -293,13 +293,10 @@ SEXP stri_sub_replacement(SEXP str, SEXP from, SEXP to, SEXP length, SEXP omit_n
293293
{
294294
R_len_t cur_from = from_tab[i % from_len];
295295
R_len_t cur_to = (to_tab)?to_tab[i % to_len]:length_tab[i % length_len];
296-
if (str_cont.isNA(i) || value_cont.isNA(i)) {
297-
SET_STRING_ELT(ret, i, NA_STRING);
298-
continue;
299-
}
300-
301-
if (cur_from == NA_INTEGER || cur_to == NA_INTEGER) {
296+
297+
if (str_cont.isNA(i) || value_cont.isNA(i) || cur_from == NA_INTEGER || cur_to == NA_INTEGER) {
302298
if (omit_na_1) {
299+
// if str_conf is NA, this will be NA_STRING as well.
303300
SET_STRING_ELT(ret, i, str_cont.toR(i));
304301
}
305302
else {

0 commit comments

Comments
 (0)