{"thread":{"id":"52022","subject":"[PATCH] utf8: use ARRAY_SIZE() in git_wcwidth()","startedAt":"2019-10-11T18:36:10Z","lastAt":"2019-10-12T01:57:27Z","messageCount":3,"participants":["Beat Bolli","Junio C Hamano"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"383915","messageId":"20191011182448.11420-1-dev+git@drbeat.li","threadId":"52022","inReplyTo":null,"subject":"[PATCH] utf8: use ARRAY_SIZE() in git_wcwidth()","fromName":"Beat Bolli","fromEmail":"dev+git@drbeat.li","sentAt":"2019-10-11T18:24:48Z","receivedAt":"2019-10-11T18:36:10Z","isPatch":true,"sender":{"key":"dev+git@drbeat.li","avatar":"https://avatars.githubusercontent.com/u/21444?v=4"},"body":"This macro has been available globally since b4f2a6ac92 (\"Use #define\nARRAY_SIZE(x) (sizeof(x)/sizeof(x[0]))\", 2006-03-09), so let's use it.\n\nSigned-off-by: Beat Bolli <dev+git@drbeat.li>\n---\nIt's idle to speculate about why the macro wasn't used in the original\n9e83266525 (\"commit-tree: encourage UTF-8 commit messages.\", 2006-12-22)\nor why it hasn't been cleaned up since then. Possible reasons may be the\nline wrap within the division or the fact that one sizeof() takes the\nvariable while the other one takes the type. Either of these would make\na normal search fail.\n\n utf8.c | 6 ++----\n 1 file changed, 2 insertions(+), 4 deletions(-)\n\ndiff --git a/utf8.c b/utf8.c\nindex 3b42fadffd..164e20c817 100644\n--- a/utf8.c\n+++ b/utf8.c\n@@ -95,13 +95,11 @@ static int git_wcwidth(ucs_char_t ch)\n \t\treturn -1;\n \n \t/* binary search in table of non-spacing characters */\n-\tif (bisearch(ch, zero_width, sizeof(zero_width)\n-\t\t\t\t/ sizeof(struct interval) - 1))\n+\tif (bisearch(ch, zero_width, ARRAY_SIZE(zero_width) - 1)\n \t\treturn 0;\n \n \t/* binary search in table of double width characters */\n-\tif (bisearch(ch, double_width, sizeof(double_width)\n-\t\t\t\t/ sizeof(struct interval) - 1))\n+\tif (bisearch(ch, double_width, ARRAY_SIZE(double_width) - 1)\n \t\treturn 2;\n \n \treturn 1;\n-- \n2.21.0.1020.gf2820cf01a\n\n"},{"id":"383917","messageId":"20191011184123.17376-1-dev+git@drbeat.li","threadId":"52022","inReplyTo":"20191011182448.11420-1-dev+git@drbeat.li","subject":"[PATCH v2] utf8: use ARRAY_SIZE() in git_wcwidth()","fromName":"Beat Bolli","fromEmail":"dev+git@drbeat.li","sentAt":"2019-10-11T18:41:23Z","receivedAt":"2019-10-11T18:41:36Z","isPatch":true,"sender":{"key":"dev+git@drbeat.li","avatar":"https://avatars.githubusercontent.com/u/21444?v=4"},"body":"This macro has been available globally since b4f2a6ac92 (\"Use #define\nARRAY_SIZE(x) (sizeof(x)/sizeof(x[0]))\", 2006-03-09), so let's use it.\n\nSigned-off-by: Beat Bolli <dev+git@drbeat.li>\n---\nBrown paperbag fix. Sorry!\n\nIt's idle to speculate about why the macro wasn't used in the original\n9e83266525 (\"commit-tree: encourage UTF-8 commit messages.\", 2006-12-22)\nor why it hasn't been cleaned up since then. Possible reasons may be the\nline wrap within the division or the fact that one sizeof() takes the\nvariable while the other one takes the type. Either of these would make\na normal search fail.\n\n utf8.c | 6 ++----\n 1 file changed, 2 insertions(+), 4 deletions(-)\n\ndiff --git a/utf8.c b/utf8.c\nindex 3b42fadffd..5c8f151f75 100644\n--- a/utf8.c\n+++ b/utf8.c\n@@ -95,13 +95,11 @@ static int git_wcwidth(ucs_char_t ch)\n \t\treturn -1;\n \n \t/* binary search in table of non-spacing characters */\n-\tif (bisearch(ch, zero_width, sizeof(zero_width)\n-\t\t\t\t/ sizeof(struct interval) - 1))\n+\tif (bisearch(ch, zero_width, ARRAY_SIZE(zero_width) - 1))\n \t\treturn 0;\n \n \t/* binary search in table of double width characters */\n-\tif (bisearch(ch, double_width, sizeof(double_width)\n-\t\t\t\t/ sizeof(struct interval) - 1))\n+\tif (bisearch(ch, double_width, ARRAY_SIZE(double_width) - 1))\n \t\treturn 2;\n \n \treturn 1;\n-- \n2.21.0.1020.gf2820cf01a\n\n"},{"id":"383962","messageId":"xmqqo8ymenep.fsf@gitster-ct.c.googlers.com","threadId":"52022","inReplyTo":"20191011184123.17376-1-dev+git@drbeat.li","subject":"Re: [PATCH v2] utf8: use ARRAY_SIZE() in git_wcwidth()","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2019-10-12T01:57:18Z","receivedAt":"2019-10-12T01:57:27Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Beat Bolli <dev+git@drbeat.li> writes:\n\n> diff --git a/utf8.c b/utf8.c\n> index 3b42fadffd..5c8f151f75 100644\n> --- a/utf8.c\n> +++ b/utf8.c\n> @@ -95,13 +95,11 @@ static int git_wcwidth(ucs_char_t ch)\n>  \t\treturn -1;\n>  \n>  \t/* binary search in table of non-spacing characters */\n> -\tif (bisearch(ch, zero_width, sizeof(zero_width)\n> -\t\t\t\t/ sizeof(struct interval) - 1))\n> +\tif (bisearch(ch, zero_width, ARRAY_SIZE(zero_width) - 1))\n\nI wondered if we want a wrapper similar to QSORT() macro so that we\ndo not have to repeat zero_width (and double_width below) like this,\nbut bisearch() itself is local to this file and there are only these\ntwo callers, so it probably is not worth it.\n\nThe original is waiting for a bug when zero_width changes its type,\nby not dividing with sizeof(*zero_width).  The use of ARRAY_SIZE()\nis quite appropriate here.\n\nThanks.\n\n>  \t\treturn 0;\n>  \n>  \t/* binary search in table of double width characters */\n> -\tif (bisearch(ch, double_width, sizeof(double_width)\n> -\t\t\t\t/ sizeof(struct interval) - 1))\n> +\tif (bisearch(ch, double_width, ARRAY_SIZE(double_width) - 1))\n>  \t\treturn 2;\n>  \n>  \treturn 1;\n"}]}