grim/gplate

Added gplate_util_get_first_word
trying_to_get_includes_working
2008-02-20, grim
f8757d67d034
Parents 2ef334beb587
Children a36b6d820500
Added gplate_util_get_first_word
Marked the regex's to be optimized since we cache them.
Other crazy cleanups.
--- a/gplate/gplate-util.c Wed Feb 20 00:48:17 2008 -0600
+++ b/gplate/gplate-util.c Wed Feb 20 00:54:01 2008 -0600
@@ -22,9 +22,10 @@
#include <gplate/gplate-errors.h>
/******************************************************************************
- * Globals
+ * Regex's
*****************************************************************************/
-static GRegex *quoted_string_regex = NULL;
+#define QUOTED_REGEX "\\s*(\"(.+)\"|'(.+)')\\s*"
+#define FIRST_WORD_REGEX "\\s*([a-zA-Z_][a-zA-Z0-9_\\.]*)(\\s*(.*))?"
/******************************************************************************
* API
@@ -82,17 +83,16 @@
*/
gboolean
gplate_util_is_quoted_string(const gchar *str, gchar **contents) {
+ static GRegex *regex = NULL;
GMatchInfo *info = NULL;
gboolean ret = FALSE;
g_return_val_if_fail(str, FALSE);
- if(!quoted_string_regex) {
- quoted_string_regex = g_regex_new("\\s*(\"(.+)\"|'(.+)')\\s*",
- 0, 0, NULL);
- }
+ if(!regex)
+ regex = g_regex_new(QUOTED_REGEX, G_REGEX_OPTIMIZE, 0, NULL);
- ret = g_regex_match(quoted_string_regex, str, 0, &info);
+ ret = g_regex_match(regex, str, 0, &info);
if(ret && contents)
*contents = g_match_info_fetch(info, 1);
@@ -101,3 +101,42 @@
return ret;
}
+
+/**
+ * gplate_util_get_first_word:
+ * @str: The string who's first word to get.
+ * @leftovers: The return address for everything after the first word.
+ *
+ * Gets the first word of a @str and returns it. The remaining contents of
+ * @str are stored in @leftovers if provided.
+ *
+ * The use of "word" is a bit of a misnomer. A word in this sense is a series
+ * of characters that begins with an letter or underscore and is followed by
+ * any number of letters, numbers, periods, or underscores.
+ *
+ * Return Value: The first word of @str, or NULL.
+ */
+gchar *
+gplate_util_get_first_word(const gchar *str, gchar **leftovers) {
+ static GRegex *regex = NULL;
+ GMatchInfo *info = NULL;
+ gchar *ret = NULL;
+
+ g_return_val_if_fail(str, NULL);
+
+ if(!regex)
+ regex = g_regex_new(FIRST_WORD_REGEX, G_REGEX_OPTIMIZE, 0, NULL);
+
+ if(!g_regex_match(regex, str, 0, &info))
+ return NULL;
+
+ ret = g_match_info_fetch(info, 1);
+
+ if(leftovers && g_match_info_get_match_count(info) > 2)
+ *leftovers = g_match_info_fetch(info, 3);
+
+ g_match_info_free(info);
+
+ return ret;
+}
+