patch 9.0.1738: Duplicate code to reverse a string

Commit: 
https://github.com/vim/vim/commit/4dd266cb66d901cf5324f09405cfea3f004bd29f
Author: zeertzjq <[email protected]>
Date:   Sat Aug 19 11:35:03 2023 +0200

    patch 9.0.1738: Duplicate code to reverse a string
    
    Problem:  Duplicate code to reverse a string
    Solution: Move reverse_text() to strings.c and remove string_reverse().
    
    closes: #12847
    
    Signed-off-by: Christian Brabandt <[email protected]>
    Co-authored-by: zeertzjq <[email protected]>

diff --git a/src/list.c b/src/list.c
index 28799d5fd..6210fd638 100644
--- a/src/list.c
+++ b/src/list.c
@@ -3001,7 +3001,13 @@ f_reverse(typval_T *argvars, typval_T *rettv)
     if (argvars[0].v_type == VAR_BLOB)
        blob_reverse(argvars[0].vval.v_blob, rettv);
     else if (argvars[0].v_type == VAR_STRING)
-       string_reverse(argvars[0].vval.v_string, rettv);
+    {
+       rettv->v_type = VAR_STRING;
+       if (argvars[0].vval.v_string != NULL)
+           rettv->vval.v_string = reverse_text(argvars[0].vval.v_string);
+       else
+           rettv->vval.v_string = NULL;
+    }
     else if (argvars[0].v_type == VAR_LIST)
        list_reverse(argvars[0].vval.v_list, rettv);
 }
diff --git a/src/proto/search.pro b/src/proto/search.pro
index 7a29ff3a0..99e279dad 100644
--- a/src/proto/search.pro
+++ b/src/proto/search.pro
@@ -1,7 +1,6 @@
 /* search.c */
 int search_regcomp(char_u *pat, char_u **used_pat, int pat_save, int pat_use, 
int options, regmmatch_T *regmatch);
 char_u *get_search_pat(void);
-char_u *reverse_text(char_u *s);
 void save_re_pat(int idx, char_u *pat, int magic);
 void save_search_patterns(void);
 void restore_search_patterns(void);
diff --git a/src/proto/strings.pro b/src/proto/strings.pro
index 8924a2571..aa8b374c3 100644
--- a/src/proto/strings.pro
+++ b/src/proto/strings.pro
@@ -21,9 +21,9 @@ char_u *vim_strrchr(char_u *string, int c);
 void sort_strings(char_u **files, int count);
 int has_non_ascii(char_u *s);
 char_u *concat_str(char_u *str1, char_u *str2);
+char_u *reverse_text(char_u *s);
 char_u *string_quote(char_u *str, int function);
 long string_count(char_u *haystack, char_u *needle, int ic);
-void string_reverse(char_u *str, typval_T *rettv);
 void string_filter_map(char_u *str, filtermap_T filtermap, typval_T *expr, 
typval_T *rettv);
 void string_reduce(typval_T *argvars, typval_T *expr, typval_T *rettv);
 void f_byteidx(typval_T *argvars, typval_T *rettv);
diff --git a/src/search.c b/src/search.c
index de1759525..d4baa9192 100644
--- a/src/search.c
+++ b/src/search.c
@@ -203,47 +203,6 @@ get_search_pat(void)
     return mr_pattern;
 }
 
-#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
-/*
- * Reverse text into allocated memory.
- * Returns the allocated string, NULL when out of memory.
- */
-    char_u *
-reverse_text(char_u *s)
-{
-    unsigned   len;
-    unsigned   s_i, rev_i;
-    char_u     *rev;
-
-    /*
-     * Reverse the pattern.
-     */
-    len = (unsigned)STRLEN(s);
-    rev = alloc(len + 1);
-    if (rev == NULL)
-       return NULL;
-
-    rev_i = len;
-    for (s_i = 0; s_i < len; ++s_i)
-    {
-       if (has_mbyte)
-       {
-           int mb_len;
-
-           mb_len = (*mb_ptr2len)(s + s_i);
-           rev_i -= mb_len;
-           mch_memmove(rev + rev_i, s + s_i, mb_len);
-           s_i += mb_len - 1;
-       }
-       else
-           rev[--rev_i] = s[s_i];
-
-    }
-    rev[len] = NUL;
-    return rev;
-}
-#endif
-
     void
 save_re_pat(int idx, char_u *pat, int magic)
 {
diff --git a/src/strings.c b/src/strings.c
index 25c32ecaf..993674406 100644
--- a/src/strings.c
+++ b/src/strings.c
@@ -770,6 +770,36 @@ concat_str(char_u *str1, char_u *str2)
     return dest;
 }
 
+#if defined(FEAT_EVAL) || defined(FEAT_RIGHTLEFT) || defined(PROTO)
+/*
+ * Reverse text into allocated memory.
+ * Returns the allocated string, NULL when out of memory.
+ */
+    char_u *
+reverse_text(char_u *s)
+{
+    size_t len = STRLEN(s);
+    char_u *rev = alloc(len + 1);
+    if (rev == NULL)
+       return NULL;
+
+    for (size_t s_i = 0, rev_i = len; s_i < len; ++s_i)
+    {
+       if (has_mbyte)
+       {
+           int mb_len = (*mb_ptr2len)(s + s_i);
+           rev_i -= mb_len;
+           mch_memmove(rev + rev_i, s + s_i, mb_len);
+           s_i += mb_len - 1;
+       }
+       else
+           rev[--rev_i] = s[s_i];
+    }
+    rev[len] = NUL;
+    return rev;
+}
+#endif
+
 #if defined(FEAT_EVAL) || defined(PROTO)
 /*
  * Return string "str" in ' quotes, doubling ' characters.
@@ -854,47 +884,6 @@ string_count(char_u *haystack, char_u *needle, int ic)
     return n;
 }
 
-/*
- * Reverse the string in 'str' and set the result in 'rettv'.
- */
-    void
-string_reverse(char_u *str, typval_T *rettv)
-{
-    rettv->v_type = VAR_STRING;
-    rettv->vval.v_string = NULL;
-    if (str == NULL)
-       return;
-
-    char_u     *rstr = vim_strsave(str);
-    rettv->vval.v_string = rstr;
-    if (rstr == NULL || *str == NUL)
-       return;
-
-    size_t     len = STRLEN(rstr);
-    if (has_mbyte)
-    {
-       char_u *src = str;
-       char_u *dest = rstr + len;
-
-       while (src < str + len)
-       {
-           int clen = mb_ptr2len(src);
-           dest -= clen;
-           mch_memmove(dest, src, (size_t)clen);
-           src += clen;
-       }
-    }
-    else
-    {
-       for (size_t i = 0; i < len / 2; i++)
-       {
-           char tmp = rstr[len - i - 1];
-           rstr[len - i - 1] = rstr[i];
-           rstr[i] = tmp;
-       }
-    }
-}
-
 /*
  * Make a typval_T of the first character of "input" and store it in "output".
  * Return OK or FAIL.
diff --git a/src/version.c b/src/version.c
index 325a46efe..f6022d2cf 100644
--- a/src/version.c
+++ b/src/version.c
@@ -695,6 +695,8 @@ static char *(features[]) =
 
 static int included_patches[] =
 {   /* Add new patch number below this line */
+/**/
+    1738,
 /**/
     1737,
 /**/

-- 
-- 
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 [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/vim_dev/E1qXIW0-008q1D-QT%40256bit.org.

Raspunde prin e-mail lui