Skip to content

Commit

Permalink
Merge pull request #37 from AdamSpannbauer/issue#35
Browse files Browse the repository at this point in the history
rework highlighting #35
  • Loading branch information
AdamSpannbauer authored Dec 29, 2021
2 parents bd16abd + 13ae998 commit d65b86c
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 5 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ Imports:
purrr
Encoding: UTF-8
LazyData: true
RoxygenNote: 7.1.0
RoxygenNote: 7.1.2
URL: https://github.com/AdamSpannbauer/r_regex_tester_app
BugReports: https://github.com/AdamSpannbauer/r_regex_tester_app/issues
Suggests:
Expand Down
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,5 @@ importFrom(golem,with_golem_options)
importFrom(htmltools,HTML)
importFrom(shiny,column)
importFrom(shiny,shinyApp)
importFrom(utils,URLdecode)
importFrom(utils,URLencode)
2 changes: 2 additions & 0 deletions R/app_server.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
#' @param input,output,session Internal parameters for {shiny}.
#' DO NOT REMOVE.
#' @import shiny
#' @importFrom utils URLdecode
#' @importFrom utils URLencode
#' @noRd
app_server = function(input, output, session) {
shiny::observe({
Expand Down
15 changes: 11 additions & 4 deletions R/highlight_test_str.R
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,11 @@ highlight_test_str = function(str, pattern, ignore_case = TRUE,
match_start, match_end)],
cols = c(match_ind, match_start, match_end))
match_df = unique(match_df)

# modifying string in place using indices
# work back to front to avoid disrupting indices
match_df = data.table::data.table(match_df)
match_df = match_df[order(match_ind, capture_start, decreasing = TRUE), ]
} else {
match_end = matches_raw + attr(matches_raw, "match.length") - 1

Expand All @@ -134,16 +139,18 @@ highlight_test_str = function(str, pattern, ignore_case = TRUE,
match,
"</span>"
)]

# modifying string in place using indices
# work back to front to avoid disrupting indices
match_df = match_df[order(match_ind, decreasing = TRUE), ]
}

txt = str
buffer = 0
for (i in seq_len(nrow(match_df))) {
stringr::str_sub(txt,
match_df$match_start[i] + buffer,
match_df$match_end[i] + buffer) = "%s"
match_df$match_start[i],
match_df$match_end[i]) = "%s"
txt = sprintf(txt, match_df$replacements[i])
buffer = buffer + nchar(match_df$replacements[i]) - nchar(match_df$match[i])
}

txt
Expand Down

0 comments on commit d65b86c

Please sign in to comment.