Commit ce8d16a5 authored by Morten Welinder's avatar Morten Welinder

Cleanup: extract implementation of SEARCH into gutils.c

We need it for conditional formats.
parent 72249987
2014-03-21 Morten Welinder <terra@gnome.org>
* src/gutils.c (gnm_excel_search_impl): New function, extracted
from fn-string.
* src/style-conditions.c (generate_end_match): Use case
insensitive comparison. Pull negation into the operator.
(gnm_style_cond_eval): "Start with..." and "Ends with..." are case
......
2014-03-21 Morten Welinder <terra@gnome.org>
* functions.c (gnumeric_search): Use gnm_excel_search_impl.
2014-03-18 Morten Welinder <terra@gnome.org>
* Release 1.12.13
......
......@@ -1328,41 +1328,15 @@ gnumeric_search (GnmFuncEvalInfo *ei, GnmValue const * const *argv)
char const *needle = value_peek_string (argv[0]);
char const *haystack = value_peek_string (argv[1]);
gnm_float start = argv[2] ? value_get_as_float (argv[2]) : 1.0;
size_t i, istart;
char const *hay2;
GORegexp r;
int res;
if (start < 1 || start >= INT_MAX)
return value_new_error_VALUE (ei->pos);
/* Make istart zero-based. */
istart = (int)(start - 1);
for (i = istart, hay2 = haystack; i > 0; i--) {
if (*hay2 == 0)
return value_new_error_VALUE (ei->pos);
hay2 = g_utf8_next_char (hay2);
}
if (gnm_regcomp_XL (&r, needle, GO_REG_ICASE, FALSE, FALSE) == GO_REG_OK) {
GORegmatch rm;
switch (go_regexec (&r, hay2, 1, &rm, 0)) {
case GO_REG_NOMATCH:
break;
case GO_REG_OK:
go_regfree (&r);
return value_new_int
(1 + istart +
g_utf8_pointer_to_offset (hay2, hay2 + rm.rm_so));
default:
g_warning ("Unexpected go_regexec result");
}
go_regfree (&r);
} else {
g_warning ("Unexpected regcomp result");
}
return value_new_error_VALUE (ei->pos);
res = gnm_excel_search_impl (needle, haystack, (int)start - 1);
return res == -1
? value_new_error_VALUE (ei->pos)
: value_new_int (1 + res);
}
/***************************************************************************/
......
......@@ -375,6 +375,55 @@ gnm_regcomp_XL (GORegexp *preg, char const *pattern, int cflags,
return retval;
}
/**
* gnm_excel_search_impl:
*
* @needle: the pattern to search for, see gnm_regcomp_XL.
* @haystack: the string to search in.
* @skip: zero-based search start point in characters.
*
* Returns: -1 for a non-match, or zero-based location in
* characters.
*
* The is the implementation of Excel's SEARCH function.
* However, note that @skip and return value are zero-based.
*/
int
gnm_excel_search_impl (const char *needle, const char *haystack,
size_t skip)
{
const char *hay2;
size_t i;
GORegexp r;
for (i = skip, hay2 = haystack; i > 0; i--) {
if (*hay2 == 0)
return -1;
hay2 = g_utf8_next_char (hay2);
}
if (gnm_regcomp_XL (&r, needle, GO_REG_ICASE, FALSE, FALSE) == GO_REG_OK) {
GORegmatch rm;
switch (go_regexec (&r, hay2, 1, &rm, 0)) {
case GO_REG_NOMATCH:
break;
case GO_REG_OK:
go_regfree (&r);
return skip +
g_utf8_pointer_to_offset (hay2, hay2 + rm.rm_so);
default:
g_warning ("Unexpected go_regexec result");
}
go_regfree (&r);
} else {
g_warning ("Unexpected regcomp result");
}
return -1;
}
#if 0
static char const *
color_to_string (PangoColor color)
......
......@@ -26,6 +26,8 @@ long gnm_utf8_strtol (const char *s, char **end);
int gnm_regcomp_XL (GORegexp *preg, char const *pattern, int cflags,
gboolean anchor_start, gboolean anchor_end);
int gnm_excel_search_impl (const char *needle, const char *haystack,
size_t skip);
gboolean gnm_pango_attr_list_equal (PangoAttrList const *l1, PangoAttrList const *l2);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment