nano-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Nano-devel] [PATCH] chars: use a fixed piece of memory instead allocati


From: Benno Schulenberg
Subject: [Nano-devel] [PATCH] chars: use a fixed piece of memory instead allocating+freeing all the time
Date: Fri, 16 Dec 2016 18:06:37 +0100

---
 src/chars.c  | 33 +++++++++------------------------
 src/global.c |  3 +++
 src/nano.c   |  3 +++
 src/proto.h  |  2 ++
 4 files changed, 17 insertions(+), 24 deletions(-)

diff --git a/src/chars.c b/src/chars.c
index c93a55cc..bb274eab 100644
--- a/src/chars.c
+++ b/src/chars.c
@@ -208,15 +208,10 @@ bool is_word_mbchar(const char *c, bool allow_punct)
        return TRUE;
 
     if (word_chars != NULL && *word_chars != '\0') {
-       bool wordforming;
-       char *symbol = charalloc(MB_CUR_MAX + 1);
        int symlen = parse_mbchar(c, symbol, NULL);
 
        symbol[symlen] = '\0';
-       wordforming = (strstr(word_chars, symbol) != NULL);
-       free(symbol);
-
-       return wordforming;
+       return (strstr(word_chars, symbol) != NULL);
     }
 
     return (allow_punct && is_punct_mbchar(c));
@@ -707,7 +702,6 @@ char *mbstrchr(const char *s, const char *c)
 #ifdef ENABLE_UTF8
     if (use_utf8) {
        bool bad_s_mb = FALSE, bad_c_mb = FALSE;
-       char *s_mb = charalloc(MB_CUR_MAX);
        const char *q = s;
        wchar_t ws, wc;
 
@@ -718,9 +712,9 @@ char *mbstrchr(const char *s, const char *c)
        }
 
        while (*s != '\0') {
-           int s_mb_len = parse_mbchar(s, s_mb, NULL);
+           int sym_len = parse_mbchar(s, symbol, NULL);
 
-           if (mbtowc(&ws, s_mb, s_mb_len) < 0) {
+           if (mbtowc(&ws, symbol, sym_len) < 0) {
                mbtowc_reset();
                ws = (unsigned char)*s;
                bad_s_mb = TRUE;
@@ -729,12 +723,10 @@ char *mbstrchr(const char *s, const char *c)
            if (bad_s_mb == bad_c_mb && ws == wc)
                break;
 
-           s += s_mb_len;
-           q += s_mb_len;
+           s += sym_len;
+           q += sym_len;
        }
 
-       free(s_mb);
-
        if (*s == '\0')
            q = NULL;
 
@@ -833,21 +825,14 @@ bool has_blank_mbchars(const char *s)
 {
 #ifdef ENABLE_UTF8
     if (use_utf8) {
-       bool retval = FALSE;
-       char *chr_mb = charalloc(MB_CUR_MAX);
-
        for (; *s != '\0'; s += move_mbright(s, 0)) {
-           parse_mbchar(s, chr_mb, NULL);
+           parse_mbchar(s, symbol, NULL);
 
-           if (is_blank_mbchar(chr_mb)) {
-               retval = TRUE;
-               break;
-           }
+           if (is_blank_mbchar(symbol))
+               return TRUE;
        }
 
-       free(chr_mb);
-
-       return retval;
+       return FALSE;
     } else
 #endif
        return has_blank_chars(s);
diff --git a/src/global.c b/src/global.c
index 77c8d842..ff4c9aca 100644
--- a/src/global.c
+++ b/src/global.c
@@ -77,6 +77,9 @@ char *last_search = NULL;
 char *present_path = NULL;
        /* The current browser directory when trying to do tab completion. */
 
+char *symbol;
+       /* A scratchpad for the character handling routines. */
+
 unsigned flags[4] = {0, 0, 0, 0};
        /* Our flag containing the states of all global options. */
 WINDOW *topwin;
diff --git a/src/nano.c b/src/nano.c
index 345db3ba..14e6d675 100644
--- a/src/nano.c
+++ b/src/nano.c
@@ -2269,6 +2269,9 @@ int main(int argc, char **argv)
 #endif
     }
 
+    /* Allocate a scratchpad for the character routines. */
+    symbol = charalloc(MB_CUR_MAX + 1);
+
     /* Set up the function and shortcut lists.  This needs to be done
      * before reading the rcfile, to be able to rebind/unbind keys. */
     shortcut_init();
diff --git a/src/proto.h b/src/proto.h
index f0f8a175..1f0b2ee8 100644
--- a/src/proto.h
+++ b/src/proto.h
@@ -69,6 +69,8 @@ extern char *last_search;
 
 extern char *present_path;
 
+extern char *symbol;
+
 extern unsigned flags[4];
 extern WINDOW *topwin;
 extern WINDOW *edit;
-- 
2.11.0




reply via email to

[Prev in Thread] Current Thread [Next in Thread]