diff options
author | David Z. Chen <david.z.chen@outlook.com> | 2014-03-10 03:48:12 -0700 |
---|---|---|
committer | Thiago de Arruda <tpadilha84@gmail.com> | 2014-03-16 12:00:41 -0300 |
commit | baab23827194175e811cfaccd287e9438f1d1b6d (patch) | |
tree | 72d3d35fc2680149fdbcc32a9d068e6a7c821354 /src/charset.c | |
parent | 8a6118ccaced623b9e6d854e3a41fbd9d6fc68df (diff) | |
download | rneovim-baab23827194175e811cfaccd287e9438f1d1b6d.tar.gz rneovim-baab23827194175e811cfaccd287e9438f1d1b6d.tar.bz2 rneovim-baab23827194175e811cfaccd287e9438f1d1b6d.zip |
Issue #311 - Clean up blowfish.c/h, charset.c/h, diff.c/h, digraph.c/h, garray.c/h, hashtab.c/h, popupmnu.c/h, sha256.c/h, version.c/h. Update uncrustify to move logical operators to the beginning of the line when splitting. Also, clean up arabic.c/h and farsi.c/h
Diffstat (limited to 'src/charset.c')
-rw-r--r-- | src/charset.c | 1862 |
1 files changed, 1110 insertions, 752 deletions
diff --git a/src/charset.c b/src/charset.c index 3f609801a2..28dacf1a15 100644 --- a/src/charset.c +++ b/src/charset.c @@ -1,11 +1,6 @@ -/* vi:set ts=8 sts=4 sw=4: - * - * VIM - Vi IMproved by Bram Moolenaar - * - * Do ":help uganda" in Vim to read copying and usage conditions. - * Do ":help credits" in Vim to see a list of people who contributed. - * See README.txt for an overview of the Vim source code. - */ +/// @file charset.c +/// +/// Code related to character sets. #include "vim.h" #include "charset.h" @@ -20,9 +15,9 @@ static int win_chartabsize(win_T *wp, char_u *p, colnr_T col); -# if defined(HAVE_WCHAR_H) -# include <wchar.h> /* for towupper() and towlower() */ -# endif +#if defined(HAVE_WCHAR_H) +# include <wchar.h> // for towupper() and towlower() +#endif // if defined(HAVE_WCHAR_H) static int win_nolbr_chartabsize(win_T *wp, char_u *s, colnr_T col, int *headp); @@ -30,212 +25,241 @@ static unsigned nr2hex(unsigned c); static int chartab_initialized = FALSE; -/* b_chartab[] is an array of 32 bytes, each bit representing one of the - * characters 0-255. */ -#define SET_CHARTAB(buf, c) (buf)->b_chartab[(unsigned)(c) >> \ - 3] |= (1 << ((c) & 0x7)) -#define RESET_CHARTAB(buf, c) (buf)->b_chartab[(unsigned)(c) >> \ - 3] &= ~(1 << ((c) & 0x7)) -#define GET_CHARTAB(buf, \ - c) ((buf)->b_chartab[(unsigned)(c) >> 3] & (1 << ((c) & 0x7))) - -/* - * Fill chartab[]. Also fills curbuf->b_chartab[] with flags for keyword - * characters for current buffer. - * - * Depends on the option settings 'iskeyword', 'isident', 'isfname', - * 'isprint' and 'encoding'. - * - * The index in chartab[] depends on 'encoding': - * - For non-multi-byte index with the byte (same as the character). - * - For DBCS index with the first byte. - * - For UTF-8 index with the character (when first byte is up to 0x80 it is - * the same as the character, if the first byte is 0x80 and above it depends - * on further bytes). - * - * The contents of chartab[]: - * - The lower two bits, masked by CT_CELL_MASK, give the number of display - * cells the character occupies (1 or 2). Not valid for UTF-8 above 0x80. - * - CT_PRINT_CHAR bit is set when the character is printable (no need to - * translate the character before displaying it). Note that only DBCS - * characters can have 2 display cells and still be printable. - * - CT_FNAME_CHAR bit is set when the character can be in a file name. - * - CT_ID_CHAR bit is set when the character can be in an identifier. - * - * Return FAIL if 'iskeyword', 'isident', 'isfname' or 'isprint' option has an - * error, OK otherwise. - */ -int init_chartab(void) { +// b_chartab[] is an array of 32 bytes, each bit representing one of the +// characters 0-255. +#define SET_CHARTAB(buf, c) \ + (buf)->b_chartab[(unsigned)(c) >> 3] |= (1 << ((c) & 0x7)) +#define RESET_CHARTAB(buf, c) \ + (buf)->b_chartab[(unsigned)(c) >> 3] &= ~(1 << ((c) & 0x7)) +#define GET_CHARTAB(buf, c) \ + ((buf)->b_chartab[(unsigned)(c) >> 3] & (1 << ((c) & 0x7))) + +/// Fill chartab[]. Also fills curbuf->b_chartab[] with flags for keyword +/// characters for current buffer. +/// +/// Depends on the option settings 'iskeyword', 'isident', 'isfname', +/// 'isprint' and 'encoding'. +/// +/// The index in chartab[] depends on 'encoding': +/// - For non-multi-byte index with the byte (same as the character). +/// - For DBCS index with the first byte. +/// - For UTF-8 index with the character (when first byte is up to 0x80 it is +/// the same as the character, if the first byte is 0x80 and above it depends +/// on further bytes). +/// +/// The contents of chartab[]: +/// - The lower two bits, masked by CT_CELL_MASK, give the number of display +/// cells the character occupies (1 or 2). Not valid for UTF-8 above 0x80. +/// - CT_PRINT_CHAR bit is set when the character is printable (no need to +/// translate the character before displaying it). Note that only DBCS +/// characters can have 2 display cells and still be printable. +/// - CT_FNAME_CHAR bit is set when the character can be in a file name. +/// - CT_ID_CHAR bit is set when the character can be in an identifier. +/// +/// @return FAIL if 'iskeyword', 'isident', 'isfname' or 'isprint' option has +/// an error, OK otherwise. +int init_chartab(void) +{ return buf_init_chartab(curbuf, TRUE); } -int -buf_init_chartab ( - buf_T *buf, - int global /* FALSE: only set buf->b_chartab[] */ -) +/// Helper for init_chartab +/// +/// @param global FALSE: only set buf->b_chartab[] +/// +/// @return FAIL if 'iskeyword', 'isident', 'isfname' or 'isprint' option has +/// an error, OK otherwise. +int buf_init_chartab(buf_T *buf, int global) { int c; int c2; - char_u *p; + char_u *p; int i; int tilde; int do_isalpha; if (global) { - /* - * Set the default size for printable characters: - * From <Space> to '~' is 1 (printable), others are 2 (not printable). - * This also inits all 'isident' and 'isfname' flags to FALSE. - * - * EBCDIC: all chars below ' ' are not printable, all others are - * printable. - */ + // Set the default size for printable characters: + // From <Space> to '~' is 1 (printable), others are 2 (not printable). + // This also inits all 'isident' and 'isfname' flags to FALSE. + // + // EBCDIC: all chars below ' ' are not printable, all others are + // printable. c = 0; - while (c < ' ') + + while (c < ' ') { chartab[c++] = (dy_flags & DY_UHEX) ? 4 : 2; - while (c <= '~') + } + + while (c <= '~') { chartab[c++] = 1 + CT_PRINT_CHAR; + } + if (p_altkeymap) { - while (c < YE) + while (c < YE) { chartab[c++] = 1 + CT_PRINT_CHAR; + } } + while (c < 256) { - /* UTF-8: bytes 0xa0 - 0xff are printable (latin1) */ - if (enc_utf8 && c >= 0xa0) + if (enc_utf8 && (c >= 0xa0)) { + // UTF-8: bytes 0xa0 - 0xff are printable (latin1) chartab[c++] = CT_PRINT_CHAR + 1; - /* euc-jp characters starting with 0x8e are single width */ - else if (enc_dbcs == DBCS_JPNU && c == 0x8e) + } else if ((enc_dbcs == DBCS_JPNU) && (c == 0x8e)) { + // euc-jp characters starting with 0x8e are single width chartab[c++] = CT_PRINT_CHAR + 1; - /* other double-byte chars can be printable AND double-width */ - else if (enc_dbcs != 0 && MB_BYTE2LEN(c) == 2) + } else if ((enc_dbcs != 0) && (MB_BYTE2LEN(c) == 2)) { + // other double-byte chars can be printable AND double-width chartab[c++] = CT_PRINT_CHAR + 2; - else - /* the rest is unprintable by default */ + } else { + // the rest is unprintable by default chartab[c++] = (dy_flags & DY_UHEX) ? 4 : 2; + } } - /* Assume that every multi-byte char is a filename character. */ - for (c = 1; c < 256; ++c) - if ((enc_dbcs != 0 && MB_BYTE2LEN(c) > 1) - || (enc_dbcs == DBCS_JPNU && c == 0x8e) - || (enc_utf8 && c >= 0xa0)) + // Assume that every multi-byte char is a filename character. + for (c = 1; c < 256; ++c) { + if (((enc_dbcs != 0) && (MB_BYTE2LEN(c) > 1)) + || ((enc_dbcs == DBCS_JPNU) && (c == 0x8e)) + || (enc_utf8 && (c >= 0xa0))) { chartab[c] |= CT_FNAME_CHAR; + } + } } - /* - * Init word char flags all to FALSE - */ + // Init word char flags all to FALSE vim_memset(buf->b_chartab, 0, (size_t)32); - if (enc_dbcs != 0) + + if (enc_dbcs != 0) { for (c = 0; c < 256; ++c) { - /* double-byte characters are probably word characters */ - if (MB_BYTE2LEN(c) == 2) + // double-byte characters are probably word characters + if (MB_BYTE2LEN(c) == 2) { SET_CHARTAB(buf, c); + } } + } - /* - * In lisp mode the '-' character is included in keywords. - */ - if (buf->b_p_lisp) + // In lisp mode the '-' character is included in keywords. + if (buf->b_p_lisp) { SET_CHARTAB(buf, '-'); + } - /* Walk through the 'isident', 'iskeyword', 'isfname' and 'isprint' - * options Each option is a list of characters, character numbers or - * ranges, separated by commas, e.g.: "200-210,x,#-178,-" - */ + // Walk through the 'isident', 'iskeyword', 'isfname' and 'isprint' + // options Each option is a list of characters, character numbers or + // ranges, separated by commas, e.g.: "200-210,x,#-178,-" for (i = global ? 0 : 3; i <= 3; ++i) { - if (i == 0) - p = p_isi; /* first round: 'isident' */ - else if (i == 1) - p = p_isp; /* second round: 'isprint' */ - else if (i == 2) - p = p_isf; /* third round: 'isfname' */ - else /* i == 3 */ - p = buf->b_p_isk; /* fourth round: 'iskeyword' */ + if (i == 0) { + // first round: 'isident' + p = p_isi; + } else if (i == 1) { + // second round: 'isprint' + p = p_isp; + } else if (i == 2) { + // third round: 'isfname' + p = p_isf; + } else { // i == 3 + // fourth round: 'iskeyword' + p = buf->b_p_isk; + } while (*p) { tilde = FALSE; do_isalpha = FALSE; - if (*p == '^' && p[1] != NUL) { + + if ((*p == '^') && (p[1] != NUL)) { tilde = TRUE; ++p; } - if (VIM_ISDIGIT(*p)) + + if (VIM_ISDIGIT(*p)) { c = getdigits(&p); - else if (has_mbyte) + } else if (has_mbyte) { c = mb_ptr2char_adv(&p); - else + } else { c = *p++; + } c2 = -1; - if (*p == '-' && p[1] != NUL) { + + if ((*p == '-') && (p[1] != NUL)) { ++p; - if (VIM_ISDIGIT(*p)) + + if (VIM_ISDIGIT(*p)) { c2 = getdigits(&p); - else if (has_mbyte) + } else if (has_mbyte) { c2 = mb_ptr2char_adv(&p); - else + } else { c2 = *p++; + } } - if (c <= 0 || c >= 256 || (c2 < c && c2 != -1) || c2 >= 256 - || !(*p == NUL || *p == ',')) + + if ((c <= 0) + || (c >= 256) + || ((c2 < c) && (c2 != -1)) + || (c2 >= 256) + || !((*p == NUL) || (*p == ','))) { return FAIL; + } - if (c2 == -1) { /* not a range */ - /* - * A single '@' (not "@-@"): - * Decide on letters being ID/printable/keyword chars with - * standard function isalpha(). This takes care of locale for - * single-byte characters). - */ + if (c2 == -1) { // not a range + // A single '@' (not "@-@"): + // Decide on letters being ID/printable/keyword chars with + // standard function isalpha(). This takes care of locale for + // single-byte characters). if (c == '@') { do_isalpha = TRUE; c = 1; c2 = 255; - } else + } else { c2 = c; + } } + while (c <= c2) { - /* Use the MB_ functions here, because isalpha() doesn't - * work properly when 'encoding' is "latin1" and the locale is - * "C". */ - if (!do_isalpha || MB_ISLOWER(c) || MB_ISUPPER(c) - || (p_altkeymap && (F_isalpha(c) || F_isdigit(c))) - ) { - if (i == 0) { /* (re)set ID flag */ - if (tilde) + // Use the MB_ functions here, because isalpha() doesn't + // work properly when 'encoding' is "latin1" and the locale is + // "C". + if (!do_isalpha + || MB_ISLOWER(c) + || MB_ISUPPER(c) + || (p_altkeymap && (F_isalpha(c) || F_isdigit(c)))) { + if (i == 0) { + // (re)set ID flag + if (tilde) { chartab[c] &= ~CT_ID_CHAR; - else + } else { chartab[c] |= CT_ID_CHAR; - } else if (i == 1) { /* (re)set printable */ - if ((c < ' ' - || c > '~' - || (p_altkeymap - && (F_isalpha(c) || F_isdigit(c))) - ) - /* For double-byte we keep the cell width, so - * that we can detect it from the first byte. */ - && !(enc_dbcs && MB_BYTE2LEN(c) == 2) - ) { + } + } else if (i == 1) { + // (re)set printable + // For double-byte we keep the cell width, so + // that we can detect it from the first byte. + if (((c < ' ') + || (c > '~') + || (p_altkeymap && (F_isalpha(c) || F_isdigit(c)))) + && !(enc_dbcs && (MB_BYTE2LEN(c) == 2))) { if (tilde) { chartab[c] = (chartab[c] & ~CT_CELL_MASK) + ((dy_flags & DY_UHEX) ? 4 : 2); chartab[c] &= ~CT_PRINT_CHAR; - } else { + } else { chartab[c] = (chartab[c] & ~CT_CELL_MASK) + 1; chartab[c] |= CT_PRINT_CHAR; } } - } else if (i == 2) { /* (re)set fname flag */ - if (tilde) + } else if (i == 2) { + // (re)set fname flag + if (tilde) { chartab[c] &= ~CT_FNAME_CHAR; - else + } else { chartab[c] |= CT_FNAME_CHAR; - } else { /* i == 3 */ /* (re)set keyword flag */ - if (tilde) + } + } else { // i == 3 + // (re)set keyword flag + if (tilde) { RESET_CHARTAB(buf, c); - else + } else { SET_CHARTAB(buf, c); + } } } ++c; @@ -243,40 +267,47 @@ buf_init_chartab ( c = *p; p = skip_to_option_part(p); - if (c == ',' && *p == NUL) - /* Trailing comma is not allowed. */ + + if ((c == ',') && (*p == NUL)) { + // Trailing comma is not allowed. return FAIL; + } } } chartab_initialized = TRUE; return OK; } -/* - * Translate any special characters in buf[bufsize] in-place. - * The result is a string with only printable characters, but if there is not - * enough room, not all characters will be translated. - */ +/// Translate any special characters in buf[bufsize] in-place. +/// +/// The result is a string with only printable characters, but if there is not +/// enough room, not all characters will be translated. +/// +/// @param buf +/// @param bufsize void trans_characters(char_u *buf, int bufsize) { - int len; /* length of string needing translation */ - int room; /* room in buffer after string */ - char_u *trs; /* translated character */ - int trs_len; /* length of trs[] */ + int len; // length of string needing translation + int room; // room in buffer after string + char_u *trs; // translated character + int trs_len; // length of trs[] len = (int)STRLEN(buf); room = bufsize - len; + while (*buf != 0) { - /* Assume a multi-byte character doesn't need translation. */ - if (has_mbyte && (trs_len = (*mb_ptr2len)(buf)) > 1) + // Assume a multi-byte character doesn't need translation. + if (has_mbyte && ((trs_len = (*mb_ptr2len)(buf)) > 1)) { len -= trs_len; - else { + } else { trs = transchar_byte(*buf); trs_len = (int)STRLEN(trs); + if (trs_len > 1) { room -= trs_len - 1; - if (room <= 0) + if (room <= 0) { return; + } mch_memmove(buf + trs_len, buf + 1, (size_t)len); } mch_memmove(buf, trs, (size_t)trs_len); @@ -286,130 +317,162 @@ void trans_characters(char_u *buf, int bufsize) } } -#if defined(FEAT_EVAL) || defined(FEAT_TITLE) || defined(FEAT_INS_EXPAND) \ +#if defined(FEAT_EVAL) \ + || defined(FEAT_TITLE) \ + || defined(FEAT_INS_EXPAND) \ || defined(PROTO) -/* - * Translate a string into allocated memory, replacing special chars with - * printable chars. Returns NULL when out of memory. - */ + +/// Translate a string into allocated memory, replacing special chars with +/// printable chars. Returns NULL when out of memory. +/// +/// @param s +/// +/// @return translated string or NULL if out of memory. char_u *transstr(char_u *s) { - char_u *res; - char_u *p; + char_u *res; + char_u *p; int l, len, c; char_u hexbuf[11]; if (has_mbyte) { - /* Compute the length of the result, taking account of unprintable - * multi-byte characters. */ + // Compute the length of the result, taking account of unprintable + // multi-byte characters. len = 0; p = s; + while (*p != NUL) { if ((l = (*mb_ptr2len)(p)) > 1) { c = (*mb_ptr2char)(p); p += l; - if (vim_isprintc(c)) + + if (vim_isprintc(c)) { len += l; - else { + } else { transchar_hex(hexbuf, c); len += (int)STRLEN(hexbuf); } } else { l = byte2cells(*p++); - if (l > 0) + + if (l > 0) { len += l; - else - len += 4; /* illegal byte sequence */ + } else { + // illegal byte sequence + len += 4; + } } } res = alloc((unsigned)(len + 1)); - } else + } else { res = alloc((unsigned)(vim_strsize(s) + 1)); + } + if (res != NULL) { *res = NUL; p = s; + while (*p != NUL) { - if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1) { + if (has_mbyte && ((l = (*mb_ptr2len)(p)) > 1)) { c = (*mb_ptr2char)(p); - if (vim_isprintc(c)) - STRNCAT(res, p, l); /* append printable multi-byte char */ - else + + if (vim_isprintc(c)) { + // append printable multi-byte char + STRNCAT(res, p, l); + } else { transchar_hex(res + STRLEN(res), c); + } p += l; - } else + } else { STRCAT(res, transchar_byte(*p++)); + } } } return res; } -#endif - -/* - * Convert the string "str[orglen]" to do ignore-case comparing. Uses the - * current locale. - * When "buf" is NULL returns an allocated string (NULL for out-of-memory). - * Otherwise puts the result in "buf[buflen]". - */ -char_u *str_foldcase(char_u *str, int orglen, char_u *buf, int buflen) + +#endif // if defined(FEAT_EVAL) || defined(FEAT_TITLE) + // || defined(FEAT_INS_EXPAND) || defined(PROTO) + +/// Convert the string "str[orglen]" to do ignore-case comparing. Uses the +/// current locale. +/// +/// When "buf" is NULL returns an allocated string (NULL for out-of-memory). +/// Otherwise puts the result in "buf[buflen]". +/// +/// @param str +/// @param orglen +/// @param buf +/// @param buflen +/// +/// @return converted string. +char_u* str_foldcase(char_u *str, int orglen, char_u *buf, int buflen) { garray_T ga; int i; int len = orglen; -#define GA_CHAR(i) ((char_u *)ga.ga_data)[i] -#define GA_PTR(i) ((char_u *)ga.ga_data + i) -#define STR_CHAR(i) (buf == NULL ? GA_CHAR(i) : buf[i]) -#define STR_PTR(i) (buf == NULL ? GA_PTR(i) : buf + i) +#define GA_CHAR(i) ((char_u *)ga.ga_data)[i] +#define GA_PTR(i) ((char_u *)ga.ga_data + i) +#define STR_CHAR(i) (buf == NULL ? GA_CHAR(i) : buf[i]) +#define STR_PTR(i) (buf == NULL ? GA_PTR(i) : buf + i) - /* Copy "str" into "buf" or allocated memory, unmodified. */ + // Copy "str" into "buf" or allocated memory, unmodified. if (buf == NULL) { ga_init2(&ga, 1, 10); - if (ga_grow(&ga, len + 1) == FAIL) + + if (ga_grow(&ga, len + 1) == FAIL) { return NULL; + } mch_memmove(ga.ga_data, str, (size_t)len); ga.ga_len = len; - } else { - if (len >= buflen) /* Ugly! */ + } else { + if (len >= buflen) { + // Ugly! len = buflen - 1; + } mch_memmove(buf, str, (size_t)len); } - if (buf == NULL) + + if (buf == NULL) { GA_CHAR(len) = NUL; - else + } else { buf[len] = NUL; + } - /* Make each character lower case. */ + // Make each character lower case. i = 0; while (STR_CHAR(i) != NUL) { - if (enc_utf8 || (has_mbyte && MB_BYTE2LEN(STR_CHAR(i)) > 1)) { + if (enc_utf8 || (has_mbyte && (MB_BYTE2LEN(STR_CHAR(i)) > 1))) { if (enc_utf8) { int c = utf_ptr2char(STR_PTR(i)); int olen = utf_ptr2len(STR_PTR(i)); int lc = utf_tolower(c); - /* Only replace the character when it is not an invalid - * sequence (ASCII character or more than one byte) and - * utf_tolower() doesn't return the original character. */ - if ((c < 0x80 || olen > 1) && c != lc) { + // Only replace the character when it is not an invalid + // sequence (ASCII character or more than one byte) and + // utf_tolower() doesn't return the original character. + if (((c < 0x80) || (olen > 1)) && (c != lc)) { int nlen = utf_char2len(lc); - /* If the byte length changes need to shift the following - * characters forward or backward. */ + // If the byte length changes need to shift the following + // characters forward or backward. if (olen != nlen) { if (nlen > olen) { - if (buf == NULL - ? ga_grow(&ga, nlen - olen + 1) == FAIL - : len + nlen - olen >= buflen) { - /* out of memory, keep old char */ + if ((buf == NULL) + ? (ga_grow(&ga, nlen - olen + 1) == FAIL) + : (len + nlen - olen >= buflen)) { + // out of memory, keep old char lc = c; nlen = olen; } } + if (olen != nlen) { if (buf == NULL) { STRMOVE(GA_PTR(i) + nlen, GA_PTR(i) + olen); ga.ga_len += nlen - olen; - } else { + } else { STRMOVE(buf + i + nlen, buf + i + olen); len += nlen - olen; } @@ -418,106 +481,123 @@ char_u *str_foldcase(char_u *str, int orglen, char_u *buf, int buflen) (void)utf_char2bytes(lc, STR_PTR(i)); } } - /* skip to next multi-byte char */ + + // skip to next multi-byte char i += (*mb_ptr2len)(STR_PTR(i)); - } else { - if (buf == NULL) + } else { + if (buf == NULL) { GA_CHAR(i) = TOLOWER_LOC(GA_CHAR(i)); - else + } else { buf[i] = TOLOWER_LOC(buf[i]); + } ++i; } } - if (buf == NULL) + if (buf == NULL) { return (char_u *)ga.ga_data; + } return buf; } -/* - * Catch 22: chartab[] can't be initialized before the options are - * initialized, and initializing options may cause transchar() to be called! - * When chartab_initialized == FALSE don't use chartab[]. - * Does NOT work for multi-byte characters, c must be <= 255. - * Also doesn't work for the first byte of a multi-byte, "c" must be a - * character! - */ +// Catch 22: chartab[] can't be initialized before the options are +// initialized, and initializing options may cause transchar() to be called! +// When chartab_initialized == FALSE don't use chartab[]. +// Does NOT work for multi-byte characters, c must be <= 255. +// Also doesn't work for the first byte of a multi-byte, "c" must be a +// character! static char_u transchar_buf[7]; -char_u *transchar(int c) +/// Translates a character +/// +/// @param c +/// +/// @return translated character. +char_u* transchar(int c) { - int i; - - i = 0; - if (IS_SPECIAL(c)) { /* special key code, display as ~@ char */ + int i = 0; + if (IS_SPECIAL(c)) { + // special key code, display as ~@ char transchar_buf[0] = '~'; transchar_buf[1] = '@'; i = 2; c = K_SECOND(c); } - if ((!chartab_initialized && ( - (c >= ' ' && c <= '~') - || F_ischar(c) - )) || (c < 256 && vim_isprintc_strict(c))) { - /* printable character */ + if ((!chartab_initialized && (((c >= ' ') && (c <= '~')) || F_ischar(c))) + || ((c < 256) && vim_isprintc_strict(c))) { + // printable character transchar_buf[i] = c; transchar_buf[i + 1] = NUL; - } else + } else { transchar_nonprint(transchar_buf + i, c); + } return transchar_buf; } -/* - * Like transchar(), but called with a byte instead of a character. Checks - * for an illegal UTF-8 byte. - */ -char_u *transchar_byte(int c) +/// Like transchar(), but called with a byte instead of a character. Checks +/// for an illegal UTF-8 byte. +/// +/// @param c +/// +/// @return pointer to translated character in transchar_buf. +char_u* transchar_byte(int c) { - if (enc_utf8 && c >= 0x80) { + if (enc_utf8 && (c >= 0x80)) { transchar_nonprint(transchar_buf, c); return transchar_buf; } return transchar(c); } -/* - * Convert non-printable character to two or more printable characters in - * "buf[]". "buf" needs to be able to hold five bytes. - * Does NOT work for multi-byte characters, c must be <= 255. - */ +/// Convert non-printable character to two or more printable characters in +/// "buf[]". "buf" needs to be able to hold five bytes. +/// Does NOT work for multi-byte characters, c must be <= 255. +/// +/// @param buf +/// @param c void transchar_nonprint(char_u *buf, int c) { - if (c == NL) - c = NUL; /* we use newline in place of a NUL */ - else if (c == CAR && get_fileformat(curbuf) == EOL_MAC) - c = NL; /* we use CR in place of NL in this case */ + if (c == NL) { + // we use newline in place of a NUL + c = NUL; + } else if ((c == CAR) && (get_fileformat(curbuf) == EOL_MAC)) { + // we use CR in place of NL in this case + c = NL; + } - if (dy_flags & DY_UHEX) /* 'display' has "uhex" */ + if (dy_flags & DY_UHEX) { + // 'display' has "uhex" transchar_hex(buf, c); - - else if (c <= 0x7f) { /* 0x00 - 0x1f and 0x7f */ + } else if (c <= 0x7f) { + // 0x00 - 0x1f and 0x7f buf[0] = '^'; - buf[1] = c ^ 0x40; /* DEL displayed as ^? */ + // DEL displayed as ^? + buf[1] = c ^ 0x40; buf[2] = NUL; - } else if (enc_utf8 && c >= 0x80) { + } else if (enc_utf8 && (c >= 0x80)) { transchar_hex(buf, c); - } else if (c >= ' ' + 0x80 && c <= '~' + 0x80) { /* 0xa0 - 0xfe */ + } else if ((c >= ' ' + 0x80) && (c <= '~' + 0x80)) { + // 0xa0 - 0xfe buf[0] = '|'; buf[1] = c - 0x80; buf[2] = NUL; - } else { /* 0x80 - 0x9f and 0xff */ - /* - * TODO: EBCDIC I don't know what to do with this chars, so I display - * them as '~?' for now - */ + } else { + // 0x80 - 0x9f and 0xff + // TODO: EBCDIC I don't know what to do with this chars, so I display + // them as '~?' for now buf[0] = '~'; - buf[1] = (c - 0x80) ^ 0x40; /* 0xff displayed as ~? */ + buf[1] = (c - 0x80) ^ 0x40; + // 0xff displayed as ~? buf[2] = NUL; } } +/// Convert a non-printable character to hex. +/// +/// @param buf +/// @param c void transchar_hex(char_u *buf, int c) { int i = 0; @@ -533,183 +613,229 @@ void transchar_hex(char_u *buf, int c) buf[++i] = NUL; } -/* - * Convert the lower 4 bits of byte "c" to its hex character. - * Lower case letters are used to avoid the confusion of <F1> being 0xf1 or - * function key 1. - */ +/// Convert the lower 4 bits of byte "c" to its hex character. +/// Lower case letters are used to avoid the confusion of <F1> being 0xf1 or +/// function key 1. +/// +/// @param c +/// +/// @return the hex character. static unsigned nr2hex(unsigned c) { - if ((c & 0xf) <= 9) + if ((c & 0xf) <= 9) { return (c & 0xf) + '0'; + } return (c & 0xf) - 10 + 'a'; } -/* - * Return number of display cells occupied by byte "b". - * Caller must make sure 0 <= b <= 255. - * For multi-byte mode "b" must be the first byte of a character. - * A TAB is counted as two cells: "^I". - * For UTF-8 mode this will return 0 for bytes >= 0x80, because the number of - * cells depends on further bytes. - */ +/// Return number of display cells occupied by byte "b". +/// +/// Caller must make sure 0 <= b <= 255. +/// For multi-byte mode "b" must be the first byte of a character. +/// A TAB is counted as two cells: "^I". +/// For UTF-8 mode this will return 0 for bytes >= 0x80, because the number of +/// cells depends on further bytes. +/// +/// @param b +/// +/// @reeturn Number of display cells. int byte2cells(int b) { - if (enc_utf8 && b >= 0x80) + if (enc_utf8 && (b >= 0x80)) { return 0; + } return chartab[b] & CT_CELL_MASK; } -/* - * Return number of display cells occupied by character "c". - * "c" can be a special key (negative number) in which case 3 or 4 is returned. - * A TAB is counted as two cells: "^I" or four: "<09>". - */ +/// Return number of display cells occupied by character "c". +/// +/// "c" can be a special key (negative number) in which case 3 or 4 is returned. +/// A TAB is counted as two cells: "^I" or four: "<09>". +/// +/// @param c +/// +/// @return Number of display cells. int char2cells(int c) { - if (IS_SPECIAL(c)) + if (IS_SPECIAL(c)) { return char2cells(K_SECOND(c)) + 2; + } + if (c >= 0x80) { - /* UTF-8: above 0x80 need to check the value */ - if (enc_utf8) + // UTF-8: above 0x80 need to check the value + if (enc_utf8) { return utf_char2cells(c); - /* DBCS: double-byte means double-width, except for euc-jp with first - * byte 0x8e */ - if (enc_dbcs != 0 && c >= 0x100) { - if (enc_dbcs == DBCS_JPNU && ((unsigned)c >> 8) == 0x8e) + } + + // DBCS: double-byte means double-width, except for euc-jp with first + // byte 0x8e + if ((enc_dbcs != 0) && (c >= 0x100)) { + if ((enc_dbcs == DBCS_JPNU) && (((unsigned)c >> 8) == 0x8e)) { return 1; + } return 2; } } return chartab[c & 0xff] & CT_CELL_MASK; } -/* - * Return number of display cells occupied by character at "*p". - * A TAB is counted as two cells: "^I" or four: "<09>". - */ +/// Return number of display cells occupied by character at "*p". +/// A TAB is counted as two cells: "^I" or four: "<09>". +/// +/// @param p +/// +/// @return number of display cells. int ptr2cells(char_u *p) { - /* For UTF-8 we need to look at more bytes if the first byte is >= 0x80. */ - if (enc_utf8 && *p >= 0x80) + // For UTF-8 we need to look at more bytes if the first byte is >= 0x80. + if (enc_utf8 && (*p >= 0x80)) { return utf_ptr2cells(p); - /* For DBCS we can tell the cell count from the first byte. */ + } + + // For DBCS we can tell the cell count from the first byte. return chartab[*p] & CT_CELL_MASK; } -/* - * Return the number of character cells string "s" will take on the screen, - * counting TABs as two characters: "^I". - * - * 's' must be non-null. - */ +/// Return the number of character cells string "s" will take on the screen, +/// counting TABs as two characters: "^I". +/// +/// 's' must be non-null. +/// +/// @param s +/// +/// @return number of character cells. int vim_strsize(char_u *s) { return vim_strnsize(s, (int)MAXCOL); } -/* - * Return the number of character cells string "s[len]" will take on the - * screen, counting TABs as two characters: "^I". - * - * 's' must be non-null. - */ +/// Return the number of character cells string "s[len]" will take on the +/// screen, counting TABs as two characters: "^I". +/// +/// 's' must be non-null. +/// +/// @param s +/// @param len +/// +/// @return Number of character cells. int vim_strnsize(char_u *s, int len) { - assert(s); + assert(s != NULL); int size = 0; - while (*s != NUL && --len >= 0) { if (has_mbyte) { int l = (*mb_ptr2len)(s); - size += ptr2cells(s); s += l; len -= l - 1; - } else + } else { size += byte2cells(*s++); + } } return size; } -/* - * Return the number of characters 'c' will take on the screen, taking - * into account the size of a tab. - * Use a define to make it fast, this is used very often!!! - * Also see getvcol() below. - */ - +/// Return the number of characters 'c' will take on the screen, taking +/// into account the size of a tab. +/// Use a define to make it fast, this is used very often!!! +/// Also see getvcol() below. +/// +/// @param p +/// @param col +/// +/// @return Number of characters. #define RET_WIN_BUF_CHARTABSIZE(wp, buf, p, col) \ - if (*(p) == TAB && (!(wp)->w_p_list || lcs_tab1)) \ - { \ + if (*(p) == TAB && (!(wp)->w_p_list || lcs_tab1)) { \ int ts; \ ts = (buf)->b_p_ts; \ return (int)(ts - (col % ts)); \ - } \ - else \ - return ptr2cells(p); + } else { \ + return ptr2cells(p); \ + } -#if defined(FEAT_VREPLACE) || defined(FEAT_EX_EXTRA) || defined(FEAT_GUI) \ - || defined(FEAT_VIRTUALEDIT) || defined(PROTO) +#if defined(FEAT_VREPLACE) \ + || defined(FEAT_EX_EXTRA) \ + || defined(FEAT_GUI) \ + || defined(FEAT_VIRTUALEDIT) \ + || defined(PROTO) int chartabsize(char_u *p, colnr_T col) { RET_WIN_BUF_CHARTABSIZE(curwin, curbuf, p, col) } -#endif + +#endif /* if defined(FEAT_VREPLACE) || defined(FEAT_EX_EXTRA) || + defined(FEAT_GUI) || defined(FEAT_VIRTUALEDIT) || defined(PROTO) */ static int win_chartabsize(win_T *wp, char_u *p, colnr_T col) { RET_WIN_BUF_CHARTABSIZE(wp, wp->w_buffer, p, col) } -/* - * Return the number of characters the string 's' will take on the screen, - * taking into account the size of a tab. - */ +/// Return the number of characters the string 's' will take on the screen, +/// taking into account the size of a tab. +/// +/// @param s +/// +/// @return Number of characters the string will take on the screen. int linetabsize(char_u *s) { return linetabsize_col(0, s); } -/* - * Like linetabsize(), but starting at column "startcol". - */ +/// Like linetabsize(), but starting at column "startcol". +/// +/// @param startcol +/// @param s +/// +/// @return Number of characters the string will take on the screen. int linetabsize_col(int startcol, char_u *s) { colnr_T col = startcol; - while (*s != NUL) + while (*s != NUL) { col += lbr_chartabsize_adv(&s, col); + } return (int)col; } -/* - * Like linetabsize(), but for a given window instead of the current one. - */ +/// Like linetabsize(), but for a given window instead of the current one. +/// +/// @param wp +/// @param p +/// @param len +/// +/// @return Number of characters the string will take on the screen. int win_linetabsize(win_T *wp, char_u *p, colnr_T len) { colnr_T col = 0; - char_u *s; + char_u *s; - for (s = p; *s != NUL && (len == MAXCOL || s < p + len); mb_ptr_adv(s)) + for (s = p; *s != NUL && (len == MAXCOL || s < p + len); mb_ptr_adv(s)) { col += win_lbr_chartabsize(wp, s, col, NULL); + } return (int)col; } -/* - * Return TRUE if 'c' is a normal identifier character: - * Letters and characters from the 'isident' option. - */ +/// Return TRUE if 'c' is a normal identifier character: +/// +/// Letters and characters from the 'isident' option. +/// +/// @param c +/// +/// @return TRUE if 'c' is a normal identifier character. int vim_isIDc(int c) { return c > 0 && c < 0x100 && (chartab[c] & CT_ID_CHAR); } -/* - * return TRUE if 'c' is a keyword character: Letters and characters from - * 'iskeyword' option for current buffer. - * For multi-byte characters mb_get_class() is used (builtin rules). - */ +/// return TRUE if 'c' is a keyword character: Letters and characters from +/// 'iskeyword' option for current buffer. +/// +/// For multi-byte characters mb_get_class() is used (builtin rules). +/// +/// @param c +/// +/// @return TRUE if 'c' is a keyword character. int vim_iswordc(int c) { return vim_iswordc_buf(c, curbuf); @@ -718,96 +844,121 @@ int vim_iswordc(int c) int vim_iswordc_buf(int c, buf_T *buf) { if (c >= 0x100) { - if (enc_dbcs != 0) + if (enc_dbcs != 0) { return dbcs_class((unsigned)c >> 8, (unsigned)(c & 0xff)) >= 2; - if (enc_utf8) + } + + if (enc_utf8) { return utf_class(c) >= 2; + } } return c > 0 && c < 0x100 && GET_CHARTAB(buf, c) != 0; } -/* - * Just like vim_iswordc() but uses a pointer to the (multi-byte) character. - */ +/// Just like vim_iswordc() but uses a pointer to the (multi-byte) character. +/// +/// @param p +/// +/// @return TRUE if 'p' points to a keyword character. int vim_iswordp(char_u *p) { - if (has_mbyte && MB_BYTE2LEN(*p) > 1) + if (has_mbyte && (MB_BYTE2LEN(*p) > 1)) { return mb_get_class(p) >= 2; + } return GET_CHARTAB(curbuf, *p) != 0; } int vim_iswordp_buf(char_u *p, buf_T *buf) { - if (has_mbyte && MB_BYTE2LEN(*p) > 1) + if (has_mbyte && (MB_BYTE2LEN(*p) > 1)) { return mb_get_class(p) >= 2; + } return GET_CHARTAB(buf, *p) != 0; } -/* - * return TRUE if 'c' is a valid file-name character - * Assume characters above 0x100 are valid (multi-byte). - */ +/// return TRUE if 'c' is a valid file-name character +/// Assume characters above 0x100 are valid (multi-byte). +/// +/// @param c +/// +/// @return TRUE if 'c' is a valid file name character. int vim_isfilec(int c) { return c >= 0x100 || (c > 0 && (chartab[c] & CT_FNAME_CHAR)); } -/* - * return TRUE if 'c' is a valid file-name character or a wildcard character - * Assume characters above 0x100 are valid (multi-byte). - * Explicitly interpret ']' as a wildcard character as mch_has_wildcard("]") - * returns false. - */ +/// return TRUE if 'c' is a valid file-name character or a wildcard character +/// Assume characters above 0x100 are valid (multi-byte). +/// Explicitly interpret ']' as a wildcard character as mch_has_wildcard("]") +/// returns false. +/// +/// @param c +/// +/// @return TRUE if 'c' is a valid file-name character or wildcard character. int vim_isfilec_or_wc(int c) { char_u buf[2]; - buf[0] = (char_u)c; buf[1] = NUL; return vim_isfilec(c) || c == ']' || mch_has_wildcard(buf); } -/* - * return TRUE if 'c' is a printable character - * Assume characters above 0x100 are printable (multi-byte), except for - * Unicode. - */ +/// return TRUE if 'c' is a printable character +/// Assume characters above 0x100 are printable (multi-byte), except for +/// Unicode. +/// +/// @param c +/// +/// @return TRUE if 'c' a printable character. int vim_isprintc(int c) { - if (enc_utf8 && c >= 0x100) + if (enc_utf8 && (c >= 0x100)) { return utf_printable(c); + } return c >= 0x100 || (c > 0 && (chartab[c] & CT_PRINT_CHAR)); } -/* - * Strict version of vim_isprintc(c), don't return TRUE if "c" is the head - * byte of a double-byte character. - */ +/// Strict version of vim_isprintc(c), don't return TRUE if "c" is the head +/// byte of a double-byte character. +/// +/// @param c +/// +/// @return TRUE if 'c' is a printable character. int vim_isprintc_strict(int c) { - if (enc_dbcs != 0 && c < 0x100 && MB_BYTE2LEN(c) > 1) + if ((enc_dbcs != 0) && (c < 0x100) && (MB_BYTE2LEN(c) > 1)) { return FALSE; - if (enc_utf8 && c >= 0x100) + } + + if (enc_utf8 && (c >= 0x100)) { return utf_printable(c); + } return c >= 0x100 || (c > 0 && (chartab[c] & CT_PRINT_CHAR)); } -/* - * like chartabsize(), but also check for line breaks on the screen - */ +/// like chartabsize(), but also check for line breaks on the screen +/// +/// @param s +/// @param col +/// +/// @return The number of characters taken up on the screen. int lbr_chartabsize(unsigned char *s, colnr_T col) { - if (!curwin->w_p_lbr && *p_sbr == NUL) { - if (curwin->w_p_wrap) + if (!curwin->w_p_lbr && (*p_sbr == NUL)) { + if (curwin->w_p_wrap) { return win_nolbr_chartabsize(curwin, s, col, NULL); + } RET_WIN_BUF_CHARTABSIZE(curwin, curbuf, s, col) } return win_lbr_chartabsize(curwin, s, col, NULL); } -/* - * Call lbr_chartabsize() and advance the pointer. - */ +/// Call lbr_chartabsize() and advance the pointer. +/// +/// @param s +/// @param col +/// +/// @return The number of characters take up on the screen. int lbr_chartabsize_adv(char_u **s, colnr_T col) { int retval; @@ -817,176 +968,211 @@ int lbr_chartabsize_adv(char_u **s, colnr_T col) return retval; } -/* - * This function is used very often, keep it fast!!!! - * - * If "headp" not NULL, set *headp to the size of what we for 'showbreak' - * string at start of line. Warning: *headp is only set if it's a non-zero - * value, init to 0 before calling. - */ +/// This function is used very often, keep it fast!!!! +/// +/// If "headp" not NULL, set *headp to the size of what we for 'showbreak' +/// string at start of line. Warning: *headp is only set if it's a non-zero +/// value, init to 0 before calling. +/// +/// @param wp +/// @param s +/// @param col +/// @param headp +/// +/// @return The number of characters taken up on the screen. int win_lbr_chartabsize(win_T *wp, char_u *s, colnr_T col, int *headp) { - int c; - int size; colnr_T col2; colnr_T colmax; int added; int mb_added = 0; int numberextra; - char_u *ps; + char_u *ps; int tab_corr = (*s == TAB); int n; - /* - * No 'linebreak' and 'showbreak': return quickly. - */ - if (!wp->w_p_lbr && *p_sbr == NUL) { - if (wp->w_p_wrap) + // No 'linebreak' and 'showbreak': return quickly. + if (!wp->w_p_lbr && (*p_sbr == NUL)) { + if (wp->w_p_wrap) { return win_nolbr_chartabsize(wp, s, col, headp); + } RET_WIN_BUF_CHARTABSIZE(wp, wp->w_buffer, s, col) } - /* - * First get normal size, without 'linebreak' - */ - size = win_chartabsize(wp, s, col); - c = *s; + // First get normal size, without 'linebreak' + int size = win_chartabsize(wp, s, col); + int c = *s; - /* - * If 'linebreak' set check at a blank before a non-blank if the line - * needs a break here - */ + // If 'linebreak' set check at a blank before a non-blank if the line + // needs a break here if (wp->w_p_lbr && vim_isbreak(c) && !vim_isbreak(s[1]) && !wp->w_p_list && wp->w_p_wrap - && wp->w_width != 0 - ) { - /* - * Count all characters from first non-blank after a blank up to next - * non-blank after a blank. - */ + && (wp->w_width != 0)) { + // Count all characters from first non-blank after a blank up to next + // non-blank after a blank. numberextra = win_col_off(wp); col2 = col; colmax = (colnr_T)(W_WIDTH(wp) - numberextra); + if (col >= colmax) { n = colmax + win_col_off2(wp); - if (n > 0) + + if (n > 0) { colmax += (((col - colmax) / n) + 1) * n; + } } - for (;; ) { + for (;;) { ps = s; mb_ptr_adv(s); c = *s; - if (!(c != NUL + + if (!((c != NUL) && (vim_isbreak(c) || (!vim_isbreak(c) - && (col2 == col || !vim_isbreak(*ps)))))) + && ((col2 == col) || !vim_isbreak(*ps)))))) { break; + } col2 += win_chartabsize(wp, s, col2); - if (col2 >= colmax) { /* doesn't fit */ + + if (col2 >= colmax) { /* doesn't fit */ size = colmax - col; tab_corr = FALSE; break; } } - } else if (has_mbyte && size == 2 && MB_BYTE2LEN(*s) > 1 - && wp->w_p_wrap && in_win_border(wp, col)) { - ++size; /* Count the ">" in the last column. */ + } else if (has_mbyte + && (size == 2) + && (MB_BYTE2LEN(*s) > 1) + && wp->w_p_wrap + && in_win_border(wp, col)) { + // Count the ">" in the last column. + ++size; mb_added = 1; } - /* - * May have to add something for 'showbreak' string at start of line - * Set *headp to the size of what we add. - */ + // May have to add something for 'showbreak' string at start of line + // Set *headp to the size of what we add. added = 0; - if (*p_sbr != NUL && wp->w_p_wrap && col != 0) { + + if ((*p_sbr != NUL) && wp->w_p_wrap && (col != 0)) { numberextra = win_col_off(wp); col += numberextra + mb_added; + if (col >= (colnr_T)W_WIDTH(wp)) { col -= W_WIDTH(wp); numberextra = W_WIDTH(wp) - (numberextra - win_col_off2(wp)); - if (numberextra > 0) + if (numberextra > 0) { col = col % numberextra; + } } - if (col == 0 || col + size > (colnr_T)W_WIDTH(wp)) { + + if ((col == 0) || (col + size > (colnr_T)W_WIDTH(wp))) { added = vim_strsize(p_sbr); - if (tab_corr) + if (tab_corr) { size += (added / wp->w_buffer->b_p_ts) * wp->w_buffer->b_p_ts; - else + } else { size += added; - if (col != 0) + } + + if (col != 0) { added = 0; + } } } - if (headp != NULL) + + if (headp != NULL) { *headp = added + mb_added; + } return size; } -/* - * Like win_lbr_chartabsize(), except that we know 'linebreak' is off and - * 'wrap' is on. This means we need to check for a double-byte character that - * doesn't fit at the end of the screen line. - */ +/// Like win_lbr_chartabsize(), except that we know 'linebreak' is off and +/// 'wrap' is on. This means we need to check for a double-byte character that +/// doesn't fit at the end of the screen line. +/// +/// @param wp +/// @param s +/// @param col +/// @param headp +/// +/// @return The number of characters take up on the screen. static int win_nolbr_chartabsize(win_T *wp, char_u *s, colnr_T col, int *headp) { int n; - if (*s == TAB && (!wp->w_p_list || lcs_tab1)) { + if ((*s == TAB) && (!wp->w_p_list || lcs_tab1)) { n = wp->w_buffer->b_p_ts; return (int)(n - (col % n)); } n = ptr2cells(s); - /* Add one cell for a double-width character in the last column of the - * window, displayed with a ">". */ - if (n == 2 && MB_BYTE2LEN(*s) > 1 && in_win_border(wp, col)) { - if (headp != NULL) + + // Add one cell for a double-width character in the last column of the + // window, displayed with a ">". + if ((n == 2) && (MB_BYTE2LEN(*s) > 1) && in_win_border(wp, col)) { + if (headp != NULL) { *headp = 1; + } return 3; } return n; } -/* - * Return TRUE if virtual column "vcol" is in the rightmost column of window - * "wp". - */ +/// Return TRUE if virtual column "vcol" is in the rightmost column of window +/// "wp". +/// +/// @param wp +/// @param vcol +/// +/// @return TRUE if the virtual column is in the rightmost column. int in_win_border(win_T *wp, colnr_T vcol) { - int width1; /* width of first line (after line number) */ - int width2; /* width of further lines */ + int width1; // width of first line (after line number) + int width2; // width of further lines - if (wp->w_width == 0) /* there is no border */ + if (wp->w_width == 0) { + // there is no border return FALSE; + } width1 = W_WIDTH(wp) - win_col_off(wp); - if ((int)vcol < width1 - 1) + + if ((int)vcol < width1 - 1) { return FALSE; - if ((int)vcol == width1 - 1) + } + + if ((int)vcol == width1 - 1) { return TRUE; + } width2 = width1 + win_col_off2(wp); - if (width2 <= 0) + + if (width2 <= 0) { return FALSE; + } return (vcol - width1) % width2 == width2 - 1; } -/* - * Get virtual column number of pos. - * start: on the first position of this character (TAB, ctrl) - * cursor: where the cursor is on this character (first char, except for TAB) - * end: on the last position of this character (TAB, ctrl) - * - * This is used very often, keep it fast! - */ -void getvcol(win_T *wp, pos_T *pos, colnr_T *start, colnr_T *cursor, colnr_T *end) +/// Get virtual column number of pos. +/// start: on the first position of this character (TAB, ctrl) +/// cursor: where the cursor is on this character (first char, except for TAB) +/// end: on the last position of this character (TAB, ctrl) +/// +/// This is used very often, keep it fast! +/// +/// @param wp +/// @param pos +/// @param start +/// @param cursor +/// @param end +void getvcol(win_T *wp, pos_T *pos, colnr_T *start, colnr_T *cursor, + colnr_T *end) { colnr_T vcol; - char_u *ptr; /* points to current char */ - char_u *posptr; /* points to char at pos->col */ + char_u *ptr; // points to current char + char_u *posptr; // points to char at pos->col int incr; int head; int ts = wp->w_buffer->b_p_ts; @@ -994,96 +1180,118 @@ void getvcol(win_T *wp, pos_T *pos, colnr_T *start, colnr_T *cursor, colnr_T *en vcol = 0; ptr = ml_get_buf(wp->w_buffer, pos->lnum, FALSE); - if (pos->col == MAXCOL) - posptr = NULL; /* continue until the NUL */ - else + + if (pos->col == MAXCOL) { + // continue until the NUL + posptr = NULL; + } else { posptr = ptr + pos->col; + } - /* - * This function is used very often, do some speed optimizations. - * When 'list', 'linebreak' and 'showbreak' are not set use a simple loop. - * Also use this when 'list' is set but tabs take their normal size. - */ - if ((!wp->w_p_list || lcs_tab1 != NUL) - && !wp->w_p_lbr && *p_sbr == NUL - ) { - for (;; ) { + // This function is used very often, do some speed optimizations. + // When 'list', 'linebreak' and 'showbreak' are not set use a simple loop. + // Also use this when 'list' is set but tabs take their normal size. + if ((!wp->w_p_list || (lcs_tab1 != NUL)) + && !wp->w_p_lbr + && (*p_sbr == NUL)) { + for (;;) { head = 0; c = *ptr; - /* make sure we don't go past the end of the line */ + + // make sure we don't go past the end of the line if (c == NUL) { - incr = 1; /* NUL at end of line only takes one column */ + // NUL at end of line only takes one column + incr = 1; break; } - /* A tab gets expanded, depending on the current column */ - if (c == TAB) + + // A tab gets expanded, depending on the current column + if (c == TAB) { incr = ts - (vcol % ts); - else { + } else { if (has_mbyte) { - /* For utf-8, if the byte is >= 0x80, need to look at - * further bytes to find the cell width. */ - if (enc_utf8 && c >= 0x80) + // For utf-8, if the byte is >= 0x80, need to look at + // further bytes to find the cell width. + if (enc_utf8 && (c >= 0x80)) { incr = utf_ptr2cells(ptr); - else + } else { incr = CHARSIZE(c); + } - /* If a double-cell char doesn't fit at the end of a line - * it wraps to the next line, it's like this char is three - * cells wide. */ - if (incr == 2 && wp->w_p_wrap && MB_BYTE2LEN(*ptr) > 1 + // If a double-cell char doesn't fit at the end of a line + // it wraps to the next line, it's like this char is three + // cells wide. + if ((incr == 2) + && wp->w_p_wrap + && (MB_BYTE2LEN(*ptr) > 1) && in_win_border(wp, vcol)) { ++incr; head = 1; } - } else + } else { incr = CHARSIZE(c); + } } - if (posptr != NULL && ptr >= posptr) /* character at pos->col */ + if ((posptr != NULL) && (ptr >= posptr)) { + // character at pos->col break; + } vcol += incr; mb_ptr_adv(ptr); } - } else { - for (;; ) { - /* A tab gets expanded, depending on the current column */ + } else { + for (;;) { + // A tab gets expanded, depending on the current column head = 0; incr = win_lbr_chartabsize(wp, ptr, vcol, &head); - /* make sure we don't go past the end of the line */ + + // make sure we don't go past the end of the line if (*ptr == NUL) { - incr = 1; /* NUL at end of line only takes one column */ + // NUL at end of line only takes one column + incr = 1; break; } - if (posptr != NULL && ptr >= posptr) /* character at pos->col */ + if ((posptr != NULL) && (ptr >= posptr)) { + // character at pos->col break; + } vcol += incr; mb_ptr_adv(ptr); } } - if (start != NULL) + + if (start != NULL) { *start = vcol + head; - if (end != NULL) + } + + if (end != NULL) { *end = vcol + incr - 1; + } + if (cursor != NULL) { - if (*ptr == TAB + if ((*ptr == TAB) && (State & NORMAL) && !wp->w_p_list && !virtual_active() - && !(VIsual_active - && (*p_sel == 'e' || ltoreq(*pos, VIsual))) - ) - *cursor = vcol + incr - 1; /* cursor at end */ - else - *cursor = vcol + head; /* cursor at start */ + && !(VIsual_active && ((*p_sel == 'e') || ltoreq(*pos, VIsual)))) { + // cursor at end + *cursor = vcol + incr - 1; + } else { + // cursor at start + *cursor = vcol + head; + } } } -/* - * Get virtual cursor column in the current window, pretending 'list' is off. - */ +/// Get virtual cursor column in the current window, pretending 'list' is off. +/// +/// @param posp +/// +/// @retujrn The virtual cursor column. colnr_T getvcol_nolist(pos_T *posp) { int list_save = curwin->w_p_list; @@ -1095,150 +1303,198 @@ colnr_T getvcol_nolist(pos_T *posp) return vcol; } -/* - * Get virtual column in virtual mode. - */ -void getvvcol(win_T *wp, pos_T *pos, colnr_T *start, colnr_T *cursor, colnr_T *end) +/// Get virtual column in virtual mode. +/// +/// @param wp +/// @param pos +/// @param start +/// @param cursor +/// @param end +void getvvcol(win_T *wp, pos_T *pos, colnr_T *start, colnr_T *cursor, + colnr_T *end) { colnr_T col; colnr_T coladd; colnr_T endadd; - char_u *ptr; + char_u *ptr; if (virtual_active()) { - /* For virtual mode, only want one value */ + // For virtual mode, only want one value getvcol(wp, pos, &col, NULL, NULL); coladd = pos->coladd; endadd = 0; - /* Cannot put the cursor on part of a wide character. */ + + // Cannot put the cursor on part of a wide character. ptr = ml_get_buf(wp->w_buffer, pos->lnum, FALSE); + if (pos->col < (colnr_T)STRLEN(ptr)) { int c = (*mb_ptr2char)(ptr + pos->col); - - if (c != TAB && vim_isprintc(c)) { + if ((c != TAB) && vim_isprintc(c)) { endadd = (colnr_T)(char2cells(c) - 1); - if (coladd > endadd) /* past end of line */ + if (coladd > endadd) { + // past end of line endadd = 0; - else + } else { coladd = 0; + } } } col += coladd; - if (start != NULL) + + if (start != NULL) { *start = col; - if (cursor != NULL) + } + + if (cursor != NULL) { *cursor = col; - if (end != NULL) + } + + if (end != NULL) { *end = col + endadd; - } else + } + } else { getvcol(wp, pos, start, cursor, end); + } } -/* - * Get the leftmost and rightmost virtual column of pos1 and pos2. - * Used for Visual block mode. - */ -void getvcols(win_T *wp, pos_T *pos1, pos_T *pos2, colnr_T *left, colnr_T *right) +/// Get the leftmost and rightmost virtual column of pos1 and pos2. +/// Used for Visual block mode. +/// +/// @param wp +/// @param pos1 +/// @param pos2 +/// @param left +/// @param right +void getvcols(win_T *wp, pos_T *pos1, pos_T *pos2, colnr_T *left, + colnr_T *right) { - colnr_T from1, from2, to1, to2; + colnr_T from1; + colnr_T from2; + colnr_T to1; + colnr_T to2; if (ltp(pos1, pos2)) { getvvcol(wp, pos1, &from1, NULL, &to1); getvvcol(wp, pos2, &from2, NULL, &to2); - } else { + } else { getvvcol(wp, pos2, &from1, NULL, &to1); getvvcol(wp, pos1, &from2, NULL, &to2); } - if (from2 < from1) + + if (from2 < from1) { *left = from2; - else + } else { *left = from1; + } + if (to2 > to1) { - if (*p_sel == 'e' && from2 - 1 >= to1) + if ((*p_sel == 'e') && (from2 - 1 >= to1)) { *right = from2 - 1; - else + } else { *right = to2; - } else + } + } else { *right = to1; + } } -/* - * skipwhite: skip over ' ' and '\t'. - */ -char_u *skipwhite(char_u *q) +/// skipwhite: skip over ' ' and '\t'. +/// +/// @param q +/// +/// @return Pointer to character after the skipped whitespace. +char_u* skipwhite(char_u *q) { - char_u *p = q; - - while (vim_iswhite(*p)) /* skip to next non-white */ - ++p; + char_u *p = q; + while (vim_iswhite(*p)) { + // skip to next non-white + p++; + } return p; } -/* - * skip over digits - */ -char_u *skipdigits(char_u *q) +/// skip over digits +/// +/// @param q +/// +/// @return Pointer to the character after the skipped digits. +char_u* skipdigits(char_u *q) { - char_u *p = q; - - while (VIM_ISDIGIT(*p)) /* skip to next non-digit */ - ++p; + char_u *p = q; + while (VIM_ISDIGIT(*p)) { + // skip to next non-digit + p++; + } return p; } -/* - * skip over digits and hex characters - */ -char_u *skiphex(char_u *q) +/// skip over digits and hex characters +/// +/// @param q +/// +/// @return Pointer to the character after the skipped digits and hex +/// characters. +char_u* skiphex(char_u *q) { - char_u *p = q; - - while (vim_isxdigit(*p)) /* skip to next non-digit */ - ++p; + char_u *p = q; + while (vim_isxdigit(*p)) { + // skip to next non-digit + p++; + } return p; } -/* - * skip to digit (or NUL after the string) - */ -char_u *skiptodigit(char_u *q) +/// skip to digit (or NUL after the string) +/// +/// @param q +/// +/// @return Pointer to the digit or (NUL after the string). +char_u* skiptodigit(char_u *q) { - char_u *p = q; - - while (*p != NUL && !VIM_ISDIGIT(*p)) /* skip to next digit */ - ++p; + char_u *p = q; + while (*p != NUL && !VIM_ISDIGIT(*p)) { + // skip to next digit + p++; + } return p; } -/* - * skip to hex character (or NUL after the string) - */ -char_u *skiptohex(char_u *q) +/// skip to hex character (or NUL after the string) +/// +/// @param q +/// +/// @return Pointer to the hex character or (NUL after the string). +char_u* skiptohex(char_u *q) { - char_u *p = q; - - while (*p != NUL && !vim_isxdigit(*p)) /* skip to next digit */ - ++p; + char_u *p = q; + while (*p != NUL && !vim_isxdigit(*p)) { + // skip to next digit + p++; + } return p; } -/* - * Variant of isdigit() that can handle characters > 0x100. - * We don't use isdigit() here, because on some systems it also considers - * superscript 1 to be a digit. - * Use the VIM_ISDIGIT() macro for simple arguments. - */ +/// Variant of isdigit() that can handle characters > 0x100. +/// We don't use isdigit() here, because on some systems it also considers +/// superscript 1 to be a digit. +/// Use the VIM_ISDIGIT() macro for simple arguments. +/// +/// @param c +/// +/// @return TRUE if the character is a digit. int vim_isdigit(int c) { return c >= '0' && c <= '9'; } -/* - * Variant of isxdigit() that can handle characters > 0x100. - * We don't use isxdigit() here, because on some systems it also considers - * superscript 1 to be a digit. - */ +/// Variant of isxdigit() that can handle characters > 0x100. +/// We don't use isxdigit() here, because on some systems it also considers +/// superscript 1 to be a digit. +/// +/// @param c +/// +/// @return TRUE if the character is a digit. int vim_isxdigit(int c) { return (c >= '0' && c <= '9') @@ -1246,191 +1502,259 @@ int vim_isxdigit(int c) || (c >= 'A' && c <= 'F'); } -/* - * Vim's own character class functions. These exist because many library - * islower()/toupper() etc. do not work properly: they crash when used with - * invalid values or can't handle latin1 when the locale is C. - * Speed is most important here. - */ +// Vim's own character class functions. These exist because many library +// islower()/toupper() etc. do not work properly: they crash when used with +// invalid values or can't handle latin1 when the locale is C. +// Speed is most important here. #define LATIN1LOWER 'l' #define LATIN1UPPER 'U' static char_u latin1flags[257] = - " UUUUUUUUUUUUUUUUUUUUUUUUUU llllllllllllllllllllllllll UUUUUUUUUUUUUUUUUUUUUUU UUUUUUUllllllllllllllllllllllll llllllll"; + " " + " UUUUUUUUUUUUUUUUUUUUUUUUUU llllllllllllllllllllllllll " + " " + "UUUUUUUUUUUUUUUUUUUUUUU UUUUUUUllllllllllllllllllllllll llllllll"; static char_u latin1upper[257] = - " !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`ABCDEFGHIJKLMNOPQRSTUVWXYZ{|}~\x7f\x80\x81\x82\x83\x84\x85\x86\x87\x88\x89\x8a\x8b\x8c\x8d\x8e\x8f\x90\x91\x92\x93\x94\x95\x96\x97\x98\x99\x9a\x9b\x9c\x9d\x9e\x9f\xa0\xa1\xa2\xa3\xa4\xa5\xa6\xa7\xa8\xa9\xaa\xab\xac\xad\xae\xaf\xb0\xb1\xb2\xb3\xb4\xb5\xb6\xb7\xb8\xb9\xba\xbb\xbc\xbd\xbe\xbf\xc0\xc1\xc2\xc3\xc4\xc5\xc6\xc7\xc8\xc9\xca\xcb\xcc\xcd\xce\xcf\xd0\xd1\xd2\xd3\xd4\xd5\xd6\xd7\xd8\xd9\xda\xdb\xdc\xdd\xde\xdf\xc0\xc1\xc2\xc3\xc4\xc5\xc6\xc7\xc8\xc9\xca\xcb\xcc\xcd\xce\xcf\xd0\xd1\xd2\xd3\xd4\xd5\xd6\xf7\xd8\xd9\xda\xdb\xdc\xdd\xde\xff"; + " !\"#$%&'()*+,-./0123456789:;<=>" + "?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`ABCDEFGHIJKLMNOPQRSTUVWXYZ{|}~" + "\x7f\x80\x81\x82\x83\x84\x85\x86\x87\x88\x89\x8a\x8b\x8c\x8d\x8e" + "\x8f\x90\x91\x92\x93\x94\x95\x96\x97\x98\x99\x9a\x9b\x9c\x9d\x9e" + "\x9f\xa0\xa1\xa2\xa3\xa4\xa5\xa6\xa7\xa8\xa9\xaa\xab\xac\xad\xae" + "\xaf\xb0\xb1\xb2\xb3\xb4\xb5\xb6\xb7\xb8\xb9\xba\xbb\xbc\xbd\xbe" + "\xbf\xc0\xc1\xc2\xc3\xc4\xc5\xc6\xc7\xc8\xc9\xca\xcb\xcc\xcd\xce" + "\xcf\xd0\xd1\xd2\xd3\xd4\xd5\xd6\xd7\xd8\xd9\xda\xdb\xdc\xdd\xde" + "\xdf\xc0\xc1\xc2\xc3\xc4\xc5\xc6\xc7\xc8\xc9\xca\xcb\xcc\xcd\xce" + "\xcf\xd0\xd1\xd2\xd3\xd4\xd5\xd6\xf7\xd8\xd9\xda\xdb\xdc\xdd\xde\xff"; static char_u latin1lower[257] = - " !\"#$%&'()*+,-./0123456789:;<=>?@abcdefghijklmnopqrstuvwxyz[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~\x7f\x80\x81\x82\x83\x84\x85\x86\x87\x88\x89\x8a\x8b\x8c\x8d\x8e\x8f\x90\x91\x92\x93\x94\x95\x96\x97\x98\x99\x9a\x9b\x9c\x9d\x9e\x9f\xa0\xa1\xa2\xa3\xa4\xa5\xa6\xa7\xa8\xa9\xaa\xab\xac\xad\xae\xaf\xb0\xb1\xb2\xb3\xb4\xb5\xb6\xb7\xb8\xb9\xba\xbb\xbc\xbd\xbe\xbf\xe0\xe1\xe2\xe3\xe4\xe5\xe6\xe7\xe8\xe9\xea\xeb\xec\xed\xee\xef\xf0\xf1\xf2\xf3\xf4\xf5\xf6\xd7\xf8\xf9\xfa\xfb\xfc\xfd\xfe\xdf\xe0\xe1\xe2\xe3\xe4\xe5\xe6\xe7\xe8\xe9\xea\xeb\xec\xed\xee\xef\xf0\xf1\xf2\xf3\xf4\xf5\xf6\xf7\xf8\xf9\xfa\xfb\xfc\xfd\xfe\xff"; + " !\"#$%&'()*+,-./0123456789:;<=>" + "?@abcdefghijklmnopqrstuvwxyz[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~" + "\x7f\x80\x81\x82\x83\x84\x85\x86\x87\x88\x89\x8a\x8b\x8c\x8d\x8e" + "\x8f\x90\x91\x92\x93\x94\x95\x96\x97\x98\x99\x9a\x9b\x9c\x9d\x9e" + "\x9f\xa0\xa1\xa2\xa3\xa4\xa5\xa6\xa7\xa8\xa9\xaa\xab\xac\xad\xae" + "\xaf\xb0\xb1\xb2\xb3\xb4\xb5\xb6\xb7\xb8\xb9\xba\xbb\xbc\xbd\xbe" + "\xbf\xe0\xe1\xe2\xe3\xe4\xe5\xe6\xe7\xe8\xe9\xea\xeb\xec\xed\xee" + "\xef\xf0\xf1\xf2\xf3\xf4\xf5\xf6\xd7\xf8\xf9\xfa\xfb\xfc\xfd\xfe" + "\xdf\xe0\xe1\xe2\xe3\xe4\xe5\xe6\xe7\xe8\xe9\xea\xeb\xec\xed\xee" + "\xef\xf0\xf1\xf2\xf3\xf4\xf5\xf6\xf7\xf8\xf9\xfa\xfb\xfc\xfd\xfe\xff"; int vim_islower(int c) { - if (c <= '@') + if (c <= '@') { return FALSE; + } + if (c >= 0x80) { - if (enc_utf8) + if (enc_utf8) { return utf_islower(c); + } + if (c >= 0x100) { #ifdef HAVE_ISWLOWER - if (has_mbyte) + + if (has_mbyte) { return iswlower(c); -#endif - /* islower() can't handle these chars and may crash */ + } +#endif // ifdef HAVE_ISWLOWER + + // islower() can't handle these chars and may crash return FALSE; } - if (enc_latin1like) + + if (enc_latin1like) { return (latin1flags[c] & LATIN1LOWER) == LATIN1LOWER; + } } return islower(c); } int vim_isupper(int c) { - if (c <= '@') + if (c <= '@') { return FALSE; + } + if (c >= 0x80) { - if (enc_utf8) + if (enc_utf8) { return utf_isupper(c); + } + if (c >= 0x100) { #ifdef HAVE_ISWUPPER - if (has_mbyte) + + if (has_mbyte) { return iswupper(c); -#endif - /* islower() can't handle these chars and may crash */ + } +#endif // ifdef HAVE_ISWUPPER + + // islower() can't handle these chars and may crash return FALSE; } - if (enc_latin1like) + + if (enc_latin1like) { return (latin1flags[c] & LATIN1UPPER) == LATIN1UPPER; + } } return isupper(c); } int vim_toupper(int c) { - if (c <= '@') + if (c <= '@') { return c; + } + if (c >= 0x80) { - if (enc_utf8) + if (enc_utf8) { return utf_toupper(c); + } + if (c >= 0x100) { #ifdef HAVE_TOWUPPER - if (has_mbyte) + + if (has_mbyte) { return towupper(c); -#endif - /* toupper() can't handle these chars and may crash */ + } +#endif // ifdef HAVE_TOWUPPER + + // toupper() can't handle these chars and may crash return c; } - if (enc_latin1like) + + if (enc_latin1like) { return latin1upper[c]; + } } return TOUPPER_LOC(c); } int vim_tolower(int c) { - if (c <= '@') + if (c <= '@') { return c; + } + if (c >= 0x80) { - if (enc_utf8) + if (enc_utf8) { return utf_tolower(c); + } + if (c >= 0x100) { #ifdef HAVE_TOWLOWER - if (has_mbyte) + + if (has_mbyte) { return towlower(c); -#endif - /* tolower() can't handle these chars and may crash */ + } +#endif // ifdef HAVE_TOWLOWER + + // tolower() can't handle these chars and may crash return c; } - if (enc_latin1like) + + if (enc_latin1like) { return latin1lower[c]; + } } return TOLOWER_LOC(c); } -/* - * skiptowhite: skip over text until ' ' or '\t' or NUL. - */ -char_u *skiptowhite(char_u *p) +/// skiptowhite: skip over text until ' ' or '\t' or NUL. +/// +/// @param p +/// +/// @return Pointer to the next whitespace or NUL character. +char_u* skiptowhite(char_u *p) { - while (*p != ' ' && *p != '\t' && *p != NUL) - ++p; + while (*p != ' ' && *p != '\t' && *p != NUL) { + p++; + } return p; } -#if defined(FEAT_LISTCMDS) || defined(FEAT_SIGNS) || defined(FEAT_SNIFF) \ +#if defined(FEAT_LISTCMDS) \ + || defined(FEAT_SIGNS) \ + || defined(FEAT_SNIFF) \ || defined(PROTO) -/* - * skiptowhite_esc: Like skiptowhite(), but also skip escaped chars - */ -char_u *skiptowhite_esc(char_u *p) + +/// skiptowhite_esc: Like skiptowhite(), but also skip escaped chars +/// +/// @param p +/// +/// @return Pointer to the next whitespace character. +char_u* skiptowhite_esc(char_u *p) { while (*p != ' ' && *p != '\t' && *p != NUL) { - if ((*p == '\\' || *p == Ctrl_V) && *(p + 1) != NUL) + if (((*p == '\\') || (*p == Ctrl_V)) && (*(p + 1) != NUL)) { ++p; + } ++p; } return p; } -#endif -/* - * Getdigits: Get a number from a string and skip over it. - * Note: the argument is a pointer to a char_u pointer! - */ +#endif // if defined(FEAT_LISTCMDS) || defined(FEAT_SIGNS) + // || defined(FEAT_SNIFF) || defined(PROTO) + +/// Getdigits: Get a number from a string and skip over it. +/// +/// Note: the argument is a pointer to a char_u pointer! +/// +/// @param pp +/// +/// @return Number from the string. long getdigits(char_u **pp) { - char_u *p; - long retval; + char_u *p = *pp; + long retval = atol((char *)p); - p = *pp; - retval = atol((char *)p); - if (*p == '-') /* skip negative sign */ + if (*p == '-') { + // skip negative sign ++p; - p = skipdigits(p); /* skip to next non-digit */ + } + // skip to next non-digit + p = skipdigits(p); *pp = p; return retval; } -/* - * Return TRUE if "lbuf" is empty or only contains blanks. - */ +/// Return TRUE if "lbuf" is empty or only contains blanks. +/// +/// @param lbuf +/// +/// @return TRUE if `lbuf` is empty or only contains blanks. int vim_isblankline(char_u *lbuf) { - char_u *p; - - p = skipwhite(lbuf); + char_u *p = skipwhite(lbuf); return *p == NUL || *p == '\r' || *p == '\n'; } -/* - * Convert a string into a long and/or unsigned long, taking care of - * hexadecimal and octal numbers. Accepts a '-' sign. - * If "hexp" is not NULL, returns a flag to indicate the type of the number: - * 0 decimal - * '0' octal - * 'X' hex - * 'x' hex - * If "len" is not NULL, the length of the number in characters is returned. - * If "nptr" is not NULL, the signed result is returned in it. - * If "unptr" is not NULL, the unsigned result is returned in it. - * If "dooct" is non-zero recognize octal numbers, when > 1 always assume - * octal number. - * If "dohex" is non-zero recognize hex numbers, when > 1 always assume - * hex number. - */ -void -vim_str2nr ( - char_u *start, - int *hexp, /* return: type of number 0 = decimal, 'x' - or 'X' is hex, '0' = octal */ - int *len, /* return: detected length of number */ - int dooct, /* recognize octal number */ - int dohex, /* recognize hex number */ - long *nptr, /* return: signed result */ - unsigned long *unptr /* return: unsigned result */ -) +/// Convert a string into a long and/or unsigned long, taking care of +/// hexadecimal and octal numbers. Accepts a '-' sign. +/// If "hexp" is not NULL, returns a flag to indicate the type of the number: +/// 0 decimal +/// '0' octal +/// 'X' hex +/// 'x' hex +/// If "len" is not NULL, the length of the number in characters is returned. +/// If "nptr" is not NULL, the signed result is returned in it. +/// If "unptr" is not NULL, the unsigned result is returned in it. +/// If "dooct" is non-zero recognize octal numbers, when > 1 always assume +/// octal number. +/// If "dohex" is non-zero recognize hex numbers, when > 1 always assume +/// hex number. +/// +/// @param start +/// @param hexp Returns type of number 0 = decimal, 'x' or 'X' is hex, +// '0' = octal +/// @param len Returns the detected length of number. +/// @param dooct recognize octal number +/// @param dohex recognize hex number +/// @param nptr Returns the signed result. +/// @param unptr Returns the unsigned result. +void vim_str2nr(char_u *start, int *hexp, int *len, int dooct, int dohex, + long *nptr, unsigned long *unptr) { - char_u *ptr = start; - int hex = 0; /* default is decimal */ + char_u *ptr = start; + int hex = 0; // default is decimal int negative = FALSE; unsigned long un = 0; int n; @@ -1440,104 +1764,135 @@ vim_str2nr ( ++ptr; } - /* Recognize hex and octal. */ - if (ptr[0] == '0' && ptr[1] != '8' && ptr[1] != '9') { + // Recognize hex and octal. + if ((ptr[0] == '0') && (ptr[1] != '8') && (ptr[1] != '9')) { hex = ptr[1]; - if (dohex && (hex == 'X' || hex == 'x') && vim_isxdigit(ptr[2])) - ptr += 2; /* hexadecimal */ - else { - hex = 0; /* default is decimal */ + + if (dohex + && ((hex == 'X') || (hex == 'x')) + && vim_isxdigit(ptr[2])) { + // hexadecimal + ptr += 2; + } else { + // default is decimal + hex = 0; + if (dooct) { - /* Don't interpret "0", "08" or "0129" as octal. */ + // Don't interpret "0", "08" or "0129" as octal. for (n = 1; VIM_ISDIGIT(ptr[n]); ++n) { if (ptr[n] > '7') { - hex = 0; /* can't be octal */ + // can't be octal + hex = 0; break; } - if (ptr[n] >= '0') - hex = '0'; /* assume octal */ + + if (ptr[n] >= '0') { + // assume octal + hex = '0'; + } } } } } - /* - * Do the string-to-numeric conversion "manually" to avoid sscanf quirks. - */ - if (hex == '0' || dooct > 1) { - /* octal */ + // Do the string-to-numeric conversion "manually" to avoid sscanf quirks. + if ((hex == '0') || (dooct > 1)) { + // octal while ('0' <= *ptr && *ptr <= '7') { un = 8 * un + (unsigned long)(*ptr - '0'); - ++ptr; + ptr++; } - } else if (hex != 0 || dohex > 1) { - /* hex */ + } else if ((hex != 0) || (dohex > 1)) { + // hex while (vim_isxdigit(*ptr)) { un = 16 * un + (unsigned long)hex2nr(*ptr); - ++ptr; + ptr++; } - } else { - /* decimal */ + } else { + // decimal while (VIM_ISDIGIT(*ptr)) { un = 10 * un + (unsigned long)(*ptr - '0'); - ++ptr; + ptr++; } } - if (hexp != NULL) + if (hexp != NULL) { *hexp = hex; - if (len != NULL) + } + + if (len != NULL) { *len = (int)(ptr - start); + } + if (nptr != NULL) { - if (negative) /* account for leading '-' for decimal numbers */ + if (negative) { + // account for leading '-' for decimal numbers *nptr = -(long)un; - else + } else { *nptr = (long)un; + } } - if (unptr != NULL) + + if (unptr != NULL) { *unptr = un; + } } -/* - * Return the value of a single hex character. - * Only valid when the argument is '0' - '9', 'A' - 'F' or 'a' - 'f'. - */ +/// Return the value of a single hex character. +/// Only valid when the argument is '0' - '9', 'A' - 'F' or 'a' - 'f'. +/// +/// @param c +/// +/// @return The value of the hex character. int hex2nr(int c) { - if (c >= 'a' && c <= 'f') + if ((c >= 'a') && (c <= 'f')) { return c - 'a' + 10; - if (c >= 'A' && c <= 'F') + } + + if ((c >= 'A') && (c <= 'F')) { return c - 'A' + 10; + } return c - '0'; } #if defined(FEAT_TERMRESPONSE) \ - || (defined(FEAT_GUI_GTK) && defined(FEAT_WINDOWS)) || defined(PROTO) -/* - * Convert two hex characters to a byte. - * Return -1 if one of the characters is not hex. - */ + || (defined(FEAT_GUI_GTK) && defined(FEAT_WINDOWS)) \ + || defined(PROTO) + +/// Convert two hex characters to a byte. +/// Return -1 if one of the characters is not hex. +/// +/// @param p +/// +/// @return The two hex characters converted to a byte or -1 if one of the +/// character is not hex. int hexhex2nr(char_u *p) { - if (!vim_isxdigit(p[0]) || !vim_isxdigit(p[1])) + if (!vim_isxdigit(p[0]) || !vim_isxdigit(p[1])) { return -1; + } return (hex2nr(p[0]) << 4) + hex2nr(p[1]); } -#endif - -/* - * Return TRUE if "str" starts with a backslash that should be removed. - * For MS-DOS, WIN32 and OS/2 this is only done when the character after the - * backslash is not a normal file name character. - * '$' is a valid file name character, we don't remove the backslash before - * it. This means it is not possible to use an environment variable after a - * backslash. "C:\$VIM\doc" is taken literally, only "$VIM\doc" works. - * Although "\ name" is valid, the backslash in "Program\ files" must be - * removed. Assume a file name doesn't start with a space. - * For multi-byte names, never remove a backslash before a non-ascii - * character, assume that all multi-byte characters are valid file name - * characters. - */ + +#endif // if defined(FEAT_TERMRESPONSE) || (defined(FEAT_GUI_GTK) + // && defined(FEAT_WINDOWS)) || defined(PROTO) + +/// Return TRUE if "str" starts with a backslash that should be removed. +/// For MS-DOS, WIN32 and OS/2 this is only done when the character after the +/// backslash is not a normal file name character. +/// '$' is a valid file name character, we don't remove the backslash before +/// it. This means it is not possible to use an environment variable after a +/// backslash. "C:\$VIM\doc" is taken literally, only "$VIM\doc" works. +/// Although "\ name" is valid, the backslash in "Program\ files" must be +/// removed. Assume a file name doesn't start with a space. +/// For multi-byte names, never remove a backslash before a non-ascii +/// character, assume that all multi-byte characters are valid file name +/// characters. +/// +/// @param str +/// +/// @return TRUE if `str` starts with a backslash that should be removed. int rem_backslash(char_u *str) { #ifdef BACKSLASH_IN_FILENAME @@ -1548,34 +1903,37 @@ int rem_backslash(char_u *str) && str[1] != '*' && str[1] != '?' && !vim_isfilec(str[1]))); -#else + +#else // ifdef BACKSLASH_IN_FILENAME return str[0] == '\\' && str[1] != NUL; -#endif +#endif // ifdef BACKSLASH_IN_FILENAME } -/* - * Halve the number of backslashes in a file name argument. - * For MS-DOS we only do this if the character after the backslash - * is not a normal file character. - */ +/// Halve the number of backslashes in a file name argument. +/// For MS-DOS we only do this if the character after the backslash +/// is not a normal file character. +/// +/// @param p void backslash_halve(char_u *p) { - for (; *p; ++p) - if (rem_backslash(p)) + for (; *p; ++p) { + if (rem_backslash(p)) { STRMOVE(p, p + 1); + } + } } -/* - * backslash_halve() plus save the result in allocated memory. - */ -char_u *backslash_halve_save(char_u *p) +/// backslash_halve() plus save the result in allocated memory. +/// +/// @param p +/// +/// @return String with the number of backslashes halved. +char_u* backslash_halve_save(char_u *p) { - char_u *res; - - res = vim_strsave(p); - if (res == NULL) + char_u *res = vim_strsave(p); + if (res == NULL) { return p; + } backslash_halve(res); return res; } - |