Navigation Menu

Skip to content

Commit

Permalink
Add Groonga::MatchOperator#exec
Browse files Browse the repository at this point in the history
  • Loading branch information
kou committed Apr 14, 2015
1 parent 8e3e228 commit c8ed815
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 1 deletion.
86 changes: 86 additions & 0 deletions ext/groonga/rb-grn-match-operator.c
@@ -0,0 +1,86 @@
/* -*- coding: utf-8; mode: C; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
Copyright (C) 2015 Kouhei Sutou <kou@clear-code.com>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License version 2.1 as published by the Free Software Foundation.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/

#include "rb-grn.h"

VALUE rb_cGrnMatchOperator;

/*
* Executes a match operation. Match operation checks whether `text`
* contains `sub_text` or not.
*
* @example Executes match operations with the default context
* Groonga::Operator::MATCH.exec("Hello Rroonga", "Rroonga") # => true
* Groonga::Operator::MATCH.exec("Hello Rroonga", "Groonga") # => false
*
* @example Executes match operations with the specified context
* context = Groonga::Context.new
* Groonga::Operator::MATCH.exec("Hello Rroonga", "Rroonga",
* :context => context) # => true
* Groonga::Operator::MATCH.exec("Hello Rroonga", "Groonga",
* :context => context) # => false
*
* @overload exec(text, sub_text, options={})
* @param text [String] The text to be matched.
* @param sub_text [String] The sub text to be contained.
* @param options [::Hash] The options.
* @option options [Groonga::Context] (Groonga::Context.default)
* The context to executes the operation.
* @return [Boolean] `true` if `text` contains `sub_text`, `false`
* otherwise.
*/
static VALUE
rb_grn_match_operator_exec (int argc, VALUE *argv, VALUE self)
{
grn_bool matched;
VALUE rb_text;
VALUE rb_sub_text;
VALUE rb_options;
VALUE rb_context;
grn_ctx *context;
grn_obj text;
grn_obj sub_text;

rb_scan_args(argc, argv, "21", &rb_text, &rb_sub_text, &rb_options);

rb_grn_scan_options(rb_options,
"context", &rb_context,
NULL);
context = rb_grn_context_ensure(&rb_context);

GRN_VOID_INIT(&text);
GRN_VOID_INIT(&sub_text);
RVAL2GRNBULK(rb_text, context, &text);
RVAL2GRNBULK(rb_sub_text, context, &sub_text);
matched = grn_operator_exec_match(context, &text, &sub_text);
GRN_OBJ_FIN(context, &text);
GRN_OBJ_FIN(context, &sub_text);

return CBOOL2RVAL(matched);
}

void
rb_grn_init_match_operator (VALUE mGrn)
{
rb_cGrnMatchOperator = rb_define_class_under(mGrn,
"MatchOperator",
rb_cGrnOperator);

rb_define_method(rb_cGrnMatchOperator, "exec",
rb_grn_match_operator_exec, -1);
}
3 changes: 2 additions & 1 deletion ext/groonga/rb-grn-operator.c
Expand Up @@ -307,6 +307,7 @@ rb_grn_init_operator (VALUE mGrn)
rb_grn_init_greater_operator(mGrn);
rb_grn_init_less_equal_operator(mGrn);
rb_grn_init_greater_equal_operator(mGrn);
rb_grn_init_match_operator(mGrn);

rb_define_const(rb_cGrnOperator, "PUSH",
rb_funcall(rb_cGrnOperator, rb_intern("new"), 2,
Expand Down Expand Up @@ -458,7 +459,7 @@ rb_grn_init_operator (VALUE mGrn)
rb_str_new_cstr("in"),
UINT2NUM(GRN_OP_IN)));
rb_define_const(rb_cGrnOperator, "MATCH",
rb_funcall(rb_cGrnOperator, rb_intern("new"), 2,
rb_funcall(rb_cGrnMatchOperator, rb_intern("new"), 2,
rb_str_new_cstr("match"),
UINT2NUM(GRN_OP_MATCH)));
rb_define_const(rb_cGrnOperator, "NEAR",
Expand Down
2 changes: 2 additions & 0 deletions ext/groonga/rb-grn.h
Expand Up @@ -292,6 +292,7 @@ RB_GRN_VAR VALUE rb_cGrnLessOperator;
RB_GRN_VAR VALUE rb_cGrnGreaterOperator;
RB_GRN_VAR VALUE rb_cGrnLessEqualOperator;
RB_GRN_VAR VALUE rb_cGrnGreaterEqualOperator;
RB_GRN_VAR VALUE rb_cGrnMatchOperator;
RB_GRN_VAR VALUE rb_cGrnExpression;
RB_GRN_VAR VALUE rb_cGrnRecordExpressionBuilder;
RB_GRN_VAR VALUE rb_cGrnColumnExpressionBuilder;
Expand Down Expand Up @@ -337,6 +338,7 @@ void rb_grn_init_less_operator (VALUE mGrn);
void rb_grn_init_greater_operator (VALUE mGrn);
void rb_grn_init_less_equal_operator (VALUE mGrn);
void rb_grn_init_greater_equal_operator (VALUE mGrn);
void rb_grn_init_match_operator (VALUE mGrn);
void rb_grn_init_expression (VALUE mGrn);
void rb_grn_init_expression_builder (VALUE mGrn);
void rb_grn_init_logger (VALUE mGrn);
Expand Down
21 changes: 21 additions & 0 deletions test/test-operator.rb
Expand Up @@ -142,4 +142,25 @@ class OperatorTest < Test::Unit::TestCase
end
end
end

sub_test_case "match" do
sub_test_case "#exec" do
test "match" do
assert_true(Groonga::Operator::MATCH.exec("Hello Rroonga",
"Rroonga"))
end

test "not match" do
assert_false(Groonga::Operator::MATCH.exec("Hello Rroonga",
"Groonga"))
end

test ":context" do
context = Groonga::Context.new
assert_true(Groonga::Operator::MATCH.exec("Hello Rroonga",
"Rroonga",
:context => context))
end
end
end
end

0 comments on commit c8ed815

Please sign in to comment.