Fix ShString.ExchangeString

And add a test.
This commit is contained in:
kichikuou
2020-07-23 18:08:58 +09:00
parent 1f5ec7a9a9
commit 616534621e
9 changed files with 53 additions and 20 deletions
+1 -19
View File
@@ -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 */
+28
View File
@@ -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() {
+1
View File
@@ -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__ */
+7
View File
@@ -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)
BIN
View File
Binary file not shown.
+10
View File
@@ -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:
+3
View File
@@ -3,3 +3,6 @@ test.adv
util.adv
array.adv
strvar.adv
#DLLHeader
ShString.HEL
BIN
View File
Binary file not shown.
+3 -1
View File
@@ -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