(I think the previous code had no bug, but was hard to follow)
git-svn-id: svn://svn.icculus.org/twilight/trunk/darkplaces@12201
d7cf8633-e32d-0410-b094-
e92efae38249
*/
void VM_altstr_prepare(prvm_prog_t *prog)
{
*/
void VM_altstr_prepare(prvm_prog_t *prog)
{
const char *instr, *in;
int size;
char outstr[VM_STRINGTEMP_LENGTH];
const char *instr, *in;
int size;
char outstr[VM_STRINGTEMP_LENGTH];
VM_SAFEPARMCOUNT( 1, VM_altstr_prepare );
instr = PRVM_G_STRING( OFS_PARM0 );
VM_SAFEPARMCOUNT( 1, VM_altstr_prepare );
instr = PRVM_G_STRING( OFS_PARM0 );
- for( out = outstr, in = instr, size = sizeof(outstr) - 1 ; size && *in ; size--, in++, out++ )
- if( *in == '\'' && size > 1) {
- *out++ = '\\';
- *out = '\'';
- size--;
- } else
- *out = *in;
- *out = 0;
+ for (in = instr, outpos = 0; *in && outpos < sizeof(outstr) - 1; ++in)
+ {
+ if (*in == '\'' && outpos < sizeof(outstr) - 2)
+ {
+ out[outpos++] = '\\';
+ out[outpos++] = '\'';
+ }
+ else
+ out[outpos++] = *in;
+ }
+ out[outpos++] = 0;
PRVM_G_INT( OFS_RETURN ) = PRVM_SetTempString(prog, outstr );
}
PRVM_G_INT( OFS_RETURN ) = PRVM_SetTempString(prog, outstr );
}