Code

wreadln: use memmove() instead of an temporary buffer
[ncmpc.git] / src / wreadln.c
index d68185a7e42ffabae1702dc74d1a8edbe465e768..0bfc414086a7ac541b990d776492a74f146576c0 100644 (file)
@@ -117,6 +117,24 @@ static inline void drawline(const struct wreadln *wr)
        doupdate();
 }
 
+static void
+wreadln_insert_byte(struct wreadln *wr, gint key)
+{
+       if (strlen(wr->line + wr->cursor)) { /* if the cursor is */
+               /* not at the last pos */
+               gsize rest = strlen(wr->line + wr->cursor) + 1;
+
+               memmove(wr->line + wr->cursor + 1,
+                       wr->line + wr->cursor, rest);
+               wr->line[wr->cursor] = key;
+       } else {
+               wr->line[wr->cursor + 1] = 0;
+               wr->line[wr->cursor] = key;
+       }
+
+       cursor_move_right(wr);
+}
+
 /* libcurses version */
 
 static gchar *
@@ -308,25 +326,8 @@ _wreadln(WINDOW *w,
                        /* ignore char */
                        break;
                default:
-                       if (key >= 32) {
-                               if (strlen(wr.line + wr.cursor)) { /* if the cursor is */
-                                       /* not at the last pos */
-                                       gchar *tmp = NULL;
-                                       gsize size = strlen(wr.line + wr.cursor) + 1;
-
-                                       tmp = g_malloc0(size);
-                                       g_strlcpy (tmp, wr.line + wr.cursor, size);
-                                       wr.line[wr.cursor] = key;
-                                       wr.line[wr.cursor + 1] = 0;
-                                       g_strlcat(&wr.line[wr.cursor + 1], tmp, size);
-                                       g_free(tmp);
-                                       cursor_move_right(&wr);
-                               } else {
-                                       wr.line[wr.cursor + 1] = 0;
-                                       wr.line[wr.cursor] = key;
-                                       cursor_move_right(&wr);
-                               }
-                       }
+                       if (key >= 32)
+                               wreadln_insert_byte(&wr, key);
                }
 
                drawline(&wr);