Re: [PATCH 3/9] fsck: factor out msg_id_info[] lazy initialization code
On Thu, May 10, 2018 at 10:19 AM Nguyễn Thái Ngọc Duywrote: > This array will be used by some other function than parse_msg_id() in > the following commit. Factor out this prep code so it could be called > from that one. > Signed-off-by: Nguyễn Thái Ngọc Duy > --- > diff --git a/fsck.c b/fsck.c > @@ -84,26 +84,32 @@ static struct { > -static int parse_msg_id(const char *text) > +static void prepare_msg_ids(void) > { > - if (!msg_id_info[0].downcased) { > - /* convert id_string to lower case, without underscores. */ > - for (i = 0; i < FSCK_MSG_MAX; i++) { > - [...] > - } > + /* convert id_string to lower case, without underscores. */ > + for (i = 0; i < FSCK_MSG_MAX; i++) { > + [...] > } > +} > + > +static int parse_msg_id(const char *text) > +{ > + if (!msg_id_info[0].downcased) > + prepare_msg_ids(); If you move the "if (!msg_id_info...)" conditional into the new parpare_msg_ids() function, then it becomes self-contained; it takes care of avoiding double-initialization so callers don't have to worry or know about it. (Doing so would also make the diff less noisy.) Not at all worth a re-roll.
[PATCH 3/9] fsck: factor out msg_id_info[] lazy initialization code
This array will be used by some other function than parse_msg_id() in the following commit. Factor out this prep code so it could be called from that one. Signed-off-by: Nguyễn Thái Ngọc Duy--- fsck.c | 38 ++ 1 file changed, 22 insertions(+), 16 deletions(-) diff --git a/fsck.c b/fsck.c index 9218c2a643..f2534abd44 100644 --- a/fsck.c +++ b/fsck.c @@ -84,26 +84,32 @@ static struct { }; #undef MSG_ID -static int parse_msg_id(const char *text) +static void prepare_msg_ids(void) { int i; - if (!msg_id_info[0].downcased) { - /* convert id_string to lower case, without underscores. */ - for (i = 0; i < FSCK_MSG_MAX; i++) { - const char *p = msg_id_info[i].id_string; - int len = strlen(p); - char *q = xmalloc(len); - - msg_id_info[i].downcased = q; - while (*p) - if (*p == '_') - p++; - else - *(q)++ = tolower(*(p)++); - *q = '\0'; - } + /* convert id_string to lower case, without underscores. */ + for (i = 0; i < FSCK_MSG_MAX; i++) { + const char *p = msg_id_info[i].id_string; + int len = strlen(p); + char *q = xmalloc(len); + + msg_id_info[i].downcased = q; + while (*p) + if (*p == '_') + p++; + else + *(q)++ = tolower(*(p)++); + *q = '\0'; } +} + +static int parse_msg_id(const char *text) +{ + int i; + + if (!msg_id_info[0].downcased) + prepare_msg_ids(); for (i = 0; i < FSCK_MSG_MAX; i++) if (!strcmp(text, msg_id_info[i].downcased)) -- 2.17.0.705.g3525833791