Skip to content

Commit

Permalink
Added Regexp.quote class method. Also: added Rakefile for testing and…
Browse files Browse the repository at this point in the history
… set gem metadata.
  • Loading branch information
dabroz committed Oct 12, 2016
1 parent 615b85b commit 6d74e0d
Show file tree
Hide file tree
Showing 6 changed files with 136 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
mruby
22 changes: 22 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
MRUBY_CONFIG=File.expand_path(ENV["MRUBY_CONFIG"] || "build_config_sample.rb")

file :mruby do
sh "git clone --depth 1 git://github.com/mruby/mruby.git"
end

task :default => :test

desc "compile binary"
task :compile => :mruby do
sh "cd mruby && MRUBY_CONFIG=#{MRUBY_CONFIG} rake all"
end

desc "test"
task :test => :mruby do
sh "cd mruby && MRUBY_CONFIG=#{MRUBY_CONFIG} rake test"
end

desc "cleanup"
task :clean do
sh "cd mruby && rake deep_clean"
end
16 changes: 16 additions & 0 deletions build_config_sample.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
MRuby::Build.new do |conf|
toolchain :gcc

conf.gem File.expand_path(File.dirname(__FILE__))
end

MRuby::Build.new('test') do |conf|
toolchain :gcc

enable_debug
#conf.enable_bintest
conf.enable_test

conf.gembox 'default'
conf.gem File.expand_path(File.dirname(__FILE__))
end
4 changes: 3 additions & 1 deletion mrbgem.rake
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
MRuby::Gem::Specification.new('mruby-pcre-regexp') do |spec|
spec.license = 'MIT'
spec.authors = 'mattn'
spec.authors = ['mattn', 'Tomasz Dabrowski']
spec.summary = 'PCRE Regexp module for mruby'
spec.version = '0.2.0'

spec.linker.libraries << ['pcre']
end
74 changes: 74 additions & 0 deletions src/mruby_pcre_regexp.c
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,78 @@ pcre_regexp_casefold_p(mrb_state *mrb, mrb_value self) {
return (reg->flag & PCRE_CASELESS) ? mrb_true_value() : mrb_false_value();
}

static mrb_value
pcre_regexp_quote_class(mrb_state *mrb, mrb_value self) {
char *pattern, *pattern_end, *output, *p, *q, c;
mrb_int pattern_length;
mrb_value return_value;

mrb_get_args(mrb, "s!", &pattern, &pattern_length);

if (!pattern_length)
{
return mrb_str_new(mrb, "", 0);
}

pattern_end = pattern + pattern_length;

/* based on PHP code:
https://github.com/php/php-src/blob/49412756df244d94a217853395d15e96cb60e18f/ext/pcre/php_pcre.c
*/

/* Allocate enough memory so that even if each character
is quoted, we won't run out of room */
output = mrb_malloc(mrb, 4 * pattern_length + 1);

/* Go through the string and quote necessary characters */
for (p = pattern, q = output; p != pattern_end; p++) {
c = *p;
switch(c) {
case '.':
case '\\':
case '+':
case '*':
case '?':
case '[':
case '^':
case ']':
case '$':
case '(':
case ')':
case '{':
case '}':
case '=':
case '!':
case '>':
case '<':
case '|':
case ':':
case '-':
*q++ = '\\';
*q++ = c;
break;

case '\0':
*q++ = '\\';
*q++ = '0';
*q++ = '0';
*q++ = '0';
break;

default:
*q++ = c;
break;
}
}
*q = '\0';

/* Reallocate string and return it */
return_value = mrb_str_new(mrb, output, q - output);
mrb_free(mrb, output);

return return_value;
}

void
mrb_mruby_pcre_regexp_gem_init(mrb_state* mrb) {
struct RClass *clazz;
Expand All @@ -201,6 +273,8 @@ mrb_mruby_pcre_regexp_gem_init(mrb_state* mrb) {
mrb_define_method(mrb, clazz, "==", pcre_regexp_equal, MRB_ARGS_REQ(1));
mrb_define_method(mrb, clazz, "match", pcre_regexp_match, MRB_ARGS_REQ(1));
mrb_define_method(mrb, clazz, "casefold?", pcre_regexp_casefold_p, MRB_ARGS_NONE());

mrb_define_class_method(mrb, clazz, "quote", pcre_regexp_quote_class, MRB_ARGS_REQ(1));
}

void
Expand Down
20 changes: 20 additions & 0 deletions test/mruby_pcre_regexp.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,26 @@
(reg === "http://example.com") == true and (reg === "htt://example.com") == false
end

assert('quote nil') do
PcreRegexp.quote(nil) == ''
end

assert('quote empty') do
PcreRegexp.quote('') == ''
end

assert('quote simple') do
PcreRegexp.quote('test') == 'test'
end

assert('quote dot') do
PcreRegexp.quote('.') == '\\.'
end

assert('quote complex') do
PcreRegexp.quote('(\d+)') == '\\(\\\\d\\+\\)'
end

# TODO =~

assert("PcreRegexp#casefold?") do
Expand Down

0 comments on commit 6d74e0d

Please sign in to comment.