refactor(text): use size_t instead of int for cursor

This commit is contained in:
Jeremy Attali 2020-01-04 17:48:02 -05:00
parent bc3264e9f1
commit f356ff9b17
4 changed files with 29 additions and 20 deletions

View File

@ -47,7 +47,7 @@ struct swappy_paint_text {
double a;
double s;
gchar *text;
int cursor;
size_t cursor;
struct swappy_point from;
struct swappy_point to;
enum swappy_text_mode mode;

View File

@ -3,5 +3,5 @@
#include <glib.h>
void string_remove_at(char *str, int pos);
gchar *string_insert_chars_at(gchar *str, gchar *chars, int pos);
void string_remove_at(char *str, size_t pos);
gchar *string_insert_chars_at(gchar *str, gchar *chars, size_t pos);

View File

@ -2,6 +2,18 @@
#include "util.h"
static void cursor_move_backward(struct swappy_paint_text *text) {
if (text->cursor > 0) {
text->cursor--;
}
}
static void cursor_move_forward(struct swappy_paint_text *text) {
if (text->cursor < strlen(text->text)) {
text->cursor++;
}
}
void paint_free(gpointer data) {
struct swappy_paint *paint = (struct swappy_paint *)data;
@ -167,7 +179,7 @@ void paint_update_temporary_text(struct swappy_state *state,
case GDK_KEY_BackSpace:
if (strlen(text->text) > 0) {
string_remove_at(text->text, text->cursor - 1);
text->cursor--;
cursor_move_backward(text);
}
break;
case GDK_KEY_Delete:
@ -176,10 +188,10 @@ void paint_update_temporary_text(struct swappy_state *state,
}
break;
case GDK_KEY_Left:
text->cursor--;
cursor_move_backward(text);
break;
case GDK_KEY_Right:
text->cursor++;
cursor_move_forward(text);
break;
default:
unicode = gdk_keyval_to_unicode(event->keyval);
@ -187,7 +199,7 @@ void paint_update_temporary_text(struct swappy_state *state,
int ll = g_unichar_to_utf8(unicode, buffer);
buffer[ll] = '\0';
g_debug("received unicode: %d - utf8: %s (%d)", unicode, buffer, ll);
g_debug("text before: %s - cursor: %d", text->text, text->cursor);
g_debug("text before: %s - cursor: %ld", text->text, text->cursor);
char *new_text =
string_insert_chars_at(text->text, buffer, text->cursor);
g_free(text->text);
@ -197,12 +209,7 @@ void paint_update_temporary_text(struct swappy_state *state,
break;
}
if (text->cursor < 0) {
text->cursor = 0;
} else if (text->cursor > (int)strlen(text->text)) {
text->cursor = (int)strlen(text->text);
}
g_debug("text->cursor: %ld", text->cursor);
g_debug("text is now: %s", text->text);
}

View File

@ -4,23 +4,25 @@
#include <glib.h>
#include <string.h>
void string_remove_at(gchar *str, int pos) {
memmove(&str[pos], &str[pos + 1], strlen(str) - pos);
void string_remove_at(gchar *str, size_t pos) {
if (str && strlen(str) > pos) {
memmove(&str[pos], &str[pos + 1], strlen(str) - pos);
}
}
gchar *string_insert_chars_at(gchar *str, gchar *chars, int pos) {
gchar *string_insert_chars_at(gchar *str, gchar *chars, size_t pos) {
gchar *new_str;
if (str && chars) {
int n = strlen(str);
int m = strlen(chars);
int i = 0, j = 0;
size_t n = strlen(str);
size_t m = strlen(chars);
size_t i = 0, j = 0;
new_str = g_new(gchar, n + m + 1);
while (j < n + m) {
if (j == pos) {
for (int k = 0; k < m; k++) {
for (size_t k = 0; k < m; k++) {
new_str[j++] = chars[k];
}
} else {