mirror of
https://github.com/RGBCube/serenity
synced 2025-07-08 02:27:35 +00:00
AK: Use stack buffers in String::number() to avoid some malloc() calls
This commit is contained in:
parent
1b2c6e8f41
commit
a88d409c74
1 changed files with 16 additions and 4 deletions
|
@ -3,6 +3,10 @@
|
||||||
#include <AK/StringBuilder.h>
|
#include <AK/StringBuilder.h>
|
||||||
#include <stdarg.h>
|
#include <stdarg.h>
|
||||||
|
|
||||||
|
#ifndef KERNEL
|
||||||
|
#include <inttypes.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
#ifdef KERNEL
|
#ifdef KERNEL
|
||||||
extern "C" char* strstr(const char* haystack, const char* needle);
|
extern "C" char* strstr(const char* haystack, const char* needle);
|
||||||
#endif
|
#endif
|
||||||
|
@ -208,21 +212,28 @@ unsigned String::to_uint(bool& ok) const
|
||||||
|
|
||||||
String String::number(u64 value)
|
String String::number(u64 value)
|
||||||
{
|
{
|
||||||
|
int size;
|
||||||
|
char buffer[32];
|
||||||
#ifdef __serenity__
|
#ifdef __serenity__
|
||||||
return String::format("%Q", value);
|
size = sprintf(buffer, "%llu", value);
|
||||||
#else
|
#else
|
||||||
return String::format("%llu", value);
|
size = sprintf(buffer, "%" PRIu64, value);
|
||||||
#endif
|
#endif
|
||||||
|
return String(buffer, size);
|
||||||
}
|
}
|
||||||
|
|
||||||
String String::number(u32 value)
|
String String::number(u32 value)
|
||||||
{
|
{
|
||||||
return String::format("%u", value);
|
char buffer[32];
|
||||||
|
int size = sprintf(buffer, "%u", value);
|
||||||
|
return String(buffer, size);
|
||||||
}
|
}
|
||||||
|
|
||||||
String String::number(i32 value)
|
String String::number(i32 value)
|
||||||
{
|
{
|
||||||
return String::format("%d", value);
|
char buffer[32];
|
||||||
|
int size = sprintf(buffer, "%d", value);
|
||||||
|
return String(buffer, size);
|
||||||
}
|
}
|
||||||
|
|
||||||
String String::format(const char* fmt, ...)
|
String String::format(const char* fmt, ...)
|
||||||
|
@ -343,3 +354,4 @@ bool String::equals_ignoring_case(const StringView& other) const
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue