From 616534621efcfc39f2e405823dca10ac1fe9a6f3 Mon Sep 17 00:00:00 2001 From: kichikuou Date: Thu, 23 Jul 2020 18:08:11 +0900 Subject: [PATCH] Fix ShString.ExchangeString And add a test. --- modules/ShString/ShString.c | 20 +------------------- src/variable.c | 28 ++++++++++++++++++++++++++++ src/variable.h | 1 + test/ShString.HEL | 7 +++++++ test/System39.ain | Bin 0 -> 308 bytes test/strvar.adv | 10 ++++++++++ test/test.hed | 3 +++ test/testSA.ALD | Bin 4112 -> 4368 bytes test/xsys35c.cfg | 4 +++- 9 files changed, 53 insertions(+), 20 deletions(-) create mode 100644 test/ShString.HEL create mode 100644 test/System39.ain diff --git a/modules/ShString/ShString.c b/modules/ShString/ShString.c index 6b6b7c5..e21839f 100644 --- a/modules/ShString/ShString.c +++ b/modules/ShString/ShString.c @@ -45,26 +45,8 @@ static void ExchangeString(void) { int target = getCaliValue(); int pat = getCaliValue(); int patr = getCaliValue(); - const char *start = v_str(target -1); - const char *next; - char dst[STRVAR_LEN] = ""; - + v_strReplaceAll(target - 1, pat - 1, patr - 1); DEBUG_COMMAND("ShString.ExchangeString: %d,%d,%d:\n", target, pat, patr); - - if (v_strlen(target -1) == 0 || v_strlen(pat -1) == 0) { - return; - } - - while(TRUE) { - next = strstr(start, v_str(pat -1)); - if (next == NULL) break; - strncat(dst, start, (size_t)(next - start)); - strncat(dst, v_str(patr -1), sizeof(dst) - strlen(dst)); - start = next + v_strlen(pat -1); - } - - strncat(dst, start, sizeof(dst) - strlen(dst)); - v_strcpy(target -1, dst); } static void SetNum16String(void) { /* 1 */ diff --git a/src/variable.c b/src/variable.c index e67441d..57f24ea 100644 --- a/src/variable.c +++ b/src/variable.c @@ -266,6 +266,34 @@ int v_strGetCharType(int no, int pos) { return CHECKSJIS1BYTE(*p) ? 2 : 1; } +void v_strReplaceAll(int no, int pattern, int replacement) { + const char *pat = v_str(pattern); + if (!*pat) + return; + const char *repl = v_str(replacement); + + if (no < 0 || no >= strvar_cnt) { + WARNING("string index out of range: %d", no); + return; + } + if (!strVar[no]) + return; + char *src = strVar[no]; + strVar[no] = NULL; + + char *start = src, *found;; + while ((found = strstr(start, pat))) { + char bak = *found; + *found = '\0'; + v_strcat(no, start); + *found = bak; + v_strcat(no, repl); + start = found + strlen(pat); + } + v_strcat(no, start); + free(src); +} + #ifdef DEBUG void debug_showvariable() { diff --git a/src/variable.h b/src/variable.h index 99a1a54..5bf6a6a 100644 --- a/src/variable.h +++ b/src/variable.h @@ -68,5 +68,6 @@ extern void v_strFromVars(int no, const int *vars); extern int v_strToVars(int no, int *vars); extern const char *v_str(int no); extern int v_strGetCharType(int no, int pos); +extern void v_strReplaceAll(int no, int pattern, int replacement); #endif /* !__VARIABLE__ */ diff --git a/test/ShString.HEL b/test/ShString.HEL new file mode 100644 index 0000000..5c27466 --- /dev/null +++ b/test/ShString.HEL @@ -0,0 +1,7 @@ +void ExchangeString(IString arg1, IString arg2, IString arg3) +void SetNum16String(pword arg1, IString arg2) +void SetNum16HalfString(pword arg1, IString arg2) +void SetNum32String(pword arg1, IString arg2) +void SetNum32HalfString(pword arg1, IString arg2) +void GetArrayString(pword arg1, int arg2, IString arg3) +void SetWindowTitle(IString arg1, ISys3xSystem arg2) diff --git a/test/System39.ain b/test/System39.ain new file mode 100644 index 0000000000000000000000000000000000000000..ffa45f367a70e092f06667db0a8edb26f49dae83 GIT binary patch literal 308 zcmZ?t^z&q7U|6tjC&-CDt!lCnGq-?;PIT3M4P6FBK0K^A?7-;X+sM(UY z?}|prN=Gs@K!u%w^u*A~7l0I4S-_n;k#l4NV`3wu?*J8X1BD?%Kx5&e4}hYsUi2?eVL0{dgzG(pr0=eE`E|B3<-?Rt>8T1oC3PHfD-n-tXUSHi8 jDkTL`(X@a;ak>{U)O_mk*lLQ0+01uL# delta 355 zcmbQBG(katk%57Ufq{jAfsKKIlYv2CqTu_D|2Y|18{w z**5tspQL36T;9gF-T|%xY<4%YoHJa`38cGsvOK>uWB+7peo5&`aCNo_+ucB_rcBP~ ZmsFUBkVm-Q6C^(aEHjy1Aa;_V002#2YzY7W diff --git a/test/xsys35c.cfg b/test/xsys35c.cfg index 6fed47b..9244823 100644 --- a/test/xsys35c.cfg +++ b/test/xsys35c.cfg @@ -1,3 +1,5 @@ ald_basename = test -sys_ver = 3.8 +sys_ver = 3.9 hed = test.hed +disable_ain_variable = 1 +disable_ain_message = 1