From 702b23ec3f7db75c94bcff065b45d0a367644b56 Mon Sep 17 00:00:00 2001 From: James R Date: Wed, 8 Jan 2020 12:58:19 -0800 Subject: [PATCH] Put the word jumping code in functions --- src/console.c | 49 +++++-------------------------------------------- src/doomdef.h | 2 ++ src/m_misc.c | 37 +++++++++++++++++++++++++++++++++++++ src/m_misc.h | 8 ++++++++ 4 files changed, 52 insertions(+), 44 deletions(-) diff --git a/src/console.c b/src/console.c index f77d9b085..786eb8c2b 100644 --- a/src/console.c +++ b/src/console.c @@ -32,6 +32,7 @@ #include "d_main.h" #include "m_menu.h" #include "filesrch.h" +#include "m_misc.h" #ifdef _WINDOWS #include "win32/win_main.h" @@ -41,8 +42,6 @@ #include "hardware/hw_main.h" #endif -#define PUNCTUATION "!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~" - #define MAXHUDLINES 20 static boolean con_started = false; // console has been initialised @@ -822,61 +821,23 @@ boolean CON_Responder(event_t *ev) if (input_cur != 0) { if (ctrldown) - { - int (*is)(int); - char *line; - int c; - line = inputlines[inputline]; - c = line[--input_cur]; - if (isspace(c)) - is = isspace; - else if (ispunct(c)) - is = ispunct; - else - is = isalnum; - c = (*is)(line[input_cur]); - while (input_cur > 0 && - (*is)(line[input_cur - 1]) == c) - input_cur--; - } + input_cur = M_JumpWordReverse(inputlines[inputline], input_cur); else - { --input_cur; - } } - if (!shiftdown) input_sel = input_cur; return true; } else if (key == KEY_RIGHTARROW) { - if (ctrldown) + if (input_cur < input_len) { - char *line; - int c; - - line = &inputlines[inputline][input_cur]; - c = line[0]; - - if (isspace(c)) - input_cur += strspn(line, " "); - else if (ispunct(c)) - input_cur += strspn(line, PUNCTUATION); + if (ctrldown) + input_cur += M_JumpWord(&inputlines[inputline][input_cur]); else - { - if (isspace(line[1])) - input_cur += 1 + strspn(&line[1], " "); - else - input_cur += strcspn(line, " "PUNCTUATION); - } - } - else - { - if (input_cur < input_len) ++input_cur; } - if (!shiftdown) input_sel = input_cur; return true; diff --git a/src/doomdef.h b/src/doomdef.h index 55ad8abed..7b4f6343f 100644 --- a/src/doomdef.h +++ b/src/doomdef.h @@ -541,6 +541,8 @@ INT32 I_GetKey(void); #define PATHSEP "/" #endif +#define PUNCTUATION "!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~" + // Compile date and time and revision. extern const char *compdate, *comptime, *comprevision, *compbranch; diff --git a/src/m_misc.c b/src/m_misc.c index 83c0c7bec..91447b415 100644 --- a/src/m_misc.c +++ b/src/m_misc.c @@ -2540,3 +2540,40 @@ void M_MkdirEach(const char *path, int start, int mode) { M_MkdirEachUntil(path, start, -1, mode); } + +int M_JumpWord(const char *line) +{ + int c; + + c = line[0]; + + if (isspace(c)) + return strspn(line, " "); + else if (ispunct(c)) + return strspn(line, PUNCTUATION); + else + { + if (isspace(line[1])) + return 1 + strspn(&line[1], " "); + else + return strcspn(line, " "PUNCTUATION); + } +} + +int M_JumpWordReverse(const char *line, int offset) +{ + int (*is)(int); + int c; + c = line[--offset]; + if (isspace(c)) + is = isspace; + else if (ispunct(c)) + is = ispunct; + else + is = isalnum; + c = (*is)(line[offset]); + while (offset > 0 && + (*is)(line[offset - 1]) == c) + offset--; + return offset; +} diff --git a/src/m_misc.h b/src/m_misc.h index e0a73e0b7..3fb9f031a 100644 --- a/src/m_misc.h +++ b/src/m_misc.h @@ -101,6 +101,14 @@ boolean M_IsPathAbsolute (const char *path); void M_MkdirEach (const char *path, int start, int mode); void M_MkdirEachUntil (const char *path, int start, int end, int mode); +/* Return offset to the first word in a string. */ +/* E.g. cursor += M_JumpWord(line + cursor); */ +int M_JumpWord (const char *s); + +/* Return index of the last word behind offset bytes in a string. */ +/* E.g. cursor = M_JumpWordReverse(line, cursor); */ +int M_JumpWordReverse (const char *line, int offset); + // counting bits, for weapon ammo code, usually FUNCMATH UINT8 M_CountBits(UINT32 num, UINT8 size);