Happy new year
[idzebra-moved-to-github.git] / index / rpnsearch.c
index ff57c68..9aee46e 100644 (file)
@@ -1,5 +1,5 @@
 /* This file is part of the Zebra server.
-   Copyright (C) 1994-2011 Index Data
+   Copyright (C) Index Data
 
 Zebra is free software; you can redistribute it and/or modify it under
 the terms of the GNU General Public License as published by the Free
@@ -187,7 +187,7 @@ static void esc_str(char *out_buf, size_t out_size,
     assert(in_buf);
     assert(out_size > 20);
     *out_buf = '\0';
-    for (k = 0; k<in_size; k++)
+    for (k = 0; k < in_size; k++)
     {
        int c = in_buf[k] & 0xff;
        int pc;
@@ -204,7 +204,7 @@ static void esc_str(char *out_buf, size_t out_size,
     }
 }
 
-#define REGEX_CHARS " ^[]()|.*+?!\"$"
+#define REGEX_CHARS " ^[]()|.*+?!\"$\\"
 
 static void add_non_space(const char *start, const char *end,
                           WRBUF term_dict,
@@ -234,6 +234,20 @@ static void add_non_space(const char *start, const char *end,
 }
 
 
+/* ICU sort keys seem to be of the form
+   basechars \x01 accents \x01 length
+   For now we'll just right truncate from basechars . This
+   may give false hits due to accents not being used.
+*/
+static size_t icu_basechars(const char *buf, size_t i)
+{
+    while (i > 0 && buf[--i] != '\x01') /* skip length */
+        ;
+    while (i > 0 && buf[--i] != '\x01') /* skip accents */
+        ;
+    return i; /* only basechars left */
+}
+
 static int term_102_icu(zebra_map_t zm,
                         const char **src, WRBUF term_dict, int space_split,
                         WRBUF display_term)
@@ -264,15 +278,8 @@ static int term_102_icu(zebra_map_t zm,
                 if (zebra_map_tokenize_next(zm, &res_buf, &res_len,
                                             &display_buf, &display_len))
                 {
-                    size_t i = res_len;
-                    while (--i >= 0 && res_buf[i] != '\x01')
-                        ;
-                    if (i > 0)
-                    {
-                        while (--i >= 0 && res_buf[i] != '\x01')
-                            ;
-                    }
-                    res_len = i; /* reduce res_len */
+                    size_t i;
+                    res_len = icu_basechars(res_buf, res_len);
                     for (i = 0; i < res_len; i++)
                     {
                         if (strchr(REGEX_CHARS "\\", res_buf[i]))
@@ -342,24 +349,7 @@ static int term_100_icu(zebra_map_t zm,
     wrbuf_write(display_term, display_buf, display_len);
     if (mode)
     {
-        /* ICU sort keys seem to be of the form
-           basechars \x01 accents \x01 length
-           For now we'll just right truncate from basechars . This
-           may give false hits due to accents not being used.
-        */
-        i = res_len;
-        while (--i >= 0 && res_buf[i] != '\x01')
-            ;
-        if (i > 0)
-        {
-            while (--i >= 0 && res_buf[i] != '\x01')
-                ;
-        }
-        if (i == 0)
-        {  /* did not find base chars at all. Throw error */
-            return -1;
-        }
-        res_len = i; /* reduce res_len */
+        res_len = icu_basechars(res_buf, res_len);
     }
     if (mode & 2)
         wrbuf_puts(term_dict, ".*");
@@ -734,7 +724,7 @@ static void gen_regular_rel(WRBUF term_dict, int val, int islt)
             else
                 dst[dst_p++] = d;
         }
-        for (i = 0; i<pos; i++)
+        for (i = 0; i < pos; i++)
         {
             dst[dst_p++] = '[';
             dst[dst_p++] = '0';
@@ -749,7 +739,7 @@ static void gen_regular_rel(WRBUF term_dict, int val, int islt)
     {
         /* match everything less than 10^(pos-1) */
         strcat(dst, "0*");
-        for (i = 1; i<pos; i++)
+        for (i = 1; i < pos; i++)
             strcat(dst, "[0-9]?");
     }
     else
@@ -1110,7 +1100,7 @@ static ZEBRA_RES string_term(ZebraHandle zh, Z_AttributesPlusTerm *zapt,
 
     wrbuf_putc(term_dict, '(');
 
-    for (i = 0; i<ord_len; i++)
+    for (i = 0; i < ord_len; i++)
     {
         wrbuf_putc(term_dict, 1);  /* our internal regexp escape char */
         wrbuf_putc(term_dict, ord_buf[i]);
@@ -1594,7 +1584,7 @@ static ZEBRA_RES rpn_search_APT_phrase(ZebraHandle zh,
         if (res != ZEBRA_OK)
         {
             int i;
-            for (i = 0; i<num_result_sets; i++)
+            for (i = 0; i < num_result_sets; i++)
                 rset_delete(result_sets[i]);
             return res;
         }
@@ -1650,7 +1640,7 @@ static ZEBRA_RES rpn_search_APT_or_list(ZebraHandle zh,
     if (res != ZEBRA_OK)
        return res;
 
-    for (i = 0; i<num_result_sets; i++)
+    for (i = 0; i < num_result_sets; i++)
     {
         RSET first_set = 0;
         res = search_position(zh, zapt, attributeSet,
@@ -1659,7 +1649,7 @@ static ZEBRA_RES rpn_search_APT_or_list(ZebraHandle zh,
                               kc);
         if (res != ZEBRA_OK)
         {
-            for (i = 0; i<num_result_sets; i++)
+            for (i = 0; i < num_result_sets; i++)
                 rset_delete(result_sets[i]);
             return res;
         }
@@ -1718,7 +1708,7 @@ static ZEBRA_RES rpn_search_APT_and_list(ZebraHandle zh,
                           kc);
     if (res != ZEBRA_OK)
        return res;
-    for (i = 0; i<num_result_sets; i++)
+    for (i = 0; i < num_result_sets; i++)
     {
         RSET first_set = 0;
         res = search_position(zh, zapt, attributeSet,
@@ -1727,7 +1717,7 @@ static ZEBRA_RES rpn_search_APT_and_list(ZebraHandle zh,
                               kc);
         if (res != ZEBRA_OK)
         {
-            for (i = 0; i<num_result_sets; i++)
+            for (i = 0; i < num_result_sets; i++)
                 rset_delete(result_sets[i]);
             return res;
         }
@@ -2073,7 +2063,7 @@ static ZEBRA_RES rpn_sort_spec(ZebraHandle zh, Z_AttributesPlusTerm *zapt,
         sort_sequence->specs = (Z_SortKeySpec **)
             nmem_malloc(stream, sort_sequence->num_specs *
                         sizeof(*sort_sequence->specs));
-        for (i = 0; i<sort_sequence->num_specs; i++)
+        for (i = 0; i < sort_sequence->num_specs; i++)
             sort_sequence->specs[i] = 0;
     }
     if (zapt->term->which != Z_Term_general)
@@ -2165,7 +2155,7 @@ static RSET xpath_trunc(ZebraHandle zh, NMEM stream,
         const char *flags = "void";
 
         wrbuf_putc(term_dict, '(');
-        for (i = 0; i<ord_len; i++)
+        for (i = 0; i < ord_len; i++)
         {
             wrbuf_putc(term_dict, 1);
             wrbuf_putc(term_dict, ord_buf[i]);
@@ -2207,7 +2197,7 @@ ZEBRA_RES rpn_search_xpath(ZebraHandle zh,
     }
 
     yaz_log(YLOG_DEBUG, "xpath len=%d", xpath_len);
-    for (i = 0; i<xpath_len; i++)
+    for (i = 0; i < xpath_len; i++)
     {
         yaz_log(log_level_rpn, "XPATH %d %s", i, xpath[i].part);
 
@@ -2580,7 +2570,7 @@ ZEBRA_RES rpn_search_top(ZebraHandle zh, Z_RPNStructure *zs,
     if (res != ZEBRA_OK)
     {
        int i;
-       for (i = 0; i<num_result_sets; i++)
+       for (i = 0; i < num_result_sets; i++)
            rset_delete(result_sets[i]);
        *result_set = 0;
     }
@@ -2623,7 +2613,7 @@ ZEBRA_RES rpn_search_structure(ZebraHandle zh, Z_RPNStructure *zs,
        if (res != ZEBRA_OK)
        {
            int i;
-           for (i = 0; i<num_result_sets_l; i++)
+           for (i = 0; i < num_result_sets_l; i++)
                rset_delete(result_sets_l[i]);
            return res;
        }
@@ -2636,9 +2626,9 @@ ZEBRA_RES rpn_search_structure(ZebraHandle zh, Z_RPNStructure *zs,
        if (res != ZEBRA_OK)
        {
            int i;
-           for (i = 0; i<num_result_sets_l; i++)
+           for (i = 0; i < num_result_sets_l; i++)
                rset_delete(result_sets_l[i]);
-           for (i = 0; i<num_result_sets_r; i++)
+           for (i = 0; i < num_result_sets_r; i++)
                rset_delete(result_sets_r[i]);
            return res;
        }