mirror of
https://github.com/neovim/neovim.git
synced 2025-02-25 18:55:25 -06:00
Merge pull request #27987 from zeertzjq/vim-8.2.2318
vim-patch:8.2.{2318,2605}
This commit is contained in:
commit
a629888427
@ -7519,29 +7519,44 @@ int check_luafunc_name(const char *const str, const bool paren)
|
|||||||
return (int)(p - str);
|
return (int)(p - str);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Return the character "str[index]" where "index" is the character index. If
|
/// Return the character "str[index]" where "index" is the character index,
|
||||||
/// "index" is out of range NULL is returned.
|
/// including composing characters.
|
||||||
|
/// If "index" is out of range NULL is returned.
|
||||||
char *char_from_string(const char *str, varnumber_T index)
|
char *char_from_string(const char *str, varnumber_T index)
|
||||||
{
|
{
|
||||||
size_t nbyte = 0;
|
|
||||||
varnumber_T nchar = index;
|
varnumber_T nchar = index;
|
||||||
|
|
||||||
if (str == NULL || index < 0) {
|
if (str == NULL) {
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
size_t slen = strlen(str);
|
size_t slen = strlen(str);
|
||||||
while (nchar > 0 && nbyte < slen) {
|
|
||||||
nbyte += (size_t)utf_ptr2len(str + nbyte);
|
// do the same as for a list: a negative index counts from the end
|
||||||
nchar--;
|
if (index < 0) {
|
||||||
|
int clen = 0;
|
||||||
|
|
||||||
|
for (size_t nbyte = 0; nbyte < slen; clen++) {
|
||||||
|
nbyte += (size_t)utfc_ptr2len(str + nbyte);
|
||||||
|
}
|
||||||
|
nchar = clen + index;
|
||||||
|
if (nchar < 0) {
|
||||||
|
// unlike list: index out of range results in empty string
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t nbyte = 0;
|
||||||
|
for (; nchar > 0 && nbyte < slen; nchar--) {
|
||||||
|
nbyte += (size_t)utfc_ptr2len(str + nbyte);
|
||||||
}
|
}
|
||||||
if (nbyte >= slen) {
|
if (nbyte >= slen) {
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
return xmemdupz(str + nbyte, (size_t)utf_ptr2len(str + nbyte));
|
return xmemdupz(str + nbyte, (size_t)utfc_ptr2len(str + nbyte));
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Get the byte index for character index "idx" in string "str" with length
|
/// Get the byte index for character index "idx" in string "str" with length
|
||||||
/// "str_len".
|
/// "str_len". Composing characters are included.
|
||||||
/// If going over the end return "str_len".
|
/// If going over the end return "str_len".
|
||||||
/// If "idx" is negative count from the end, -1 is the last character.
|
/// If "idx" is negative count from the end, -1 is the last character.
|
||||||
/// When going over the start return -1.
|
/// When going over the start return -1.
|
||||||
@ -7552,7 +7567,7 @@ static ssize_t char_idx2byte(const char *str, size_t str_len, varnumber_T idx)
|
|||||||
|
|
||||||
if (nchar >= 0) {
|
if (nchar >= 0) {
|
||||||
while (nchar > 0 && nbyte < str_len) {
|
while (nchar > 0 && nbyte < str_len) {
|
||||||
nbyte += (size_t)utf_ptr2len(str + nbyte);
|
nbyte += (size_t)utfc_ptr2len(str + nbyte);
|
||||||
nchar--;
|
nchar--;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
@ -7569,7 +7584,8 @@ static ssize_t char_idx2byte(const char *str, size_t str_len, varnumber_T idx)
|
|||||||
return (ssize_t)nbyte;
|
return (ssize_t)nbyte;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Return the slice "str[first:last]" using character indexes.
|
/// Return the slice "str[first : last]" using character indexes. Composing
|
||||||
|
/// characters are included.
|
||||||
///
|
///
|
||||||
/// @param exclusive true for slice().
|
/// @param exclusive true for slice().
|
||||||
///
|
///
|
||||||
@ -7591,7 +7607,7 @@ char *string_slice(const char *str, varnumber_T first, varnumber_T last, bool ex
|
|||||||
end_byte = char_idx2byte(str, slen, last);
|
end_byte = char_idx2byte(str, slen, last);
|
||||||
if (!exclusive && end_byte >= 0 && end_byte < (ssize_t)slen) {
|
if (!exclusive && end_byte >= 0 && end_byte < (ssize_t)slen) {
|
||||||
// end index is inclusive
|
// end index is inclusive
|
||||||
end_byte += utf_ptr2len(str + end_byte);
|
end_byte += utfc_ptr2len(str + end_byte);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -859,7 +859,7 @@ int tv_list_slice_or_index(list_T *list, bool range, varnumber_T n1_arg, varnumb
|
|||||||
// A list index out of range is an error.
|
// A list index out of range is an error.
|
||||||
if (!range) {
|
if (!range) {
|
||||||
if (verbose) {
|
if (verbose) {
|
||||||
semsg(_(e_list_index_out_of_range_nr), (int64_t)n1);
|
semsg(_(e_list_index_out_of_range_nr), (int64_t)n1_arg);
|
||||||
}
|
}
|
||||||
return FAIL;
|
return FAIL;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user