- optimization of WideString and FileReader::Printf.

Both now omit the intermediate FString as the target buffer can be directly written to.
WideString was also moved to utf8.cpp/h to uncouple from zstring.
This commit is contained in:
Christoph Oelckers 2023-08-17 18:44:26 +02:00
parent a4573b4ad1
commit 9b790d23a8
5 changed files with 33 additions and 25 deletions

View file

@ -34,8 +34,8 @@
*/
#include "files.h"
// just for 'clamp'
#include "zstring.h"
#include "utf8.h"
#include "stb_sprintf.h"
FILE *myfopen(const char *filename, const char *flags)
@ -472,13 +472,18 @@ long FileWriter::Seek(long offset, int mode)
size_t FileWriter::Printf(const char *fmt, ...)
{
va_list ap;
FString out;
char workbuf[STB_SPRINTF_MIN];
va_list arglist;
va_start(arglist, fmt);
auto r = stbsp_vsprintfcb([](const char* cstr, void* data, int len) -> char*
{
auto fr = (FileWriter*)data;
auto writ = fr->Write(cstr, len);
return writ == (size_t)len? (char*)cstr : nullptr; // abort if writing caused an error.
}, this, workbuf, fmt, arglist);
va_start(ap, fmt);
out.VFormat(fmt, ap);
va_end(ap);
return Write(out.GetChars(), out.Len());
va_end(arglist);
return r;
}
size_t BufferWriter::Write(const void *buffer, size_t len)

View file

@ -1157,3 +1157,17 @@ bool myisupper(int code)
return false;
}
std::wstring WideString(const char* cin)
{
std::wstring buildbuffer;
if (cin)
{
// This is a bit tricky because we need to support both UTF-8 and legacy content in ISO-8859-1 / Windows 1252
// and thanks to user-side string manipulation it can be that a text mixes both.
// To convert the string this uses the same function as all text printing in the engine.
const uint8_t* in = (const uint8_t*)cin;
while (*in) buildbuffer.push_back((wchar_t)GetCharFromString(in));
}
return buildbuffer;
}

View file

@ -18,3 +18,8 @@ int getAlternative(int code);
extern uint16_t win1252map[];
extern uint16_t lowerforupper[65536];
extern uint16_t upperforlower[65536];
// make this only visible on Windows, on other platforms this should not be called.
#ifdef _WIN32
std::wstring WideString(const char*);
#endif

View file

@ -1326,19 +1326,6 @@ FString &FString::operator=(const wchar_t *copyStr)
return *this;
}
std::wstring WideString(const char *cin)
{
if (!cin) return L"";
const uint8_t *in = (const uint8_t*)cin;
// This is a bit tricky because we need to support both UTF-8 and legacy content in ISO-8859-1
// and thanks to user-side string manipulation it can be that a text mixes both.
// To convert the string this uses the same function as all text printing in the engine.
TArray<wchar_t> buildbuffer;
while (*in) buildbuffer.Push((wchar_t)GetCharFromString(in));
buildbuffer.Push(0);
return std::wstring(buildbuffer.Data());
}
static HANDLE StringHeap;
const SIZE_T STRING_HEAP_SIZE = 64*1024;
#endif

View file

@ -39,6 +39,7 @@
#include <stddef.h>
#include <string>
#include "tarray.h"
#include "utf8.h"
#ifdef __GNUC__
#define PRINTFISH(x) __attribute__((format(printf, 2, x)))
@ -57,10 +58,6 @@
#define IGNORE_FORMAT_POST
#endif
#ifdef _WIN32
std::wstring WideString(const char *);
#endif
struct FStringData
{
unsigned int Len; // Length of string, excluding terminating null