From 3c857900fe9a7eff87aa4fbe6e7b80ba602ad2bd Mon Sep 17 00:00:00 2001 From: Felipe Oliveira Carvalho Date: Sat, 13 Dec 2014 11:10:19 -0300 Subject: Define and use the ARRAY_SIZE macro A similar macro is defined in the Linux kernel [1]. To refactor the code I used a slightly modified Coccinelle script I found in [2]. ```diff // Use the macro ARRAY_SIZE when possible // // Confidence: High // Copyright: (C) Gilles Muller, Julia Lawall, EMN, DIKU. GPLv2. // URL: http://www.emn.fr/x-info/coccinelle/rules/array.html // Options: -I ... -all_includes can give more complete results @@ type T; T[] E; @@ - (sizeof(E)/sizeof(*E)) + ARRAY_SIZE(E) @@ type T; T[] E; @@ - (sizeof(E)/sizeof(E[...])) + ARRAY_SIZE(E) @@ type T; T[] E; @@ - (sizeof(E)/sizeof(T)) + ARRAY_SIZE(E) @n@ identifier AS,E; @@ - #define AS(E) ARRAY_SIZE(E) @@ expression E; identifier n.AS; @@ - AS(E) + ARRAY_SIZE(E) ``` `spatch --in-place --sp-file array_size.cocci -I src/ -I build/include/ -I build/src/nvim/auto/ src/nvim/*.c` [1] http://lxr.free-electrons.com/source/include/linux/kernel.h#L54 [2] http://www.emn.fr/z-info/coccinelle/rules/#macros --- src/nvim/mbyte.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/nvim/mbyte.c') diff --git a/src/nvim/mbyte.c b/src/nvim/mbyte.c index db4516527a..08d9e50208 100644 --- a/src/nvim/mbyte.c +++ b/src/nvim/mbyte.c @@ -2138,7 +2138,7 @@ int utf_class(int c) {0x2f800, 0x2fa1f, 0x4e00}, /* CJK Ideographs */ }; int bot = 0; - int top = sizeof(classes) / sizeof(struct clinterval) - 1; + int top = ARRAY_SIZE(classes) - 1; int mid; /* First quick check for Latin1 characters, use 'iskeyword'. */ -- cgit From 2140f8f1f048f9b666915d1788cf8250f9adae62 Mon Sep 17 00:00:00 2001 From: Felipe Oliveira Carvalho Date: Sat, 13 Dec 2014 11:28:55 -0300 Subject: Change the signature of intable() (mbyte.c) to use ARRAY_SIZE --- src/nvim/mbyte.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) (limited to 'src/nvim/mbyte.c') diff --git a/src/nvim/mbyte.c b/src/nvim/mbyte.c index 08d9e50208..0edd872047 100644 --- a/src/nvim/mbyte.c +++ b/src/nvim/mbyte.c @@ -925,9 +925,9 @@ static int dbcs_ptr2len_len(const char_u *p, int size) } /* - * Return true if "c" is in "table[size / sizeof(struct interval)]". + * Return true if "c" is in "table". */ -static bool intable(const struct interval *table, size_t size, int c) +static bool intable(const struct interval *table, size_t n_items, int c) { int mid, bot, top; @@ -937,7 +937,7 @@ static bool intable(const struct interval *table, size_t size, int c) /* binary search in table */ bot = 0; - top = (int)(size / sizeof(struct interval) - 1); + top = (int)(n_items - 1); while (top >= bot) { mid = (bot + top) / 2; if (table[mid].last < c) @@ -1204,7 +1204,7 @@ int utf_char2cells(int c) #else if (!utf_printable(c)) return 6; /* unprintable, displays */ - if (intable(doublewidth, sizeof(doublewidth), c)) + if (intable(doublewidth, ARRAY_SIZE(doublewidth), c)) return 2; #endif } @@ -1212,7 +1212,7 @@ int utf_char2cells(int c) else if (c >= 0x80 && !vim_isprintc(c)) return 4; /* unprintable, displays */ - if (c >= 0x80 && *p_ambw == 'd' && intable(ambiguous, sizeof(ambiguous), c)) + if (c >= 0x80 && *p_ambw == 'd' && intable(ambiguous, ARRAY_SIZE(ambiguous), c)) return 2; return 1; @@ -2026,7 +2026,7 @@ bool utf_iscomposing(int c) {0xe0100, 0xe01ef} }; - return intable(combining, sizeof(combining), c); + return intable(combining, ARRAY_SIZE(combining), c); } /* @@ -2050,7 +2050,7 @@ bool utf_printable(int c) {0xfffe, 0xffff} }; - return !intable(nonprint, sizeof(nonprint), c); + return !intable(nonprint, ARRAY_SIZE(nonprint), c); #endif } -- cgit From 478c99c128df506479b37d7ffbd1adee3134607f Mon Sep 17 00:00:00 2001 From: Felipe Oliveira Carvalho Date: Sat, 13 Dec 2014 11:35:00 -0300 Subject: Change the signature of utf_convert() (mbyte.c) to use ARRAY_SIZE --- src/nvim/mbyte.c | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) (limited to 'src/nvim/mbyte.c') diff --git a/src/nvim/mbyte.c b/src/nvim/mbyte.c index 0edd872047..429fbc7427 100644 --- a/src/nvim/mbyte.c +++ b/src/nvim/mbyte.c @@ -2346,13 +2346,12 @@ static convertStruct foldCase[] = * Return the converted equivalent of "a", which is a UCS-4 character. Use * the given conversion "table". Uses binary search on "table". */ -static int utf_convert(int a, convertStruct *table, int tableSize) +static int utf_convert(int a, convertStruct *table, size_t n_items) { - int start, mid, end; /* indices into table */ - int entries = tableSize / sizeof(convertStruct); + size_t start, mid, end; /* indices into table */ start = 0; - end = entries; + end = n_items; while (start < end) { /* need to search further */ mid = (end + start) / 2; @@ -2361,7 +2360,7 @@ static int utf_convert(int a, convertStruct *table, int tableSize) else end = mid; } - if (start < entries + if (start < n_items && table[start].rangeStart <= a && a <= table[start].rangeEnd && (a - table[start].rangeStart) % table[start].step == 0) @@ -2376,7 +2375,7 @@ static int utf_convert(int a, convertStruct *table, int tableSize) */ int utf_fold(int a) { - return utf_convert(a, foldCase, (int)sizeof(foldCase)); + return utf_convert(a, foldCase, ARRAY_SIZE(foldCase)); } static convertStruct toLower[] = @@ -2702,7 +2701,7 @@ int utf_toupper(int a) return TOUPPER_LOC(a); /* For any other characters use the above mapping table. */ - return utf_convert(a, toUpper, (int)sizeof(toUpper)); + return utf_convert(a, toUpper, ARRAY_SIZE(toUpper)); } bool utf_islower(int a) @@ -2732,7 +2731,7 @@ int utf_tolower(int a) return TOLOWER_LOC(a); /* For any other characters use the above mapping table. */ - return utf_convert(a, toLower, (int)sizeof(toLower)); + return utf_convert(a, toLower, ARRAY_SIZE(toLower)); } bool utf_isupper(int a) -- cgit