The issues I fixed while resolving this are these:
foo() was not checking step was valid, I added an IF_FAIL( step <= 0, SLRE_UNBALANCED_BRACKETS ); beneath it
set_len() was suboptimal with a needless conditional at the end
op_len() Had a sub optimal conditional
get_op_len() was always adding 1 to the result of set_len() preventing it's -1 from reaching foo()
Since foo() has only 1 line of change it's not worth displaying the code below, these 3 however are right next to each and small so I decided to just display the lot in their current state.
static int op_len(const char *re) {
return re[0] == '\\' ? (re[1] == 'x' ? 4 : 2) : 1;
}
static int set_len(const char *re, int re_len) {
int len = 0;
while (len < re_len) {
len += op_len(re + len);
if ( re[len] == ']' )
return len + 1;
}
return -1;
}
static int get_op_len(const char *re, int re_len) {
return re[0] == '[' ? set_len(re + 1, re_len - 1) : op_len(re);
}
The issues I fixed while resolving this are these:
foo()was not checkingstepwas valid, I added anIF_FAIL( step <= 0, SLRE_UNBALANCED_BRACKETS );beneath itset_len()was suboptimal with a needless conditional at the endop_len()Had a sub optimal conditionalget_op_len()was always adding 1 to the result ofset_len()preventing it's -1 from reachingfoo()Since
foo()has only 1 line of change it's not worth displaying the code below, these 3 however are right next to each and small so I decided to just display the lot in their current state.