Skip to content

Commit

Permalink
do not pop the unbalance brackets in the current row
Browse files Browse the repository at this point in the history
close #530
  • Loading branch information
randy3k committed Apr 18, 2022
1 parent 5fc32c3 commit 1d68253
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/search.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ SEXP find_unbalanced_bracket(SEXP content, SEXP _row, SEXP _col, SEXP _skip_el)
stack pos;
stack codept_pos;
int nbracket = 0;
int ncurrentunbalanced = 0;
int nunbalanced = 0;
char brac[2] = " \x00";

Expand All @@ -47,6 +48,7 @@ SEXP find_unbalanced_bracket(SEXP content, SEXP _row, SEXP _col, SEXP _skip_el)
fsm_initialize(&state);
stack_initialize(&pos);
stack_initialize(&codept_pos);
ncurrentunbalanced = 0;
while (j < n && (i < row || k <= col)) {
cj = c[j];
if (0x80 <= cj && cj <= 0xbf) {
Expand All @@ -66,7 +68,7 @@ SEXP find_unbalanced_bracket(SEXP content, SEXP _row, SEXP _col, SEXP _skip_el)
stack_pop(&pos);
stack_pop(&codept_pos);
} else {
nunbalanced += 1;
ncurrentunbalanced += 1;
}
}
}
Expand All @@ -80,6 +82,7 @@ SEXP find_unbalanced_bracket(SEXP content, SEXP _row, SEXP _col, SEXP _skip_el)
nunbalanced -= 1;
nbracket -= 1;
}
nunbalanced += ncurrentunbalanced;
j = stack_pop(&pos);
k = stack_pop(&codept_pos);
stack_clear(&pos);
Expand Down
4 changes: 4 additions & 0 deletions tests/testthat/test-search.R
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ test_that("find_unbalanced_bracket works as expected", {
expect_equal(bsearch(c("foo({ ", "}, xyz"), 1, 3)[[1]], c(0, 3))
expect_equal(bsearch(c("foo(( ", "), xyz"), 1, 3)[[1]], c(0, 3))

# issue #530
expect_equal(bsearch(c("if (sum()) ", "xyz"), 0, 7)[[1]], c(0, 7))
expect_equal(bsearch(c("} if (sum()) ", "xyz"), 0, 9)[[1]], c(0, 9))
=
# other brackets
expect_equal(bsearch("foo[xy", 0, 5), list(c(0, 3), "["))
expect_equal(bsearch("foo{xy", 0, 5), list(c(0, 3), "{"))
Expand Down

0 comments on commit 1d68253

Please sign in to comment.