From: sajt Date: Tue, 9 Oct 2007 19:48:13 +0000 (+0000) Subject: Added DP_QC_STRREPLACE - adds strreplace and strireplace functions (behave like str_r... X-Git-Tag: xonotic-v0.1.0preview~2863 X-Git-Url: https://git.rm.cloudns.org/?a=commitdiff_plain;h=6cdea2644603f8f6644976b73797f92b5f2e10c0;p=xonotic%2Fdarkplaces.git Added DP_QC_STRREPLACE - adds strreplace and strireplace functions (behave like str_replace and str_ireplace in PHP) git-svn-id: svn://svn.icculus.org/twilight/trunk/darkplaces@7615 d7cf8633-e32d-0410-b094-e92efae38249 --- diff --git a/clvm_cmds.c b/clvm_cmds.c index 9afd7812..f4cab9c5 100644 --- a/clvm_cmds.c +++ b/clvm_cmds.c @@ -3234,8 +3234,8 @@ VM_strtolower, // #480 string(string s) VM_strtolower (DP_QC_STRING_CASE_FUN VM_strtoupper, // #481 string(string s) VM_strtoupper (DP_QC_STRING_CASE_FUNCTIONS) VM_cvar_defstring, // #482 string(string s) cvar_defstring (DP_QC_CVAR_DEFSTRING) VM_CL_pointsound, // #483 void(vector origin, string sample, float volume, float attenuation) (DP_SV_POINTSOUND) -NULL, // #484 -NULL, // #485 +VM_strreplace, // #484 string(string search, string replace, string subject) strreplace (DP_QC_STRREPLACE) +VM_strireplace, // #485 string(string search, string replace, string subject) strireplace (DP_QC_STRREPLACE) NULL, // #486 NULL, // #487 NULL, // #488 diff --git a/mvm_cmds.c b/mvm_cmds.c index cef5830c..69391109 100644 --- a/mvm_cmds.c +++ b/mvm_cmds.c @@ -14,6 +14,7 @@ char *vm_m_extensions = "DP_QC_TOKENIZEBYSEPARATOR " "DP_QC_UNLIMITEDTEMPSTRINGS " "DP_QC_CMD " +"DP_QC_STRREPLACE " ; /* @@ -1243,8 +1244,8 @@ VM_strtolower, // #480 string(string s) VM_strtolower : DRESK - Return string VM_strtoupper, // #481 string(string s) VM_strtoupper : DRESK - Return string as uppercase NULL, // #482 NULL, // #483 -NULL, // #484 -NULL, // #485 +VM_strreplace, // #484 string(string search, string replace, string subject) strreplace (DP_QC_STRREPLACE) +VM_strireplace, // #485 string(string search, string replace, string subject) strireplace (DP_QC_STRREPLACE) NULL, // #486 NULL, // #487 NULL, // #488 diff --git a/prvm_cmds.c b/prvm_cmds.c index f2e1720d..6276252c 100644 --- a/prvm_cmds.c +++ b/prvm_cmds.c @@ -1875,6 +1875,104 @@ void VM_substring(void) PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(string); } +/* +========= +VM_strreplace + +string(string search, string replace, string subject) strreplace = #484; +========= +*/ +// replaces all occurrences of search with replace in the string subject, and returns the result +void VM_strreplace(void) +{ + int i, j, si; + const char *search, *replace, *subject; + char string[VM_STRINGTEMP_LENGTH]; + int search_len, replace_len, subject_len; + + VM_SAFEPARMCOUNT(3,VM_strreplace); + + search = PRVM_G_STRING(OFS_PARM0); + replace = PRVM_G_STRING(OFS_PARM1); + subject = PRVM_G_STRING(OFS_PARM2); + + search_len = (int)strlen(search); + replace_len = (int)strlen(replace); + subject_len = (int)strlen(subject); + + for (i = 0; i < subject_len; i++) + { + for (j = 0; j < search_len && i+j < subject_len; j++) + if (subject[i+j] != search[j]) + break; + if (j == search_len) + { + // found it at offset 'i' + for (j = 0; j < replace_len && si < (int)sizeof(string) - 1; j++) + string[si++] = replace[j]; + i += search_len - 1; + } + else + { + // not found + if (si < (int)sizeof(string) - 1) + string[si++] = subject[i]; + } + } + string[si] = '\0'; + + PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(string); +} + +/* +========= +VM_strireplace + +string(string search, string replace, string subject) strireplace = #485; +========= +*/ +// case-insensitive version of strreplace +void VM_strireplace(void) +{ + int i, j, si; + const char *search, *replace, *subject; + char string[VM_STRINGTEMP_LENGTH]; + int search_len, replace_len, subject_len; + + VM_SAFEPARMCOUNT(3,VM_strreplace); + + search = PRVM_G_STRING(OFS_PARM0); + replace = PRVM_G_STRING(OFS_PARM1); + subject = PRVM_G_STRING(OFS_PARM2); + + search_len = (int)strlen(search); + replace_len = (int)strlen(replace); + subject_len = (int)strlen(subject); + + for (i = 0; i < subject_len; i++) + { + for (j = 0; j < search_len && i+j < subject_len; j++) + if (tolower(subject[i+j]) != tolower(search[j])) + break; + if (j == search_len) + { + // found it at offset 'i' + for (j = 0; j < replace_len && si < (int)sizeof(string) - 1; j++) + string[si++] = replace[j]; + i += search_len - 1; + } + else + { + // not found + if (si < (int)sizeof(string) - 1) + string[si++] = subject[i]; + } + } + string[si] = '\0'; + + PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(string); +} + /* ========= VM_stov diff --git a/prvm_cmds.h b/prvm_cmds.h index 20c4927b..1efbd261 100644 --- a/prvm_cmds.h +++ b/prvm_cmds.h @@ -381,6 +381,9 @@ void VM_strncasecmp (void); void VM_registercvar (void); void VM_wasfreed (void); +void VM_strreplace (void); +void VM_strireplace (void); + void VM_SetTraceGlobals(const trace_t *trace); void VM_Cmd_Init(void); diff --git a/svvm_cmds.c b/svvm_cmds.c index a1150ddf..5700b21a 100644 --- a/svvm_cmds.c +++ b/svvm_cmds.c @@ -143,6 +143,7 @@ char *vm_sv_extensions = "DP_QC_CMD " "FTE_STRINGS " "DP_CON_BESTWEAPON " +"DP_QC_STRREPLACE " ; /* @@ -3216,8 +3217,8 @@ VM_strtolower, // #480 string(string s) VM_strtolower (DP_QC_STRING_CASE_FUN VM_strtoupper, // #481 string(string s) VM_strtoupper (DP_QC_STRING_CASE_FUNCTIONS) VM_cvar_defstring, // #482 string(string s) cvar_defstring (DP_QC_CVAR_DEFSTRING) VM_SV_pointsound, // #483 void(vector origin, string sample, float volume, float attenuation) (DP_SV_POINTSOUND) -NULL, // #484 -NULL, // #485 +VM_strreplace, // #484 string(string search, string replace, string subject) strreplace (DP_QC_STRREPLACE) +VM_strireplace, // #485 string(string search, string replace, string subject) strireplace (DP_QC_STRREPLACE) NULL, // #486 NULL, // #487 NULL, // #488