diff options
Diffstat (limited to 'src/nvim/spellfile.c')
-rw-r--r-- | src/nvim/spellfile.c | 374 |
1 files changed, 129 insertions, 245 deletions
diff --git a/src/nvim/spellfile.c b/src/nvim/spellfile.c index 67b29e25a4..f83511dda7 100644 --- a/src/nvim/spellfile.c +++ b/src/nvim/spellfile.c @@ -594,16 +594,12 @@ static inline int spell_check_magic_string(FILE *const fd) /// @return the slang_T the spell file was loaded into. NULL for error. slang_T *spell_load_file(char *fname, char *lang, slang_T *old_lp, bool silent) { - FILE *fd; char *p; - int n; - int len; slang_T *lp = NULL; - int c = 0; int res; bool did_estack_push = false; - fd = os_fopen(fname, "r"); + FILE *fd = os_fopen(fname, "r"); if (fd == NULL) { if (!silent) { semsg(_(e_notopen), fname); @@ -650,7 +646,7 @@ slang_T *spell_load_file(char *fname, char *lang, slang_T *old_lp, bool silent) case 0: break; } - c = getc(fd); // <versionnr> + int c = getc(fd); // <versionnr> if (c < VIMSPELLVERSION) { emsg(_("E771: Old spell file, needs to be updated")); goto endFAIL; @@ -662,12 +658,12 @@ slang_T *spell_load_file(char *fname, char *lang, slang_T *old_lp, bool silent) // <SECTIONS>: <section> ... <sectionend> // <section>: <sectionID> <sectionflags> <sectionlen> (section contents) while (true) { - n = getc(fd); // <sectionID> or <sectionend> + int n = getc(fd); // <sectionID> or <sectionend> if (n == SN_END) { break; } c = getc(fd); // <sectionflags> - len = get4c(fd); // <sectionlen> + int len = get4c(fd); // <sectionlen> if (len < 0) { goto truncerr; } @@ -841,17 +837,14 @@ endOK: // Returns the total number of words. static void tree_count_words(const uint8_t *byts, idx_T *idxs) { - int depth; idx_T arridx[MAXWLEN]; int curi[MAXWLEN]; - int c; - idx_T n; int wordcount[MAXWLEN]; arridx[0] = 0; curi[0] = 1; wordcount[0] = 0; - depth = 0; + int depth = 0; while (depth >= 0 && !got_int) { if (curi[depth] > byts[arridx[depth]]) { // Done all bytes at this node, go up one level. @@ -864,10 +857,10 @@ static void tree_count_words(const uint8_t *byts, idx_T *idxs) fast_breakcheck(); } else { // Do one more byte at this node. - n = arridx[depth] + curi[depth]; + idx_T n = arridx[depth] + curi[depth]; curi[depth]++; - c = byts[n]; + int c = byts[n]; if (c == 0) { // End of word, count it. wordcount[depth]++; @@ -892,33 +885,25 @@ static void tree_count_words(const uint8_t *byts, idx_T *idxs) /// Load the .sug files for languages that have one and weren't loaded yet. void suggest_load_files(void) { - langp_T *lp; - slang_T *slang; - char *dotp; - FILE *fd; char buf[MAXWLEN]; - time_t timestamp; - int wcount; - int wordnr; garray_T ga; - int c; // Do this for all languages that support sound folding. for (int lpi = 0; lpi < curwin->w_s->b_langp.ga_len; lpi++) { - lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi); - slang = lp->lp_slang; + langp_T *lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi); + slang_T *slang = lp->lp_slang; if (slang->sl_sugtime != 0 && !slang->sl_sugloaded) { // Change ".spl" to ".sug" and open the file. When the file isn't // found silently skip it. Do set "sl_sugloaded" so that we // don't try again and again. slang->sl_sugloaded = true; - dotp = strrchr(slang->sl_fname, '.'); + char *dotp = strrchr(slang->sl_fname, '.'); if (dotp == NULL || path_fnamecmp(dotp, ".spl") != 0) { continue; } STRCPY(dotp, ".sug"); - fd = os_fopen(slang->sl_fname, "r"); + FILE *fd = os_fopen(slang->sl_fname, "r"); if (fd == NULL) { goto nextone; } @@ -932,7 +917,7 @@ void suggest_load_files(void) slang->sl_fname); goto nextone; } - c = getc(fd); // <versionnr> + int c = getc(fd); // <versionnr> if (c < VIMSUGVERSION) { semsg(_("E779: Old .sug file, needs to be updated: %s"), slang->sl_fname); @@ -945,7 +930,7 @@ void suggest_load_files(void) // Check the timestamp, it must be exactly the same as the one in // the .spl file. Otherwise the word numbers won't match. - timestamp = get8ctime(fd); // <timestamp> + time_t timestamp = get8ctime(fd); // <timestamp> if (timestamp != slang->sl_sugtime) { semsg(_("E781: .sug file doesn't match .spl file: %s"), slang->sl_fname); @@ -971,7 +956,7 @@ someerror: slang->sl_sugbuf = open_spellbuf(); // <sugwcount> - wcount = get4c(fd); + int wcount = get4c(fd); if (wcount < 0) { goto someerror; } @@ -979,7 +964,7 @@ someerror: // Read all the wordnr lists into the buffer, one NUL terminated // list per line. ga_init(&ga, 1, 100); - for (wordnr = 0; wordnr < wcount; wordnr++) { + for (int wordnr = 0; wordnr < wcount; wordnr++) { ga.ga_len = 0; while (true) { c = getc(fd); // <sugline> @@ -1020,7 +1005,6 @@ nextone: static char *read_cnt_string(FILE *fd, int cnt_bytes, int *cntp) { int cnt = 0; - char *str; // read the length bytes, MSB first for (int i = 0; i < cnt_bytes; i++) { @@ -1036,7 +1020,7 @@ static char *read_cnt_string(FILE *fd, int cnt_bytes, int *cntp) if (cnt == 0) { return NULL; // nothing to read, return NULL } - str = read_string(fd, (size_t)cnt); + char *str = read_string(fd, (size_t)cnt); if (str == NULL) { *cntp = SP_OTHERERROR; } @@ -1060,18 +1044,16 @@ static int read_region_section(FILE *fd, slang_T *lp, int len) // Return SP_*ERROR flags. static int read_charflags_section(FILE *fd) { - char *flags; - char *fol; int flagslen, follen; // <charflagslen> <charflags> - flags = read_cnt_string(fd, 1, &flagslen); + char *flags = read_cnt_string(fd, 1, &flagslen); if (flagslen < 0) { return flagslen; } // <folcharslen> <folchars> - fol = read_cnt_string(fd, 2, &follen); + char *fol = read_cnt_string(fd, 2, &follen); if (follen < 0) { xfree(flags); return follen; @@ -1129,10 +1111,9 @@ static int read_prefcond_section(FILE *fd, slang_T *lp) // Return SP_*ERROR flags. static int read_rep_section(FILE *fd, garray_T *gap, int16_t *first) { - int cnt; fromto_T *ftp; - cnt = get2c(fd); // <repcount> + int cnt = get2c(fd); // <repcount> if (cnt < 0) { return SP_TRUNCERROR; } @@ -1177,12 +1158,6 @@ static int read_rep_section(FILE *fd, garray_T *gap, int16_t *first) // Return SP_*ERROR flags. static int read_sal_section(FILE *fd, slang_T *slang) { - int cnt; - garray_T *gap; - salitem_T *smp; - int ccnt; - char *p; - slang->sl_sofo = false; const int flags = getc(fd); // <salflags> @@ -1196,12 +1171,12 @@ static int read_sal_section(FILE *fd, slang_T *slang) slang->sl_rem_accents = true; } - cnt = get2c(fd); // <salcount> + int cnt = get2c(fd); // <salcount> if (cnt < 0) { return SP_TRUNCERROR; } - gap = &slang->sl_sal; + garray_T *gap = &slang->sl_sal; ga_init(gap, sizeof(salitem_T), 10); ga_grow(gap, cnt + 1); @@ -1209,12 +1184,12 @@ static int read_sal_section(FILE *fd, slang_T *slang) for (; gap->ga_len < cnt; gap->ga_len++) { int c = NUL; - smp = &((salitem_T *)gap->ga_data)[gap->ga_len]; - ccnt = getc(fd); // <salfromlen> + salitem_T *smp = &((salitem_T *)gap->ga_data)[gap->ga_len]; + int ccnt = getc(fd); // <salfromlen> if (ccnt < 0) { return SP_TRUNCERROR; } - p = xmalloc((size_t)ccnt + 2); + char *p = xmalloc((size_t)ccnt + 2); smp->sm_lead = p; // Read up to the first special char into sm_lead. @@ -1287,8 +1262,8 @@ static int read_sal_section(FILE *fd, slang_T *slang) if (!GA_EMPTY(gap)) { // Add one extra entry to mark the end with an empty sm_lead. Avoids // that we need to check the index every time. - smp = &((salitem_T *)gap->ga_data)[gap->ga_len]; - p = xmalloc(1); + salitem_T *smp = &((salitem_T *)gap->ga_data)[gap->ga_len]; + char *p = xmalloc(1); p[0] = NUL; smp->sm_lead = p; smp->sm_lead_w = mb_str2wide(smp->sm_lead); @@ -1313,13 +1288,12 @@ static int read_words_section(FILE *fd, slang_T *lp, int len) { int done = 0; int i; - int c; uint8_t word[MAXWLEN]; while (done < len) { // Read one word at a time. for (i = 0;; i++) { - c = getc(fd); + int c = getc(fd); if (c == EOF) { return SP_TRUNCERROR; } @@ -1344,19 +1318,18 @@ static int read_words_section(FILE *fd, slang_T *lp, int len) static int read_sofo_section(FILE *fd, slang_T *slang) { int cnt; - char *from, *to; int res; slang->sl_sofo = true; // <sofofromlen> <sofofrom> - from = read_cnt_string(fd, 2, &cnt); + char *from = read_cnt_string(fd, 2, &cnt); if (cnt < 0) { return cnt; } // <sofotolen> <sofoto> - to = read_cnt_string(fd, 2, &cnt); + char *to = read_cnt_string(fd, 2, &cnt); if (cnt < 0) { xfree(from); return cnt; @@ -1382,15 +1355,13 @@ static int read_sofo_section(FILE *fd, slang_T *slang) static int read_compound(FILE *fd, slang_T *slang, int len) { int todo = len; - int c; - int atstart; int cnt; if (todo < 2) { return SP_FORMERROR; // need at least two bytes } todo--; - c = getc(fd); // <compmax> + int c = getc(fd); // <compmax> if (c < 2) { c = MAXWLEN; } @@ -1469,7 +1440,7 @@ static int read_compound(FILE *fd, slang_T *slang, int len) *pp++ = '\\'; *pp++ = '('; - atstart = 1; + int atstart = 1; while (todo-- > 0) { c = getc(fd); // <compflags> if (c == EOF) { @@ -1609,15 +1580,13 @@ static int set_sofo(slang_T *lp, const char *from, const char *to) // Fill the first-index table for "lp". static void set_sal_first(slang_T *lp) { - salfirst_T *sfirst; - salitem_T *smp; garray_T *gap = &lp->sl_sal; - sfirst = lp->sl_sal_first; + salfirst_T *sfirst = lp->sl_sal_first; for (int i = 0; i < 256; i++) { sfirst[i] = -1; } - smp = (salitem_T *)gap->ga_data; + salitem_T *smp = (salitem_T *)gap->ga_data; for (int i = 0; i < gap->ga_len; i++) { // Use the lowest byte of the first character. For latin1 it's // the character, for other encodings it should differ for most @@ -1679,10 +1648,6 @@ static int spell_read_tree(FILE *fd, uint8_t **bytsp, int *bytsp_len, idx_T **id bool prefixtree, int prefixcnt) FUNC_ATTR_NONNULL_ARG(1, 2, 4) { - int idx; - uint8_t *bp; - idx_T *ip; - // The tree size was computed when writing the file, so that we can // allocate it as one long block. <nodecount> int len = get4c(fd); @@ -1698,18 +1663,18 @@ static int spell_read_tree(FILE *fd, uint8_t **bytsp, int *bytsp_len, idx_T **id } // Allocate the byte array. - bp = xmalloc((size_t)len); + uint8_t *bp = xmalloc((size_t)len); *bytsp = bp; if (bytsp_len != NULL) { *bytsp_len = len; } // Allocate the index array. - ip = xcalloc((size_t)len, sizeof(*ip)); + idx_T *ip = xcalloc((size_t)len, sizeof(*ip)); *idxsp = ip; // Recursively read the tree and store it in the array. - idx = read_tree_node(fd, bp, ip, len, 0, prefixtree, prefixcnt); + int idx = read_tree_node(fd, bp, ip, len, 0, prefixtree, prefixcnt); if (idx < 0) { return idx; } @@ -1732,13 +1697,10 @@ static int spell_read_tree(FILE *fd, uint8_t **bytsp, int *bytsp_len, idx_T **id static idx_T read_tree_node(FILE *fd, uint8_t *byts, idx_T *idxs, int maxidx, idx_T startidx, bool prefixtree, int maxprefcondnr) { - int len; - int n; idx_T idx = startidx; - int c2; #define SHARED_MASK 0x8000000 - len = getc(fd); // <siblingcount> + int len = getc(fd); // <siblingcount> if (len <= 0) { return SP_TRUNCERROR; } @@ -1772,7 +1734,7 @@ static idx_T read_tree_node(FILE *fd, uint8_t *byts, idx_T *idxs, int maxidx, id c |= getc(fd); // <affixID> - n = get2c(fd); // <prefcondnr> + int n = get2c(fd); // <prefcondnr> if (n >= maxprefcondnr) { return SP_FORMERROR; } @@ -1781,7 +1743,7 @@ static idx_T read_tree_node(FILE *fd, uint8_t *byts, idx_T *idxs, int maxidx, id // Read flags and optional region and prefix ID. In // idxs[] the flags go in the low two bytes, region above // that and prefix ID above the region. - c2 = c; + int c2 = c; c = getc(fd); // <flags> if (c2 == BY_FLAGS2) { c = (getc(fd) << 8) + c; // <flags2> @@ -1798,7 +1760,7 @@ static idx_T read_tree_node(FILE *fd, uint8_t *byts, idx_T *idxs, int maxidx, id c = 0; } else { // c == BY_INDEX // <nodeidx> - n = get3c(fd); + int n = get3c(fd); if (n < 0 || n >= maxidx) { return SP_FORMERROR; } @@ -1834,10 +1796,9 @@ static idx_T read_tree_node(FILE *fd, uint8_t *byts, idx_T *idxs, int maxidx, id /// @param added_word invoked through "zg" static void spell_reload_one(char *fname, bool added_word) { - slang_T *slang; bool didit = false; - for (slang = first_lang; slang != NULL; slang = slang->sl_next) { + for (slang_T *slang = first_lang; slang != NULL; slang = slang->sl_next) { if (path_full_compare(fname, slang->sl_fname, false, true) == kEqualFiles) { slang_clear(slang); if (spell_load_file(fname, NULL, slang, false) == NULL) { @@ -1880,15 +1841,12 @@ static int compress_added = 500000; // word count int spell_check_msm(void) { char *p = p_msm; - int start = 0; - int incr = 0; - int added = 0; if (!ascii_isdigit(*p)) { return FAIL; } // block count = (value * 1024) / SBLOCKSIZE (but avoid overflow) - start = (getdigits_int(&p, true, 0) * 10) / (SBLOCKSIZE / 102); + int start = (getdigits_int(&p, true, 0) * 10) / (SBLOCKSIZE / 102); if (*p != ',') { return FAIL; } @@ -1896,7 +1854,7 @@ int spell_check_msm(void) if (!ascii_isdigit(*p)) { return FAIL; } - incr = (getdigits_int(&p, true, 0) * 102) / (SBLOCKSIZE / 10); + int incr = (getdigits_int(&p, true, 0) * 102) / (SBLOCKSIZE / 10); if (*p != ',') { return FAIL; } @@ -1904,7 +1862,7 @@ int spell_check_msm(void) if (!ascii_isdigit(*p)) { return FAIL; } - added = getdigits_int(&p, true, 0) * 1024; + int added = getdigits_int(&p, true, 0) * 1024; if (*p != NUL) { return FAIL; } @@ -2017,13 +1975,11 @@ static void spell_print_tree(wordnode_T *root) // Returns an afffile_T, NULL for complete failure. static afffile_T *spell_read_aff(spellinfo_T *spin, char *fname) { - FILE *fd; char rline[MAXLINELEN]; char *line; char *pc = NULL; #define MAXITEMCNT 30 char *(items[MAXITEMCNT]); - int itemcnt; char *p; int lnum = 0; affheader_T *cur_aff = NULL; @@ -2033,13 +1989,8 @@ static afffile_T *spell_read_aff(spellinfo_T *spin, char *fname) char *low = NULL; char *fol = NULL; char *upp = NULL; - int do_rep; - int do_repsal; - int do_sal; - int do_mapline; bool found_map = false; hashitem_T *hi; - int l; int compminlen = 0; // COMPOUNDMIN value int compsylmax = 0; // COMPOUNDSYLMAX value int compoptions = 0; // COMP_ flags @@ -2052,7 +2003,7 @@ static afffile_T *spell_read_aff(spellinfo_T *spin, char *fname) char *sofoto = NULL; // SOFOTO value // Open the file. - fd = os_fopen(fname, "r"); + FILE *fd = os_fopen(fname, "r"); if (fd == NULL) { semsg(_(e_notopen), fname); return NULL; @@ -2062,16 +2013,16 @@ static afffile_T *spell_read_aff(spellinfo_T *spin, char *fname) spell_message(spin, IObuff); // Only do REP lines when not done in another .aff file already. - do_rep = GA_EMPTY(&spin->si_rep); + int do_rep = GA_EMPTY(&spin->si_rep); // Only do REPSAL lines when not done in another .aff file already. - do_repsal = GA_EMPTY(&spin->si_repsal); + int do_repsal = GA_EMPTY(&spin->si_repsal); // Only do SAL lines when not done in another .aff file already. - do_sal = GA_EMPTY(&spin->si_sal); + int do_sal = GA_EMPTY(&spin->si_sal); // Only do MAP lines when not done in another .aff file already. - do_mapline = GA_EMPTY(&spin->si_map); + int do_mapline = GA_EMPTY(&spin->si_map); // Allocate and init the afffile_T structure. afffile_T *aff = getroom(spin, sizeof(*aff), true); @@ -2106,7 +2057,7 @@ static afffile_T *spell_read_aff(spellinfo_T *spin, char *fname) // Split the line up in white separated items. Put a NUL after each // item. - itemcnt = 0; + int itemcnt = 0; for (p = line;;) { while (*p != NUL && (uint8_t)(*p) <= ' ') { // skip white space and CR/NL p++; @@ -2252,7 +2203,7 @@ static afffile_T *spell_read_aff(spellinfo_T *spin, char *fname) if (compflags != NULL || *skipdigits(items[1]) != NUL) { // Concatenate this string to previously defined ones, // using a slash to separate them. - l = (int)strlen(items[1]) + 1; + int l = (int)strlen(items[1]) + 1; if (compflags != NULL) { l += (int)strlen(compflags) + 1; } @@ -2774,11 +2725,9 @@ static bool is_aff_rule(char **items, int itemcnt, char *rulename, int mincount) // ae_flags to ae_comppermit and ae_compforbid. static void aff_process_flags(afffile_T *affile, affentry_T *entry) { - char *p; - if (entry->ae_flags != NULL && (affile->af_compforbid != 0 || affile->af_comppermit != 0)) { - for (p = entry->ae_flags; *p != NUL;) { + for (char *p = entry->ae_flags; *p != NUL;) { char *prevp = p; unsigned flag = get_affitem(affile->af_flagtype, &p); if (flag == affile->af_comppermit || flag == affile->af_compforbid) { @@ -2870,12 +2819,9 @@ static unsigned get_affitem(int flagtype, char **pp) /// they fit in one byte. static void process_compflags(spellinfo_T *spin, afffile_T *aff, char *compflags) { - char *prevp; - unsigned flag; compitem_T *ci; int id; char key[AH_KEY_LEN]; - hashitem_T *hi; // Make room for the old and the new compflags, concatenated with a / in // between. Processing it makes it shorter, but we don't know by how @@ -2898,13 +2844,13 @@ static void process_compflags(spellinfo_T *spin, afffile_T *aff, char *compflags *tp++ = (uint8_t)(*p++); } else { // First get the flag number, also checks validity. - prevp = p; - flag = get_affitem(aff->af_flagtype, &p); + char *prevp = p; + unsigned flag = get_affitem(aff->af_flagtype, &p); if (flag != 0) { // Find the flag in the hashtable. If it was used before, use // the existing ID. Otherwise add a new entry. xstrlcpy(key, prevp, (size_t)(p - prevp) + 1); - hi = hash_find(&aff->af_comp, key); + hashitem_T *hi = hash_find(&aff->af_comp, key); if (!HASHITEM_EMPTY(hi)) { id = HI2CI(hi)->ci_newID; } else { @@ -2946,17 +2892,14 @@ static void check_renumber(spellinfo_T *spin) // Returns true if flag "flag" appears in affix list "afflist". static bool flag_in_afflist(int flagtype, char *afflist, unsigned flag) { - char *p; - unsigned n; - switch (flagtype) { case AFT_CHAR: return vim_strchr(afflist, (int)flag) != NULL; case AFT_CAPLONG: case AFT_LONG: - for (p = afflist; *p != NUL;) { - n = (unsigned)mb_ptr2char_adv((const char **)&p); + for (char *p = afflist; *p != NUL;) { + unsigned n = (unsigned)mb_ptr2char_adv((const char **)&p); if ((flagtype == AFT_LONG || (n >= 'A' && n <= 'Z')) && *p != NUL) { n = (unsigned)mb_ptr2char_adv((const char **)&p) + (n << 16); @@ -2968,10 +2911,10 @@ static bool flag_in_afflist(int flagtype, char *afflist, unsigned flag) break; case AFT_NUM: - for (p = afflist; *p != NUL;) { + for (char *p = afflist; *p != NUL;) { int digits = getdigits_int(&p, true, 0); assert(digits >= 0); - n = (unsigned)digits; + unsigned n = (unsigned)digits; if (n == 0) { n = ZERO_FLAG; } @@ -3037,21 +2980,16 @@ static bool sal_to_bool(char *s) // Free the structure filled by spell_read_aff(). static void spell_free_aff(afffile_T *aff) { - hashtab_T *ht; - hashitem_T *hi; - affheader_T *ah; - affentry_T *ae; - xfree(aff->af_enc); // All this trouble to free the "ae_prog" items... - for (ht = &aff->af_pref;; ht = &aff->af_suff) { + for (hashtab_T *ht = &aff->af_pref;; ht = &aff->af_suff) { int todo = (int)ht->ht_used; - for (hi = ht->ht_array; todo > 0; hi++) { + for (hashitem_T *hi = ht->ht_array; todo > 0; hi++) { if (!HASHITEM_EMPTY(hi)) { todo--; - ah = HI2AH(hi); - for (ae = ah->ah_first; ae != NULL; ae = ae->ae_next) { + affheader_T *ah = HI2AH(hi); + for (affentry_T *ae = ah->ah_first; ae != NULL; ae = ae->ae_next) { vim_regfree(ae->ae_prog); } } @@ -3072,22 +3010,13 @@ static int spell_read_dic(spellinfo_T *spin, char *fname, afffile_T *affile) { hashtab_T ht; char line[MAXLINELEN]; - char *p; - char *afflist; char store_afflist[MAXWLEN]; - int pfxlen; - bool need_affix; - char *dw; char *pc; char *w; - int l; - hash_T hash; - hashitem_T *hi; int lnum = 1; int non_ascii = 0; int retval = OK; char message[MAXLINELEN + MAXWLEN]; - int flags; int duplicate = 0; Timestamp last_msg_time = 0; @@ -3124,7 +3053,7 @@ static int spell_read_dic(spellinfo_T *spin, char *fname, afffile_T *affile) } // Remove CR, LF and white space from the end. White space halfway through // the word is kept to allow multi-word terms like "et al.". - l = (int)strlen(line); + int l = (int)strlen(line); while (l > 0 && (uint8_t)line[l - 1] <= ' ') { l--; } @@ -3149,8 +3078,8 @@ static int spell_read_dic(spellinfo_T *spin, char *fname, afffile_T *affile) // Truncate the word at the "/", set "afflist" to what follows. // Replace "\/" by "/" and "\\" by "\". - afflist = NULL; - for (p = w; *p != NUL; MB_PTR_ADV(p)) { + char *afflist = NULL; + for (char *p = w; *p != NUL; MB_PTR_ADV(p)) { if (*p == '\\' && (p[1] == '\\' || p[1] == '/')) { STRMOVE(p, p + 1); } else if (*p == '/') { @@ -3186,15 +3115,15 @@ static int spell_read_dic(spellinfo_T *spin, char *fname, afffile_T *affile) } // Store the word in the hashtable to be able to find duplicates. - dw = getroom_save(spin, w); + char *dw = getroom_save(spin, w); if (dw == NULL) { retval = FAIL; xfree(pc); break; } - hash = hash_hash(dw); - hi = hash_lookup(&ht, dw, strlen(dw), hash); + hash_T hash = hash_hash(dw); + hashitem_T *hi = hash_lookup(&ht, dw, strlen(dw), hash); if (!HASHITEM_EMPTY(hi)) { if (p_verbose > 0) { smsg(0, _("Duplicate word in %s line %d: %s"), @@ -3208,10 +3137,10 @@ static int spell_read_dic(spellinfo_T *spin, char *fname, afffile_T *affile) hash_add_item(&ht, hi, dw, hash); } - flags = 0; + int flags = 0; store_afflist[0] = NUL; - pfxlen = 0; - need_affix = false; + int pfxlen = 0; + bool need_affix = false; if (afflist != NULL) { // Extract flags from the affix list. flags |= get_affix_flags(affile, afflist); @@ -3317,9 +3246,7 @@ static int get_affix_flags(afffile_T *affile, char *afflist) static int get_pfxlist(afffile_T *affile, char *afflist, char *store_afflist) { int cnt = 0; - int id; char key[AH_KEY_LEN]; - hashitem_T *hi; for (char *p = afflist; *p != NUL;) { char *prevp = p; @@ -3327,9 +3254,9 @@ static int get_pfxlist(afffile_T *affile, char *afflist, char *store_afflist) // A flag is a postponed prefix flag if it appears in "af_pref" // and its ID is not zero. xstrlcpy(key, prevp, (size_t)(p - prevp) + 1); - hi = hash_find(&affile->af_pref, key); + hashitem_T *hi = hash_find(&affile->af_pref, key); if (!HASHITEM_EMPTY(hi)) { - id = HI2AH(hi)->ah_newID; + int id = HI2AH(hi)->ah_newID; if (id != 0) { store_afflist[cnt++] = (char)(uint8_t)id; } @@ -3351,14 +3278,13 @@ static void get_compflags(afffile_T *affile, char *afflist, char *store_afflist) { int cnt = 0; char key[AH_KEY_LEN]; - hashitem_T *hi; for (char *p = afflist; *p != NUL;) { char *prevp = p; if (get_affitem(affile->af_flagtype, &p) != 0) { // A flag is a compound flag if it appears in "af_comp". xstrlcpy(key, prevp, (size_t)(p - prevp) + 1); - hi = hash_find(&affile->af_comp, key); + hashitem_T *hi = hash_find(&affile->af_comp, key); if (!HASHITEM_EMPTY(hi)) { store_afflist[cnt++] = (char)(uint8_t)HI2CI(hi)->ci_newID; } @@ -3390,27 +3316,19 @@ static int store_aff_word(spellinfo_T *spin, char *word, char *afflist, afffile_ hashtab_T *ht, hashtab_T *xht, int condit, int flags, char *pfxlist, int pfxlen) { - hashitem_T *hi; - affheader_T *ah; affentry_T *ae; char newword[MAXWLEN]; int retval = OK; int j; - char *p; - int use_flags; - char *use_pfxlist; - int use_pfxlen; - bool need_affix; char store_afflist[MAXWLEN]; char pfx_pfxlist[MAXWLEN]; size_t wordlen = strlen(word); - int use_condit; int todo = (int)ht->ht_used; - for (hi = ht->ht_array; todo > 0 && retval == OK; hi++) { + for (hashitem_T *hi = ht->ht_array; todo > 0 && retval == OK; hi++) { if (!HASHITEM_EMPTY(hi)) { todo--; - ah = HI2AH(hi); + affheader_T *ah = HI2AH(hi); // Check that the affix combines, if required, and that the word // supports this affix. @@ -3449,7 +3367,7 @@ static int store_aff_word(spellinfo_T *spin, char *word, char *afflist, afffile_ } else { xstrlcpy(newword, ae->ae_add, MAXWLEN); } - p = word; + char *p = word; if (ae->ae_chop != NULL) { // Skip chop string. int i = mb_charlen(ae->ae_chop); @@ -3463,7 +3381,7 @@ static int store_aff_word(spellinfo_T *spin, char *word, char *afflist, afffile_ xstrlcpy(newword, word, MAXWLEN); if (ae->ae_chop != NULL) { // Remove chop string. - p = newword + strlen(newword); + char *p = newword + strlen(newword); int i = mb_charlen(ae->ae_chop); for (; i > 0; i--) { MB_PTR_BACK(newword, p); @@ -3475,11 +3393,11 @@ static int store_aff_word(spellinfo_T *spin, char *word, char *afflist, afffile_ } } - use_flags = flags; - use_pfxlist = pfxlist; - use_pfxlen = pfxlen; - need_affix = false; - use_condit = condit | CONDIT_COMB | CONDIT_AFF; + int use_flags = flags; + char *use_pfxlist = pfxlist; + int use_pfxlen = pfxlen; + bool need_affix = false; + int use_condit = condit | CONDIT_COMB | CONDIT_AFF; if (ae->ae_flags != NULL) { // Extract flags from the affix list. use_flags |= get_affix_flags(affile, ae->ae_flags); @@ -3631,21 +3549,16 @@ static int store_aff_word(spellinfo_T *spin, char *word, char *afflist, afffile_ // Read a file with a list of words. static int spell_read_wordfile(spellinfo_T *spin, char *fname) { - FILE *fd; linenr_T lnum = 0; char rline[MAXLINELEN]; char *line; char *pc = NULL; - char *p; - int l; int retval = OK; bool did_word = false; int non_ascii = 0; - int flags; - int regionmask; // Open the file. - fd = os_fopen(fname, "r"); + FILE *fd = os_fopen(fname, "r"); if (fd == NULL) { semsg(_(e_notopen), fname); return FAIL; @@ -3665,7 +3578,7 @@ static int spell_read_wordfile(spellinfo_T *spin, char *fname) } // Remove CR, LF and white space from the end. - l = (int)strlen(rline); + int l = (int)strlen(rline); while (l > 0 && (uint8_t)rline[l - 1] <= ' ') { l--; } @@ -3740,11 +3653,11 @@ static int spell_read_wordfile(spellinfo_T *spin, char *fname) continue; } - flags = 0; - regionmask = spin->si_region; + int flags = 0; + int regionmask = spin->si_region; // Check for flags and region after a slash. - p = vim_strchr(line, '/'); + char *p = vim_strchr(line, '/'); if (p != NULL) { *p++ = NUL; while (*p != NUL) { @@ -3813,7 +3726,6 @@ static int spell_read_wordfile(spellinfo_T *spin, char *fname) static void *getroom(spellinfo_T *spin, size_t len, bool align) FUNC_ATTR_NONNULL_RET { - char *p; sblock_T *bl = spin->si_blocks; assert(len <= SBLOCKSIZE); @@ -3833,7 +3745,7 @@ static void *getroom(spellinfo_T *spin, size_t len, bool align) spin->si_blocks_cnt++; } - p = bl->sb_data + bl->sb_used; + char *p = bl->sb_data + bl->sb_used; bl->sb_used += (int)len; return p; @@ -3851,10 +3763,8 @@ static char *getroom_save(spellinfo_T *spin, char *s) // Free the list of allocated sblock_T. static void free_blocks(sblock_T *bl) { - sblock_T *next; - while (bl != NULL) { - next = bl->sb_next; + sblock_T *next = bl->sb_next; xfree(bl); bl = next; } @@ -3943,8 +3853,6 @@ static int tree_add_word(spellinfo_T *spin, const char *word, wordnode_T *root, int region, int affixID) { wordnode_T *node = root; - wordnode_T *np; - wordnode_T *copyp, **copyprev; wordnode_T **prev = NULL; // Add each byte of the word to the tree, including the NUL at the end. @@ -3954,10 +3862,10 @@ static int tree_add_word(spellinfo_T *spin, const char *word, wordnode_T *root, // (we don't optimize for a partly shared list of siblings). if (node != NULL && node->wn_refs > 1) { node->wn_refs--; - copyprev = prev; - for (copyp = node; copyp != NULL; copyp = copyp->wn_sibling) { + wordnode_T **copyprev = prev; + for (wordnode_T *copyp = node; copyp != NULL; copyp = copyp->wn_sibling) { // Allocate a new node and copy the info. - np = get_wordnode(spin); + wordnode_T *np = get_wordnode(spin); if (np == NULL) { return FAIL; } @@ -4012,7 +3920,7 @@ static int tree_add_word(spellinfo_T *spin, const char *word, wordnode_T *root, || node->wn_flags != (flags & WN_MASK) || node->wn_affixID != affixID))) { // Allocate a new node. - np = get_wordnode(spin); + wordnode_T *np = get_wordnode(spin); if (np == NULL) { return FAIL; } @@ -4134,11 +4042,10 @@ static wordnode_T *get_wordnode(spellinfo_T *spin) static int deref_wordnode(spellinfo_T *spin, wordnode_T *node) FUNC_ATTR_NONNULL_ALL { - wordnode_T *np; int cnt = 0; if (--node->wn_refs == 0) { - for (np = node; np != NULL; np = np->wn_sibling) { + for (wordnode_T *np = node; np != NULL; np = np->wn_sibling) { if (np->wn_child != NULL) { cnt += deref_wordnode(spin, np->wn_child); } @@ -4206,29 +4113,26 @@ static void wordtree_compress(spellinfo_T *spin, wordnode_T *root, const char *n static int node_compress(spellinfo_T *spin, wordnode_T *node, hashtab_T *ht, int *tot) FUNC_ATTR_NONNULL_ALL { - wordnode_T *np; wordnode_T *tp; wordnode_T *child; - hash_T hash; - hashitem_T *hi; int len = 0; - unsigned nr, n; + unsigned n; int compressed = 0; // Go through the list of siblings. Compress each child and then try // finding an identical child to replace it. // Note that with "child" we mean not just the node that is pointed to, // but the whole list of siblings of which the child node is the first. - for (np = node; np != NULL && !got_int; np = np->wn_sibling) { + for (wordnode_T *np = node; np != NULL && !got_int; np = np->wn_sibling) { len++; if ((child = np->wn_child) != NULL) { // Compress the child first. This fills hashkey. compressed += node_compress(spin, child, ht, tot); // Try to find an identical child. - hash = hash_hash((char *)child->wn_u1.hashkey); - hi = hash_lookup(ht, (const char *)child->wn_u1.hashkey, - strlen((char *)child->wn_u1.hashkey), hash); + hash_T hash = hash_hash((char *)child->wn_u1.hashkey); + hashitem_T *hi = hash_lookup(ht, (const char *)child->wn_u1.hashkey, + strlen((char *)child->wn_u1.hashkey), hash); if (!HASHITEM_EMPTY(hi)) { // There are children we encountered before with a hash value // identical to the current child. Now check if there is one @@ -4265,8 +4169,8 @@ static int node_compress(spellinfo_T *spin, wordnode_T *node, hashtab_T *ht, int // find a lookalike node. This must be done after compressing the sibling // list, otherwise the hash key would become invalid by the compression. node->wn_u1.hashkey[0] = (uint8_t)len; - nr = 0; - for (np = node; np != NULL; np = np->wn_sibling) { + unsigned nr = 0; + for (wordnode_T *np = node; np != NULL; np = np->wn_sibling) { if (np->wn_byte == NUL) { // end node: use wn_flags, wn_region and wn_affixID n = (unsigned)(np->wn_flags + (np->wn_region << 8) + (np->wn_affixID << 16)); @@ -4714,10 +4618,8 @@ theend: // space. static void clear_node(wordnode_T *node) { - wordnode_T *np; - if (node != NULL) { - for (np = node; np != NULL; np = np->wn_sibling) { + for (wordnode_T *np = node; np != NULL; np = np->wn_sibling) { np->wn_u1.index = 0; np->wn_u2.wnode = NULL; @@ -4875,7 +4777,6 @@ void ex_mkspell(exarg_T *eap) static void spell_make_sugfile(spellinfo_T *spin, char *wfname) { char *fname = NULL; - int len; slang_T *slang; bool free_slang = false; @@ -4933,7 +4834,7 @@ static void spell_make_sugfile(spellinfo_T *spin, char *wfname) // Make the file name by changing ".spl" to ".sug". fname = xmalloc(MAXPATHL); xstrlcpy(fname, wfname, MAXPATHL); - len = (int)strlen(fname); + int len = (int)strlen(fname); fname[len - 2] = 'u'; fname[len - 1] = 'g'; sug_write(spin, fname); @@ -4950,15 +4851,10 @@ theend: // Build the soundfold trie for language "slang". static int sug_filltree(spellinfo_T *spin, slang_T *slang) { - uint8_t *byts; - idx_T *idxs; - int depth; idx_T arridx[MAXWLEN]; int curi[MAXWLEN]; char tword[MAXWLEN]; char tsalword[MAXWLEN]; - int c; - idx_T n; unsigned words_done = 0; int wordcount[MAXWLEN]; @@ -4970,8 +4866,8 @@ static int sug_filltree(spellinfo_T *spin, slang_T *slang) // Go through the whole case-folded tree, soundfold each word and put it // in the trie. Bail out if the tree is empty. - byts = slang->sl_fbyts; - idxs = slang->sl_fidxs; + uint8_t *byts = slang->sl_fbyts; + idx_T *idxs = slang->sl_fidxs; if (byts == NULL || idxs == NULL) { return FAIL; } @@ -4980,7 +4876,7 @@ static int sug_filltree(spellinfo_T *spin, slang_T *slang) curi[0] = 1; wordcount[0] = 0; - depth = 0; + int depth = 0; while (depth >= 0 && !got_int) { if (curi[depth] > byts[arridx[depth]]) { // Done all bytes at this node, go up one level. @@ -4993,10 +4889,10 @@ static int sug_filltree(spellinfo_T *spin, slang_T *slang) line_breakcheck(); } else { // Do one more byte at this node. - n = arridx[depth] + curi[depth]; + idx_T n = arridx[depth] + curi[depth]; curi[depth]++; - c = byts[n]; + int c = byts[n]; if (c == 0) { // Sound-fold the word. tword[depth] = NUL; @@ -5073,17 +4969,15 @@ static int sug_maketable(spellinfo_T *spin) static int sug_filltable(spellinfo_T *spin, wordnode_T *node, int startwordnr, garray_T *gap) { int wordnr = startwordnr; - int nr; - int prev_nr; for (wordnode_T *p = node; p != NULL; p = p->wn_sibling) { if (p->wn_byte == NUL) { gap->ga_len = 0; - prev_nr = 0; + int prev_nr = 0; for (wordnode_T *np = p; np != NULL && np->wn_byte == NUL; np = np->wn_sibling) { ga_grow(gap, 10); - nr = (np->wn_flags << 16) + (np->wn_region & 0xffff); + int nr = (np->wn_flags << 16) + (np->wn_region & 0xffff); // Compute the offset from the previous nr and store the // offset in a way that it takes a minimum number of bytes. // It's a bit like utf-8, but without the need to mark @@ -5128,16 +5022,14 @@ static int sug_filltable(spellinfo_T *spin, wordnode_T *node, int startwordnr, g static int offset2bytes(int nr, char *buf_in) { uint8_t *buf = (uint8_t *)buf_in; - int rem; - int b1, b2, b3, b4; // Split the number in parts of base 255. We need to avoid NUL bytes. - b1 = nr % 255 + 1; - rem = nr / 255; - b2 = rem % 255 + 1; + int b1 = nr % 255 + 1; + int rem = nr / 255; + int b2 = rem % 255 + 1; rem = rem / 255; - b3 = rem % 255 + 1; - b4 = rem / 255 + 1; + int b3 = rem % 255 + 1; + int b4 = rem / 255 + 1; if (b4 > 1 || b3 > 0x1f) { // 4 bytes buf[0] = (uint8_t)(0xe0 + b4); @@ -5249,11 +5141,7 @@ theend: static void mkspell(int fcount, char **fnames, bool ascii, bool over_write, bool added_word) { char *fname = NULL; - char **innames; - int incount; afffile_T *(afile[MAXREGIONS]); - int i; - int len; bool error = false; spellinfo_T spin; @@ -5273,13 +5161,13 @@ static void mkspell(int fcount, char **fnames, bool ascii, bool over_write, bool // default: fnames[0] is output file, following are input files // When "fcount" is 1 there is only one file. - innames = &fnames[fcount == 1 ? 0 : 1]; - incount = fcount - 1; + char **innames = &fnames[fcount == 1 ? 0 : 1]; + int incount = fcount - 1; char *wfname = xmalloc(MAXPATHL); if (fcount >= 1) { - len = (int)strlen(fnames[0]); + int len = (int)strlen(fnames[0]); if (fcount == 1 && len > 4 && strcmp(fnames[0] + len - 4, ".add") == 0) { // For ":mkspell path/en.latin1.add" output file is // "path/en.latin1.add.spl". @@ -5332,11 +5220,11 @@ static void mkspell(int fcount, char **fnames, bool ascii, bool over_write, bool // Init the aff and dic pointers. // Get the region names if there are more than 2 arguments. - for (i = 0; i < incount; i++) { + for (int i = 0; i < incount; i++) { afile[i] = NULL; if (incount > 1) { - len = (int)strlen(innames[i]); + int len = (int)strlen(innames[i]); if (strlen(path_tail(innames[i])) < 5 || innames[i][len - 3] != '_') { semsg(_("E755: Invalid region in %s"), innames[i]); @@ -5364,7 +5252,7 @@ static void mkspell(int fcount, char **fnames, bool ascii, bool over_write, bool // Read all the .aff and .dic files. // Text is converted to 'encoding'. // Words are stored in the case-folded and keep-case trees. - for (i = 0; i < incount && !error; i++) { + for (int i = 0; i < incount && !error; i++) { spin.si_conv.vc_type = CONV_NONE; spin.si_region = 1 << i; @@ -5435,7 +5323,7 @@ static void mkspell(int fcount, char **fnames, bool ascii, bool over_write, bool hash_clear_all(&spin.si_commonwords, 0); // Free the .aff file structures. - for (i = 0; i < incount; i++) { + for (int i = 0; i < incount; i++) { if (afile[i] != NULL) { spell_free_aff(afile[i]); } @@ -5652,10 +5540,7 @@ void spell_add_word(char *word, int len, SpellAddType what, int idx, bool undo) // Initialize 'spellfile' for the current buffer. static void init_spellfile(void) { - char *buf; int l; - char *fname; - char *rtp; char *lend; bool aspath = false; char *lstart = curbuf->b_s.b_p_spl; @@ -5664,7 +5549,7 @@ static void init_spellfile(void) return; } - buf = xmalloc(MAXPATHL); + char *buf = xmalloc(MAXPATHL); // Find the end of the language name. Exclude the region. If there // is a path separator remember the start of the tail. @@ -5678,7 +5563,7 @@ static void init_spellfile(void) // Loop over all entries in 'runtimepath'. Use the first one where we // are allowed to write. - rtp = p_rtp; + char *rtp = p_rtp; while (*rtp != NUL) { if (aspath) { // Use directory of an entry with path, e.g., for @@ -5706,8 +5591,8 @@ static void init_spellfile(void) "/%.*s", (int)(lend - lstart), lstart); } l = (int)strlen(buf); - fname = LANGP_ENTRY(curwin->w_s->b_langp, 0) - ->lp_slang->sl_fname; + char *fname = LANGP_ENTRY(curwin->w_s->b_langp, 0) + ->lp_slang->sl_fname; vim_snprintf(buf + l, MAXPATHL - (size_t)l, ".%s.add", ((fname != NULL && strstr(path_tail(fname), ".ascii.") != NULL) @@ -5732,7 +5617,6 @@ static void set_spell_charflags(const char *flags_in, int cnt, const char *fol) // previous one. spelltab_T new_st; const char *p = fol; - int c; clear_spell_chartab(&new_st); @@ -5743,7 +5627,7 @@ static void set_spell_charflags(const char *flags_in, int cnt, const char *fol) } if (*p != NUL) { - c = mb_ptr2char_adv(&p); + int c = mb_ptr2char_adv(&p); new_st.st_fold[i + 128] = (uint8_t)c; if (i + 128 != c && new_st.st_isu[i + 128] && c < 256) { new_st.st_upper[c] = (uint8_t)(i + 128); |