mirror of
https://github.com/boykopovar/AnyPS5.git
synced 2026-10-10 01:28:44 +02:00
fix(libc): count wide string precision in output characters
This commit is contained in:
@@ -50,6 +50,15 @@ if(BUILD_TESTING)
|
||||
set_tests_properties(file_position PROPERTIES ENVIRONMENT_MODIFICATION
|
||||
"PATH=path_list_prepend:$<TARGET_FILE_DIR:libc>")
|
||||
endif()
|
||||
add_test_executable(guest_wide_formatting_tests ${LIBS_INCLUDE_DIR}/tests/GuestWideFormatting.cpp)
|
||||
target_include_directories(guest_wide_formatting_tests PRIVATE ${LIBS_INCLUDE_DIR})
|
||||
target_link_libraries(guest_wide_formatting_tests PRIVATE libc)
|
||||
configure_windows_unwind(guest_wide_formatting_tests)
|
||||
add_test(NAME guest_wide_formatting COMMAND guest_wide_formatting_tests)
|
||||
if(WIN32)
|
||||
set_tests_properties(guest_wide_formatting PROPERTIES ENVIRONMENT_MODIFICATION
|
||||
"PATH=path_list_prepend:$<TARGET_FILE_DIR:libc>")
|
||||
endif()
|
||||
add_test_executable(guest_character_tests ${LIBS_INCLUDE_DIR}/tests/GuestCharacter.cpp)
|
||||
target_include_directories(guest_character_tests PRIVATE ${LIBS_INCLUDE_DIR})
|
||||
target_link_libraries(guest_character_tests PRIVATE libc)
|
||||
|
||||
@@ -28,7 +28,7 @@ bool In(char16_t character, const char* set) {
|
||||
}
|
||||
|
||||
void AppendUtf16(std::u16string& out, const char* text, std::size_t limit) {
|
||||
for (std::size_t index = 0; index < limit && text[index] != '\0';) {
|
||||
for (std::size_t index = 0; out.size() < limit && text[index] != '\0';) {
|
||||
const unsigned char lead = static_cast<unsigned char>(text[index]);
|
||||
std::size_t extra = lead >= 0xf0 ? 3 : lead >= 0xe0 ? 2 : lead >= 0xc0 ? 1 : 0;
|
||||
std::uint32_t code = extra == 3 ? lead & 0x07 : extra == 2 ? lead & 0x0f : extra == 1 ? lead & 0x1f : lead;
|
||||
@@ -41,6 +41,7 @@ void AppendUtf16(std::u16string& out, const char* text, std::size_t limit) {
|
||||
if (extra != 0) used = extra + 1;
|
||||
index += used;
|
||||
if (code >= 0x10000) {
|
||||
if (limit - out.size() < 2) break;
|
||||
code -= 0x10000;
|
||||
out.push_back(static_cast<char16_t>(0xd800 + (code >> 10)));
|
||||
out.push_back(static_cast<char16_t>(0xdc00 + (code & 0x3ff)));
|
||||
|
||||
@@ -0,0 +1,49 @@
|
||||
#include "SceTypes.hpp"
|
||||
#include "prx/libc/include/general/VabiMacros.hpp"
|
||||
#include <cstdarg>
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <string>
|
||||
|
||||
extern "C" int APS5_VABI vswprintf_nid_postfix(char16_t*, std::size_t, const char16_t*, VaList*);
|
||||
|
||||
static int APS5_VABI Format(char16_t* buffer, std::size_t size, const char16_t* format, ...) {
|
||||
#ifdef _WIN32
|
||||
__builtin_sysv_va_list args;
|
||||
__builtin_sysv_va_start(args, format);
|
||||
#else
|
||||
std::va_list args;
|
||||
va_start(args, format);
|
||||
#endif
|
||||
const int result = vswprintf_nid_postfix(buffer, size, format, reinterpret_cast<VaList*>(args));
|
||||
#ifdef _WIN32
|
||||
__builtin_sysv_va_end(args);
|
||||
#else
|
||||
va_end(args);
|
||||
#endif
|
||||
return result;
|
||||
}
|
||||
|
||||
static void Check(const char16_t* format, const char* input, const char16_t* expected) {
|
||||
char16_t buffer[32]{};
|
||||
const int result = Format(buffer, 32, format, input);
|
||||
const std::u16string wanted(expected);
|
||||
if (result != static_cast<int>(wanted.size()) || buffer != wanted) {
|
||||
std::fprintf(stderr, "wide precision: expected %zu units, got %d\n", wanted.size(), result);
|
||||
std::abort();
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
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"");
|
||||
Check(u"%.3s", "\xc3\xa9", u"\u00e9");
|
||||
Check(u"%s", "\xc3\xa9\xc3\xa8", u"\u00e9\u00e8");
|
||||
Check(u"%.2s", "abcd", u"ab");
|
||||
Check(u"%4.2s", "\xc3\xa9\xc3\xa8", u" \u00e9\u00e8");
|
||||
Check(u"%-4.2s", "\xc3\xa9\xc3\xa8", u"\u00e9\u00e8 ");
|
||||
Check(u"%.2s", "\xf0\x9f\x98\x80x", u"\U0001f600");
|
||||
Check(u"%.1s", "\xf0\x9f\x98\x80x", u"");
|
||||
Check(u"%.3s", "\xf0\x9f\x98\x80x", u"\U0001f600x");
|
||||
}
|
||||
Reference in New Issue
Block a user