patch 9.1.0461: too many strlen() calls in drawline.c

Commit: 
https://github.com/vim/vim/commit/f51ff96532ab8f97f779b44d17dccdda9d8ce566
Author: John Marriott <basil...@internode.on.net>
Date:   Sun Jun 2 19:44:11 2024 +0200

    patch 9.1.0461: too many strlen() calls in drawline.c
    
    Problem:  too many strlen() calls in drawline.c
    Solution: Refactor code to avoid strlen()
              (John Marriott)
    
    closes: #14890
    
    Signed-off-by: John Marriott <basil...@internode.on.net>
    Signed-off-by: Christian Brabandt <c...@256bit.org>

diff --git a/src/drawline.c b/src/drawline.c
index 9502fc53a..e388bdbee 100644
--- a/src/drawline.c
+++ b/src/drawline.c
@@ -296,10 +296,9 @@ get_sign_display_info(
            if (nrcol)
            {
                wlv->c_extra = NUL;
-               sprintf((char *)wlv->extra, "%-*c ",
-                                                 number_width(wp), SIGN_BYTE);
+               wlv->n_extra = vim_snprintf((char *)wlv->extra, 
sizeof(wlv->extra),
+                                               "%-*c ", number_width(wp), 
SIGN_BYTE);
                wlv->p_extra = wlv->extra;
-               wlv->n_extra = (int)STRLEN(wlv->p_extra);
            }
            else
                wlv->c_extra = SIGN_BYTE;
@@ -310,10 +309,9 @@ get_sign_display_info(
                if (nrcol)
                {
                    wlv->c_extra = NUL;
-                   sprintf((char *)wlv->extra, "%-*c ", number_width(wp),
-                                                       MULTISIGN_BYTE);
+                   wlv->n_extra = vim_snprintf((char *)wlv->extra, 
sizeof(wlv->extra),
+                                               "%-*c ", number_width(wp), 
MULTISIGN_BYTE);
                    wlv->p_extra = wlv->extra;
-                   wlv->n_extra = (int)STRLEN(wlv->p_extra);
                }
                else
                    wlv->c_extra = MULTISIGN_BYTE;
@@ -332,17 +330,18 @@ get_sign_display_info(
                    if (nrcol)
                    {
                        int width = number_width(wp) - 2;
-                       int n;
 
-                       for (n = 0; n < width; n++)
-                           wlv->extra[n] = ' ';
-                       vim_snprintf((char *)wlv->extra + n,
-                                 sizeof(wlv->extra) - n, "%s ", wlv->p_extra);
+                       vim_memset(wlv->extra, ' ', width);
+                       wlv->n_extra = width;
+                       wlv->n_extra += vim_snprintf((char *)wlv->extra + width,
+                                 sizeof(wlv->extra) - width, "%s ", 
wlv->p_extra);
                        wlv->p_extra = wlv->extra;
                    }
+                   else
+                       wlv->n_extra = (int)STRLEN(wlv->p_extra);
+
                    wlv->c_extra = NUL;
                    wlv->c_final = NUL;
-                   wlv->n_extra = (int)STRLEN(wlv->p_extra);
                }
 
                if (use_cursor_line_highlight(wp, wlv->lnum)
@@ -417,7 +416,7 @@ handle_lnum_col(
                  }
              }
 
-             sprintf((char *)wlv->extra, fmt, number_width(wp), num);
+             vim_snprintf((char *)wlv->extra, sizeof(wlv->extra), fmt, 
number_width(wp), num);
              if (wp->w_skipcol > 0 && wlv->startrow == 0)
                  for (wlv->p_extra = wlv->extra; *wlv->p_extra == ' ';
                          ++wlv->p_extra)
@@ -621,25 +620,26 @@ textprop_size_after_trunc(
 {
     int        space = (flags & (TP_FLAG_ALIGN_BELOW | TP_FLAG_ALIGN_ABOVE))
                                       ? wp->w_width - win_col_off(wp) : added;
-    int len = (int)STRLEN(text);
     int strsize = 0;
-    int n_used;
+    char_u *p;
 
-    // if the remaining size is to small and 'wrap' is set we wrap anyway and
+    // if the remaining size is too small and 'wrap' is set we wrap anyway and
     // use the next line
     if (space < PROP_TEXT_MIN_CELLS && wp->w_p_wrap)
        space += wp->w_width;
     if (flags & (TP_FLAG_ALIGN_BELOW | TP_FLAG_ALIGN_ABOVE))
        space -= padding;
-    for (n_used = 0; n_used < len; n_used += (*mb_ptr2len)(text + n_used))
+
+    for (p = text; *p != NUL; p += (*mb_ptr2len)(p))
     {
-       int clen = ptr2cells(text + n_used);
+       int clen = ptr2cells(p);
 
        if (strsize + clen > space)
            break;
        strsize += clen;
     }
-    *n_used_ptr = n_used;
+    *n_used_ptr = (int)(p - text);
+
     return strsize;
 }
 
@@ -1807,7 +1807,7 @@ win_line(
            line = ml_get_buf(wp->w_buffer, lnum, FALSE);
            ptr = line + linecol;
 
-           if (len == 0 || (int)wp->w_cursor.col > ptr - line)
+           if (len == 0 || (int)wp->w_cursor.col > linecol)
            {
                // no bad word found at line start, don't check until end of a
                // word
@@ -2821,15 +2821,16 @@ win_line(
                                // head byte at end of line
                                mb_l = 1;
                                transchar_nonprint(wp->w_buffer, wlv.extra, c);
+                               wlv.n_extra = (int)STRLEN(wlv.extra) - 1;
                            }
                            else
                            {
                                // illegal tail byte
                                mb_l = 2;
                                STRCPY(wlv.extra, "XX");
+                               wlv.n_extra = 1;
                            }
                            wlv.p_extra = wlv.extra;
-                           wlv.n_extra = (int)STRLEN(wlv.extra) - 1;
                            wlv.c_extra = NUL;
                            wlv.c_final = NUL;
                            c = *wlv.p_extra++;
@@ -3388,11 +3389,16 @@ win_line(
 
                        c = *wlv.p_extra;
                        p = alloc(wlv.n_extra + 1);
-                       vim_memset(p, ' ', wlv.n_extra);
-                       STRNCPY(p, wlv.p_extra + 1, STRLEN(wlv.p_extra) - 1);
-                       p[wlv.n_extra] = NUL;
-                       vim_free(wlv.p_extra_free);
-                       wlv.p_extra_free = wlv.p_extra = p;
+                       if (p == NULL)
+                           wlv.n_extra = 0;
+                       else
+                       {
+                           vim_memset(p, ' ', wlv.n_extra);
+                           STRNCPY(p, wlv.p_extra + 1, STRLEN(wlv.p_extra) - 
1);
+                           p[wlv.n_extra] = NUL;
+                           vim_free(wlv.p_extra_free);
+                           wlv.p_extra_free = wlv.p_extra = p;
+                       }
                    }
                    else
 #endif
@@ -4251,7 +4257,7 @@ win_line(
            if (!wp->w_p_wrap && text_prop_follows && !text_prop_above)
            {
                // do not output more of the line, only the "below" prop
-               ptr += STRLEN(ptr);
+               ptr = line + (size_t)ml_get_buf_len(wp->w_buffer, lnum);
 # ifdef FEAT_LINEBREAK
                wlv.dont_use_showbreak = TRUE;
 # endif
diff --git a/src/version.c b/src/version.c
index f8951af06..13010a386 100644
--- a/src/version.c
+++ b/src/version.c
@@ -704,6 +704,8 @@ static char *(features[]) =
 
 static int included_patches[] =
 {   /* Add new patch number below this line */
+/**/
+    461,
 /**/
     460,
 /**/

-- 
-- 
You received this message from the "vim_dev" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

--- 
You received this message because you are subscribed to the Google Groups 
"vim_dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to vim_dev+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/vim_dev/E1sDpV0-005ut8-99%40256bit.org.

Raspunde prin e-mail lui