diff options
author | Michael J. Chudobiak <mjc@avtechpulse.com> | 2012-08-17 08:42:01 -0400 |
---|---|---|
committer | Michael J. Chudobiak <mjc@avtechpulse.com> | 2012-08-17 08:42:01 -0400 |
commit | 5dded60be2dfa7284d3760f1fe48eb4bf04b94e4 (patch) | |
tree | 6c1ff41eadcfaea08b76e3214f295b164f8c34cd /parser.c | |
parent | fcb2f1c3eb72ec2a9c8f85edc394efde2c0178c8 (diff) |
refactor regex filtering code
Diffstat (limited to 'parser.c')
-rw-r--r-- | parser.c | 25 |
1 files changed, 13 insertions, 12 deletions
@@ -514,33 +514,34 @@ static int Parser_get_unit(char **parameter, char **units) return units_present; } + +static gchar* regex_replace (gchar* in_string, gchar* regex_string, gchar* replace_with) +{ + GRegex *regex = g_regex_new (regex_string, 0, 0, NULL); + gchar *out = g_regex_replace_literal (regex, in_string, -1, 0, replace_with, 0, NULL); + g_regex_unref (regex); + return out; +} + static gchar* filter_input (gchar *raw_in) { g_strstrip (raw_in); gchar *step1 = g_strdup (raw_in); // replace multiple whitespace with single space - GRegex *strip_space = g_regex_new ("\\s+", 0, 0, NULL); - gchar *step2 = g_regex_replace_literal (strip_space, step1, -1, 0, " ", 0, NULL); - g_regex_unref (strip_space); + gchar *step2 = regex_replace (step1, "\\s+", " "); g_free (step1); // remove spaces before and after semicolons in compound messages - GRegex *semi_space = g_regex_new ("\\s*;\\s*", 0, 0, NULL); - gchar *step3 = g_regex_replace_literal (semi_space, step2, -1, 0, ";", 0, NULL); - g_regex_unref (semi_space); + gchar *step3 = regex_replace (step2, "\\s*;\\s*", ";"); g_free (step2); // remove semicolon at end - GRegex *end_semi = g_regex_new (";$", 0, 0, NULL); - gchar *step4 = g_regex_replace_literal (end_semi, step3, -1, 0, "", 0, NULL); - g_regex_unref (end_semi); + gchar *step4 = regex_replace (step3, ";$", ""); g_free (step3); // remove leading spaces, colons, semicolons - GRegex *leading = g_regex_new ("^[\\s:;]*", 0, 0, NULL); - gchar *step5 = g_regex_replace_literal (leading, step4, -1, 0, "", 0, NULL); - g_regex_unref (leading); + gchar *step5 = regex_replace (step4, "^[\\s:;]*", ""); g_free (step4); // last step may leave hanging whitespace at end |