mirror of
https://github.com/boykopovar/AnyPS5.git
synced 2026-10-10 01:28:44 +02:00
Merge pull request #882 from adriadam10/feat/libc-wide-format-count
feat(libc): support %n in wide formatting
This commit is contained in:
@@ -89,7 +89,7 @@ void AppendPadded(std::u16string& out, const std::u16string& text, bool left, in
|
||||
if (left) out.append(pad, u' ');
|
||||
}
|
||||
|
||||
std::u16string FormatWide(const char16_t* format, VaList* source, bool rejectNullStrings = false) {
|
||||
std::u16string FormatWide(const char16_t* format, VaList* source, bool secure = false) {
|
||||
if (format == nullptr || source == nullptr) throw std::invalid_argument("Null formatting argument");
|
||||
LibcDetail::FormatArguments args(source);
|
||||
std::u16string out;
|
||||
@@ -172,15 +172,23 @@ std::u16string FormatWide(const char16_t* format, VaList* source, bool rejectNul
|
||||
const std::size_t limit = precision < 0 ? SIZE_MAX : static_cast<std::size_t>(precision);
|
||||
if (conversion == u'S' || length == "l") {
|
||||
const char16_t* value = args.Next<const char16_t*>();
|
||||
if (value == nullptr && rejectNullStrings) throw std::invalid_argument("Null string argument");
|
||||
if (value == nullptr && secure) throw std::invalid_argument("Null string argument");
|
||||
if (value == nullptr) value = u"(null)";
|
||||
for (std::size_t index = 0; index < limit && value[index] != 0; ++index) text.push_back(value[index]);
|
||||
} else {
|
||||
const char* value = args.Next<const char*>();
|
||||
if (value == nullptr && rejectNullStrings) throw std::invalid_argument("Null string argument");
|
||||
if (value == nullptr && secure) throw std::invalid_argument("Null string argument");
|
||||
AppendUtf16(text, value != nullptr ? value : "(null)", limit);
|
||||
}
|
||||
AppendPadded(out, text, left, width);
|
||||
} else if (conversion == u'n' && integerLength && spec == "%" && !secure) {
|
||||
void* pointer = args.Next<void*>();
|
||||
if (!pointer) throw std::invalid_argument("Null format count pointer");
|
||||
const int count = static_cast<int>(out.size());
|
||||
if (length == "hh") *static_cast<signed char*>(pointer) = static_cast<signed char>(count);
|
||||
else if (length == "h") *static_cast<short*>(pointer) = static_cast<short>(count);
|
||||
else if (length.empty()) *static_cast<int*>(pointer) = count;
|
||||
else *static_cast<long long*>(pointer) = count;
|
||||
} else {
|
||||
throw std::invalid_argument("Unsupported format conversion");
|
||||
}
|
||||
|
||||
@@ -88,8 +88,32 @@ static void CheckBounded() {
|
||||
Require(Format(buffer, 16, u"%s", nullNarrow) == 6 && buffer == std::u16string(u"(null)"), "vswprintf null %s argument");
|
||||
}
|
||||
|
||||
static void CheckCount() {
|
||||
char16_t buffer[16];
|
||||
int count = -7;
|
||||
Require(Format(buffer, 16, u"ab%ncd", &count) == 4 && buffer == std::u16string(u"abcd") && count == 2, "vswprintf %n");
|
||||
count = -7;
|
||||
Require(Format(buffer, 16, u"%s%n!", "\xf0\x9f\x98\x80", &count) == 3 && count == 2, "vswprintf %n counts UTF-16 units");
|
||||
count = -7;
|
||||
Require(Format(buffer, 16, u"%n", &count) == 0 && buffer[0] == 0 && count == 0, "vswprintf %n at start");
|
||||
unsigned char bytes[2] = {0xaa, 0xaa};
|
||||
Require(Format(buffer, 16, u"%3d%hhn", 5, bytes) == 3 && bytes[0] == 3 && bytes[1] == 0xaa, "vswprintf %hhn");
|
||||
unsigned short halves[2] = {0xaaaa, 0xaaaa};
|
||||
Require(Format(buffer, 16, u"%5d%hn", 5, halves) == 5 && halves[0] == 5 && halves[1] == 0xaaaa, "vswprintf %hn");
|
||||
long long wide = -1;
|
||||
Require(Format(buffer, 16, u"%4d%lln", 5, &wide) == 4 && wide == 4, "vswprintf %lln");
|
||||
wide = -1;
|
||||
Require(Format(buffer, 16, u"%2d%ln", 5, &wide) == 2 && wide == 2, "vswprintf %ln");
|
||||
count = -7;
|
||||
Require(Format(buffer, 16, u"a%5n", &count) < 0 && count == -7, "vswprintf %n with a width");
|
||||
Require(Format(buffer, 16, u"a%Ln", &count) < 0 && count == -7, "vswprintf %Ln");
|
||||
const int* const nullCount = nullptr;
|
||||
Require(Format(buffer, 16, u"a%n", nullCount) < 0, "vswprintf null %n argument");
|
||||
}
|
||||
|
||||
int main() {
|
||||
CheckBounded();
|
||||
CheckCount();
|
||||
Check(u"%.2s", "\xc3\xa9\xc3\xa8", u"\u00e9\u00e8");
|
||||
Check(u"%.1s", "\xc3\xa9\xc3\xa8", u"\u00e9");
|
||||
Check(u"%.0s", "\xc3\xa9", u"");
|
||||
|
||||
Reference in New Issue
Block a user