]> git.xonotic.org Git - xonotic/darkplaces.git/blobdiff - prvm_cmds.c
fix loading "g"-less .obj files
[xonotic/darkplaces.git] / prvm_cmds.c
index f4a10b6741e0f4a9a08a4080780b93cdb25c0532..f604516e9b04e5419381d92422a5e641c0ca0fae 100644 (file)
@@ -874,7 +874,7 @@ void VM_ftoe(void)
        VM_SAFEPARMCOUNT(1, VM_ftoe);
 
        ent = (int)PRVM_G_FLOAT(OFS_PARM0);
-       if (ent < 0 || ent >= MAX_EDICTS || PRVM_PROG_TO_EDICT(ent)->priv.required->free)
+       if (ent < 0 || ent >= prog->max_edicts || PRVM_PROG_TO_EDICT(ent)->priv.required->free)
                ent = 0; // return world instead of a free or invalid entity
 
        PRVM_G_INT(OFS_RETURN) = ent;
@@ -2382,25 +2382,42 @@ void VM_strreplace(void)
        subject_len = (int)strlen(subject);
 
        si = 0;
-       for (i = 0; i < subject_len; i++)
+       for (i = 0; i <= subject_len - search_len; i++)
        {
-               for (j = 0; j < search_len && i+j < subject_len; j++)
+               for (j = 0; j < search_len; j++) // thus, i+j < subject_len
                        if (subject[i+j] != search[j])
                                break;
-               if (j == search_len || i+j == subject_len)
+               if (j == search_len)
                {
-               // found it at offset 'i'
+                       // NOTE: if search_len == 0, we always hit THIS case, and never the other
+                       // 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;
+                       if(search_len > 0)
+                       {
+                               i += search_len - 1;
+                       }
+                       else
+                       {
+                               // the above would subtract 1 from i... so we
+                               // don't do that, but instead output the next
+                               // char
+                               if (si < (int)sizeof(string) - 1)
+                                       string[si++] = subject[i];
+                       }
                }
                else
                {
-               // not found
+                       // in THIS case, we know search_len > 0, thus i < subject_len
+                       // not found
                        if (si < (int)sizeof(string) - 1)
                                string[si++] = subject[i];
                }
        }
+       // remaining chars (these cannot match)
+       for (; i < subject_len; i++)
+               if (si < (int)sizeof(string) - 1)
+                       string[si++] = subject[i];
        string[si] = '\0';
 
        PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(string);
@@ -2432,25 +2449,42 @@ void VM_strireplace(void)
        subject_len = (int)strlen(subject);
 
        si = 0;
-       for (i = 0; i < subject_len; i++)
+       for (i = 0; i <= subject_len - search_len; i++)
        {
-               for (j = 0; j < search_len && i+j < subject_len; j++)
+               for (j = 0; j < search_len; j++) // thus, i+j < subject_len
                        if (tolower(subject[i+j]) != tolower(search[j]))
                                break;
-               if (j == search_len || i+j == subject_len)
+               if (j == search_len)
                {
-               // found it at offset 'i'
+                       // NOTE: if search_len == 0, we always hit THIS case, and never the other
+                       // 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;
+                       if(search_len > 0)
+                       {
+                               i += search_len - 1;
+                       }
+                       else
+                       {
+                               // the above would subtract 1 from i... so we
+                               // don't do that, but instead output the next
+                               // char
+                               if (si < (int)sizeof(string) - 1)
+                                       string[si++] = subject[i];
+                       }
                }
                else
                {
-               // not found
+                       // in THIS case, we know search_len > 0, thus i < subject_len
+                       // not found
                        if (si < (int)sizeof(string) - 1)
                                string[si++] = subject[i];
                }
        }
+       // remaining chars (these cannot match)
+       for (; i < subject_len; i++)
+               if (si < (int)sizeof(string) - 1)
+                       string[si++] = subject[i];
        string[si] = '\0';
 
        PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(string);
@@ -6402,8 +6436,11 @@ nolength:
                                        if(flags & PRINTF_SPACEPOSITIVE) *f++ = ' ';
                                        if(flags & PRINTF_SIGNPOSITIVE) *f++ = '+';
                                        *f++ = '*';
-                                       *f++ = '.';
-                                       *f++ = '*';
+                                       if(precision >= 0)
+                                       {
+                                               *f++ = '.';
+                                               *f++ = '*';
+                                       }
                                        *f++ = *s;
                                        *f++ = 0;
 
@@ -6414,50 +6451,70 @@ nolength:
                                        {
                                                case 'd': case 'i':
                                                        if(precision < 0) // not set
-                                                               precision = 1;
-                                                       o += dpsnprintf(o, end - o, formatbuf, width, precision, (isfloat ? (int) GETARG_FLOAT(thisarg) : (int) GETARG_INT(thisarg)));
+                                                               o += dpsnprintf(o, end - o, formatbuf, width, (isfloat ? (int) GETARG_FLOAT(thisarg) : (int) GETARG_INT(thisarg)));
+                                                       else
+                                                               o += dpsnprintf(o, end - o, formatbuf, width, precision, (isfloat ? (int) GETARG_FLOAT(thisarg) : (int) GETARG_INT(thisarg)));
                                                        break;
                                                case 'o': case 'u': case 'x': case 'X':
                                                        if(precision < 0) // not set
-                                                               precision = 1;
-                                                       o += dpsnprintf(o, end - o, formatbuf, width, precision, (isfloat ? (unsigned int) GETARG_FLOAT(thisarg) : (unsigned int) GETARG_INT(thisarg)));
+                                                               o += dpsnprintf(o, end - o, formatbuf, width, (isfloat ? (unsigned int) GETARG_FLOAT(thisarg) : (unsigned int) GETARG_INT(thisarg)));
+                                                       else
+                                                               o += dpsnprintf(o, end - o, formatbuf, width, precision, (isfloat ? (unsigned int) GETARG_FLOAT(thisarg) : (unsigned int) GETARG_INT(thisarg)));
                                                        break;
                                                case 'e': case 'E': case 'f': case 'F': case 'g': case 'G':
                                                        if(precision < 0) // not set
-                                                               precision = 6;
-                                                       o += dpsnprintf(o, end - o, formatbuf, width, precision, (isfloat ? (double) GETARG_FLOAT(thisarg) : (double) GETARG_INT(thisarg)));
+                                                               o += dpsnprintf(o, end - o, formatbuf, width, (isfloat ? (double) GETARG_FLOAT(thisarg) : (double) GETARG_INT(thisarg)));
+                                                       else
+                                                               o += dpsnprintf(o, end - o, formatbuf, width, precision, (isfloat ? (double) GETARG_FLOAT(thisarg) : (double) GETARG_INT(thisarg)));
                                                        break;
                                                case 'v': case 'V':
                                                        f[-2] += 'g' - 'v';
                                                        if(precision < 0) // not set
-                                                               precision = 6;
-                                                       o += dpsnprintf(o, end - o, va("%s %s %s", /* NESTED SPRINTF IS NESTED */ formatbuf, formatbuf, formatbuf),
-                                                               width, precision, (isfloat ? (double) GETARG_VECTOR(thisarg)[0] : (double) GETARG_INTVECTOR(thisarg)[0]),
-                                                               width, precision, (isfloat ? (double) GETARG_VECTOR(thisarg)[1] : (double) GETARG_INTVECTOR(thisarg)[1]),
-                                                               width, precision, (isfloat ? (double) GETARG_VECTOR(thisarg)[2] : (double) GETARG_INTVECTOR(thisarg)[2])
-                                                       );
+                                                               o += dpsnprintf(o, end - o, va("%s %s %s", /* NESTED SPRINTF IS NESTED */ formatbuf, formatbuf, formatbuf),
+                                                                       width, (isfloat ? (double) GETARG_VECTOR(thisarg)[0] : (double) GETARG_INTVECTOR(thisarg)[0]),
+                                                                       width, (isfloat ? (double) GETARG_VECTOR(thisarg)[1] : (double) GETARG_INTVECTOR(thisarg)[1]),
+                                                                       width, (isfloat ? (double) GETARG_VECTOR(thisarg)[2] : (double) GETARG_INTVECTOR(thisarg)[2])
+                                                               );
+                                                       else
+                                                               o += dpsnprintf(o, end - o, va("%s %s %s", /* NESTED SPRINTF IS NESTED */ formatbuf, formatbuf, formatbuf),
+                                                                       width, precision, (isfloat ? (double) GETARG_VECTOR(thisarg)[0] : (double) GETARG_INTVECTOR(thisarg)[0]),
+                                                                       width, precision, (isfloat ? (double) GETARG_VECTOR(thisarg)[1] : (double) GETARG_INTVECTOR(thisarg)[1]),
+                                                                       width, precision, (isfloat ? (double) GETARG_VECTOR(thisarg)[2] : (double) GETARG_INTVECTOR(thisarg)[2])
+                                                               );
                                                        break;
                                                case 'c':
-                                                       if(precision < 0) // not set
-                                                               precision = end - o - 1;
                                                        if(flags & PRINTF_ALTERNATE)
-                                                               o += dpsnprintf(o, end - o, formatbuf, width, precision, (isfloat ? (unsigned int) GETARG_FLOAT(thisarg) : (unsigned int) GETARG_INT(thisarg)));
+                                                       {
+                                                               if(precision < 0) // not set
+                                                                       o += dpsnprintf(o, end - o, formatbuf, width, (isfloat ? (unsigned int) GETARG_FLOAT(thisarg) : (unsigned int) GETARG_INT(thisarg)));
+                                                               else
+                                                                       o += dpsnprintf(o, end - o, formatbuf, width, precision, (isfloat ? (unsigned int) GETARG_FLOAT(thisarg) : (unsigned int) GETARG_INT(thisarg)));
+                                                       }
                                                        else
                                                        {
                                                                unsigned int c = (isfloat ? (unsigned int) GETARG_FLOAT(thisarg) : (unsigned int) GETARG_INT(thisarg));
                                                                const char *buf = u8_encodech(c, NULL);
                                                                if(!buf)
                                                                        buf = "";
+                                                               if(precision < 0) // not set
+                                                                       precision = end - o - 1;
                                                                o += u8_strpad(o, end - o, buf, (flags & PRINTF_LEFT) != 0, width, precision);
                                                        }
                                                        break;
                                                case 's':
-                                                       if(precision < 0) // not set
-                                                               precision = end - o - 1;
                                                        if(flags & PRINTF_ALTERNATE)
-                                                               o += dpsnprintf(o, end - o, formatbuf, width, precision, GETARG_STRING(thisarg));
+                                                       {
+                                                               if(precision < 0) // not set
+                                                                       o += dpsnprintf(o, end - o, formatbuf, width, GETARG_STRING(thisarg));
+                                                               else
+                                                                       o += dpsnprintf(o, end - o, formatbuf, width, precision, GETARG_STRING(thisarg));
+                                                       }
                                                        else
+                                                       {
+                                                               if(precision < 0) // not set
+                                                                       precision = end - o - 1;
                                                                o += u8_strpad(o, end - o, GETARG_STRING(thisarg), (flags & PRINTF_LEFT) != 0, width, precision);
+                                                       }
                                                        break;
                                                default:
                                                        VM_Warning("VM_sprintf: invalid directive in %s: %s\n", PRVM_NAME, s0);