[gnumeric] Cleanup: extract implementation of SEARCH into gutils.c
- From: Morten Welinder <mortenw src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gnumeric] Cleanup: extract implementation of SEARCH into gutils.c
- Date: Fri, 21 Mar 2014 15:31:23 +0000 (UTC)
commit ce8d16a58529386d3a3c9d7550046f54fc495655
Author: Morten Welinder <terra gnome org>
Date: Fri Mar 21 11:30:44 2014 -0400
Cleanup: extract implementation of SEARCH into gutils.c
We need it for conditional formats.
ChangeLog | 3 ++
plugins/fn-string/ChangeLog | 4 +++
plugins/fn-string/functions.c | 36 ++++--------------------------
src/gutils.c | 49 +++++++++++++++++++++++++++++++++++++++++
src/gutils.h | 2 +
5 files changed, 63 insertions(+), 31 deletions(-)
---
diff --git a/ChangeLog b/ChangeLog
index e6d60e5..4a41187 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,8 @@
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
diff --git a/plugins/fn-string/ChangeLog b/plugins/fn-string/ChangeLog
index c5054b0..66cccea 100644
--- a/plugins/fn-string/ChangeLog
+++ b/plugins/fn-string/ChangeLog
@@ -1,3 +1,7 @@
+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
diff --git a/plugins/fn-string/functions.c b/plugins/fn-string/functions.c
index 9c4d21c..7e0b536 100644
--- a/plugins/fn-string/functions.c
+++ b/plugins/fn-string/functions.c
@@ -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);
}
/***************************************************************************/
diff --git a/src/gutils.c b/src/gutils.c
index 9b54974..d4ebe01 100644
--- a/src/gutils.c
+++ b/src/gutils.c
@@ -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)
diff --git a/src/gutils.h b/src/gutils.h
index 7a88408..faa96a0 100644
--- a/src/gutils.h
+++ b/src/gutils.h
@@ -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);
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]