Kouhei Sutou
null+****@clear*****
Tue Jan 21 11:17:01 JST 2014
Kouhei Sutou 2014-01-21 11:17:01 +0900 (Tue, 21 Jan 2014) New Revision: 8ae01c6c6756ecdbfaa0cf455a41e0a49092188d https://github.com/groonga/groonga/commit/8ae01c6c6756ecdbfaa0cf455a41e0a49092188d Message: Add normalizer accessor to grn_snip New functions: * grn_snip_set_normalizer() * grn_snip_get_normalizer() Modified files: include/groonga.h lib/snip.c lib/snip.h test/unit/util/test-snip.c Modified: include/groonga.h (+4 -1) =================================================================== --- include/groonga.h 2014-01-20 21:30:23 +0900 (fa8de78) +++ include/groonga.h 2014-01-21 11:17:01 +0900 (200690a) @@ -1,5 +1,5 @@ /* - Copyright(C) 2009-2013 Brazil + Copyright(C) 2009-2014 Brazil This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public @@ -1019,6 +1019,9 @@ GRN_API grn_rc grn_snip_add_cond(grn_ctx *ctx, grn_snip *snip, const char *keyword, unsigned int keyword_len, const char *opentag, unsigned int opentag_len, const char *closetag, unsigned int closetag_len); +GRN_API grn_rc grn_snip_set_normalizer(grn_ctx *ctx, grn_snip *snip, + grn_obj *normalizer); +GRN_API grn_obj *grn_snip_get_normalizer(grn_ctx *ctx, grn_snip *snip); GRN_API grn_rc grn_snip_exec(grn_ctx *ctx, grn_snip *snip, const char *string, unsigned int string_len, unsigned int *nresults, unsigned int *max_tagged_len); Modified: lib/snip.c (+27 -8) =================================================================== --- lib/snip.c 2014-01-20 21:30:23 +0900 (38c2c89) +++ lib/snip.c 2014-01-21 11:17:01 +0900 (9940623) @@ -1,5 +1,5 @@ /* -*- c-basic-offset: 2 -*- */ -/* Copyright(C) 2009-2012 Brazil +/* Copyright(C) 2009-2014 Brazil This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public @@ -259,14 +259,12 @@ grn_snip_cond_close(grn_ctx *ctx, snip_cond *cond) grn_rc grn_snip_cond_init(grn_ctx *ctx, snip_cond *sc, const char *keyword, unsigned int keyword_len, - grn_encoding enc, int flags) + grn_encoding enc, grn_obj *normalizer, int flags) { const char *norm; unsigned int norm_blen; - grn_obj *normalizer = NULL; int f = GRN_STR_REMOVEBLANK; memset(sc, 0, sizeof(snip_cond)); - if (flags & GRN_SNIP_NORMALIZE) { normalizer = GRN_NORMALIZER_AUTO; } if (!(sc->keyword = grn_string_open(ctx, keyword, keyword_len, normalizer, f))) { GRN_LOG(ctx, GRN_LOG_ALERT, @@ -340,6 +338,24 @@ grn_snip_cond_set_tag(grn_ctx *ctx, } grn_rc +grn_snip_set_normalizer(grn_ctx *ctx, grn_snip *snip, + grn_obj *normalizer) +{ + if (!snip) { + return GRN_INVALID_ARGUMENT; + } + + snip->normalizer = normalizer; + return GRN_SUCCESS; +} + +grn_obj * +grn_snip_get_normalizer(grn_ctx *ctx, grn_snip *snip) +{ + return snip->normalizer; +} + +grn_rc grn_snip_add_cond(grn_ctx *ctx, grn_snip *snip, const char *keyword, unsigned int keyword_len, const char *opentag, unsigned int opentag_len, @@ -355,7 +371,7 @@ grn_snip_add_cond(grn_ctx *ctx, grn_snip *snip, } cond = snip->cond + snip->cond_len; if ((rc = grn_snip_cond_init(ctx, cond, keyword, keyword_len, - snip->encoding, snip->flags))) { + snip->encoding, snip->normalizer, snip->flags))) { return rc; } grn_string_get_normalized(ctx, cond->keyword, NULL, &norm_blen, NULL); @@ -488,6 +504,11 @@ grn_snip_open(grn_ctx *ctx, int flags, unsigned int width, ret->nstr = NULL; ret->tag_count = 0; ret->snip_count = 0; + if (ret->flags & GRN_SNIP_NORMALIZE) { + ret->normalizer = GRN_NORMALIZER_AUTO; + } else { + ret->normalizer = NULL; + } GRN_DB_OBJ_SET_TYPE(ret, GRN_SNIP); { @@ -565,7 +586,6 @@ grn_snip_exec(grn_ctx *ctx, grn_snip *snip, const char *string, unsigned int str unsigned int *nresults, unsigned int *max_tagged_len) { size_t i; - grn_obj *normalizer = NULL; int f = GRN_STR_WITH_CHECKS|GRN_STR_REMOVEBLANK; if (!snip || !string || !nresults || !max_tagged_len) { return GRN_INVALID_ARGUMENT; @@ -573,8 +593,7 @@ grn_snip_exec(grn_ctx *ctx, grn_snip *snip, const char *string, unsigned int str GRN_API_ENTER; exec_clean(ctx, snip); *nresults = 0; - if (snip->flags & GRN_SNIP_NORMALIZE) { normalizer = GRN_NORMALIZER_AUTO; } - snip->nstr = grn_string_open(ctx, string, string_len, normalizer, f); + snip->nstr = grn_string_open(ctx, string, string_len, snip->normalizer, f); if (!snip->nstr) { exec_clean(ctx, snip); GRN_LOG(ctx, GRN_LOG_ALERT, "grn_string_open on grn_snip_exec failed !"); Modified: lib/snip.h (+4 -2) =================================================================== --- lib/snip.h 2014-01-20 21:30:23 +0900 (12d87c3) +++ lib/snip.h 2014-01-21 11:17:01 +0900 (c626922) @@ -1,5 +1,5 @@ /* -*- c-basic-offset: 2 -*- */ -/* Copyright(C) 2009-2012 Brazil +/* Copyright(C) 2009-2014 Brazil This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public @@ -114,11 +114,13 @@ struct _grn_snip _snip_tag_result tag_result[MAX_SNIP_TAG_COUNT]; size_t max_tagged_len; + + grn_obj *normalizer; }; grn_rc grn_snip_close_real(grn_ctx *ctx, grn_snip *snip); grn_rc grn_snip_cond_init(grn_ctx *ctx, snip_cond *sc, const char *keyword, unsigned int keyword_len, - grn_encoding enc, int flags); + grn_encoding enc, grn_obj *normalizer, int flags); void grn_snip_cond_reinit(snip_cond *cond); grn_rc grn_snip_cond_close(grn_ctx *ctx, snip_cond *cond); void grn_bm_tunedbm(grn_ctx *ctx, snip_cond *cond, grn_obj *string, int flags); Modified: test/unit/util/test-snip.c (+27 -1) =================================================================== --- test/unit/util/test-snip.c 2014-01-20 21:30:23 +0900 (60f30a0) +++ test/unit/util/test-snip.c 2014-01-21 11:17:01 +0900 (5a18f1d) @@ -1,6 +1,6 @@ /* -*- c-basic-offset: 2; coding: utf-8 -*- */ /* - Copyright (C) 2008-2012 Kouhei Sutou <kou �� clear-code.com> + Copyright (C) 2008-2014 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 @@ -44,6 +44,8 @@ void test_open_with_invalid_max_results(void); void test_add_cond_with_invalid_argument(void); void test_add_cond_with_too_large_keyword(void); void test_add_cond_with_copy_tag_flag(void); +void test_flag_normalize(void); +void test_normalizer_accessor(void); static grn_ctx context; static grn_obj *database; @@ -946,3 +948,27 @@ test_add_cond_with_copy_tag(void) NULL, 0, NULL, 0)); } + +void +test_flag_normalize(void) +{ + default_flags = GRN_SNIP_NORMALIZE; + cut_assert_open_snip(); + cut_assert_equal_pointer(GRN_NORMALIZER_AUTO, + grn_snip_get_normalizer(&context, snip)); +} + +void +test_normalizer_accessor(void) +{ + grn_obj *normalizer; + + cut_assert_open_snip(); + cut_assert_null(grn_snip_get_normalizer(&context, snip)); + + normalizer = grn_ctx_get(&context, "NormalizerNFKC51", -1); + cut_assert_not_null(normalizer); + + grn_snip_set_normalizer(&context, snip, normalizer); + cut_assert_equal_pointer(normalizer, grn_snip_get_normalizer(&context, snip)); +} -------------- next part -------------- HTML����������������������������...Download