Skip to content

Commit

Permalink
Add Regexp.quote as an alias of Regexp.escape
Browse files Browse the repository at this point in the history
  • Loading branch information
adambeynon committed Jan 27, 2014
1 parent 8027655 commit 64e0455
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 15 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -117,6 +117,8 @@

* Fix `Regexp.escape` to properly escape \n, \t, \r, \f characters.

* Add `Regexp.quote` as an alias of `escape`.

## 0.5.5 2013-11-25

* Fix regression: add `%i[foo bar]` style words back to lexer
Expand Down
32 changes: 18 additions & 14 deletions opal/corelib/regexp.rb
@@ -1,22 +1,26 @@
class Regexp
`def._isRegexp = true`

def self.escape(string)
%x{
return string.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$| ]/g, '\\$&')
.replace(/[\n]/g, '\\n')
.replace(/[\r]/g, '\\r')
.replace(/[\f]/g, '\\f')
.replace(/[\t]/g, '\\t');
}
end
class << self
def escape(string)
%x{
return string.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$| ]/g, '\\$&')
.replace(/[\n]/g, '\\n')
.replace(/[\r]/g, '\\r')
.replace(/[\f]/g, '\\f')
.replace(/[\t]/g, '\\t');
}
end

def self.union(*parts)
`new RegExp(parts.join(''))`
end
alias quote escape

def union(*parts)
`new RegExp(parts.join(''))`
end

def self.new(regexp, options = undefined)
`new RegExp(regexp, options)`
def new(regexp, options = undefined)
`new RegExp(regexp, options)`
end
end

def ==(other)
Expand Down
6 changes: 6 additions & 0 deletions spec/opal/filters/unsupported/encoding.rb
Expand Up @@ -54,3 +54,9 @@
fails "Regexp.escape sets the encoding of the result to the encoding of the String if any non-US-ASCII characters are present in an input String with valid encoding"
fails "Regexp.escape sets the encoding of the result to ASCII-8BIT if any non-US-ASCII characters are present in an input String with invalid encoding"
end

opal_filter "Regexp.quote" do
fails "Regexp.quote sets the encoding of the result to US-ASCII if there are only US-ASCII characters present in the input String"
fails "Regexp.quote sets the encoding of the result to the encoding of the String if any non-US-ASCII characters are present in an input String with valid encoding"
fails "Regexp.quote sets the encoding of the result to ASCII-8BIT if any non-US-ASCII characters are present in an input String with invalid encoding"
end
2 changes: 1 addition & 1 deletion spec/opal/rubyspecs
Expand Up @@ -188,7 +188,7 @@ core/time/year_spec

core/regexp/escape_spec
core/regexp/match_spec
#core/regexp/quote_spec
core/regexp/quote_spec

language/BEGIN_spec
language/alias_spec
Expand Down

0 comments on commit 64e0455

Please sign in to comment.