Skip to content

Commit 27ae085

Browse files
committed
fix: correct UTF-8 cursor offset and clipboard range handling
1 parent 5987191 commit 27ae085

File tree

2 files changed

+14
-12
lines changed

2 files changed

+14
-12
lines changed

src/nuklear_edit.c

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -278,17 +278,19 @@ nk_do_edit(nk_flags *state, struct nk_command_buffer *out,
278278
int cut = nk_input_is_key_pressed(in, NK_KEY_CUT);
279279
if ((copy || cut) && (flags & NK_EDIT_CLIPBOARD))
280280
{
281-
int glyph_len;
282-
nk_rune unicode;
283-
const char *text;
284-
int b = edit->select_start;
285-
int e = edit->select_end;
286-
287-
int begin = NK_MIN(b, e);
288-
int end = NK_MAX(b, e);
289-
text = nk_str_at_const(&edit->string, begin, &unicode, &glyph_len);
290-
if (edit->clip.copy)
291-
edit->clip.copy(edit->clip.userdata, text, end - begin);
281+
int begin = NK_MIN(edit->select_start, edit->select_end);
282+
int end = NK_MAX(edit->select_start, edit->select_end);
283+
284+
if (edit->clip.copy) {
285+
int glyph_len;
286+
nk_rune unicode;
287+
const char *text_begin, *text_end;
288+
text_begin = nk_str_at_const(&edit->string, begin, &unicode, &glyph_len);
289+
/*Reuse temporary variables (unicode, glyph_len)*/
290+
text_end = nk_str_at_const(&edit->string, end, &unicode, &glyph_len);
291+
292+
edit->clip.copy(edit->clip.userdata, text_begin, text_end - text_begin);
293+
}
292294
if (cut && !(flags & NK_EDIT_READ_ONLY)){
293295
nk_textedit_cut(edit);
294296
cursor_follow = nk_true;

src/nuklear_text_editor.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,7 @@ nk_textedit_paste(struct nk_text_edit *state, char const *ctext, int len)
355355
glyphs = nk_utf_len(ctext, len);
356356
if (nk_str_insert_text_char(&state->string, state->cursor, text, len)) {
357357
nk_textedit_makeundo_insert(state, state->cursor, glyphs);
358-
state->cursor += len;
358+
state->cursor += glyphs;
359359
state->has_preferred_x = 0;
360360
return 1;
361361
}

0 commit comments

Comments
 (0)