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

When a character and regex are concatenated with c(), the regex objects are coerced to character. #10

Open
jimhester opened this issue Sep 24, 2014 · 3 comments

Comments

@jimhester
Copy link
Member

# the end dollar is improperly escaped here
rex('x' %or% c('.', end)) # (?:x|\.\$)

#using `list` prevents this
rex('x' %or% list('.', end))  # (?:x|\.$)

I don't think we want to do anything to prevent this, but we may want to make a warning when regexs get coerced to character.

@kevinushey
Copy link
Collaborator

Can we implement as.character.regex to do the right thing here? (Does c call a corresponding as.x.y method on coercion?)

@jimhester
Copy link
Member Author

So playing around with this a little bit, c is a generic function and you can create a method for regex objects

c.regex <- function(..., recursive = FALSE) {
  x <- structure(unlist(lapply(list(...), escape)), class = "Regex")
  p(x)
}

Which does what we want(not escaping $) if end is the first object in the c call

rex('x' %or% c(rex::shortcuts$end, '.'))
# (?:x|$\.)

But if a character is the first arg c converts all the arguments to character rather than regex, so this doesn't work.

rex('x' %or% c('.', rex::shortcuts$end))
# (?:x|\.\$)

In order to get this to actually work you would need a way to force c to always coerce to regex if any of the arguments are regex, which doesn't seem possible from what I can tell.

jimhester added a commit that referenced this issue Oct 3, 2014
Partially addresses #10 when the `regex` object is the first object to
be Concatenated.
@jimhester
Copy link
Member Author

Also to address your question

(Does c call a corresponding as.x.y method on coercion?

It does not seem to

as.character.regex <- function(x) { stop() }
rex('x' %or% c('.', end))
# (?:x|\.\$)
as.character(shortcuts$end)
# Error in as.character.regex(shortcuts$end) :

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

No branches or pull requests

2 participants