aboutsummaryrefslogtreecommitdiff
path: root/src/nvim/regexp.c
diff options
context:
space:
mode:
authorJosh Rahm <joshuarahm@gmail.com>2022-08-03 00:08:17 -0600
committerJosh Rahm <joshuarahm@gmail.com>2022-08-03 00:08:17 -0600
commit9449e1b8d273ff78eb894c588110ffa0c17d6ee3 (patch)
tree9e4470c33bd4187d9f42f0b2c4aaa995310c5be8 /src/nvim/regexp.c
parent308e1940dcd64aa6c344c403d4f9e0dda58d9c5c (diff)
parentb8dcbcc732baf84fc48d6b272c3ade0bcb129b3b (diff)
downloadrneovim-9449e1b8d273ff78eb894c588110ffa0c17d6ee3.tar.gz
rneovim-9449e1b8d273ff78eb894c588110ffa0c17d6ee3.tar.bz2
rneovim-9449e1b8d273ff78eb894c588110ffa0c17d6ee3.zip
Merge remote-tracking branch 'upstream/master' into rahm
Diffstat (limited to 'src/nvim/regexp.c')
-rw-r--r--src/nvim/regexp.c112
1 files changed, 52 insertions, 60 deletions
diff --git a/src/nvim/regexp.c b/src/nvim/regexp.c
index 4c49d30819..fbbf904f8b 100644
--- a/src/nvim/regexp.c
+++ b/src/nvim/regexp.c
@@ -176,12 +176,10 @@ static int backslash_trans(int c)
return c;
}
-/*
- * Check for a character class name "[:name:]". "pp" points to the '['.
- * Returns one of the CLASS_ items. CLASS_NONE means that no item was
- * recognized. Otherwise "pp" is advanced to after the item.
- */
-static int get_char_class(char_u **pp)
+/// Check for a character class name "[:name:]". "pp" points to the '['.
+/// Returns one of the CLASS_ items. CLASS_NONE means that no item was
+/// recognized. Otherwise "pp" is advanced to after the item.
+static int get_char_class(char **pp)
{
static const char *(class_names[]) =
{
@@ -306,7 +304,7 @@ static void init_class_tab(void)
// Global work variables for vim_regcomp().
-static char_u *regparse; ///< Input-scan pointer.
+static char *regparse; ///< Input-scan pointer.
static int regnpar; ///< () count.
static bool wants_nfa; ///< regex should use NFA engine
static int regnzpar; ///< \z() count.
@@ -395,11 +393,11 @@ int re_multiline(const regprog_T *prog)
* Returns a character representing the class. Zero means that no item was
* recognized. Otherwise "pp" is advanced to after the item.
*/
-static int get_equi_class(char_u **pp)
+static int get_equi_class(char **pp)
{
int c;
int l = 1;
- char_u *p = *pp;
+ char_u *p = (char_u *)(*pp);
if (p[1] == '=' && p[2] != NUL) {
l = utfc_ptr2len((char *)p + 2);
@@ -418,11 +416,11 @@ static int get_equi_class(char_u **pp)
* "pp" is advanced to after the item.
* Currently only single characters are recognized!
*/
-static int get_coll_element(char_u **pp)
+static int get_coll_element(char **pp)
{
int c;
int l = 1;
- char_u *p = *pp;
+ char_u *p = (char_u *)(*pp);
if (p[0] != NUL && p[1] == '.' && p[2] != NUL) {
l = utfc_ptr2len((char *)p + 2);
@@ -442,12 +440,10 @@ static void get_cpo_flags(void)
reg_cpo_lit = vim_strchr(p_cpo, CPO_LITERAL) != NULL;
}
-/*
- * Skip over a "[]" range.
- * "p" must point to the character after the '['.
- * The returned pointer is on the matching ']', or the terminating NUL.
- */
-static char_u *skip_anyof(char_u *p)
+/// Skip over a "[]" range.
+/// "p" must point to the character after the '['.
+/// The returned pointer is on the matching ']', or the terminating NUL.
+static char_u *skip_anyof(char *p)
{
int l;
@@ -458,7 +454,7 @@ static char_u *skip_anyof(char_u *p)
p++;
}
while (*p != NUL && *p != ']') {
- if ((l = utfc_ptr2len((char *)p)) > 1) {
+ if ((l = utfc_ptr2len(p)) > 1) {
p += l;
} else if (*p == '-') {
p++;
@@ -482,7 +478,7 @@ static char_u *skip_anyof(char_u *p)
}
}
- return p;
+ return (char_u *)p;
}
/*
@@ -512,7 +508,7 @@ char_u *skip_regexp(char_u *startp, int dirc, int magic, char_u **newp)
}
if ((p[0] == '[' && mymagic >= MAGIC_ON)
|| (p[0] == '\\' && p[1] == '[' && mymagic <= MAGIC_OFF)) {
- p = skip_anyof(p + 1);
+ p = skip_anyof((char *)p + 1);
if (p[0] == NUL) {
break;
}
@@ -547,7 +543,7 @@ static int prev_at_start; // True when on the second character
*/
static void initchr(char_u *str)
{
- regparse = str;
+ regparse = (char *)str;
prevchr_len = 0;
curchr = prevprevchr = prevchr = nextchr = -1;
at_start = true;
@@ -560,7 +556,7 @@ static void initchr(char_u *str)
*/
static void save_parse_state(parse_state_T *ps)
{
- ps->regparse = regparse;
+ ps->regparse = (char_u *)regparse;
ps->prevchr_len = prevchr_len;
ps->curchr = curchr;
ps->prevchr = prevchr;
@@ -576,7 +572,7 @@ static void save_parse_state(parse_state_T *ps)
*/
static void restore_parse_state(parse_state_T *ps)
{
- regparse = ps->regparse;
+ regparse = (char *)ps->regparse;
prevchr_len = ps->prevchr_len;
curchr = ps->curchr;
prevchr = ps->prevchr;
@@ -598,7 +594,7 @@ static int peekchr(void)
return curchr;
}
- switch (curchr = regparse[0]) {
+ switch (curchr = (uint8_t)regparse[0]) {
case '.':
case '[':
case '~':
@@ -669,7 +665,7 @@ static int peekchr(void)
// '$' is only magic as the very last char and if it's in front of
// either "\|", "\)", "\&", or "\n"
if (reg_magic >= MAGIC_OFF) {
- char_u *p = regparse + 1;
+ char_u *p = (char_u *)regparse + 1;
bool is_magic_all = (reg_magic == MAGIC_ALL);
// ignore \c \C \m \M \v \V and \Z after '$'
@@ -696,7 +692,7 @@ static int peekchr(void)
}
break;
case '\\': {
- int c = regparse[1];
+ int c = (uint8_t)regparse[1];
if (c == NUL) {
curchr = '\\'; // trailing '\'
@@ -725,13 +721,13 @@ static int peekchr(void)
} else {
// Next character can never be (made) magic?
// Then backslashing it won't do anything.
- curchr = utf_ptr2char((char *)regparse + 1);
+ curchr = utf_ptr2char(regparse + 1);
}
break;
}
default:
- curchr = utf_ptr2char((char *)regparse);
+ curchr = utf_ptr2char(regparse);
}
return curchr;
@@ -750,7 +746,7 @@ static void skipchr(void)
}
if (regparse[prevchr_len] != NUL) {
// Exclude composing chars that utfc_ptr2len does include.
- prevchr_len += utf_ptr2len((char *)regparse + prevchr_len);
+ prevchr_len += utf_ptr2len(regparse + prevchr_len);
}
regparse += prevchr_len;
prev_at_start = at_start;
@@ -820,8 +816,8 @@ static int64_t gethexchrs(int maxinputlen)
int c;
int i;
- for (i = 0; i < maxinputlen; ++i) {
- c = regparse[0];
+ for (i = 0; i < maxinputlen; i++) {
+ c = (uint8_t)regparse[0];
if (!ascii_isxdigit(c)) {
break;
}
@@ -846,8 +842,8 @@ static int64_t getdecchrs(void)
int c;
int i;
- for (i = 0;; ++i) {
- c = regparse[0];
+ for (i = 0;; i++) {
+ c = (uint8_t)regparse[0];
if (c < '0' || c > '9') {
break;
}
@@ -878,7 +874,7 @@ static int64_t getoctchrs(void)
int i;
for (i = 0; i < 3 && nr < 040; i++) { // -V536
- c = regparse[0];
+ c = (uint8_t)regparse[0];
if (c < '0' || c > '7') {
break;
}
@@ -910,7 +906,7 @@ static int read_limits(long *minval, long *maxval)
regparse++;
reverse = true;
}
- first_char = regparse;
+ first_char = (char_u *)regparse;
*minval = getdigits_long(&regparse, false, 0);
if (*regparse == ',') { // There is a comma.
if (ascii_isdigit(*++regparse)) {
@@ -1270,8 +1266,8 @@ static int match_with_backref(linenr_T start_lnum, colnr_T start_col, linenr_T e
if (reg_tofree == NULL || len >= (int)reg_tofreelen) {
len += 50; // get some extra
xfree(reg_tofree);
- reg_tofree = xmalloc(len);
- reg_tofreelen = len;
+ reg_tofree = xmalloc((size_t)len);
+ reg_tofreelen = (unsigned)len;
}
STRCPY(reg_tofree, rex.line);
rex.input = reg_tofree + (rex.input - rex.line);
@@ -1554,7 +1550,7 @@ char_u *regtilde(char_u *source, int magic, bool preview)
if (reg_prev_sub != NULL) {
// length = len(newsub) - 1 + len(prev_sub) + 1
prevlen = (int)STRLEN(reg_prev_sub);
- tmpsub = xmalloc(STRLEN(newsub) + prevlen);
+ tmpsub = xmalloc(STRLEN(newsub) + (size_t)prevlen);
// copy prefix
len = (int)(p - newsub); // not including ~
memmove(tmpsub, newsub, (size_t)len);
@@ -1587,12 +1583,10 @@ char_u *regtilde(char_u *source, int magic, bool preview)
// Only change reg_prev_sub when not previewing.
if (!preview) {
+ // Store a copy of newsub in reg_prev_sub. It is always allocated,
+ // because recursive calls may make the returned string invalid.
xfree(reg_prev_sub);
- if (newsub != source) { // newsub was allocated, just keep it
- reg_prev_sub = newsub;
- } else { // no ~ found, need to save newsub
- reg_prev_sub = vim_strsave(newsub);
- }
+ reg_prev_sub = vim_strsave(newsub);
}
return newsub;
@@ -1635,7 +1629,7 @@ static int fill_submatch_list(int argc FUNC_ATTR_UNUSED, typval_T *argv, int arg
if (s == NULL || rsm.sm_match->endp[i] == NULL) {
s = NULL;
} else {
- s = vim_strnsave(s, rsm.sm_match->endp[i] - s);
+ s = vim_strnsave(s, (size_t)(rsm.sm_match->endp[i] - s));
}
TV_LIST_ITEM_TV(li)->v_type = VAR_STRING;
TV_LIST_ITEM_TV(li)->vval.v_string = (char *)s;
@@ -1655,7 +1649,7 @@ static void clear_submatch_list(staticList10_T *sl)
/// vim_regexec_multi() match.
///
/// If "flags" has REGSUB_COPY really copy into "dest[destlen]".
-/// Oterwise nothing is copied, only compue the length of the result.
+/// Otherwise nothing is copied, only compute the length of the result.
///
/// If "flags" has REGSUB_MAGIC then behave like 'magic' is set.
///
@@ -1922,7 +1916,7 @@ static int vim_regsub_both(char_u *source, typval_T *expr, char_u *dest, int des
iemsg("vim_regsub_both(): not enough space");
return 0;
}
- *dst++ = c;
+ *dst++ = (char_u)c;
*dst++ = *src++;
*dst++ = *src++;
} else {
@@ -2197,7 +2191,7 @@ char_u *reg_submatch(int no)
if (round == 2) {
STRCPY(retval + len, s);
}
- len += STRLEN(s);
+ len += (ssize_t)STRLEN(s);
if (round == 2) {
retval[len] = '\n';
}
@@ -2215,7 +2209,7 @@ char_u *reg_submatch(int no)
}
if (retval == NULL) {
- retval = xmalloc(len);
+ retval = xmalloc((size_t)len);
}
}
} else {
@@ -2223,7 +2217,7 @@ char_u *reg_submatch(int no)
if (s == NULL || rsm.sm_match->endp[no] == NULL) {
retval = NULL;
} else {
- retval = vim_strnsave(s, rsm.sm_match->endp[no] - s);
+ retval = vim_strnsave(s, (size_t)(rsm.sm_match->endp[no] - s));
}
}
@@ -2328,7 +2322,7 @@ regprog_T *vim_regcomp(char *expr_arg, int re_flags)
regprog_T *prog = NULL;
char_u *expr = (char_u *)expr_arg;
- regexp_engine = p_re;
+ regexp_engine = (int)p_re;
// Check for prefix "\%#=", that sets the regexp engine
if (STRNCMP(expr, "\\%#=", 4) == 0) {
@@ -2396,8 +2390,8 @@ regprog_T *vim_regcomp(char *expr_arg, int re_flags)
if (prog != NULL) {
// Store the info needed to call regcomp() again when the engine turns out
// to be very slow when executing it.
- prog->re_engine = regexp_engine;
- prog->re_flags = re_flags;
+ prog->re_engine = (unsigned)regexp_engine;
+ prog->re_flags = (unsigned)re_flags;
}
return prog;
@@ -2475,8 +2469,8 @@ static bool vim_regexec_string(regmatch_T *rmp, char_u *line, colnr_T col, bool
// NFA engine aborted because it's very slow, use backtracking engine instead.
if (rmp->regprog->re_engine == AUTOMATIC_ENGINE
&& result == NFA_TOO_EXPENSIVE) {
- int save_p_re = p_re;
- int re_flags = rmp->regprog->re_flags;
+ int save_p_re = (int)p_re;
+ int re_flags = (int)rmp->regprog->re_flags;
char_u *pat = vim_strsave(((nfa_regprog_T *)rmp->regprog)->pattern);
p_re = BACKTRACKING_ENGINE;
@@ -2560,15 +2554,14 @@ long vim_regexec_multi(regmmatch_T *rmp, win_T *win, buf_T *buf, linenr_T lnum,
}
rex_in_use = true;
- int result = rmp->regprog->engine->regexec_multi(rmp, win, buf, lnum, col,
- tm, timed_out);
+ long result = rmp->regprog->engine->regexec_multi(rmp, win, buf, lnum, col, tm, timed_out);
rmp->regprog->re_in_use = false;
// NFA engine aborted because it's very slow, use backtracking engine instead.
if (rmp->regprog->re_engine == AUTOMATIC_ENGINE
&& result == NFA_TOO_EXPENSIVE) {
- int save_p_re = p_re;
- int re_flags = rmp->regprog->re_flags;
+ int save_p_re = (int)p_re;
+ int re_flags = (int)rmp->regprog->re_flags;
char_u *pat = vim_strsave(((nfa_regprog_T *)rmp->regprog)->pattern);
p_re = BACKTRACKING_ENGINE;
@@ -2589,8 +2582,7 @@ long vim_regexec_multi(regmmatch_T *rmp, win_T *win, buf_T *buf, linenr_T lnum,
vim_regfree(prev_prog);
rmp->regprog->re_in_use = true;
- result = rmp->regprog->engine->regexec_multi(rmp, win, buf, lnum, col,
- tm, timed_out);
+ result = rmp->regprog->engine->regexec_multi(rmp, win, buf, lnum, col, tm, timed_out);
rmp->regprog->re_in_use = false;
}