GlibのGRegexをglibcのregex.hで置き換える
X11 Mouse Cursor plug-in for GIMP 2.0.5とGIMP 2.4 用のX11 Mouse Cursor plug-in for GIMP 1.1.5をリリースした。細かいバグを潰している。
GIMP 2.4 ではGLibの最小バージョンは2.12だがGRegexはGLib-2.14以降でのみ利用できるので、1.1.5に関してはGRegexをregex.hで置き換えた。その要になる部分を整理してdiff形式でメモしておく。
やっていることは「framename」に格納された文字列に対して「(XXms)」や「(XXpx)」(XXは数字)となる部分文字列を探し、数字を「digits」に、"ms"や"px"を「suffix」に格納することである。
+#include <regex.h> - GRegex re; + regex_t re; - GMatchInfo *info = NULL; + regmatch_t info[3]; /* store matched atoms' offsets */ + const gchar *p; /* the place of "framename" on which we try to match regex */ - re = g_regex_new ("[(][ ]*(\\d+)[ ]*(px|ms)[ ]*[)]", - G_REGEX_CASELESS | G_REGEX_OPTIMIZE, - 0, - NULL); + regcomp (&re, "[(][ ]*([[:digit:]]+)[ ]*(px|ms)[ ]*[)]", REG_ICASE | REG_EXTENDED); - for (g_regex_match (re, framename, 0, &info); g_match_info_matches (info); g_match_info_free (info)) + for (p = framename; regexec(&re, p, 3, info, 0) == 0; p += info[0].rm_eo) { - digits = g_match_info_fetch (info, 1); + digits = g_strndup (p + info[1].rm_so, info[1].rm_eo - info[1].rm_so); - suffix = g_match_info_fetch (info, 2); + suffix = g_strndup (p + info[2].rm_so, info[2].rm_eo - info[2].rm_so); - g_match_info_next (info, NULL); } - g_regex_unref(re); + regfree(&re);