Closed wooksong closed 5 years ago
다음과 같이 정규 표현식을 적용해 보았습니다.
#include <regex.h>
// . . .
static gboolean
_parse_bool_string (const gchar * strval, gboolean def)
{
gboolean res = def;
regex_t reg_true, reg_false;
regcomp (®_true, "^([1ty]|on).*", REG_EXTENDED | REG_ICASE | REG_NOSUB);
regcomp (®_false, "^([0fn]|of).*", REG_EXTENDED | REG_ICASE | REG_NOSUB);
if (strval) {
/* 1/0, true/false, t/f, yes/no, on/off. case incensitive. */
if (!regexec (®_true, strval, 0, 0, 0)) {
res = TRUE;
} else if (!regexec (®_false, strval, 0, 0, 0)) {
res = FALSE;
}
}
regfree (®_true);
regfree (®_false);
return res;
}
Merged!
In order to check matching patterns in a variable, we have used combined if-statements a lot. This is obviously very simple way, but also inefficient way IMO.
For example, Talse could be handled as True in the following code.
How about applying regular expressions to such logics in our project?