Kouhei Sutou
null+****@clear*****
Mon Jan 21 17:56:26 JST 2013
Kouhei Sutou 2013-01-21 17:56:26 +0900 (Mon, 21 Jan 2013) New Revision: ac4c05151f91fc13444497bc474ea15c0b36247f https://github.com/groonga/groonga/commit/ac4c05151f91fc13444497bc474ea15c0b36247f Log: Use grn_string_open() instead of deprecated grn_str_open() grn_str_open() is deprecated but it should not be removed. Modified files: plugins/suggest/suggest.c test/unit/util/test-string.c Modified: plugins/suggest/suggest.c (+17 -6) =================================================================== --- plugins/suggest/suggest.c 2013-01-21 17:55:05 +0900 (0d84cfc) +++ plugins/suggest/suggest.c 2013-01-21 17:56:26 +0900 (b47174c) @@ -304,16 +304,25 @@ complete(grn_ctx *ctx, grn_obj *items, grn_obj *items_boost, grn_obj *col, if ((res = grn_table_create(ctx, NULL, 0, NULL, GRN_TABLE_HASH_KEY|GRN_OBJ_WITH_SUBREC, items, NULL))) { grn_id tid = grn_table_get(ctx, items, TEXT_VALUE_LEN(query)); - grn_str *norm; + grn_obj *string; if (GRN_TEXT_LEN(query) && - (norm = grn_str_open(ctx, TEXT_VALUE_LEN(query), GRN_STR_NORMALIZE))) { + (string = grn_string_open(ctx, TEXT_VALUE_LEN(query), + GRN_NORMALIZER_AUTO, 0))) { grn_table_cursor *cur; /* RK search + prefix search */ grn_obj *index; - /* FIXME: support index selection */ + const char *normalized; + unsigned int normalized_length_in_bytes; + grn_string_get_normalized(ctx, string, + &normalized, + &normalized_length_in_bytes, + NULL); + /* FIXME: support index selection */ if (grn_column_index(ctx, col, GRN_OP_PREFIX, &index, 1, NULL)) { if ((cur = grn_table_cursor_open(ctx, grn_ctx_at(ctx, index->header.domain), - norm->norm, norm->norm_blen, NULL, 0, 0, -1, + normalized, + normalized_length_in_bytes, + NULL, 0, 0, -1, GRN_CURSOR_PREFIX|GRN_CURSOR_RK))) { grn_id id; while ((id = grn_table_cursor_next(ctx, cur))) { @@ -342,7 +351,9 @@ complete(grn_ctx *ctx, grn_obj *items, grn_obj *items_boost, grn_obj *col, if (((prefix_search_mode == GRN_SUGGEST_SEARCH_YES) || (prefix_search_mode == GRN_SUGGEST_SEARCH_AUTO && !grn_table_size(ctx, res))) && - (cur = grn_table_cursor_open(ctx, items, norm->norm, norm->norm_blen, + (cur = grn_table_cursor_open(ctx, items, + normalized, + normalized_length_in_bytes, NULL, 0, 0, -1, GRN_CURSOR_PREFIX))) { grn_id id; while ((id = grn_table_cursor_next(ctx, cur))) { @@ -351,7 +362,7 @@ complete(grn_ctx *ctx, grn_obj *items, grn_obj *items_boost, grn_obj *col, } grn_table_cursor_close(ctx, cur); } - grn_str_close(ctx, norm); + grn_obj_close(ctx, string); } output(ctx, items, res, tid, sortby, output_columns, offset, limit); grn_obj_close(ctx, res); Modified: test/unit/util/test-string.c (+34 -14) =================================================================== --- test/unit/util/test-string.c 2013-01-21 17:55:05 +0900 (b1b617b) +++ test/unit/util/test-string.c 2013-01-21 17:56:26 +0900 (05fcb67) @@ -190,26 +190,36 @@ test_normalize(gconstpointer data) { const gchar *utf8_expected, *encoded_expected; const gchar *utf8_input, *encoded_input; - grn_str *string; + grn_obj *string; const gchar *normalized_text; - guint normalized_text_len; + guint normalized_text_length; + guint normalized_text_n_characters; int flags; grn_encoding encoding; encoding = gcut_data_get_int(data, "encoding"); GRN_CTX_SET_ENCODING(&context, encoding); - flags = GRN_STR_NORMALIZE | GRN_STR_WITH_CHECKS | GRN_STR_WITH_CTYPES; + flags = GRN_STRING_WITH_CHECKS | GRN_STRING_WITH_TYPES; utf8_input = gcut_data_get_string(data, "input"); encoded_input = convert_encoding(utf8_input, encoding); - string = grn_str_open(&context, encoded_input, strlen(encoded_input), flags); - normalized_text = cut_take_strndup(string->norm, string->norm_blen); - normalized_text_len = string->norm_blen; - grn_test_assert(grn_str_close(&context, string)); + string = grn_string_open(&context, + encoded_input, + strlen(encoded_input), + GRN_NORMALIZER_AUTO, + flags); + grn_string_get_normalized(&context, string, + &normalized_text, + &normalized_text_length, + &normalized_text_n_characters); + normalized_text = cut_take_strndup(normalized_text, normalized_text_length); + grn_obj_unlink(&context, string); utf8_expected = gcut_data_get_string(data, "expected"); encoded_expected = convert_encoding(utf8_expected, encoding); cut_assert_equal_string(encoded_expected, normalized_text); - cut_assert_equal_int(strlen(encoded_expected), normalized_text_len); + cut_assert_equal_uint(strlen(encoded_expected), normalized_text_length); + cut_assert_equal_uint(g_utf8_strlen(utf8_expected, -1), + normalized_text_n_characters); } void @@ -243,11 +253,13 @@ data_normalize_broken(void) void test_normalize_broken(gconstpointer data) { - grn_str *string; + grn_obj *string; const gchar *input, *encoded_input; + const gchar *normalized_text; grn_encoding input_encoding, context_encoding; gint input_length; - int flags = GRN_STR_NORMALIZE | GRN_STR_WITH_CHECKS | GRN_STR_WITH_CTYPES; + guint normalized_text_length, normalized_text_n_characters; + int flags = GRN_STRING_WITH_CHECKS | GRN_STRING_WITH_TYPES; context_encoding = gcut_data_get_int(data, "context-encoding"); GRN_CTX_SET_ENCODING(&context, context_encoding); @@ -259,10 +271,18 @@ test_normalize_broken(gconstpointer data) if (input_length < 0) { input_length = strlen(encoded_input); } - string = grn_str_open(&context, encoded_input, input_length, flags); - cut_assert_equal_string("", string->norm); - cut_assert_equal_int(0, string->norm_blen); - grn_test_assert(grn_str_close(&context, string)); + string = grn_string_open(&context, encoded_input, input_length, + GRN_NORMALIZER_AUTO, flags); + grn_string_get_normalized(&context, string, + &normalized_text, + &normalized_text_length, + &normalized_text_n_characters); + normalized_text = cut_take_strndup(normalized_text, normalized_text_length); + grn_obj_unlink(&context, string); + + cut_assert_equal_string("", normalized_text); + cut_assert_equal_int(0, normalized_text_length); + cut_assert_equal_int(0, normalized_text_n_characters); } void -------------- next part -------------- HTML����������������������������...ダウンロード