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 0000000..ffa45f3 Binary files /dev/null and b/test/System39.ain differ diff --git a/test/strvar.adv b/test/strvar.adv index f795b65..3cc17ee 100644 --- a/test/strvar.adv +++ b/test/strvar.adv @@ -96,6 +96,7 @@ const word str4 = 13: \test_MG7: \test_strFuncs: + \test_ShString: %0: @@ -153,6 +154,15 @@ const word str4 = 13: \0: +*test_ShString: + MS str1,"あああああ": + MS str2,"ああ": + MS str3,"いいい": + ShString.ExchangeString str1,str2,str3: + MS str4,"いいいいいいあ": + ~AssertStrEquals str1,str4,__LINE__: + \0: + **MPtoString dst,src,len: MG 100,0: MG 1,dst: diff --git a/test/test.hed b/test/test.hed index 67cc26a..5797f33 100644 --- a/test/test.hed +++ b/test/test.hed @@ -3,3 +3,6 @@ test.adv util.adv array.adv strvar.adv + +#DLLHeader +ShString.HEL diff --git a/test/testSA.ALD b/test/testSA.ALD index 22eb3b0..4092c57 100644 Binary files a/test/testSA.ALD and b/test/testSA.ALD differ 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