]> git.rm.cloudns.org Git - xonotic/darkplaces.git/commitdiff
Added DP_QC_STRREPLACE - adds strreplace and strireplace functions (behave like str_r...
authorsajt <sajt@d7cf8633-e32d-0410-b094-e92efae38249>
Tue, 9 Oct 2007 19:48:13 +0000 (19:48 +0000)
committersajt <sajt@d7cf8633-e32d-0410-b094-e92efae38249>
Tue, 9 Oct 2007 19:48:13 +0000 (19:48 +0000)
git-svn-id: svn://svn.icculus.org/twilight/trunk/darkplaces@7615 d7cf8633-e32d-0410-b094-e92efae38249

clvm_cmds.c
mvm_cmds.c
prvm_cmds.c
prvm_cmds.h
svvm_cmds.c

index 9afd7812cdf3c20dcf81592b0ce499ecb3502dc6..f4cab9c58d70d50ba07abda1b21144ca745eb7ef 100644 (file)
@@ -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
index cef5830c09b207828a12b211d30cc10ca07763c8..69391109c2310f74d8fcd9c9dbcd91c8151166d4 100644 (file)
@@ -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
index f2e1720d6ba00b0fcfaa6dc2c4021ba9e79ea9ef..6276252c0f50d46d6e26786e75a896968b027956 100644 (file)
@@ -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
index 20c4927beda304814542b66ea7bf2d59037811d4..1efbd2612e8d873bc80799409b89650aa1a16865 100644 (file)
@@ -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);
index a1150ddf7d89dae38133fd162d8cb429c3be84ec..5700b21aca1a34d8fcf9e2b5422c4bfba0fa980e 100644 (file)
@@ -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