]> de.git.xonotic.org Git - xonotic/darkplaces.git/blobdiff - prvm_cmds.c
new menu QC builtin "stringwidth(string text, float handleColors)", does not do anyth...
[xonotic/darkplaces.git] / prvm_cmds.c
index 7710cf786f340a7809a3055d7b289854276079c0..1c7f42330487338afd692bd696363af5b07d9011 100644 (file)
@@ -1875,6 +1875,106 @@ 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);
+
+       si = 0;
+       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 || i+j == subject_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);
+
+       si = 0;
+       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 || i+j == subject_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
@@ -2214,7 +2314,7 @@ void VM_parseentitydata(void)
        data = PRVM_G_STRING(OFS_PARM1);
 
        // parse the opening brace
-       if (!COM_ParseToken_Simple(&data, false) || com_token[0] != '{' )
+       if (!COM_ParseToken_Simple(&data, false, false) || com_token[0] != '{' )
                PRVM_ERROR ("VM_parseentitydata: %s: Couldn't parse entity data:\n%s", PRVM_NAME, data );
 
        PRVM_ED_ParseEdict (data, ent);
@@ -2629,6 +2729,24 @@ void VM_drawcolorcodedstring(void)
 }
 /*
 =========
+VM_stringwidth
+
+float  stringwidth(string text, float allowColorCodes)
+=========
+*/
+void VM_stringwidth(void)
+{
+       const char  *string;
+       int colors;
+       VM_SAFEPARMCOUNT(2,VM_drawstring);
+
+       string = PRVM_G_STRING(OFS_PARM0);
+       colors = (int)PRVM_G_FLOAT(OFS_PARM1);
+
+       PRVM_G_FLOAT(OFS_RETURN) = DrawQ_String(0, 0, string, 0, 1, 1, 0, 0, 0, 0, 0, NULL, !colors); // 1x1 characters, don't actually draw
+}
+/*
+=========
 VM_drawpic
 
 float  drawpic(vector position, string pic, vector size, vector rgb, float alpha, float flag)