GUI: U32: Fix problems with u32strings formatting

- Improve U32's format helper
- Add insertString helper to u32
This commit is contained in:
aryanrawlani28 2020-06-18 20:29:52 +05:30 committed by Eugene Sandulenko
parent 0118839c59
commit 1ca1712b4d
5 changed files with 61 additions and 56 deletions

View file

@ -2464,9 +2464,8 @@ void SurfaceSdlGraphicsManager::handleScalerHotkeys(int scalefactor, int scalerT
g++;
}
if (newScalerName) {
const Common::U32String message = Common::String::format(
"%s %s\n%d x %d -> %d x %d",
_("Active graphics filter:").encode().c_str(),
const Common::U32String message = Common::U32String::format(
_("Active graphics filter: ") + Common::U32String("%s\n%d x %d -> %d x %d"),
newScalerName,
_videoMode.screenWidth, _videoMode.screenHeight,
_hwScreen->w, _hwScreen->h);

View file

@ -238,6 +238,12 @@ void U32String::insertChar(value_type c, uint32 p) {
_str[p] = c;
}
void U32String::insertString(String s, uint32 p) {
for (String::iterator i = s.begin(); i != s.end(); i++) {
U32String::insertChar(*i, p++);
}
}
void U32String::deleteChar(uint32 p) {
assert(p < _size);
@ -520,68 +526,65 @@ void U32String::trim() {
}
}
// static
U32String U32String::format(const char *fmt, ...) {
U32String output;
U32String U32String::format(U32String fmt, ...) {
U32String output = fmt;
int len;
va_list va;
va_start(va, fmt);
output = U32String::vformat(fmt, va);
len = U32String::vformat(output, output.begin(), va);
va_end(va);
return output;
}
// static
U32String U32String::vformat(const char *fmt, va_list args) {
U32String output = "";
assert(output.isStorageIntern());
output._str;
va_list va;
scumm_va_copy(va, args);
int len = vsnprintf((char *)output._str, _builtinCapacity, fmt, va);
va_end(va);
int U32String::vformat(U32String &output, U32String::iterator fmt, va_list args) {
int int_temp;
char char_temp;
char *string_temp;
if (len == -1 || len == _builtinCapacity - 1) {
// MSVC and IRIX don't return the size the full string would take up.
// MSVC returns -1, IRIX returns the number of characters actually written,
// which is at the most the size of the buffer minus one, as the string is
// truncated to fit.
char ch;
int length = 0;
int len = 0;
int pos = 0;
// We assume MSVC failed to output the correct, null-terminated string
// if the return value is either -1 or size.
// For IRIX, because we lack a better mechanism, we assume failure
// if the return value equals size - 1.
// The downside to this is that whenever we try to format a string where the
// size is 1 below the built-in capacity, the size is needlessly increased.
char buffer[512];
// Try increasing the size of the string until it fits.
int size = _builtinCapacity;
do {
size *= 2;
output.ensureCapacity(size - 1, false);
assert(!output.isStorageIntern());
size = output._extern._capacity;
scumm_va_copy(va, args);
len = vsnprintf((char *)output._str, size, fmt, va);
va_end(va);
} while (len == -1 || len >= size - 1);
output._size = len;
} else if (len < (int)_builtinCapacity) {
// vsnprintf succeeded
output._size = len;
} else {
// vsnprintf didn't have enough space, so grow buffer
output.ensureCapacity(len, false);
scumm_va_copy(va, args);
int len2 = vsnprintf((char *)output._str, len + 1, fmt, va);
va_end(va);
assert(len == len2);
output._size = len2;
while (fmt != output.end() && pos < output.size()) {
ch = *fmt++;
if (ch == '%') {
switch (ch = *fmt++)
{
case 's':
string_temp = va_arg(args, char *);
len = strlen(string_temp);
length += len;
fmt -= 2;
output.deleteChar(pos); // remove %
output.deleteChar(pos); // remove s
output.insertString(string_temp, pos);
fmt += len;
pos += len;
break;
case 'd':
int_temp = va_arg(args, int);
fmt -= 2;
output.deleteChar(pos); // remove %
output.deleteChar(pos); // remove d
itoa(int_temp, buffer, 10);
output.insertString(buffer, pos);
len = strlen(buffer);
length += len;
fmt += len;
break;
default:
break;
}
}
pos++;
}
return output;
return length;
}
} // End of namespace Common

View file

@ -167,6 +167,7 @@ public:
/** Insert character c before position p. */
void insertChar(value_type c, uint32 p);
void insertString(String s, uint32 p);
/**
* Removes the value at position p from the string.
@ -240,14 +241,14 @@ public:
* except that it stores the result in (variably sized) String
* instead of a fixed size buffer.
*/
static U32String format(const char *fmt, ...) GCC_PRINTF(1, 2);
static U32String format(U32String fmt, ...) GCC_PRINTF(1, 2);
/**
* Print formatted data into a String object. Similar to vsprintf,
* except that it stores the result in (variably sized) String
* instead of a fixed size buffer.
*/
static U32String vformat(const char *fmt, va_list args);
static int vformat(U32String &output, U32String::iterator fmt, va_list args);
private:
void makeUnique();

View file

@ -99,7 +99,7 @@ AboutDialog::AboutDialog()
version += gScummVMVersion;
_lines.push_back(version);
Common::U32String date = Common::String::format(_("(built on %s)").encode().c_str(), gScummVMBuildDate);
Common::U32String date = Common::U32String::format(_("(built on %s)"), gScummVMBuildDate);
_lines.push_back(U32String("C2") + date);
for (i = 0; i < ARRAYSIZE(copyright_text); i++)

View file

@ -25,6 +25,7 @@
#include "gui/dialog.h"
#include "common/str.h"
#include "common/ustr.h"
#include "common/array.h"
#include "common/keyboard.h"
@ -33,6 +34,7 @@ namespace GUI {
class EEHandler;
class AboutDialog : public Dialog {
typedef Common::String String;
typedef Common::U32String U32String;
typedef Common::Array<Common::U32String> U32StringArray;
protected: