vim-patch:8.1.0805: too many #ifdefs

Problem:    Too many #ifdefs.
Solution:   Graduate FEAT_MBYTE, part 1.
135059724f
This commit is contained in:
Jan Edmund Lazo
2020-11-12 18:31:59 -05:00
parent 93c18867a0
commit 5d6ecfa3c7
9 changed files with 221 additions and 343 deletions

View File

@@ -783,10 +783,10 @@ static void byteidx(typval_T *argvars, typval_T *rettv, int comp)
if (*t == NUL) { // EOL reached.
return;
}
if (enc_utf8 && comp) {
if (comp) {
t += utf_ptr2len((const char_u *)t);
} else {
t += (*mb_ptr2len)((const char_u *)t);
t += utfc_ptr2len((const char_u *)t);
}
}
rettv->vval.v_number = (varnumber_T)(t - str);
@@ -1427,9 +1427,7 @@ static void f_cursor(typval_T *argvars, typval_T *rettv, FunPtr fptr)
// Make sure the cursor is in a valid position.
check_cursor();
// Correct cursor for multi-byte character.
if (has_mbyte) {
mb_adjust_cursor();
}
mb_adjust_cursor();
curwin->w_set_curswant = set_curswant;
rettv->vval.v_number = 0;
@@ -4198,7 +4196,7 @@ static void f_has(typval_T *argvars, typval_T *rettv, FunPtr fptr)
} else if (STRICMP(name, "ttyout") == 0) {
n = stdout_isatty;
} else if (STRICMP(name, "multi_byte_encoding") == 0) {
n = has_mbyte != 0;
n = true;
} else if (STRICMP(name, "syntax_items") == 0) {
n = syntax_present(curwin);
#ifdef UNIX
@@ -8025,14 +8023,9 @@ static void f_setcharsearch(typval_T *argvars, typval_T *rettv, FunPtr fptr)
if ((d = argvars[0].vval.v_dict) != NULL) {
char_u *const csearch = (char_u *)tv_dict_get_string(d, "char", false);
if (csearch != NULL) {
if (enc_utf8) {
int pcc[MAX_MCO];
int c = utfc_ptr2char(csearch, pcc);
set_last_csearch(c, csearch, utfc_ptr2len(csearch));
}
else
set_last_csearch(PTR2CHAR(csearch),
csearch, utfc_ptr2len(csearch));
int pcc[MAX_MCO];
const int c = utfc_ptr2char(csearch, pcc);
set_last_csearch(c, csearch, utfc_ptr2len(csearch));
}
di = tv_dict_find(d, S_LEN("forward"));
@@ -10711,72 +10704,54 @@ static void f_tr(typval_T *argvars, typval_T *rettv, FunPtr fptr)
garray_T ga;
ga_init(&ga, (int)sizeof(char), 80);
if (!has_mbyte) {
// Not multi-byte: fromstr and tostr must be the same length.
if (strlen(fromstr) != strlen(tostr)) {
goto error;
}
}
// fromstr and tostr have to contain the same number of chars.
bool first = true;
while (*in_str != NUL) {
if (has_mbyte) {
const char *cpstr = in_str;
const int inlen = (*mb_ptr2len)((const char_u *)in_str);
int cplen = inlen;
int idx = 0;
int fromlen;
for (const char *p = fromstr; *p != NUL; p += fromlen) {
fromlen = (*mb_ptr2len)((const char_u *)p);
if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0) {
int tolen;
for (p = tostr; *p != NUL; p += tolen) {
tolen = (*mb_ptr2len)((const char_u *)p);
if (idx-- == 0) {
cplen = tolen;
cpstr = (char *)p;
break;
}
}
if (*p == NUL) { // tostr is shorter than fromstr.
goto error;
}
break;
}
idx++;
}
if (first && cpstr == in_str) {
// Check that fromstr and tostr have the same number of
// (multi-byte) characters. Done only once when a character
// of in_str doesn't appear in fromstr.
first = false;
const char *cpstr = in_str;
const int inlen = utfc_ptr2len((const char_u *)in_str);
int cplen = inlen;
int idx = 0;
int fromlen;
for (const char *p = fromstr; *p != NUL; p += fromlen) {
fromlen = utfc_ptr2len((const char_u *)p);
if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0) {
int tolen;
for (const char *p = tostr; *p != NUL; p += tolen) {
tolen = (*mb_ptr2len)((const char_u *)p);
idx--;
for (p = tostr; *p != NUL; p += tolen) {
tolen = utfc_ptr2len((const char_u *)p);
if (idx-- == 0) {
cplen = tolen;
cpstr = (char *)p;
break;
}
}
if (idx != 0) {
if (*p == NUL) { // tostr is shorter than fromstr.
goto error;
}
break;
}
ga_grow(&ga, cplen);
memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
ga.ga_len += cplen;
in_str += inlen;
} else {
// When not using multi-byte chars we can do it faster.
const char *const p = strchr(fromstr, *in_str);
if (p != NULL) {
ga_append(&ga, tostr[p - fromstr]);
} else {
ga_append(&ga, *in_str);
}
in_str++;
idx++;
}
if (first && cpstr == in_str) {
// Check that fromstr and tostr have the same number of
// (multi-byte) characters. Done only once when a character
// of in_str doesn't appear in fromstr.
first = false;
int tolen;
for (const char *p = tostr; *p != NUL; p += tolen) {
tolen = utfc_ptr2len((const char_u *)p);
idx--;
}
if (idx != 0) {
goto error;
}
}
ga_grow(&ga, cplen);
memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
ga.ga_len += cplen;
in_str += inlen;
}
// add a terminating NUL