From 04c0658024a98a0586997f0ea8af1e3f774cc83e Mon Sep 17 00:00:00 2001 From: Eliseo Martínez Date: Sun, 11 Jan 2015 12:55:38 +0100 Subject: Cleanup: Refactor getdigits(). Problem : getdigits() currently returns a long, but at most places, return value is casted (unsafely) into an int. Making casts safe would introduce a lot of fuss in the form of assertions checking for limits. Note : We cannot just change return type to int, because, at some places, legitimate long values are used. For example, in diff.c, for line numbers. Solution : Introduce new functions: - get_digits() : Gets an intmax_t from a string. - get_int_digits() : Wrapper for ints. - get_long_digits() : Wrapper for longs. And replace getdigits() invocations by the appropiate wrapper invocations. --- src/nvim/spell.c | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) (limited to 'src/nvim/spell.c') diff --git a/src/nvim/spell.c b/src/nvim/spell.c index b8713909b8..af020198d3 100644 --- a/src/nvim/spell.c +++ b/src/nvim/spell.c @@ -5162,7 +5162,7 @@ static unsigned get_affitem(int flagtype, char_u **pp) ++*pp; // always advance, avoid getting stuck return 0; } - res = getdigits(pp); + res = get_int_digits(pp); } else { res = mb_ptr2char_adv(pp); if (flagtype == AFT_LONG || (flagtype == AFT_CAPLONG @@ -5283,7 +5283,9 @@ static bool flag_in_afflist(int flagtype, char_u *afflist, unsigned flag) case AFT_NUM: for (p = afflist; *p != NUL; ) { - n = getdigits(&p); + int digits = get_int_digits(&p); + assert(digits >= 0); + n = (unsigned int)digits; if (n == flag) return true; if (*p != NUL) // skip over comma @@ -6357,19 +6359,19 @@ int spell_check_msm(void) if (!VIM_ISDIGIT(*p)) return FAIL; // block count = (value * 1024) / SBLOCKSIZE (but avoid overflow) - start = (getdigits(&p) * 10) / (SBLOCKSIZE / 102); + start = (get_long_digits(&p) * 10) / (SBLOCKSIZE / 102); if (*p != ',') return FAIL; ++p; if (!VIM_ISDIGIT(*p)) return FAIL; - incr = (getdigits(&p) * 102) / (SBLOCKSIZE / 10); + incr = (get_long_digits(&p) * 102) / (SBLOCKSIZE / 10); if (*p != ',') return FAIL; ++p; if (!VIM_ISDIGIT(*p)) return FAIL; - added = getdigits(&p) * 1024; + added = get_long_digits(&p) * 1024; if (*p != NUL) return FAIL; @@ -8355,7 +8357,7 @@ int spell_check_sps(void) f = 0; if (VIM_ISDIGIT(*buf)) { s = buf; - sps_limit = getdigits(&s); + sps_limit = get_int_digits(&s); if (*s != NUL && !VIM_ISDIGIT(*s)) f = -1; } else if (STRCMP(buf, "best") == 0) -- cgit From 7f7262e93390a1855ac9c687bd492eadfe10cf98 Mon Sep 17 00:00:00 2001 From: Eliseo Martínez Date: Sun, 11 Jan 2015 20:57:33 +0100 Subject: Cleanup: Rename getdigits() family functions. --- src/nvim/spell.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'src/nvim/spell.c') diff --git a/src/nvim/spell.c b/src/nvim/spell.c index af020198d3..5e69a935ca 100644 --- a/src/nvim/spell.c +++ b/src/nvim/spell.c @@ -5162,7 +5162,7 @@ static unsigned get_affitem(int flagtype, char_u **pp) ++*pp; // always advance, avoid getting stuck return 0; } - res = get_int_digits(pp); + res = getdigits_int(pp); } else { res = mb_ptr2char_adv(pp); if (flagtype == AFT_LONG || (flagtype == AFT_CAPLONG @@ -5283,7 +5283,7 @@ static bool flag_in_afflist(int flagtype, char_u *afflist, unsigned flag) case AFT_NUM: for (p = afflist; *p != NUL; ) { - int digits = get_int_digits(&p); + int digits = getdigits_int(&p); assert(digits >= 0); n = (unsigned int)digits; if (n == flag) @@ -6359,19 +6359,19 @@ int spell_check_msm(void) if (!VIM_ISDIGIT(*p)) return FAIL; // block count = (value * 1024) / SBLOCKSIZE (but avoid overflow) - start = (get_long_digits(&p) * 10) / (SBLOCKSIZE / 102); + start = (getdigits_long(&p) * 10) / (SBLOCKSIZE / 102); if (*p != ',') return FAIL; ++p; if (!VIM_ISDIGIT(*p)) return FAIL; - incr = (get_long_digits(&p) * 102) / (SBLOCKSIZE / 10); + incr = (getdigits_long(&p) * 102) / (SBLOCKSIZE / 10); if (*p != ',') return FAIL; ++p; if (!VIM_ISDIGIT(*p)) return FAIL; - added = get_long_digits(&p) * 1024; + added = getdigits_long(&p) * 1024; if (*p != NUL) return FAIL; @@ -8357,7 +8357,7 @@ int spell_check_sps(void) f = 0; if (VIM_ISDIGIT(*buf)) { s = buf; - sps_limit = get_int_digits(&s); + sps_limit = getdigits_int(&s); if (*s != NUL && !VIM_ISDIGIT(*s)) f = -1; } else if (STRCMP(buf, "best") == 0) -- cgit