Remove long_u: regexp: Refactor long_u.

This commit is contained in:
Eliseo Martínez 2015-01-14 13:59:17 +01:00
parent 79b5a629eb
commit 2ceb1c74d5
4 changed files with 69 additions and 39 deletions

View File

@ -791,15 +791,17 @@ int linetabsize_col(int startcol, char_u *s)
/// @param len
///
/// @return Number of characters the string will take on the screen.
int win_linetabsize(win_T *wp, char_u *line, colnr_T len)
unsigned int win_linetabsize(win_T *wp, char_u *line, colnr_T len)
{
colnr_T col = 0;
char_u *s;
for (s = line; *s != NUL && (len == MAXCOL || s < line + len); mb_ptr_adv(s)) {
for (char_u *s = line;
*s != NUL && (len == MAXCOL || s < line + len);
mb_ptr_adv(s)) {
col += win_lbr_chartabsize(wp, line, s, col, NULL);
}
return (int)col;
return (unsigned int)col;
}
/// Return TRUE if 'c' is a normal identifier character:

View File

@ -10,6 +10,7 @@
* misc1.c: functions that didn't seem to fit elsewhere
*/
#include <assert.h>
#include <errno.h>
#include <inttypes.h>
#include <stdbool.h>
@ -1271,7 +1272,7 @@ plines_win_nofill (
int plines_win_nofold(win_T *wp, linenr_T lnum)
{
char_u *s;
long col;
unsigned int col;
int width;
s = ml_get_buf(wp->w_buffer, lnum, FALSE);
@ -1292,11 +1293,12 @@ int plines_win_nofold(win_T *wp, linenr_T lnum)
width = wp->w_width - win_col_off(wp);
if (width <= 0)
return 32000;
if (col <= width)
if (col <= (unsigned int)width)
return 1;
col -= width;
width += win_col_off2(wp);
return (col + (width - 1)) / width + 1;
assert(col <= INT_MAX && (int)col < INT_MAX - (width -1));
return ((int)col + (width - 1)) / width + 1;
}
/*

View File

@ -1693,7 +1693,7 @@ static char_u *regpiece(int *flagp)
if (lop == BEHIND || lop == NOBEHIND) {
if (nr < 0)
nr = 0; /* no limit is same as zero limit */
reginsert_nr(lop, nr, ret);
reginsert_nr(lop, (uint32_t)nr, ret);
} else
reginsert(lop, ret);
break;
@ -2122,14 +2122,14 @@ static char_u *regatom(int *flagp)
default:
if (VIM_ISDIGIT(c) || c == '<' || c == '>'
|| c == '\'') {
long_u n = 0;
uint32_t n = 0;
int cmp;
cmp = c;
if (cmp == '<' || cmp == '>')
c = getchr();
while (VIM_ISDIGIT(c)) {
n = n * 10 + (c - '0');
n = n * 10 + (uint32_t)(c - '0');
c = getchr();
}
if (c == '\'' && n == 0) {
@ -2155,7 +2155,7 @@ static char_u *regatom(int *flagp)
else {
/* put the number and the optional
* comparator after the opcode */
regcode = re_put_long(regcode, n);
regcode = re_put_uint32(regcode, n);
*regcode++ = cmp;
}
break;
@ -2580,7 +2580,8 @@ static void reginsert_nr(int op, long val, char_u *opnd)
*place++ = op;
*place++ = NUL;
*place++ = NUL;
re_put_long(place, (long_u)val);
assert(val >= 0 && (uintmax_t)val <= UINT32_MAX);
re_put_uint32(place, (uint32_t)val);
}
/*
@ -2609,15 +2610,17 @@ static void reginsert_limits(int op, long minval, long maxval, char_u *opnd)
*place++ = op;
*place++ = NUL;
*place++ = NUL;
place = re_put_long(place, (long_u)minval);
place = re_put_long(place, (long_u)maxval);
assert(minval >= 0 && (uintmax_t)minval <= UINT32_MAX);
place = re_put_uint32(place, (uint32_t)minval);
assert(maxval >= 0 && (uintmax_t)maxval <= UINT32_MAX);
place = re_put_uint32(place, (uint32_t)maxval);
regtail(opnd, place);
}
/*
* Write a long as four bytes at "p" and return pointer to the next char.
* Write a four bytes number at "p" and return pointer to the next char.
*/
static char_u *re_put_long(char_u *p, long_u val)
static char_u *re_put_uint32(char_u *p, uint32_t val)
{
*p++ = (char_u) ((val >> 24) & 0377);
*p++ = (char_u) ((val >> 16) & 0377);
@ -3643,7 +3646,6 @@ static int reg_match_visual(void)
int mode;
colnr_T start, end;
colnr_T start2, end2;
colnr_T cols;
/* Check if the buffer is the current buffer. */
if (reg_buf != curbuf || VIsual.lnum == 0)
@ -3686,7 +3688,10 @@ static int reg_match_visual(void)
end = end2;
if (top.col == MAXCOL || bot.col == MAXCOL)
end = MAXCOL;
cols = win_linetabsize(wp, regline, (colnr_T)(reginput - regline));
unsigned int cols_u = win_linetabsize(wp, regline,
(colnr_T)(reginput - regline));
assert(cols_u <= MAXCOL);
colnr_T cols = (colnr_T)cols_u;
if (cols < start || cols > end - (*p_sel == 'e'))
return FALSE;
}
@ -3862,20 +3867,25 @@ regmatch (
break;
case RE_LNUM:
if (!REG_MULTI || !re_num_cmp((long_u)(reglnum + reg_firstlnum),
assert(reglnum + reg_firstlnum >= 0
&& (uintmax_t)(reglnum + reg_firstlnum) <= UINT32_MAX);
if (!REG_MULTI || !re_num_cmp((uint32_t)(reglnum + reg_firstlnum),
scan))
status = RA_NOMATCH;
break;
case RE_COL:
if (!re_num_cmp((long_u)(reginput - regline) + 1, scan))
assert(reginput - regline + 1 >= 0
&& (uintmax_t)(reginput - regline + 1) <= UINT32_MAX);
if (!re_num_cmp((uint32_t)(reginput - regline + 1), scan))
status = RA_NOMATCH;
break;
case RE_VCOL:
if (!re_num_cmp((long_u)win_linetabsize(
reg_win == NULL ? curwin : reg_win,
regline, (colnr_T)(reginput - regline)) + 1, scan))
if (!re_num_cmp(win_linetabsize(reg_win == NULL ? curwin : reg_win,
regline,
(colnr_T)(reginput - regline)) + 1,
scan))
status = RA_NOMATCH;
break;
@ -5599,9 +5609,9 @@ static void save_se_one(save_se_T *savep, char_u **pp)
/*
* Compare a number with the operand of RE_LNUM, RE_COL or RE_VCOL.
*/
static int re_num_cmp(long_u val, char_u *scan)
static int re_num_cmp(uint32_t val, char_u *scan)
{
long_u n = OPERAND_MIN(scan);
uint32_t n = (uint32_t)OPERAND_MIN(scan);
if (OPERAND_CMP(scan) == '>')
return val > n;

View File

@ -4,8 +4,10 @@
* This file is included in "regexp.c".
*/
#include <assert.h>
#include <inttypes.h>
#include <stdbool.h>
#include <stdint.h>
#include "nvim/ascii.h"
#include "nvim/misc2.h"
@ -4403,7 +4405,7 @@ static void nfa_restore_listids(nfa_regprog_T *prog, int *list)
}
}
static int nfa_re_num_cmp(long_u val, int op, long_u pos)
static bool nfa_re_num_cmp(uintmax_t val, int op, uintmax_t pos)
{
if (op == 1) return pos > val;
if (op == 2) return pos < val;
@ -5684,9 +5686,14 @@ static int nfa_regmatch(nfa_regprog_T *prog, nfa_state_T *start, regsubs_T *subm
case NFA_LNUM:
case NFA_LNUM_GT:
case NFA_LNUM_LT:
result = (REG_MULTI &&
nfa_re_num_cmp(t->state->val, t->state->c - NFA_LNUM,
(long_u)(reglnum + reg_firstlnum)));
assert(t->state->val >= 0
&& !((reg_firstlnum > 0 && reglnum > LONG_MAX - reg_firstlnum)
|| (reg_firstlnum <0 && reglnum < LONG_MIN + reg_firstlnum))
&& reglnum + reg_firstlnum >= 0);
result = (REG_MULTI
&& nfa_re_num_cmp((uintmax_t)t->state->val,
t->state->c - NFA_LNUM,
(uintmax_t)(reglnum + reg_firstlnum)));
if (result) {
add_here = TRUE;
add_state = t->state->out;
@ -5696,8 +5703,12 @@ static int nfa_regmatch(nfa_regprog_T *prog, nfa_state_T *start, regsubs_T *subm
case NFA_COL:
case NFA_COL_GT:
case NFA_COL_LT:
result = nfa_re_num_cmp(t->state->val, t->state->c - NFA_COL,
(long_u)(reginput - regline) + 1);
assert(t->state->val >= 0
&& reginput >= regline
&& (uintmax_t)(reginput - regline) <= UINTMAX_MAX - 1);
result = nfa_re_num_cmp((uintmax_t)t->state->val,
t->state->c - NFA_COL,
(uintmax_t)(reginput - regline + 1));
if (result) {
add_here = TRUE;
add_state = t->state->out;
@ -5707,14 +5718,19 @@ static int nfa_regmatch(nfa_regprog_T *prog, nfa_state_T *start, regsubs_T *subm
case NFA_VCOL:
case NFA_VCOL_GT:
case NFA_VCOL_LT:
result = nfa_re_num_cmp(t->state->val, t->state->c - NFA_VCOL,
(long_u)win_linetabsize(
reg_win == NULL ? curwin : reg_win,
regline, (colnr_T)(reginput - regline)) + 1);
{
uintmax_t lts = win_linetabsize(reg_win == NULL ? curwin : reg_win,
regline,
(colnr_T)(reginput - regline));
assert(t->state->val >= 0);
result = nfa_re_num_cmp((uintmax_t)t->state->val,
t->state->c - NFA_VCOL,
lts + 1);
if (result) {
add_here = TRUE;
add_state = t->state->out;
}
}
break;
case NFA_MARK: