Repository: neovim/neovim
File: src/nvim/eval/funcs.c (C)

Code before:
  if (tv_check_for_string_or_list_arg(argvars, 0) == FAIL
      || tv_check_for_opt_number_arg(argvars, 1) == FAIL) {
    return;
  }

  win_T *wp = curwin;

  if (argvars[1].v_type != VAR_UNKNOWN) {
    // use the window specified in the second argument
    tabpage_T *tp;
    wp = win_id2wp_tp((int)tv_get_number(&argvars[1]), &tp);
    if (wp == NULL || tp == NULL) {
      return;
    }
    check_cursor(wp);
  }

  buf_T *bp = wp->w_buffer;
  colnr_T col = 0;
  int fnum = bp->b_fnum;
  pos_T *fp = var2fpos(&argvars[0], false, &fnum, charcol, wp);
  if (fp != NULL && fnum == bp->b_fnum) {
    if (fp->col == MAXCOL) {
      // '> can be MAXCOL, get the length of the line then
      if (fp->lnum <= bp->b_ml.ml_line_count) {
        col = ml_get_len(fp->lnum) + 1;
      } else {
        col = MAXCOL;
      }
    } else {
      col = fp->col + 1;
      // col(".") when the cursor is on the NUL at the end of the line
      // because of "coladd" can be seen as an extra column.
      if (virtual_active(wp) && fp == &wp->w_cursor) {
        char *p = get_cursor_pos_ptr();
        if (wp->w_cursor.coladd >=
            (colnr_T)win_chartabsize(wp, p,
                                     wp->w_virtcol - wp->w_cursor.coladd)) {
          int l;
          if (*p != NUL && p[(l = utfc_ptr2len(p))] == NUL) {
            col += l;
          }
        }
      }
    }
  }
  rettv->vval.v_number = col;
}

/// "charcol()" function
static void f_charcol(typval_T *argvars, typval_T *rettv, EvalFuncData fptr)

Review comment (suggestion):
```suggestion
        col = ml_get_buf_len(bp, fp->lnum) + 1;
```

Code after:
  if (tv_check_for_string_or_list_arg(argvars, 0) == FAIL
      || tv_check_for_opt_number_arg(argvars, 1) == FAIL) {
    return;
  }

  win_T *wp = curwin;

  if (argvars[1].v_type != VAR_UNKNOWN) {
    // use the window specified in the second argument
    tabpage_T *tp;
    wp = win_id2wp_tp((int)tv_get_number(&argvars[1]), &tp);
    if (wp == NULL || tp == NULL) {
      return;
    }
    check_cursor(wp);
  }

  buf_T *bp = wp->w_buffer;
  colnr_T col = 0;
  int fnum = bp->b_fnum;
  pos_T *fp = var2fpos(&argvars[0], false, &fnum, charcol, wp);
  if (fp != NULL && fnum == bp->b_fnum) {
    if (fp->col == MAXCOL) {
      // '> can be MAXCOL, get the length of the line then
      if (fp->lnum <= bp->b_ml.ml_line_count) {
        col = ml_get_buf_len(bp, fp->lnum) + 1;
      } else {
        col = MAXCOL;
      }
    } else {
      col = fp->col + 1;
      // col(".") when the cursor is on the NUL at the end of the line
      // because of "coladd" can be seen as an extra column.
      if (virtual_active(wp) && fp == &wp->w_cursor) {
        char *p = ml_get_buf(bp, wp->w_cursor.lnum) + wp->w_cursor.col;
        if (wp->w_cursor.coladd >=
            (colnr_T)win_chartabsize(wp, p,
                                     wp->w_virtcol - wp->w_cursor.coladd)) {
          int l;
          if (*p != NUL && p[(l = utfc_ptr2len(p))] == NUL) {
            col += l;
          }
        }
      }
    }
  }
  rettv->vval.v_number = col;
}

/// "charcol()" function
static void f_charcol(typval_T *argvars, typval_T *rettv, EvalFuncData fptr)