Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conversion fails if function name contains a regex metacharacter #32

Closed
joshuaulrich opened this issue Mar 10, 2024 · 1 comment · Fixed by #33
Closed

Conversion fails if function name contains a regex metacharacter #32

joshuaulrich opened this issue Mar 10, 2024 · 1 comment · Fixed by #33

Comments

@joshuaulrich
Copy link
Collaborator

Rd2roxygen() throws the error below if the function name contains a regex metacharacter (e.g. "[" for a subset method).

##------ Sun Mar 10 10:10:18 2024 ------##
parsed: subset.xts.Rd
looking for the object '[.xts' in:
Error in grep(sprintf(pattern, gsub("\\.", "\\\\.", fname)), (r.Rd = readLines(r,  : 
  invalid regular expression '^[[:space:]]*(`|"|'|)([\.xts)(\1)[[:space:]]*(<-|=)', reason 'Missing ')''
In addition: Warning message:
In grep(sprintf(pattern, gsub("\\.", "\\\\.", fname)), (r.Rd = readLines(r,  :
  TRE pattern compilation error 'Missing ')''

You already escape any "." in the function name. Here's a patch that escapes all the regular expression metacharacters. This worked for me. Let me know if you'd like a PR.

diff --git a/R/rd2roxygen.R b/R/rd2roxygen.R
index 40ba00f..f5ab07d 100644
--- a/R/rd2roxygen.R
+++ b/R/rd2roxygen.R
@@ -207,7 +207,7 @@ Rd2roxygen = function(pkg, nomatch, usage = FALSE) {
     for (i in tryf) {
       r = file.path(R.dir, i)
       idx = grep(sprintf('^[[:space:]]*(`|"|\'|)(%s)(\\1)[[:space:]]*(<-|=)',
-                          gsub('\\.', '\\\\.', fname)),
+                          gsub('([][{}()+*^$|?.])', '\\\\\\1', fname)),
                   (r.Rd = readLines(r, warn = FALSE)))
       message('  ', i, ': ', appendLF = FALSE)
       message(ifelse(length(idx), paste('line', idx), 'not found'))
@yihui
Copy link
Owner

yihui commented Mar 11, 2024

Yes, please send a PR. Thank you very much!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants