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 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; int size = 256;
while (true) { while (true) {
THREAD_RUNTIME_ARRAY(t, char, buffer, size); va_list a;
va_start(a, format);
object s = makeByteArray(t, format, a, size);
va_end(a);
int r = vm::vsnprintf(RUNTIME_ARRAY_BODY(buffer), size - 1, format, a); if (s) {
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; return s;
} else { } else {
size *= 2; 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 object
makeString(Thread* t, const char* format, ...) makeString(Thread* t, const char* format, ...)
{ {
va_list a; int size = 256;
va_start(a, format); while (true) {
object s = makeByteArray(t, format, a); va_list a;
va_end(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 int

View File

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