Kouhei Sutou
null+****@clear*****
Tue Apr 14 13:04:29 JST 2015
Kouhei Sutou 2015-04-14 13:04:29 +0900 (Tue, 14 Apr 2015) New Revision: 37f8084920742e09c7debe14230467a686f6d90d https://github.com/ranguba/rroonga/commit/37f8084920742e09c7debe14230467a686f6d90d Message: Add Groonga::PrefixOperator#exec Added files: ext/groonga/rb-grn-prefix-operator.c Modified files: ext/groonga/rb-grn-operator.c ext/groonga/rb-grn.h test/test-operator.rb Modified: ext/groonga/rb-grn-operator.c (+2 -1) =================================================================== --- ext/groonga/rb-grn-operator.c 2015-04-14 11:50:49 +0900 (6596fc1) +++ ext/groonga/rb-grn-operator.c 2015-04-14 13:04:29 +0900 (f89fb22) @@ -308,6 +308,7 @@ rb_grn_init_operator (VALUE mGrn) rb_grn_init_less_equal_operator(mGrn); rb_grn_init_greater_equal_operator(mGrn); rb_grn_init_match_operator(mGrn); + rb_grn_init_prefix_operator(mGrn); rb_define_const(rb_cGrnOperator, "PUSH", rb_funcall(rb_cGrnOperator, rb_intern("new"), 2, @@ -555,7 +556,7 @@ rb_grn_init_operator (VALUE mGrn) rb_str_new_cstr("unsplit"), UINT2NUM(GRN_OP_UNSPLIT))); rb_define_const(rb_cGrnOperator, "PREFIX", - rb_funcall(rb_cGrnOperator, rb_intern("new"), 2, + rb_funcall(rb_cGrnPrefixOperator, rb_intern("new"), 2, rb_str_new_cstr("prefix"), UINT2NUM(GRN_OP_PREFIX))); rb_define_const(rb_cGrnOperator, "SUFFIX", Added: ext/groonga/rb-grn-prefix-operator.c (+86 -0) 100644 =================================================================== --- /dev/null +++ ext/groonga/rb-grn-prefix-operator.c 2015-04-14 13:04:29 +0900 (c2dd74e) @@ -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_cGrnPrefixOperator; + +/* + * Executes a prefix-search operation. Prefix-serach operation checks + * whether `text` starts with `prefix` or not. + * + * @example Executes prefix-search operations with the default context + * Groonga::Operator::PREFIX.exec("Hello Rroonga", "Hello") # => true + * Groonga::Operator::PREFIX.exec("Hello Rroonga", "Rroonga") # => false + * + * @example Executes prefix-search operations with the specified context + * context = Groonga::Context.new + * Groonga::Operator::PREFIX.exec("Hello Rroonga", "Hello", + * :context => context) # => true + * Groonga::Operator::PREFIX.exec("Hello Rroonga", "Rroonga", + * :context => context) # => false + * + * @overload exec(text, prefix, options={}) + * @param text [String] The text to be searched. + * @param prefix [String] The prefix 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` starts with `prefix`, `false` + * otherwise. + */ +static VALUE +rb_grn_prefix_operator_exec (int argc, VALUE *argv, VALUE self) +{ + grn_bool have_prefix; + VALUE rb_text; + VALUE rb_prefix; + VALUE rb_options; + VALUE rb_context; + grn_ctx *context; + grn_obj text; + grn_obj prefix; + + rb_scan_args(argc, argv, "21", &rb_text, &rb_prefix, &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(&prefix); + RVAL2GRNBULK(rb_text, context, &text); + RVAL2GRNBULK(rb_prefix, context, &prefix); + have_prefix = grn_operator_exec_prefix(context, &text, &prefix); + GRN_OBJ_FIN(context, &text); + GRN_OBJ_FIN(context, &prefix); + + return CBOOL2RVAL(have_prefix); +} + +void +rb_grn_init_prefix_operator (VALUE mGrn) +{ + rb_cGrnPrefixOperator = rb_define_class_under(mGrn, + "PrefixOperator", + rb_cGrnOperator); + + rb_define_method(rb_cGrnPrefixOperator, "exec", + rb_grn_prefix_operator_exec, -1); +} Modified: ext/groonga/rb-grn.h (+2 -0) =================================================================== --- ext/groonga/rb-grn.h 2015-04-14 11:50:49 +0900 (215343e) +++ ext/groonga/rb-grn.h 2015-04-14 13:04:29 +0900 (d5c12a3) @@ -293,6 +293,7 @@ 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_cGrnPrefixOperator; RB_GRN_VAR VALUE rb_cGrnExpression; RB_GRN_VAR VALUE rb_cGrnRecordExpressionBuilder; RB_GRN_VAR VALUE rb_cGrnColumnExpressionBuilder; @@ -339,6 +340,7 @@ 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_prefix_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); Modified: test/test-operator.rb (+21 -0) =================================================================== --- test/test-operator.rb 2015-04-14 11:50:49 +0900 (160985e) +++ test/test-operator.rb 2015-04-14 13:04:29 +0900 (af6aeb3) @@ -163,4 +163,25 @@ class OperatorTest < Test::Unit::TestCase end end end + + sub_test_case "prefix" do + sub_test_case "#exec" do + test "have prefix" do + assert_true(Groonga::Operator::PREFIX.exec("Hello Rroonga", + "Hello")) + end + + test "not have prefix" do + assert_false(Groonga::Operator::PREFIX.exec("Hello Rroonga", + "Rroonga")) + end + + test ":context" do + context = Groonga::Context.new + assert_true(Groonga::Operator::PREFIX.exec("Hello Rroonga", + "Hello", + :context => context)) + end + end + end end -------------- next part -------------- HTML����������������������������...Download