fix makeByteArray and makeString for strings longer than 256 characters

This commit is contained in:
Joel Dice 2012-02-29 11:49:13 -07:00
parent 5a5d2a8dd2
commit 6fceca940f
2 changed files with 36 additions and 25 deletions

View File

@ -3048,17 +3048,32 @@ popResources(Thread* t)
}
object
makeByteArray(Thread* t, const char* format, va_list a)
makeByteArray(Thread* t, const char* format, va_list a, int size)
{
THREAD_RUNTIME_ARRAY(t, char, buffer, size);
int r = vm::vsnprintf(RUNTIME_ARRAY_BODY(buffer), size - 1, format, a);
if (r >= 0 and r < size - 1) {
object s = makeByteArray(t, strlen(RUNTIME_ARRAY_BODY(buffer)) + 1);
memcpy(&byteArrayBody(t, s, 0), RUNTIME_ARRAY_BODY(buffer),
byteArrayLength(t, s));
return s;
} else {
return 0;
}
}
object
makeByteArray(Thread* t, const char* format, ...)
{
int size = 256;
while (true) {
THREAD_RUNTIME_ARRAY(t, char, buffer, size);
int r = vm::vsnprintf(RUNTIME_ARRAY_BODY(buffer), size - 1, format, a);
if (r >= 0 and r < size - 1) {
object s = makeByteArray(t, strlen(RUNTIME_ARRAY_BODY(buffer)) + 1);
memcpy(&byteArrayBody(t, s, 0), RUNTIME_ARRAY_BODY(buffer),
byteArrayLength(t, s));
va_list a;
va_start(a, format);
object s = makeByteArray(t, format, a, size);
va_end(a);
if (s) {
return s;
} else {
size *= 2;
@ -3066,26 +3081,22 @@ makeByteArray(Thread* t, const char* format, va_list a)
}
}
object
makeByteArray(Thread* t, const char* format, ...)
{
va_list a;
va_start(a, format);
object s = makeByteArray(t, format, a);
va_end(a);
return s;
}
object
makeString(Thread* t, const char* format, ...)
{
va_list a;
va_start(a, format);
object s = makeByteArray(t, format, a);
va_end(a);
int size = 256;
while (true) {
va_list a;
va_start(a, format);
object s = makeByteArray(t, format, a, size);
va_end(a);
return t->m->classpath->makeString(t, s, 0, byteArrayLength(t, s) - 1);
if (s) {
return t->m->classpath->makeString(t, s, 0, byteArrayLength(t, s) - 1);
} else {
size *= 2;
}
}
}
int

View File

@ -2214,7 +2214,7 @@ make(Thread* t, object class_)
}
object
makeByteArray(Thread* t, const char* format, va_list a);
makeByteArray(Thread* t, const char* format, va_list a, int size);
object
makeByteArray(Thread* t, const char* format, ...);