2015-06-21 17:33:46 +02:00
|
|
|
/*
|
|
|
|
|
Simple DirectMedia Layer
|
2024-01-01 13:15:26 -08:00
|
|
|
Copyright (C) 1997-2024 Sam Lantinga <slouken@libsdl.org>
|
2015-06-21 17:33:46 +02:00
|
|
|
|
|
|
|
|
This software is provided 'as-is', without any express or implied
|
|
|
|
|
warranty. In no event will the authors be held liable for any damages
|
|
|
|
|
arising from the use of this software.
|
|
|
|
|
|
|
|
|
|
Permission is granted to anyone to use this software for any purpose,
|
|
|
|
|
including commercial applications, and to alter it and redistribute it
|
|
|
|
|
freely, subject to the following restrictions:
|
|
|
|
|
|
|
|
|
|
1. The origin of this software must not be misrepresented; you must not
|
|
|
|
|
claim that you wrote the original software. If you use this software
|
|
|
|
|
in a product, an acknowledgment in the product documentation would be
|
|
|
|
|
appreciated but is not required.
|
|
|
|
|
2. Altered source versions must be plainly marked as such, and must not be
|
|
|
|
|
misrepresented as being the original software.
|
|
|
|
|
3. This notice may not be removed or altered from any source distribution.
|
|
|
|
|
*/
|
2022-11-29 18:34:15 -08:00
|
|
|
#include "SDL_internal.h"
|
2015-06-21 17:33:46 +02:00
|
|
|
|
Use C++ style comments consistently in SDL source code
Implemented using this script:
find . -type f -exec sed -i'' -e 's,/\* *\([^*]*\)\*/ *$,// \1,' -e 's, \+$,,' {} \;
git checkout \
core/linux/SDL_evdev_kbd_default_keymap.h \
events/imKStoUCS.* \
hidapi \
joystick/controller_type.c \
joystick/controller_type.h \
joystick/hidapi/steam/controller_constants.h \
joystick/hidapi/steam/controller_structs.h \
joystick/SDL_gamepad_db.h \
libm \
render/*/*Shader*.h \
render/vitagxm/SDL_render_vita_gxm_shaders.h \
render/metal/SDL_shaders_metal_*.h \
stdlib/SDL_malloc.c \
stdlib/SDL_qsort.c \
stdlib/SDL_strtokr.c \
test/ \
video/directx/SDL_d3d12_xbox_cmacros.h \
video/directx/d3d12.h \
video/directx/d3d12sdklayers.h \
video/khronos \
video/x11/edid-parse.c \
video/x11/xsettings-client.* \
video/yuv2rgb
sed -i'' -e 's,/\* *\([^*]*\)\*/ *$,// \1,' -e 's, \+$,,' hidapi/SDL_hidapi.c
2024-08-22 10:30:45 -07:00
|
|
|
// This file contains portable string manipulation functions for SDL
|
2015-06-21 17:33:46 +02:00
|
|
|
|
2022-05-06 10:51:55 -07:00
|
|
|
#include "SDL_vacopy.h"
|
2015-06-21 17:33:46 +02:00
|
|
|
|
2024-01-24 02:40:51 +01:00
|
|
|
#ifdef SDL_PLATFORM_VITA
|
2022-09-17 11:52:19 +03:00
|
|
|
#include <psp2/kernel/clib.h>
|
|
|
|
|
#endif
|
|
|
|
|
|
2024-04-04 00:56:07 -04:00
|
|
|
#include "SDL_sysstdlib.h"
|
|
|
|
|
|
stdlib: Improve Unicode support and consistency in string comparison functions.
SDL_strcasecmp (even when calling into a C runtime) does not work with
Unicode chars, and depending on the user's locale, might not work with
even basic ASCII strings.
This implements the function from scratch, using "case-folding,"
which is a more robust method that deals with various languages. It
involves a hashtable of a few hundred codepoints that are "uppercase" and
how to map them to lowercase equivalents (possibly increasing the size of
the string in the process). The vast majority of human languages (and
Unicode) do not have letters with different cases, but still, this static
table takes about 10 kilobytes on a 64-bit machine.
Even this will fail in one known case: the Turkish 'i' folds differently
if you're writing in Turkish vs other languages. Generally this is seen as
unfortunate collateral damage in cases where you can't specify the language
in use.
In addition to case-folding the codepoints, the new functions also know how
to decode the various formats to turn them into codepoints in the first
place, instead of blindly stepping by one byte (or one wchar_t) per
character.
Also included is casefolding.txt from the Unicode Consortium and a perl
script to generate the hashtable from that text file, so we can trivially
update this if new languages are added in the future.
A simple test using the new function:
```c
#include <SDL3/SDL.h>
int main(void)
{
const char *a = "α ε η";
const char *b = "Α Ε Η";
SDL_Log(" strcasecmp(\"%s\", \"%s\") == %d\n", a, b, strcasecmp(a, b));
SDL_Log("SDL_strcasecmp(\"%s\", \"%s\") == %d\n", a, b, SDL_strcasecmp(a, b));
return 0;
}
```
Produces:
```
INFO: strcasecmp("α ε η", "Α Ε Η") == 32
INFO: SDL_strcasecmp("α ε η", "Α Ε Η") == 0
```
glibc strcasecmp() fails to compare a Greek lowercase string to its uppercase
equivalent, even with a UTF-8 locale, but SDL_strcasecmp() works.
Other SDL_stdinc.h functions are changed to be more consistent, which is to
say they now ignore any C runtime and often dictate that only English-based
low-ASCII works with them.
Fixes Issue #9313.
2024-03-26 13:22:38 -04:00
|
|
|
#include "SDL_casefolding.h"
|
|
|
|
|
|
|
|
|
|
#if defined(__SIZEOF_WCHAR_T__)
|
|
|
|
|
#define SDL_SIZEOF_WCHAR_T __SIZEOF_WCHAR_T__
|
|
|
|
|
#elif defined(SDL_PLATFORM_WINDOWS)
|
|
|
|
|
#define SDL_SIZEOF_WCHAR_T 2
|
|
|
|
|
#else // assume everything else is UTF-32 (add more tests if compiler-assert fails below!)
|
|
|
|
|
#define SDL_SIZEOF_WCHAR_T 4
|
|
|
|
|
#endif
|
|
|
|
|
SDL_COMPILE_TIME_ASSERT(sizeof_wchar_t, sizeof(wchar_t) == SDL_SIZEOF_WCHAR_T);
|
|
|
|
|
|
|
|
|
|
|
2024-07-04 01:09:46 -04:00
|
|
|
char *SDL_UCS4ToUTF8(Uint32 codepoint, char *dst)
|
|
|
|
|
{
|
|
|
|
|
if (!dst) {
|
|
|
|
|
return NULL; // I guess...?
|
|
|
|
|
} else if (codepoint > 0x10FFFF) { // Outside the range of Unicode codepoints (also, larger than can be encoded in 4 bytes of UTF-8!).
|
|
|
|
|
codepoint = SDL_INVALID_UNICODE_CODEPOINT;
|
|
|
|
|
} else if ((codepoint >= 0xD800) && (codepoint <= 0xDFFF)) { // UTF-16 surrogate values are illegal in UTF-8.
|
|
|
|
|
codepoint = SDL_INVALID_UNICODE_CODEPOINT;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Uint8 *p = (Uint8 *)dst;
|
|
|
|
|
if (codepoint <= 0x7F) {
|
|
|
|
|
*p = (Uint8)codepoint;
|
|
|
|
|
++dst;
|
|
|
|
|
} else if (codepoint <= 0x7FF) {
|
|
|
|
|
p[0] = 0xC0 | (Uint8)((codepoint >> 6) & 0x1F);
|
|
|
|
|
p[1] = 0x80 | (Uint8)(codepoint & 0x3F);
|
|
|
|
|
dst += 2;
|
|
|
|
|
} else if (codepoint <= 0xFFFF) {
|
|
|
|
|
p[0] = 0xE0 | (Uint8)((codepoint >> 12) & 0x0F);
|
|
|
|
|
p[1] = 0x80 | (Uint8)((codepoint >> 6) & 0x3F);
|
|
|
|
|
p[2] = 0x80 | (Uint8)(codepoint & 0x3F);
|
|
|
|
|
dst += 3;
|
|
|
|
|
} else {
|
|
|
|
|
SDL_assert(codepoint <= 0x10FFFF);
|
|
|
|
|
p[0] = 0xF0 | (Uint8)((codepoint >> 18) & 0x07);
|
|
|
|
|
p[1] = 0x80 | (Uint8)((codepoint >> 12) & 0x3F);
|
|
|
|
|
p[2] = 0x80 | (Uint8)((codepoint >> 6) & 0x3F);
|
|
|
|
|
p[3] = 0x80 | (Uint8)(codepoint & 0x3F);
|
|
|
|
|
dst += 4;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return dst;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
stdlib: Improve Unicode support and consistency in string comparison functions.
SDL_strcasecmp (even when calling into a C runtime) does not work with
Unicode chars, and depending on the user's locale, might not work with
even basic ASCII strings.
This implements the function from scratch, using "case-folding,"
which is a more robust method that deals with various languages. It
involves a hashtable of a few hundred codepoints that are "uppercase" and
how to map them to lowercase equivalents (possibly increasing the size of
the string in the process). The vast majority of human languages (and
Unicode) do not have letters with different cases, but still, this static
table takes about 10 kilobytes on a 64-bit machine.
Even this will fail in one known case: the Turkish 'i' folds differently
if you're writing in Turkish vs other languages. Generally this is seen as
unfortunate collateral damage in cases where you can't specify the language
in use.
In addition to case-folding the codepoints, the new functions also know how
to decode the various formats to turn them into codepoints in the first
place, instead of blindly stepping by one byte (or one wchar_t) per
character.
Also included is casefolding.txt from the Unicode Consortium and a perl
script to generate the hashtable from that text file, so we can trivially
update this if new languages are added in the future.
A simple test using the new function:
```c
#include <SDL3/SDL.h>
int main(void)
{
const char *a = "α ε η";
const char *b = "Α Ε Η";
SDL_Log(" strcasecmp(\"%s\", \"%s\") == %d\n", a, b, strcasecmp(a, b));
SDL_Log("SDL_strcasecmp(\"%s\", \"%s\") == %d\n", a, b, SDL_strcasecmp(a, b));
return 0;
}
```
Produces:
```
INFO: strcasecmp("α ε η", "Α Ε Η") == 32
INFO: SDL_strcasecmp("α ε η", "Α Ε Η") == 0
```
glibc strcasecmp() fails to compare a Greek lowercase string to its uppercase
equivalent, even with a UTF-8 locale, but SDL_strcasecmp() works.
Other SDL_stdinc.h functions are changed to be more consistent, which is to
say they now ignore any C runtime and often dictate that only English-based
low-ASCII works with them.
Fixes Issue #9313.
2024-03-26 13:22:38 -04:00
|
|
|
// this expects `from` and `to` to be UTF-32 encoding!
|
2024-04-04 00:56:07 -04:00
|
|
|
int SDL_CaseFoldUnicode(const Uint32 from, Uint32 *to)
|
stdlib: Improve Unicode support and consistency in string comparison functions.
SDL_strcasecmp (even when calling into a C runtime) does not work with
Unicode chars, and depending on the user's locale, might not work with
even basic ASCII strings.
This implements the function from scratch, using "case-folding,"
which is a more robust method that deals with various languages. It
involves a hashtable of a few hundred codepoints that are "uppercase" and
how to map them to lowercase equivalents (possibly increasing the size of
the string in the process). The vast majority of human languages (and
Unicode) do not have letters with different cases, but still, this static
table takes about 10 kilobytes on a 64-bit machine.
Even this will fail in one known case: the Turkish 'i' folds differently
if you're writing in Turkish vs other languages. Generally this is seen as
unfortunate collateral damage in cases where you can't specify the language
in use.
In addition to case-folding the codepoints, the new functions also know how
to decode the various formats to turn them into codepoints in the first
place, instead of blindly stepping by one byte (or one wchar_t) per
character.
Also included is casefolding.txt from the Unicode Consortium and a perl
script to generate the hashtable from that text file, so we can trivially
update this if new languages are added in the future.
A simple test using the new function:
```c
#include <SDL3/SDL.h>
int main(void)
{
const char *a = "α ε η";
const char *b = "Α Ε Η";
SDL_Log(" strcasecmp(\"%s\", \"%s\") == %d\n", a, b, strcasecmp(a, b));
SDL_Log("SDL_strcasecmp(\"%s\", \"%s\") == %d\n", a, b, SDL_strcasecmp(a, b));
return 0;
}
```
Produces:
```
INFO: strcasecmp("α ε η", "Α Ε Η") == 32
INFO: SDL_strcasecmp("α ε η", "Α Ε Η") == 0
```
glibc strcasecmp() fails to compare a Greek lowercase string to its uppercase
equivalent, even with a UTF-8 locale, but SDL_strcasecmp() works.
Other SDL_stdinc.h functions are changed to be more consistent, which is to
say they now ignore any C runtime and often dictate that only English-based
low-ASCII works with them.
Fixes Issue #9313.
2024-03-26 13:22:38 -04:00
|
|
|
{
|
|
|
|
|
// !!! FIXME: since the hashtable is static, maybe we should binary
|
|
|
|
|
// !!! FIXME: search it instead of walking the whole bucket.
|
|
|
|
|
|
|
|
|
|
if (from < 128) { // low-ASCII, easy!
|
|
|
|
|
if ((from >= 'A') && (from <= 'Z')) {
|
|
|
|
|
*to = 'a' + (from - 'A');
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
} else if (from <= 0xFFFF) { // the Basic Multilingual Plane.
|
|
|
|
|
const Uint8 hash = ((from ^ (from >> 8)) & 0xFF);
|
|
|
|
|
const Uint16 from16 = (Uint16) from;
|
|
|
|
|
|
|
|
|
|
// see if it maps to a single char (most common)...
|
|
|
|
|
{
|
|
|
|
|
const CaseFoldHashBucket1_16 *bucket = &case_fold_hash1_16[hash];
|
|
|
|
|
const int count = (int) bucket->count;
|
|
|
|
|
for (int i = 0; i < count; i++) {
|
|
|
|
|
const CaseFoldMapping1_16 *mapping = &bucket->list[i];
|
|
|
|
|
if (mapping->from == from16) {
|
|
|
|
|
*to = mapping->to0;
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// see if it folds down to two chars...
|
|
|
|
|
{
|
|
|
|
|
const CaseFoldHashBucket2_16 *bucket = &case_fold_hash2_16[hash & 15];
|
|
|
|
|
const int count = (int) bucket->count;
|
|
|
|
|
for (int i = 0; i < count; i++) {
|
|
|
|
|
const CaseFoldMapping2_16 *mapping = &bucket->list[i];
|
|
|
|
|
if (mapping->from == from16) {
|
|
|
|
|
to[0] = mapping->to0;
|
|
|
|
|
to[1] = mapping->to1;
|
|
|
|
|
return 2;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// okay, maybe it's _three_ characters!
|
|
|
|
|
{
|
|
|
|
|
const CaseFoldHashBucket3_16 *bucket = &case_fold_hash3_16[hash & 3];
|
|
|
|
|
const int count = (int) bucket->count;
|
|
|
|
|
for (int i = 0; i < count; i++) {
|
|
|
|
|
const CaseFoldMapping3_16 *mapping = &bucket->list[i];
|
|
|
|
|
if (mapping->from == from16) {
|
|
|
|
|
to[0] = mapping->to0;
|
|
|
|
|
to[1] = mapping->to1;
|
|
|
|
|
to[2] = mapping->to2;
|
|
|
|
|
return 3;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} else { // codepoint that doesn't fit in 16 bits.
|
|
|
|
|
const Uint8 hash = ((from ^ (from >> 8)) & 0xFF);
|
|
|
|
|
const CaseFoldHashBucket1_32 *bucket = &case_fold_hash1_32[hash & 15];
|
|
|
|
|
const int count = (int) bucket->count;
|
|
|
|
|
for (int i = 0; i < count; i++) {
|
|
|
|
|
const CaseFoldMapping1_32 *mapping = &bucket->list[i];
|
|
|
|
|
if (mapping->from == from) {
|
|
|
|
|
*to = mapping->to0;
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Not found...there's no folding needed for this codepoint.
|
|
|
|
|
*to = from;
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#define UNICODE_STRCASECMP(bits, slen1, slen2, update_slen1, update_slen2) \
|
|
|
|
|
Uint32 folded1[3], folded2[3]; \
|
|
|
|
|
int head1 = 0, tail1 = 0, head2 = 0, tail2 = 0; \
|
2024-08-22 09:21:26 -07:00
|
|
|
while (true) { \
|
stdlib: Improve Unicode support and consistency in string comparison functions.
SDL_strcasecmp (even when calling into a C runtime) does not work with
Unicode chars, and depending on the user's locale, might not work with
even basic ASCII strings.
This implements the function from scratch, using "case-folding,"
which is a more robust method that deals with various languages. It
involves a hashtable of a few hundred codepoints that are "uppercase" and
how to map them to lowercase equivalents (possibly increasing the size of
the string in the process). The vast majority of human languages (and
Unicode) do not have letters with different cases, but still, this static
table takes about 10 kilobytes on a 64-bit machine.
Even this will fail in one known case: the Turkish 'i' folds differently
if you're writing in Turkish vs other languages. Generally this is seen as
unfortunate collateral damage in cases where you can't specify the language
in use.
In addition to case-folding the codepoints, the new functions also know how
to decode the various formats to turn them into codepoints in the first
place, instead of blindly stepping by one byte (or one wchar_t) per
character.
Also included is casefolding.txt from the Unicode Consortium and a perl
script to generate the hashtable from that text file, so we can trivially
update this if new languages are added in the future.
A simple test using the new function:
```c
#include <SDL3/SDL.h>
int main(void)
{
const char *a = "α ε η";
const char *b = "Α Ε Η";
SDL_Log(" strcasecmp(\"%s\", \"%s\") == %d\n", a, b, strcasecmp(a, b));
SDL_Log("SDL_strcasecmp(\"%s\", \"%s\") == %d\n", a, b, SDL_strcasecmp(a, b));
return 0;
}
```
Produces:
```
INFO: strcasecmp("α ε η", "Α Ε Η") == 32
INFO: SDL_strcasecmp("α ε η", "Α Ε Η") == 0
```
glibc strcasecmp() fails to compare a Greek lowercase string to its uppercase
equivalent, even with a UTF-8 locale, but SDL_strcasecmp() works.
Other SDL_stdinc.h functions are changed to be more consistent, which is to
say they now ignore any C runtime and often dictate that only English-based
low-ASCII works with them.
Fixes Issue #9313.
2024-03-26 13:22:38 -04:00
|
|
|
Uint32 cp1, cp2; \
|
|
|
|
|
if (head1 != tail1) { \
|
|
|
|
|
cp1 = folded1[tail1++]; \
|
|
|
|
|
} else { \
|
|
|
|
|
const Uint##bits *str1start = (const Uint##bits *) str1; \
|
2024-06-26 21:32:45 -04:00
|
|
|
head1 = SDL_CaseFoldUnicode(StepUTF##bits(&str1, slen1), folded1); \
|
stdlib: Improve Unicode support and consistency in string comparison functions.
SDL_strcasecmp (even when calling into a C runtime) does not work with
Unicode chars, and depending on the user's locale, might not work with
even basic ASCII strings.
This implements the function from scratch, using "case-folding,"
which is a more robust method that deals with various languages. It
involves a hashtable of a few hundred codepoints that are "uppercase" and
how to map them to lowercase equivalents (possibly increasing the size of
the string in the process). The vast majority of human languages (and
Unicode) do not have letters with different cases, but still, this static
table takes about 10 kilobytes on a 64-bit machine.
Even this will fail in one known case: the Turkish 'i' folds differently
if you're writing in Turkish vs other languages. Generally this is seen as
unfortunate collateral damage in cases where you can't specify the language
in use.
In addition to case-folding the codepoints, the new functions also know how
to decode the various formats to turn them into codepoints in the first
place, instead of blindly stepping by one byte (or one wchar_t) per
character.
Also included is casefolding.txt from the Unicode Consortium and a perl
script to generate the hashtable from that text file, so we can trivially
update this if new languages are added in the future.
A simple test using the new function:
```c
#include <SDL3/SDL.h>
int main(void)
{
const char *a = "α ε η";
const char *b = "Α Ε Η";
SDL_Log(" strcasecmp(\"%s\", \"%s\") == %d\n", a, b, strcasecmp(a, b));
SDL_Log("SDL_strcasecmp(\"%s\", \"%s\") == %d\n", a, b, SDL_strcasecmp(a, b));
return 0;
}
```
Produces:
```
INFO: strcasecmp("α ε η", "Α Ε Η") == 32
INFO: SDL_strcasecmp("α ε η", "Α Ε Η") == 0
```
glibc strcasecmp() fails to compare a Greek lowercase string to its uppercase
equivalent, even with a UTF-8 locale, but SDL_strcasecmp() works.
Other SDL_stdinc.h functions are changed to be more consistent, which is to
say they now ignore any C runtime and often dictate that only English-based
low-ASCII works with them.
Fixes Issue #9313.
2024-03-26 13:22:38 -04:00
|
|
|
update_slen1; \
|
|
|
|
|
cp1 = folded1[0]; \
|
|
|
|
|
tail1 = 1; \
|
|
|
|
|
} \
|
|
|
|
|
if (head2 != tail2) { \
|
|
|
|
|
cp2 = folded2[tail2++]; \
|
|
|
|
|
} else { \
|
|
|
|
|
const Uint##bits *str2start = (const Uint##bits *) str2; \
|
2024-06-26 21:32:45 -04:00
|
|
|
head2 = SDL_CaseFoldUnicode(StepUTF##bits(&str2, slen2), folded2); \
|
stdlib: Improve Unicode support and consistency in string comparison functions.
SDL_strcasecmp (even when calling into a C runtime) does not work with
Unicode chars, and depending on the user's locale, might not work with
even basic ASCII strings.
This implements the function from scratch, using "case-folding,"
which is a more robust method that deals with various languages. It
involves a hashtable of a few hundred codepoints that are "uppercase" and
how to map them to lowercase equivalents (possibly increasing the size of
the string in the process). The vast majority of human languages (and
Unicode) do not have letters with different cases, but still, this static
table takes about 10 kilobytes on a 64-bit machine.
Even this will fail in one known case: the Turkish 'i' folds differently
if you're writing in Turkish vs other languages. Generally this is seen as
unfortunate collateral damage in cases where you can't specify the language
in use.
In addition to case-folding the codepoints, the new functions also know how
to decode the various formats to turn them into codepoints in the first
place, instead of blindly stepping by one byte (or one wchar_t) per
character.
Also included is casefolding.txt from the Unicode Consortium and a perl
script to generate the hashtable from that text file, so we can trivially
update this if new languages are added in the future.
A simple test using the new function:
```c
#include <SDL3/SDL.h>
int main(void)
{
const char *a = "α ε η";
const char *b = "Α Ε Η";
SDL_Log(" strcasecmp(\"%s\", \"%s\") == %d\n", a, b, strcasecmp(a, b));
SDL_Log("SDL_strcasecmp(\"%s\", \"%s\") == %d\n", a, b, SDL_strcasecmp(a, b));
return 0;
}
```
Produces:
```
INFO: strcasecmp("α ε η", "Α Ε Η") == 32
INFO: SDL_strcasecmp("α ε η", "Α Ε Η") == 0
```
glibc strcasecmp() fails to compare a Greek lowercase string to its uppercase
equivalent, even with a UTF-8 locale, but SDL_strcasecmp() works.
Other SDL_stdinc.h functions are changed to be more consistent, which is to
say they now ignore any C runtime and often dictate that only English-based
low-ASCII works with them.
Fixes Issue #9313.
2024-03-26 13:22:38 -04:00
|
|
|
update_slen2; \
|
|
|
|
|
cp2 = folded2[0]; \
|
|
|
|
|
tail2 = 1; \
|
|
|
|
|
} \
|
|
|
|
|
if (cp1 < cp2) { \
|
|
|
|
|
return -1; \
|
|
|
|
|
} else if (cp1 > cp2) { \
|
|
|
|
|
return 1; \
|
|
|
|
|
} else if (cp1 == 0) { \
|
|
|
|
|
break; /* complete match. */ \
|
|
|
|
|
} \
|
|
|
|
|
} \
|
|
|
|
|
return 0
|
|
|
|
|
|
|
|
|
|
|
2024-06-26 21:32:45 -04:00
|
|
|
static Uint32 StepUTF8(const char **_str, const size_t slen)
|
stdlib: Improve Unicode support and consistency in string comparison functions.
SDL_strcasecmp (even when calling into a C runtime) does not work with
Unicode chars, and depending on the user's locale, might not work with
even basic ASCII strings.
This implements the function from scratch, using "case-folding,"
which is a more robust method that deals with various languages. It
involves a hashtable of a few hundred codepoints that are "uppercase" and
how to map them to lowercase equivalents (possibly increasing the size of
the string in the process). The vast majority of human languages (and
Unicode) do not have letters with different cases, but still, this static
table takes about 10 kilobytes on a 64-bit machine.
Even this will fail in one known case: the Turkish 'i' folds differently
if you're writing in Turkish vs other languages. Generally this is seen as
unfortunate collateral damage in cases where you can't specify the language
in use.
In addition to case-folding the codepoints, the new functions also know how
to decode the various formats to turn them into codepoints in the first
place, instead of blindly stepping by one byte (or one wchar_t) per
character.
Also included is casefolding.txt from the Unicode Consortium and a perl
script to generate the hashtable from that text file, so we can trivially
update this if new languages are added in the future.
A simple test using the new function:
```c
#include <SDL3/SDL.h>
int main(void)
{
const char *a = "α ε η";
const char *b = "Α Ε Η";
SDL_Log(" strcasecmp(\"%s\", \"%s\") == %d\n", a, b, strcasecmp(a, b));
SDL_Log("SDL_strcasecmp(\"%s\", \"%s\") == %d\n", a, b, SDL_strcasecmp(a, b));
return 0;
}
```
Produces:
```
INFO: strcasecmp("α ε η", "Α Ε Η") == 32
INFO: SDL_strcasecmp("α ε η", "Α Ε Η") == 0
```
glibc strcasecmp() fails to compare a Greek lowercase string to its uppercase
equivalent, even with a UTF-8 locale, but SDL_strcasecmp() works.
Other SDL_stdinc.h functions are changed to be more consistent, which is to
say they now ignore any C runtime and often dictate that only English-based
low-ASCII works with them.
Fixes Issue #9313.
2024-03-26 13:22:38 -04:00
|
|
|
{
|
2024-06-26 21:32:45 -04:00
|
|
|
/*
|
|
|
|
|
* From rfc3629, the UTF-8 spec:
|
|
|
|
|
* https://www.ietf.org/rfc/rfc3629.txt
|
|
|
|
|
*
|
|
|
|
|
* Char. number range | UTF-8 octet sequence
|
|
|
|
|
* (hexadecimal) | (binary)
|
|
|
|
|
* --------------------+---------------------------------------------
|
|
|
|
|
* 0000 0000-0000 007F | 0xxxxxxx
|
|
|
|
|
* 0000 0080-0000 07FF | 110xxxxx 10xxxxxx
|
|
|
|
|
* 0000 0800-0000 FFFF | 1110xxxx 10xxxxxx 10xxxxxx
|
|
|
|
|
* 0001 0000-0010 FFFF | 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
const Uint8 *str = (const Uint8 *) *_str;
|
|
|
|
|
const Uint32 octet = (Uint32) (slen ? *str : 0);
|
stdlib: Improve Unicode support and consistency in string comparison functions.
SDL_strcasecmp (even when calling into a C runtime) does not work with
Unicode chars, and depending on the user's locale, might not work with
even basic ASCII strings.
This implements the function from scratch, using "case-folding,"
which is a more robust method that deals with various languages. It
involves a hashtable of a few hundred codepoints that are "uppercase" and
how to map them to lowercase equivalents (possibly increasing the size of
the string in the process). The vast majority of human languages (and
Unicode) do not have letters with different cases, but still, this static
table takes about 10 kilobytes on a 64-bit machine.
Even this will fail in one known case: the Turkish 'i' folds differently
if you're writing in Turkish vs other languages. Generally this is seen as
unfortunate collateral damage in cases where you can't specify the language
in use.
In addition to case-folding the codepoints, the new functions also know how
to decode the various formats to turn them into codepoints in the first
place, instead of blindly stepping by one byte (or one wchar_t) per
character.
Also included is casefolding.txt from the Unicode Consortium and a perl
script to generate the hashtable from that text file, so we can trivially
update this if new languages are added in the future.
A simple test using the new function:
```c
#include <SDL3/SDL.h>
int main(void)
{
const char *a = "α ε η";
const char *b = "Α Ε Η";
SDL_Log(" strcasecmp(\"%s\", \"%s\") == %d\n", a, b, strcasecmp(a, b));
SDL_Log("SDL_strcasecmp(\"%s\", \"%s\") == %d\n", a, b, SDL_strcasecmp(a, b));
return 0;
}
```
Produces:
```
INFO: strcasecmp("α ε η", "Α Ε Η") == 32
INFO: SDL_strcasecmp("α ε η", "Α Ε Η") == 0
```
glibc strcasecmp() fails to compare a Greek lowercase string to its uppercase
equivalent, even with a UTF-8 locale, but SDL_strcasecmp() works.
Other SDL_stdinc.h functions are changed to be more consistent, which is to
say they now ignore any C runtime and often dictate that only English-based
low-ASCII works with them.
Fixes Issue #9313.
2024-03-26 13:22:38 -04:00
|
|
|
|
|
|
|
|
if (octet == 0) { // null terminator, end of string.
|
|
|
|
|
return 0; // don't advance `*_str`.
|
|
|
|
|
} else if ((octet & 0x80) == 0) { // 0xxxxxxx: one byte codepoint.
|
|
|
|
|
(*_str)++;
|
|
|
|
|
return octet;
|
|
|
|
|
} else if (((octet & 0xE0) == 0xC0) && (slen >= 2)) { // 110xxxxx 10xxxxxx: two byte codepoint.
|
2024-06-26 21:32:45 -04:00
|
|
|
const Uint8 str1 = str[1];
|
|
|
|
|
if ((str1 & 0xC0) == 0x80) { // If trailing bytes aren't 10xxxxxx, sequence is bogus.
|
2024-08-22 17:33:49 -07:00
|
|
|
const Uint32 result = ((octet & 0x1F) << 6) | (str1 & 0x3F);
|
|
|
|
|
if (result >= 0x0080) { // rfc3629 says you can't use overlong sequences for smaller values.
|
2024-06-26 21:32:45 -04:00
|
|
|
*_str += 2;
|
2024-08-22 17:33:49 -07:00
|
|
|
return result;
|
2024-06-26 21:32:45 -04:00
|
|
|
}
|
stdlib: Improve Unicode support and consistency in string comparison functions.
SDL_strcasecmp (even when calling into a C runtime) does not work with
Unicode chars, and depending on the user's locale, might not work with
even basic ASCII strings.
This implements the function from scratch, using "case-folding,"
which is a more robust method that deals with various languages. It
involves a hashtable of a few hundred codepoints that are "uppercase" and
how to map them to lowercase equivalents (possibly increasing the size of
the string in the process). The vast majority of human languages (and
Unicode) do not have letters with different cases, but still, this static
table takes about 10 kilobytes on a 64-bit machine.
Even this will fail in one known case: the Turkish 'i' folds differently
if you're writing in Turkish vs other languages. Generally this is seen as
unfortunate collateral damage in cases where you can't specify the language
in use.
In addition to case-folding the codepoints, the new functions also know how
to decode the various formats to turn them into codepoints in the first
place, instead of blindly stepping by one byte (or one wchar_t) per
character.
Also included is casefolding.txt from the Unicode Consortium and a perl
script to generate the hashtable from that text file, so we can trivially
update this if new languages are added in the future.
A simple test using the new function:
```c
#include <SDL3/SDL.h>
int main(void)
{
const char *a = "α ε η";
const char *b = "Α Ε Η";
SDL_Log(" strcasecmp(\"%s\", \"%s\") == %d\n", a, b, strcasecmp(a, b));
SDL_Log("SDL_strcasecmp(\"%s\", \"%s\") == %d\n", a, b, SDL_strcasecmp(a, b));
return 0;
}
```
Produces:
```
INFO: strcasecmp("α ε η", "Α Ε Η") == 32
INFO: SDL_strcasecmp("α ε η", "Α Ε Η") == 0
```
glibc strcasecmp() fails to compare a Greek lowercase string to its uppercase
equivalent, even with a UTF-8 locale, but SDL_strcasecmp() works.
Other SDL_stdinc.h functions are changed to be more consistent, which is to
say they now ignore any C runtime and often dictate that only English-based
low-ASCII works with them.
Fixes Issue #9313.
2024-03-26 13:22:38 -04:00
|
|
|
}
|
|
|
|
|
} else if (((octet & 0xF0) == 0xE0) && (slen >= 3)) { // 1110xxxx 10xxxxxx 10xxxxxx: three byte codepoint.
|
2024-06-26 21:32:45 -04:00
|
|
|
const Uint8 str1 = str[1];
|
|
|
|
|
const Uint8 str2 = str[2];
|
|
|
|
|
if (((str1 & 0xC0) == 0x80) && ((str2 & 0xC0) == 0x80)) { // If trailing bytes aren't 10xxxxxx, sequence is bogus.
|
|
|
|
|
const Uint32 octet2 = ((Uint32) (str1 & 0x3F)) << 6;
|
|
|
|
|
const Uint32 octet3 = ((Uint32) (str2 & 0x3F));
|
2024-08-22 17:33:49 -07:00
|
|
|
const Uint32 result = ((octet & 0x0F) << 12) | octet2 | octet3;
|
|
|
|
|
if (result >= 0x800) { // rfc3629 says you can't use overlong sequences for smaller values.
|
|
|
|
|
if ((result < 0xD800) || (result > 0xDFFF)) { // UTF-16 surrogate values are illegal in UTF-8.
|
2024-06-26 21:32:45 -04:00
|
|
|
*_str += 3;
|
2024-08-22 17:33:49 -07:00
|
|
|
return result;
|
2024-06-26 21:32:45 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
stdlib: Improve Unicode support and consistency in string comparison functions.
SDL_strcasecmp (even when calling into a C runtime) does not work with
Unicode chars, and depending on the user's locale, might not work with
even basic ASCII strings.
This implements the function from scratch, using "case-folding,"
which is a more robust method that deals with various languages. It
involves a hashtable of a few hundred codepoints that are "uppercase" and
how to map them to lowercase equivalents (possibly increasing the size of
the string in the process). The vast majority of human languages (and
Unicode) do not have letters with different cases, but still, this static
table takes about 10 kilobytes on a 64-bit machine.
Even this will fail in one known case: the Turkish 'i' folds differently
if you're writing in Turkish vs other languages. Generally this is seen as
unfortunate collateral damage in cases where you can't specify the language
in use.
In addition to case-folding the codepoints, the new functions also know how
to decode the various formats to turn them into codepoints in the first
place, instead of blindly stepping by one byte (or one wchar_t) per
character.
Also included is casefolding.txt from the Unicode Consortium and a perl
script to generate the hashtable from that text file, so we can trivially
update this if new languages are added in the future.
A simple test using the new function:
```c
#include <SDL3/SDL.h>
int main(void)
{
const char *a = "α ε η";
const char *b = "Α Ε Η";
SDL_Log(" strcasecmp(\"%s\", \"%s\") == %d\n", a, b, strcasecmp(a, b));
SDL_Log("SDL_strcasecmp(\"%s\", \"%s\") == %d\n", a, b, SDL_strcasecmp(a, b));
return 0;
}
```
Produces:
```
INFO: strcasecmp("α ε η", "Α Ε Η") == 32
INFO: SDL_strcasecmp("α ε η", "Α Ε Η") == 0
```
glibc strcasecmp() fails to compare a Greek lowercase string to its uppercase
equivalent, even with a UTF-8 locale, but SDL_strcasecmp() works.
Other SDL_stdinc.h functions are changed to be more consistent, which is to
say they now ignore any C runtime and often dictate that only English-based
low-ASCII works with them.
Fixes Issue #9313.
2024-03-26 13:22:38 -04:00
|
|
|
} else if (((octet & 0xF8) == 0xF0) && (slen >= 4)) { // 11110xxxx 10xxxxxx 10xxxxxx 10xxxxxx: four byte codepoint.
|
2024-06-26 21:32:45 -04:00
|
|
|
const Uint8 str1 = str[1];
|
|
|
|
|
const Uint8 str2 = str[2];
|
|
|
|
|
const Uint8 str3 = str[3];
|
|
|
|
|
if (((str1 & 0xC0) == 0x80) && ((str2 & 0xC0) == 0x80) && ((str3 & 0xC0) == 0x80)) { // If trailing bytes aren't 10xxxxxx, sequence is bogus.
|
|
|
|
|
const Uint32 octet2 = ((Uint32) (str1 & 0x1F)) << 12;
|
|
|
|
|
const Uint32 octet3 = ((Uint32) (str2 & 0x3F)) << 6;
|
|
|
|
|
const Uint32 octet4 = ((Uint32) (str3 & 0x3F));
|
2024-08-22 17:33:49 -07:00
|
|
|
const Uint32 result = ((octet & 0x07) << 18) | octet2 | octet3 | octet4;
|
|
|
|
|
if (result >= 0x10000) { // rfc3629 says you can't use overlong sequences for smaller values.
|
2024-06-26 21:32:45 -04:00
|
|
|
*_str += 4;
|
2024-08-22 17:33:49 -07:00
|
|
|
return result;
|
2024-06-26 21:32:45 -04:00
|
|
|
}
|
|
|
|
|
}
|
stdlib: Improve Unicode support and consistency in string comparison functions.
SDL_strcasecmp (even when calling into a C runtime) does not work with
Unicode chars, and depending on the user's locale, might not work with
even basic ASCII strings.
This implements the function from scratch, using "case-folding,"
which is a more robust method that deals with various languages. It
involves a hashtable of a few hundred codepoints that are "uppercase" and
how to map them to lowercase equivalents (possibly increasing the size of
the string in the process). The vast majority of human languages (and
Unicode) do not have letters with different cases, but still, this static
table takes about 10 kilobytes on a 64-bit machine.
Even this will fail in one known case: the Turkish 'i' folds differently
if you're writing in Turkish vs other languages. Generally this is seen as
unfortunate collateral damage in cases where you can't specify the language
in use.
In addition to case-folding the codepoints, the new functions also know how
to decode the various formats to turn them into codepoints in the first
place, instead of blindly stepping by one byte (or one wchar_t) per
character.
Also included is casefolding.txt from the Unicode Consortium and a perl
script to generate the hashtable from that text file, so we can trivially
update this if new languages are added in the future.
A simple test using the new function:
```c
#include <SDL3/SDL.h>
int main(void)
{
const char *a = "α ε η";
const char *b = "Α Ε Η";
SDL_Log(" strcasecmp(\"%s\", \"%s\") == %d\n", a, b, strcasecmp(a, b));
SDL_Log("SDL_strcasecmp(\"%s\", \"%s\") == %d\n", a, b, SDL_strcasecmp(a, b));
return 0;
}
```
Produces:
```
INFO: strcasecmp("α ε η", "Α Ε Η") == 32
INFO: SDL_strcasecmp("α ε η", "Α Ε Η") == 0
```
glibc strcasecmp() fails to compare a Greek lowercase string to its uppercase
equivalent, even with a UTF-8 locale, but SDL_strcasecmp() works.
Other SDL_stdinc.h functions are changed to be more consistent, which is to
say they now ignore any C runtime and often dictate that only English-based
low-ASCII works with them.
Fixes Issue #9313.
2024-03-26 13:22:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// bogus byte, skip ahead, return a REPLACEMENT CHARACTER.
|
|
|
|
|
(*_str)++;
|
2024-06-26 21:32:45 -04:00
|
|
|
return SDL_INVALID_UNICODE_CODEPOINT;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Uint32 SDL_StepUTF8(const char **pstr, size_t *pslen)
|
|
|
|
|
{
|
|
|
|
|
if (!pslen) {
|
|
|
|
|
return StepUTF8(pstr, 4); // 4 == max codepoint size.
|
|
|
|
|
}
|
|
|
|
|
const char *origstr = *pstr;
|
2024-08-22 17:33:49 -07:00
|
|
|
const Uint32 result = StepUTF8(pstr, *pslen);
|
2024-06-26 21:32:45 -04:00
|
|
|
*pslen -= (size_t) (*pstr - origstr);
|
2024-08-22 17:33:49 -07:00
|
|
|
return result;
|
stdlib: Improve Unicode support and consistency in string comparison functions.
SDL_strcasecmp (even when calling into a C runtime) does not work with
Unicode chars, and depending on the user's locale, might not work with
even basic ASCII strings.
This implements the function from scratch, using "case-folding,"
which is a more robust method that deals with various languages. It
involves a hashtable of a few hundred codepoints that are "uppercase" and
how to map them to lowercase equivalents (possibly increasing the size of
the string in the process). The vast majority of human languages (and
Unicode) do not have letters with different cases, but still, this static
table takes about 10 kilobytes on a 64-bit machine.
Even this will fail in one known case: the Turkish 'i' folds differently
if you're writing in Turkish vs other languages. Generally this is seen as
unfortunate collateral damage in cases where you can't specify the language
in use.
In addition to case-folding the codepoints, the new functions also know how
to decode the various formats to turn them into codepoints in the first
place, instead of blindly stepping by one byte (or one wchar_t) per
character.
Also included is casefolding.txt from the Unicode Consortium and a perl
script to generate the hashtable from that text file, so we can trivially
update this if new languages are added in the future.
A simple test using the new function:
```c
#include <SDL3/SDL.h>
int main(void)
{
const char *a = "α ε η";
const char *b = "Α Ε Η";
SDL_Log(" strcasecmp(\"%s\", \"%s\") == %d\n", a, b, strcasecmp(a, b));
SDL_Log("SDL_strcasecmp(\"%s\", \"%s\") == %d\n", a, b, SDL_strcasecmp(a, b));
return 0;
}
```
Produces:
```
INFO: strcasecmp("α ε η", "Α Ε Η") == 32
INFO: SDL_strcasecmp("α ε η", "Α Ε Η") == 0
```
glibc strcasecmp() fails to compare a Greek lowercase string to its uppercase
equivalent, even with a UTF-8 locale, but SDL_strcasecmp() works.
Other SDL_stdinc.h functions are changed to be more consistent, which is to
say they now ignore any C runtime and often dictate that only English-based
low-ASCII works with them.
Fixes Issue #9313.
2024-03-26 13:22:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#if (SDL_SIZEOF_WCHAR_T == 2)
|
2024-06-26 21:32:45 -04:00
|
|
|
static Uint32 StepUTF16(const Uint16 **_str, const size_t slen)
|
stdlib: Improve Unicode support and consistency in string comparison functions.
SDL_strcasecmp (even when calling into a C runtime) does not work with
Unicode chars, and depending on the user's locale, might not work with
even basic ASCII strings.
This implements the function from scratch, using "case-folding,"
which is a more robust method that deals with various languages. It
involves a hashtable of a few hundred codepoints that are "uppercase" and
how to map them to lowercase equivalents (possibly increasing the size of
the string in the process). The vast majority of human languages (and
Unicode) do not have letters with different cases, but still, this static
table takes about 10 kilobytes on a 64-bit machine.
Even this will fail in one known case: the Turkish 'i' folds differently
if you're writing in Turkish vs other languages. Generally this is seen as
unfortunate collateral damage in cases where you can't specify the language
in use.
In addition to case-folding the codepoints, the new functions also know how
to decode the various formats to turn them into codepoints in the first
place, instead of blindly stepping by one byte (or one wchar_t) per
character.
Also included is casefolding.txt from the Unicode Consortium and a perl
script to generate the hashtable from that text file, so we can trivially
update this if new languages are added in the future.
A simple test using the new function:
```c
#include <SDL3/SDL.h>
int main(void)
{
const char *a = "α ε η";
const char *b = "Α Ε Η";
SDL_Log(" strcasecmp(\"%s\", \"%s\") == %d\n", a, b, strcasecmp(a, b));
SDL_Log("SDL_strcasecmp(\"%s\", \"%s\") == %d\n", a, b, SDL_strcasecmp(a, b));
return 0;
}
```
Produces:
```
INFO: strcasecmp("α ε η", "Α Ε Η") == 32
INFO: SDL_strcasecmp("α ε η", "Α Ε Η") == 0
```
glibc strcasecmp() fails to compare a Greek lowercase string to its uppercase
equivalent, even with a UTF-8 locale, but SDL_strcasecmp() works.
Other SDL_stdinc.h functions are changed to be more consistent, which is to
say they now ignore any C runtime and often dictate that only English-based
low-ASCII works with them.
Fixes Issue #9313.
2024-03-26 13:22:38 -04:00
|
|
|
{
|
|
|
|
|
const Uint16 *str = *_str;
|
|
|
|
|
Uint32 cp = (Uint32) *(str++);
|
|
|
|
|
if (cp == 0) {
|
|
|
|
|
return 0; // don't advance string pointer.
|
|
|
|
|
} else if ((cp >= 0xDC00) && (cp <= 0xDFFF)) {
|
2024-06-26 21:32:45 -04:00
|
|
|
cp = SDL_INVALID_UNICODE_CODEPOINT; // Orphaned second half of surrogate pair
|
stdlib: Improve Unicode support and consistency in string comparison functions.
SDL_strcasecmp (even when calling into a C runtime) does not work with
Unicode chars, and depending on the user's locale, might not work with
even basic ASCII strings.
This implements the function from scratch, using "case-folding,"
which is a more robust method that deals with various languages. It
involves a hashtable of a few hundred codepoints that are "uppercase" and
how to map them to lowercase equivalents (possibly increasing the size of
the string in the process). The vast majority of human languages (and
Unicode) do not have letters with different cases, but still, this static
table takes about 10 kilobytes on a 64-bit machine.
Even this will fail in one known case: the Turkish 'i' folds differently
if you're writing in Turkish vs other languages. Generally this is seen as
unfortunate collateral damage in cases where you can't specify the language
in use.
In addition to case-folding the codepoints, the new functions also know how
to decode the various formats to turn them into codepoints in the first
place, instead of blindly stepping by one byte (or one wchar_t) per
character.
Also included is casefolding.txt from the Unicode Consortium and a perl
script to generate the hashtable from that text file, so we can trivially
update this if new languages are added in the future.
A simple test using the new function:
```c
#include <SDL3/SDL.h>
int main(void)
{
const char *a = "α ε η";
const char *b = "Α Ε Η";
SDL_Log(" strcasecmp(\"%s\", \"%s\") == %d\n", a, b, strcasecmp(a, b));
SDL_Log("SDL_strcasecmp(\"%s\", \"%s\") == %d\n", a, b, SDL_strcasecmp(a, b));
return 0;
}
```
Produces:
```
INFO: strcasecmp("α ε η", "Α Ε Η") == 32
INFO: SDL_strcasecmp("α ε η", "Α Ε Η") == 0
```
glibc strcasecmp() fails to compare a Greek lowercase string to its uppercase
equivalent, even with a UTF-8 locale, but SDL_strcasecmp() works.
Other SDL_stdinc.h functions are changed to be more consistent, which is to
say they now ignore any C runtime and often dictate that only English-based
low-ASCII works with them.
Fixes Issue #9313.
2024-03-26 13:22:38 -04:00
|
|
|
} else if ((cp >= 0xD800) && (cp <= 0xDBFF)) { // start of surrogate pair!
|
|
|
|
|
const Uint32 pair = (Uint32) *str;
|
|
|
|
|
if ((pair == 0) || ((pair < 0xDC00) || (pair > 0xDFFF))) {
|
2024-06-26 21:32:45 -04:00
|
|
|
cp = SDL_INVALID_UNICODE_CODEPOINT;
|
stdlib: Improve Unicode support and consistency in string comparison functions.
SDL_strcasecmp (even when calling into a C runtime) does not work with
Unicode chars, and depending on the user's locale, might not work with
even basic ASCII strings.
This implements the function from scratch, using "case-folding,"
which is a more robust method that deals with various languages. It
involves a hashtable of a few hundred codepoints that are "uppercase" and
how to map them to lowercase equivalents (possibly increasing the size of
the string in the process). The vast majority of human languages (and
Unicode) do not have letters with different cases, but still, this static
table takes about 10 kilobytes on a 64-bit machine.
Even this will fail in one known case: the Turkish 'i' folds differently
if you're writing in Turkish vs other languages. Generally this is seen as
unfortunate collateral damage in cases where you can't specify the language
in use.
In addition to case-folding the codepoints, the new functions also know how
to decode the various formats to turn them into codepoints in the first
place, instead of blindly stepping by one byte (or one wchar_t) per
character.
Also included is casefolding.txt from the Unicode Consortium and a perl
script to generate the hashtable from that text file, so we can trivially
update this if new languages are added in the future.
A simple test using the new function:
```c
#include <SDL3/SDL.h>
int main(void)
{
const char *a = "α ε η";
const char *b = "Α Ε Η";
SDL_Log(" strcasecmp(\"%s\", \"%s\") == %d\n", a, b, strcasecmp(a, b));
SDL_Log("SDL_strcasecmp(\"%s\", \"%s\") == %d\n", a, b, SDL_strcasecmp(a, b));
return 0;
}
```
Produces:
```
INFO: strcasecmp("α ε η", "Α Ε Η") == 32
INFO: SDL_strcasecmp("α ε η", "Α Ε Η") == 0
```
glibc strcasecmp() fails to compare a Greek lowercase string to its uppercase
equivalent, even with a UTF-8 locale, but SDL_strcasecmp() works.
Other SDL_stdinc.h functions are changed to be more consistent, which is to
say they now ignore any C runtime and often dictate that only English-based
low-ASCII works with them.
Fixes Issue #9313.
2024-03-26 13:22:38 -04:00
|
|
|
} else {
|
|
|
|
|
str++; // eat the other surrogate.
|
|
|
|
|
cp = 0x10000 + (((cp - 0xD800) << 10) | (pair - 0xDC00));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
*_str = str;
|
2024-06-26 21:32:45 -04:00
|
|
|
return (cp > 0x10FFFF) ? SDL_INVALID_UNICODE_CODEPOINT : cp;
|
stdlib: Improve Unicode support and consistency in string comparison functions.
SDL_strcasecmp (even when calling into a C runtime) does not work with
Unicode chars, and depending on the user's locale, might not work with
even basic ASCII strings.
This implements the function from scratch, using "case-folding,"
which is a more robust method that deals with various languages. It
involves a hashtable of a few hundred codepoints that are "uppercase" and
how to map them to lowercase equivalents (possibly increasing the size of
the string in the process). The vast majority of human languages (and
Unicode) do not have letters with different cases, but still, this static
table takes about 10 kilobytes on a 64-bit machine.
Even this will fail in one known case: the Turkish 'i' folds differently
if you're writing in Turkish vs other languages. Generally this is seen as
unfortunate collateral damage in cases where you can't specify the language
in use.
In addition to case-folding the codepoints, the new functions also know how
to decode the various formats to turn them into codepoints in the first
place, instead of blindly stepping by one byte (or one wchar_t) per
character.
Also included is casefolding.txt from the Unicode Consortium and a perl
script to generate the hashtable from that text file, so we can trivially
update this if new languages are added in the future.
A simple test using the new function:
```c
#include <SDL3/SDL.h>
int main(void)
{
const char *a = "α ε η";
const char *b = "Α Ε Η";
SDL_Log(" strcasecmp(\"%s\", \"%s\") == %d\n", a, b, strcasecmp(a, b));
SDL_Log("SDL_strcasecmp(\"%s\", \"%s\") == %d\n", a, b, SDL_strcasecmp(a, b));
return 0;
}
```
Produces:
```
INFO: strcasecmp("α ε η", "Α Ε Η") == 32
INFO: SDL_strcasecmp("α ε η", "Α Ε Η") == 0
```
glibc strcasecmp() fails to compare a Greek lowercase string to its uppercase
equivalent, even with a UTF-8 locale, but SDL_strcasecmp() works.
Other SDL_stdinc.h functions are changed to be more consistent, which is to
say they now ignore any C runtime and often dictate that only English-based
low-ASCII works with them.
Fixes Issue #9313.
2024-03-26 13:22:38 -04:00
|
|
|
}
|
|
|
|
|
#elif (SDL_SIZEOF_WCHAR_T == 4)
|
2024-06-26 21:32:45 -04:00
|
|
|
static Uint32 StepUTF32(const Uint32 **_str, const size_t slen)
|
stdlib: Improve Unicode support and consistency in string comparison functions.
SDL_strcasecmp (even when calling into a C runtime) does not work with
Unicode chars, and depending on the user's locale, might not work with
even basic ASCII strings.
This implements the function from scratch, using "case-folding,"
which is a more robust method that deals with various languages. It
involves a hashtable of a few hundred codepoints that are "uppercase" and
how to map them to lowercase equivalents (possibly increasing the size of
the string in the process). The vast majority of human languages (and
Unicode) do not have letters with different cases, but still, this static
table takes about 10 kilobytes on a 64-bit machine.
Even this will fail in one known case: the Turkish 'i' folds differently
if you're writing in Turkish vs other languages. Generally this is seen as
unfortunate collateral damage in cases where you can't specify the language
in use.
In addition to case-folding the codepoints, the new functions also know how
to decode the various formats to turn them into codepoints in the first
place, instead of blindly stepping by one byte (or one wchar_t) per
character.
Also included is casefolding.txt from the Unicode Consortium and a perl
script to generate the hashtable from that text file, so we can trivially
update this if new languages are added in the future.
A simple test using the new function:
```c
#include <SDL3/SDL.h>
int main(void)
{
const char *a = "α ε η";
const char *b = "Α Ε Η";
SDL_Log(" strcasecmp(\"%s\", \"%s\") == %d\n", a, b, strcasecmp(a, b));
SDL_Log("SDL_strcasecmp(\"%s\", \"%s\") == %d\n", a, b, SDL_strcasecmp(a, b));
return 0;
}
```
Produces:
```
INFO: strcasecmp("α ε η", "Α Ε Η") == 32
INFO: SDL_strcasecmp("α ε η", "Α Ε Η") == 0
```
glibc strcasecmp() fails to compare a Greek lowercase string to its uppercase
equivalent, even with a UTF-8 locale, but SDL_strcasecmp() works.
Other SDL_stdinc.h functions are changed to be more consistent, which is to
say they now ignore any C runtime and often dictate that only English-based
low-ASCII works with them.
Fixes Issue #9313.
2024-03-26 13:22:38 -04:00
|
|
|
{
|
|
|
|
|
if (!slen) {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const Uint32 *str = *_str;
|
|
|
|
|
const Uint32 cp = *str;
|
|
|
|
|
if (cp == 0) {
|
|
|
|
|
return 0; // don't advance string pointer.
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
(*_str)++;
|
2024-06-26 21:32:45 -04:00
|
|
|
return (cp > 0x10FFFF) ? SDL_INVALID_UNICODE_CODEPOINT : cp;
|
stdlib: Improve Unicode support and consistency in string comparison functions.
SDL_strcasecmp (even when calling into a C runtime) does not work with
Unicode chars, and depending on the user's locale, might not work with
even basic ASCII strings.
This implements the function from scratch, using "case-folding,"
which is a more robust method that deals with various languages. It
involves a hashtable of a few hundred codepoints that are "uppercase" and
how to map them to lowercase equivalents (possibly increasing the size of
the string in the process). The vast majority of human languages (and
Unicode) do not have letters with different cases, but still, this static
table takes about 10 kilobytes on a 64-bit machine.
Even this will fail in one known case: the Turkish 'i' folds differently
if you're writing in Turkish vs other languages. Generally this is seen as
unfortunate collateral damage in cases where you can't specify the language
in use.
In addition to case-folding the codepoints, the new functions also know how
to decode the various formats to turn them into codepoints in the first
place, instead of blindly stepping by one byte (or one wchar_t) per
character.
Also included is casefolding.txt from the Unicode Consortium and a perl
script to generate the hashtable from that text file, so we can trivially
update this if new languages are added in the future.
A simple test using the new function:
```c
#include <SDL3/SDL.h>
int main(void)
{
const char *a = "α ε η";
const char *b = "Α Ε Η";
SDL_Log(" strcasecmp(\"%s\", \"%s\") == %d\n", a, b, strcasecmp(a, b));
SDL_Log("SDL_strcasecmp(\"%s\", \"%s\") == %d\n", a, b, SDL_strcasecmp(a, b));
return 0;
}
```
Produces:
```
INFO: strcasecmp("α ε η", "Α Ε Η") == 32
INFO: SDL_strcasecmp("α ε η", "Α Ε Η") == 0
```
glibc strcasecmp() fails to compare a Greek lowercase string to its uppercase
equivalent, even with a UTF-8 locale, but SDL_strcasecmp() works.
Other SDL_stdinc.h functions are changed to be more consistent, which is to
say they now ignore any C runtime and often dictate that only English-based
low-ASCII works with them.
Fixes Issue #9313.
2024-03-26 13:22:38 -04:00
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
2022-11-30 12:51:59 -08:00
|
|
|
#define UTF8_IsLeadByte(c) ((c) >= 0xC0 && (c) <= 0xF4)
|
2015-06-21 17:33:46 +02:00
|
|
|
#define UTF8_IsTrailingByte(c) ((c) >= 0x80 && (c) <= 0xBF)
|
|
|
|
|
|
2023-06-04 02:06:52 -07:00
|
|
|
static size_t UTF8_GetTrailingBytes(unsigned char c)
|
2015-06-21 17:33:46 +02:00
|
|
|
{
|
2022-12-01 16:07:03 -05:00
|
|
|
if (c >= 0xC0 && c <= 0xDF) {
|
2015-06-21 17:33:46 +02:00
|
|
|
return 1;
|
2022-12-01 16:07:03 -05:00
|
|
|
} else if (c >= 0xE0 && c <= 0xEF) {
|
2015-06-21 17:33:46 +02:00
|
|
|
return 2;
|
2022-12-01 16:07:03 -05:00
|
|
|
} else if (c >= 0xF0 && c <= 0xF4) {
|
2015-06-21 17:33:46 +02:00
|
|
|
return 3;
|
2022-12-01 16:07:03 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0;
|
2015-06-21 17:33:46 +02:00
|
|
|
}
|
|
|
|
|
|
2024-09-11 22:32:45 +02:00
|
|
|
#if !defined(HAVE_VSSCANF) || !defined(HAVE_STRTOL) || !defined(HAVE_STRTOUL) || !defined(HAVE_STRTOLL) || !defined(HAVE_STRTOULL) || !defined(HAVE_STRTOD)
|
|
|
|
|
/**
|
|
|
|
|
* Parses an unsigned long long and returns the unsigned value and sign bit.
|
|
|
|
|
*
|
|
|
|
|
* Positive values are clamped to ULLONG_MAX.
|
|
|
|
|
* The result `value == 0 && negative` indicates negative overflow
|
|
|
|
|
* and might need to be handled differently depending on whether a
|
|
|
|
|
* signed or unsigned integer is being parsed.
|
|
|
|
|
*/
|
|
|
|
|
static size_t SDL_ScanUnsignedLongLongInternal(const char *text, int count, int radix, unsigned long long *valuep, bool *negativep)
|
|
|
|
|
{
|
|
|
|
|
const unsigned long long ullong_max = ~0ULL;
|
|
|
|
|
|
|
|
|
|
const char *text_start = text;
|
|
|
|
|
const char *number_start = text_start;
|
|
|
|
|
unsigned long long value = 0;
|
|
|
|
|
bool negative = false;
|
|
|
|
|
bool overflow = false;
|
|
|
|
|
|
|
|
|
|
if (radix == 0 || (radix >= 2 && radix <= 36)) {
|
|
|
|
|
while (SDL_isspace(*text)) {
|
|
|
|
|
++text;
|
|
|
|
|
}
|
|
|
|
|
if (*text == '-' || *text == '+') {
|
|
|
|
|
negative = *text == '-';
|
|
|
|
|
++text;
|
|
|
|
|
}
|
|
|
|
|
if ((radix == 0 || radix == 16) && *text == '0') {
|
|
|
|
|
++text;
|
|
|
|
|
if (*text == 'x' || *text == 'X') {
|
|
|
|
|
radix = 16;
|
|
|
|
|
++text;
|
|
|
|
|
} else if (radix == 0) {
|
|
|
|
|
radix = 8;
|
|
|
|
|
}
|
|
|
|
|
} else if (radix == 0) {
|
|
|
|
|
radix = 10;
|
|
|
|
|
}
|
|
|
|
|
number_start = text;
|
|
|
|
|
do {
|
|
|
|
|
unsigned long long digit;
|
|
|
|
|
if (*text >= '0' && *text <= '9') {
|
|
|
|
|
digit = *text - '0';
|
|
|
|
|
} else if (radix > 10) {
|
|
|
|
|
if (*text >= 'A' && *text < 'A' + (radix - 10)) {
|
|
|
|
|
digit = 10 + (*text - 'A');
|
|
|
|
|
} else if (*text >= 'a' && *text < 'a' + (radix - 10)) {
|
|
|
|
|
digit = 10 + (*text - 'a');
|
|
|
|
|
} else {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
if (value != 0 && radix > ullong_max / value) {
|
|
|
|
|
overflow = true;
|
|
|
|
|
} else {
|
|
|
|
|
value *= radix;
|
|
|
|
|
if (digit > ullong_max - value) {
|
|
|
|
|
overflow = true;
|
|
|
|
|
} else {
|
|
|
|
|
value += digit;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
++text;
|
|
|
|
|
} while (count == 0 || (text - text_start) != count);
|
|
|
|
|
}
|
2024-09-12 00:02:47 +02:00
|
|
|
if (text == number_start) {
|
|
|
|
|
// no number was parsed, and thus no characters were consumed
|
2024-09-11 22:32:45 +02:00
|
|
|
text = text_start;
|
|
|
|
|
}
|
|
|
|
|
if (overflow) {
|
|
|
|
|
if (negative) {
|
|
|
|
|
value = 0;
|
|
|
|
|
} else {
|
|
|
|
|
value = ullong_max;
|
|
|
|
|
}
|
|
|
|
|
} else if (value == 0) {
|
|
|
|
|
negative = false;
|
|
|
|
|
}
|
|
|
|
|
*valuep = value;
|
|
|
|
|
*negativep = negative;
|
|
|
|
|
return text - text_start;
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
2024-09-12 00:32:07 +02:00
|
|
|
#ifndef HAVE_WCSTOL
|
|
|
|
|
// SDL_ScanUnsignedLongLongInternalW assumes that wchar_t can be converted to int without truncating bits
|
2024-09-10 21:50:48 +02:00
|
|
|
SDL_COMPILE_TIME_ASSERT(wchar_t_int, sizeof(wchar_t) <= sizeof(int));
|
|
|
|
|
|
2024-09-12 00:32:07 +02:00
|
|
|
/**
|
|
|
|
|
* Parses an unsigned long long and returns the unsigned value and sign bit.
|
|
|
|
|
*
|
|
|
|
|
* Positive values are clamped to ULLONG_MAX.
|
|
|
|
|
* The result `value == 0 && negative` indicates negative overflow
|
|
|
|
|
* and might need to be handled differently depending on whether a
|
|
|
|
|
* signed or unsigned integer is being parsed.
|
|
|
|
|
*/
|
|
|
|
|
static size_t SDL_ScanUnsignedLongLongInternalW(const wchar_t *text, int count, int radix, unsigned long long *valuep, bool *negativep)
|
2023-05-24 08:14:47 -07:00
|
|
|
{
|
2024-09-12 00:32:07 +02:00
|
|
|
const unsigned long long ullong_max = ~0ULL;
|
|
|
|
|
|
2024-09-10 21:50:48 +02:00
|
|
|
const wchar_t *text_start = text;
|
|
|
|
|
const wchar_t *number_start = text_start;
|
2024-09-12 00:32:07 +02:00
|
|
|
unsigned long long value = 0;
|
2024-08-22 09:21:26 -07:00
|
|
|
bool negative = false;
|
2024-09-12 00:32:07 +02:00
|
|
|
bool overflow = false;
|
2023-05-24 08:14:47 -07:00
|
|
|
|
2024-09-10 21:50:48 +02:00
|
|
|
if (radix == 0 || (radix >= 2 && radix <= 36)) {
|
|
|
|
|
while (SDL_isspace(*text)) {
|
|
|
|
|
++text;
|
2023-05-24 08:14:47 -07:00
|
|
|
}
|
2024-09-10 21:50:48 +02:00
|
|
|
if (*text == '-' || *text == '+') {
|
|
|
|
|
negative = *text == '-';
|
|
|
|
|
++text;
|
|
|
|
|
}
|
|
|
|
|
if ((radix == 0 || radix == 16) && *text == '0') {
|
|
|
|
|
++text;
|
|
|
|
|
if (*text == 'x' || *text == 'X') {
|
|
|
|
|
radix = 16;
|
|
|
|
|
++text;
|
|
|
|
|
} else if (radix == 0) {
|
|
|
|
|
radix = 8;
|
|
|
|
|
}
|
|
|
|
|
} else if (radix == 0) {
|
|
|
|
|
radix = 10;
|
2023-05-24 08:14:47 -07:00
|
|
|
}
|
2024-09-10 21:50:48 +02:00
|
|
|
number_start = text;
|
|
|
|
|
do {
|
2024-09-12 00:32:07 +02:00
|
|
|
unsigned long long digit;
|
2024-09-10 21:50:48 +02:00
|
|
|
if (*text >= '0' && *text <= '9') {
|
|
|
|
|
digit = *text - '0';
|
|
|
|
|
} else if (radix > 10) {
|
|
|
|
|
if (*text >= 'A' && *text < 'A' + (radix - 10)) {
|
|
|
|
|
digit = 10 + (*text - 'A');
|
|
|
|
|
} else if (*text >= 'a' && *text < 'a' + (radix - 10)) {
|
|
|
|
|
digit = 10 + (*text - 'a');
|
|
|
|
|
} else {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
break;
|
|
|
|
|
}
|
2024-09-12 00:32:07 +02:00
|
|
|
if (value != 0 && radix > ullong_max / value) {
|
|
|
|
|
overflow = true;
|
|
|
|
|
} else {
|
|
|
|
|
value *= radix;
|
|
|
|
|
if (digit > ullong_max - value) {
|
|
|
|
|
overflow = true;
|
|
|
|
|
} else {
|
|
|
|
|
value += digit;
|
2024-09-10 21:50:48 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
++text;
|
|
|
|
|
} while (count == 0 || (text - text_start) != count);
|
2023-05-24 08:14:47 -07:00
|
|
|
}
|
2024-09-12 00:32:07 +02:00
|
|
|
if (text == number_start) {
|
|
|
|
|
// no number was parsed, and thus no characters were consumed
|
2024-09-10 21:50:48 +02:00
|
|
|
text = text_start;
|
|
|
|
|
}
|
2024-09-12 00:32:07 +02:00
|
|
|
if (overflow) {
|
2024-09-10 21:50:48 +02:00
|
|
|
if (negative) {
|
2024-09-12 00:32:07 +02:00
|
|
|
value = 0;
|
2023-05-24 08:14:47 -07:00
|
|
|
} else {
|
2024-09-12 00:32:07 +02:00
|
|
|
value = ullong_max;
|
2023-05-24 08:14:47 -07:00
|
|
|
}
|
2024-09-12 00:32:07 +02:00
|
|
|
} else if (value == 0) {
|
|
|
|
|
negative = false;
|
2023-05-24 08:14:47 -07:00
|
|
|
}
|
2024-09-12 00:32:07 +02:00
|
|
|
*valuep = value;
|
|
|
|
|
*negativep = negative;
|
2024-09-10 21:50:48 +02:00
|
|
|
return text - text_start;
|
2023-05-24 08:14:47 -07:00
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
2024-09-12 00:32:07 +02:00
|
|
|
#if !defined(HAVE_VSSCANF) || !defined(HAVE_STRTOL)
|
|
|
|
|
static size_t SDL_ScanLong(const char *text, int count, int radix, long *valuep)
|
|
|
|
|
{
|
|
|
|
|
const unsigned long long_max = (~0UL) >> 1;
|
|
|
|
|
unsigned long long value;
|
|
|
|
|
bool negative;
|
|
|
|
|
size_t len = SDL_ScanUnsignedLongLongInternal(text, count, radix, &value, &negative);
|
|
|
|
|
if (negative) {
|
|
|
|
|
const unsigned long abs_long_min = long_max + 1;
|
|
|
|
|
if (value == 0 || value > abs_long_min) {
|
|
|
|
|
value = 0ULL - abs_long_min;
|
|
|
|
|
} else {
|
|
|
|
|
value = 0ULL - value;
|
|
|
|
|
}
|
|
|
|
|
} else if (value > long_max) {
|
|
|
|
|
value = long_max;
|
|
|
|
|
}
|
|
|
|
|
*valuep = value;
|
|
|
|
|
return len;
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#ifndef HAVE_WCSTOL
|
|
|
|
|
static size_t SDL_ScanLongW(const wchar_t *text, int count, int radix, long *valuep)
|
|
|
|
|
{
|
|
|
|
|
const unsigned long long_max = (~0UL) >> 1;
|
|
|
|
|
unsigned long long value;
|
|
|
|
|
bool negative;
|
|
|
|
|
size_t len = SDL_ScanUnsignedLongLongInternalW(text, count, radix, &value, &negative);
|
|
|
|
|
if (negative) {
|
|
|
|
|
const unsigned long abs_long_min = long_max + 1;
|
|
|
|
|
if (value == 0 || value > abs_long_min) {
|
|
|
|
|
value = 0ULL - abs_long_min;
|
|
|
|
|
} else {
|
|
|
|
|
value = 0ULL - value;
|
|
|
|
|
}
|
|
|
|
|
} else if (value > long_max) {
|
|
|
|
|
value = long_max;
|
|
|
|
|
}
|
|
|
|
|
*valuep = value;
|
|
|
|
|
return len;
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
2024-09-12 01:23:56 +02:00
|
|
|
#if !defined(HAVE_VSSCANF) || !defined(HAVE_STRTOUL)
|
2023-05-23 11:29:41 -07:00
|
|
|
static size_t SDL_ScanUnsignedLong(const char *text, int count, int radix, unsigned long *valuep)
|
2015-06-21 17:33:46 +02:00
|
|
|
{
|
2024-09-12 00:02:47 +02:00
|
|
|
const unsigned long ulong_max = ~0UL;
|
|
|
|
|
unsigned long long value;
|
|
|
|
|
bool negative;
|
|
|
|
|
size_t len = SDL_ScanUnsignedLongLongInternal(text, count, radix, &value, &negative);
|
|
|
|
|
if (negative) {
|
|
|
|
|
if (value == 0 || value > ulong_max) {
|
|
|
|
|
value = ulong_max;
|
|
|
|
|
} else if (value == ulong_max) {
|
|
|
|
|
value = 1;
|
2015-06-21 17:33:46 +02:00
|
|
|
} else {
|
2024-09-12 00:02:47 +02:00
|
|
|
value = 0ULL - value;
|
2022-06-18 07:02:38 -07:00
|
|
|
}
|
2024-09-12 00:02:47 +02:00
|
|
|
} else if (value > ulong_max) {
|
|
|
|
|
value = ulong_max;
|
2015-06-21 17:33:46 +02:00
|
|
|
}
|
2024-09-12 00:02:47 +02:00
|
|
|
*valuep = value;
|
|
|
|
|
return len;
|
2015-06-21 17:33:46 +02:00
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#ifndef HAVE_VSSCANF
|
2024-09-12 00:26:36 +02:00
|
|
|
static size_t SDL_ScanUintPtrT(const char *text, uintptr_t *valuep)
|
2015-06-21 17:33:46 +02:00
|
|
|
{
|
2024-09-12 00:26:36 +02:00
|
|
|
const uintptr_t uintptr_max = ~(uintptr_t)0;
|
|
|
|
|
unsigned long long value;
|
|
|
|
|
bool negative;
|
|
|
|
|
size_t len = SDL_ScanUnsignedLongLongInternal(text, 0, 16, &value, &negative);
|
|
|
|
|
if (negative) {
|
|
|
|
|
if (value == 0 || value > uintptr_max) {
|
|
|
|
|
value = uintptr_max;
|
|
|
|
|
} else if (value == uintptr_max) {
|
|
|
|
|
value = 1;
|
2015-06-21 17:33:46 +02:00
|
|
|
} else {
|
2024-09-12 00:26:36 +02:00
|
|
|
value = 0ULL - value;
|
2015-06-21 17:33:46 +02:00
|
|
|
}
|
2024-09-12 00:26:36 +02:00
|
|
|
} else if (value > uintptr_max) {
|
|
|
|
|
value = uintptr_max;
|
2015-06-21 17:33:46 +02:00
|
|
|
}
|
2024-09-12 00:26:36 +02:00
|
|
|
*valuep = value;
|
|
|
|
|
return len;
|
2015-06-21 17:33:46 +02:00
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
2024-09-12 00:02:47 +02:00
|
|
|
#if !defined(HAVE_VSSCANF) || !defined(HAVE_STRTOLL)
|
2024-09-09 14:54:50 -07:00
|
|
|
static size_t SDL_ScanLongLong(const char *text, int count, int radix, long long *valuep)
|
2015-06-21 17:33:46 +02:00
|
|
|
{
|
2024-09-12 00:02:47 +02:00
|
|
|
const unsigned long long llong_max = (~0ULL) >> 1;
|
|
|
|
|
unsigned long long value;
|
|
|
|
|
bool negative;
|
|
|
|
|
size_t len = SDL_ScanUnsignedLongLongInternal(text, count, radix, &value, &negative);
|
|
|
|
|
if (negative) {
|
|
|
|
|
const unsigned long long abs_llong_min = llong_max + 1;
|
|
|
|
|
if (value == 0 || value > abs_llong_min) {
|
|
|
|
|
value = 0ULL - abs_llong_min;
|
2015-06-21 17:33:46 +02:00
|
|
|
} else {
|
2024-09-12 00:02:47 +02:00
|
|
|
value = 0ULL - value;
|
2015-06-21 17:33:46 +02:00
|
|
|
}
|
2024-09-12 00:02:47 +02:00
|
|
|
} else if (value > llong_max) {
|
|
|
|
|
value = llong_max;
|
2015-06-21 17:33:46 +02:00
|
|
|
}
|
2024-09-12 00:02:47 +02:00
|
|
|
*valuep = value;
|
|
|
|
|
return len;
|
2015-06-21 17:33:46 +02:00
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
2024-09-12 01:23:56 +02:00
|
|
|
#if !defined(HAVE_VSSCANF) || !defined(HAVE_STRTOULL) || !defined(HAVE_STRTOD)
|
2024-09-09 14:54:50 -07:00
|
|
|
static size_t SDL_ScanUnsignedLongLong(const char *text, int count, int radix, unsigned long long *valuep)
|
2015-06-21 17:33:46 +02:00
|
|
|
{
|
2024-09-12 00:02:47 +02:00
|
|
|
const unsigned long long ullong_max = ~0ULL;
|
2024-09-11 22:32:45 +02:00
|
|
|
bool negative;
|
|
|
|
|
size_t len = SDL_ScanUnsignedLongLongInternal(text, count, radix, valuep, &negative);
|
|
|
|
|
if (negative) {
|
|
|
|
|
if (*valuep == 0) {
|
2024-09-12 00:02:47 +02:00
|
|
|
*valuep = ullong_max;
|
2015-06-21 17:33:46 +02:00
|
|
|
} else {
|
2024-09-11 22:32:45 +02:00
|
|
|
*valuep = 0ULL - *valuep;
|
2022-06-18 07:02:38 -07:00
|
|
|
}
|
2015-06-21 17:33:46 +02:00
|
|
|
}
|
2024-09-11 22:32:45 +02:00
|
|
|
return len;
|
2015-06-21 17:33:46 +02:00
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#if !defined(HAVE_VSSCANF) || !defined(HAVE_STRTOD)
|
2023-05-23 11:29:41 -07:00
|
|
|
static size_t SDL_ScanFloat(const char *text, double *valuep)
|
2015-06-21 17:33:46 +02:00
|
|
|
{
|
2024-09-12 01:23:56 +02:00
|
|
|
const char *text_start = text;
|
|
|
|
|
const char *number_start = text_start;
|
2015-06-21 17:33:46 +02:00
|
|
|
double value = 0.0;
|
2024-08-22 09:21:26 -07:00
|
|
|
bool negative = false;
|
2015-06-21 17:33:46 +02:00
|
|
|
|
2024-09-12 01:23:56 +02:00
|
|
|
while (SDL_isspace(*text)) {
|
2015-06-21 17:33:46 +02:00
|
|
|
++text;
|
|
|
|
|
}
|
2024-09-12 01:23:56 +02:00
|
|
|
if (*text == '-' || *text == '+') {
|
|
|
|
|
negative = *text == '-';
|
2015-06-21 17:33:46 +02:00
|
|
|
++text;
|
2024-09-12 01:23:56 +02:00
|
|
|
}
|
|
|
|
|
number_start = text;
|
|
|
|
|
if (SDL_isdigit(*text)) {
|
|
|
|
|
value += SDL_strtoull(text, (char **)(&text), 10);
|
|
|
|
|
if (*text == '.') {
|
|
|
|
|
double denom = 10;
|
2015-06-21 17:33:46 +02:00
|
|
|
++text;
|
2024-09-12 01:23:56 +02:00
|
|
|
while (SDL_isdigit(*text)) {
|
|
|
|
|
value += (double)(*text - '0') / denom;
|
|
|
|
|
denom *= 10;
|
|
|
|
|
++text;
|
|
|
|
|
}
|
2015-06-21 17:33:46 +02:00
|
|
|
}
|
|
|
|
|
}
|
2024-09-12 01:23:56 +02:00
|
|
|
if (text == number_start) {
|
|
|
|
|
// no number was parsed, and thus no characters were consumed
|
|
|
|
|
text = text_start;
|
|
|
|
|
}
|
|
|
|
|
if (negative) {
|
|
|
|
|
value = -value;
|
2015-06-21 17:33:46 +02:00
|
|
|
}
|
2024-09-12 01:23:56 +02:00
|
|
|
*valuep = value;
|
|
|
|
|
return text - text_start;
|
2015-06-21 17:33:46 +02:00
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
2022-11-30 12:51:59 -08:00
|
|
|
int SDL_memcmp(const void *s1, const void *s2, size_t len)
|
2015-06-21 17:33:46 +02:00
|
|
|
{
|
2024-01-24 02:40:51 +01:00
|
|
|
#ifdef SDL_PLATFORM_VITA
|
2022-09-17 11:52:19 +03:00
|
|
|
/*
|
|
|
|
|
Using memcmp on NULL is UB per POSIX / C99 7.21.1/2.
|
|
|
|
|
But, both linux and bsd allow that, with an exception:
|
|
|
|
|
zero length strings are always identical, so NULLs are never dereferenced.
|
|
|
|
|
sceClibMemcmp on PSVita doesn't allow that, so we check ourselves.
|
|
|
|
|
*/
|
|
|
|
|
if (len == 0) {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
return sceClibMemcmp(s1, s2, len);
|
|
|
|
|
#elif defined(HAVE_MEMCMP)
|
2015-06-21 17:33:46 +02:00
|
|
|
return memcmp(s1, s2, len);
|
|
|
|
|
#else
|
2022-11-30 12:51:59 -08:00
|
|
|
char *s1p = (char *)s1;
|
|
|
|
|
char *s2p = (char *)s2;
|
2015-06-21 17:33:46 +02:00
|
|
|
while (len--) {
|
|
|
|
|
if (*s1p != *s2p) {
|
2022-11-27 17:38:43 +01:00
|
|
|
return *s1p - *s2p;
|
2015-06-21 17:33:46 +02:00
|
|
|
}
|
|
|
|
|
++s1p;
|
|
|
|
|
++s2p;
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
Use C++ style comments consistently in SDL source code
Implemented using this script:
find . -type f -exec sed -i'' -e 's,/\* *\([^*]*\)\*/ *$,// \1,' -e 's, \+$,,' {} \;
git checkout \
core/linux/SDL_evdev_kbd_default_keymap.h \
events/imKStoUCS.* \
hidapi \
joystick/controller_type.c \
joystick/controller_type.h \
joystick/hidapi/steam/controller_constants.h \
joystick/hidapi/steam/controller_structs.h \
joystick/SDL_gamepad_db.h \
libm \
render/*/*Shader*.h \
render/vitagxm/SDL_render_vita_gxm_shaders.h \
render/metal/SDL_shaders_metal_*.h \
stdlib/SDL_malloc.c \
stdlib/SDL_qsort.c \
stdlib/SDL_strtokr.c \
test/ \
video/directx/SDL_d3d12_xbox_cmacros.h \
video/directx/d3d12.h \
video/directx/d3d12sdklayers.h \
video/khronos \
video/x11/edid-parse.c \
video/x11/xsettings-client.* \
video/yuv2rgb
sed -i'' -e 's,/\* *\([^*]*\)\*/ *$,// \1,' -e 's, \+$,,' hidapi/SDL_hidapi.c
2024-08-22 10:30:45 -07:00
|
|
|
#endif // HAVE_MEMCMP
|
2015-06-21 17:33:46 +02:00
|
|
|
}
|
|
|
|
|
|
2023-05-23 11:29:41 -07:00
|
|
|
size_t SDL_strlen(const char *string)
|
2015-06-21 17:33:46 +02:00
|
|
|
{
|
2023-03-30 20:26:31 +02:00
|
|
|
#ifdef HAVE_STRLEN
|
2015-06-21 17:33:46 +02:00
|
|
|
return strlen(string);
|
|
|
|
|
#else
|
|
|
|
|
size_t len = 0;
|
|
|
|
|
while (*string++) {
|
|
|
|
|
++len;
|
|
|
|
|
}
|
|
|
|
|
return len;
|
Use C++ style comments consistently in SDL source code
Implemented using this script:
find . -type f -exec sed -i'' -e 's,/\* *\([^*]*\)\*/ *$,// \1,' -e 's, \+$,,' {} \;
git checkout \
core/linux/SDL_evdev_kbd_default_keymap.h \
events/imKStoUCS.* \
hidapi \
joystick/controller_type.c \
joystick/controller_type.h \
joystick/hidapi/steam/controller_constants.h \
joystick/hidapi/steam/controller_structs.h \
joystick/SDL_gamepad_db.h \
libm \
render/*/*Shader*.h \
render/vitagxm/SDL_render_vita_gxm_shaders.h \
render/metal/SDL_shaders_metal_*.h \
stdlib/SDL_malloc.c \
stdlib/SDL_qsort.c \
stdlib/SDL_strtokr.c \
test/ \
video/directx/SDL_d3d12_xbox_cmacros.h \
video/directx/d3d12.h \
video/directx/d3d12sdklayers.h \
video/khronos \
video/x11/edid-parse.c \
video/x11/xsettings-client.* \
video/yuv2rgb
sed -i'' -e 's,/\* *\([^*]*\)\*/ *$,// \1,' -e 's, \+$,,' hidapi/SDL_hidapi.c
2024-08-22 10:30:45 -07:00
|
|
|
#endif // HAVE_STRLEN
|
2015-06-21 17:33:46 +02:00
|
|
|
}
|
|
|
|
|
|
2023-07-17 18:10:57 -07:00
|
|
|
size_t SDL_strnlen(const char *string, size_t maxlen)
|
|
|
|
|
{
|
|
|
|
|
#ifdef HAVE_STRNLEN
|
|
|
|
|
return strnlen(string, maxlen);
|
|
|
|
|
#else
|
|
|
|
|
size_t len = 0;
|
|
|
|
|
while (len < maxlen && *string++) {
|
|
|
|
|
++len;
|
|
|
|
|
}
|
|
|
|
|
return len;
|
Use C++ style comments consistently in SDL source code
Implemented using this script:
find . -type f -exec sed -i'' -e 's,/\* *\([^*]*\)\*/ *$,// \1,' -e 's, \+$,,' {} \;
git checkout \
core/linux/SDL_evdev_kbd_default_keymap.h \
events/imKStoUCS.* \
hidapi \
joystick/controller_type.c \
joystick/controller_type.h \
joystick/hidapi/steam/controller_constants.h \
joystick/hidapi/steam/controller_structs.h \
joystick/SDL_gamepad_db.h \
libm \
render/*/*Shader*.h \
render/vitagxm/SDL_render_vita_gxm_shaders.h \
render/metal/SDL_shaders_metal_*.h \
stdlib/SDL_malloc.c \
stdlib/SDL_qsort.c \
stdlib/SDL_strtokr.c \
test/ \
video/directx/SDL_d3d12_xbox_cmacros.h \
video/directx/d3d12.h \
video/directx/d3d12sdklayers.h \
video/khronos \
video/x11/edid-parse.c \
video/x11/xsettings-client.* \
video/yuv2rgb
sed -i'' -e 's,/\* *\([^*]*\)\*/ *$,// \1,' -e 's, \+$,,' hidapi/SDL_hidapi.c
2024-08-22 10:30:45 -07:00
|
|
|
#endif // HAVE_STRNLEN
|
2023-07-17 18:10:57 -07:00
|
|
|
}
|
|
|
|
|
|
2023-05-23 11:29:41 -07:00
|
|
|
size_t SDL_wcslen(const wchar_t *string)
|
2015-06-21 17:33:46 +02:00
|
|
|
{
|
2023-03-30 20:26:31 +02:00
|
|
|
#ifdef HAVE_WCSLEN
|
2015-06-21 17:33:46 +02:00
|
|
|
return wcslen(string);
|
|
|
|
|
#else
|
|
|
|
|
size_t len = 0;
|
|
|
|
|
while (*string++) {
|
|
|
|
|
++len;
|
|
|
|
|
}
|
|
|
|
|
return len;
|
Use C++ style comments consistently in SDL source code
Implemented using this script:
find . -type f -exec sed -i'' -e 's,/\* *\([^*]*\)\*/ *$,// \1,' -e 's, \+$,,' {} \;
git checkout \
core/linux/SDL_evdev_kbd_default_keymap.h \
events/imKStoUCS.* \
hidapi \
joystick/controller_type.c \
joystick/controller_type.h \
joystick/hidapi/steam/controller_constants.h \
joystick/hidapi/steam/controller_structs.h \
joystick/SDL_gamepad_db.h \
libm \
render/*/*Shader*.h \
render/vitagxm/SDL_render_vita_gxm_shaders.h \
render/metal/SDL_shaders_metal_*.h \
stdlib/SDL_malloc.c \
stdlib/SDL_qsort.c \
stdlib/SDL_strtokr.c \
test/ \
video/directx/SDL_d3d12_xbox_cmacros.h \
video/directx/d3d12.h \
video/directx/d3d12sdklayers.h \
video/khronos \
video/x11/edid-parse.c \
video/x11/xsettings-client.* \
video/yuv2rgb
sed -i'' -e 's,/\* *\([^*]*\)\*/ *$,// \1,' -e 's, \+$,,' hidapi/SDL_hidapi.c
2024-08-22 10:30:45 -07:00
|
|
|
#endif // HAVE_WCSLEN
|
2015-06-21 17:33:46 +02:00
|
|
|
}
|
|
|
|
|
|
2023-07-17 18:10:57 -07:00
|
|
|
size_t SDL_wcsnlen(const wchar_t *string, size_t maxlen)
|
|
|
|
|
{
|
|
|
|
|
#ifdef HAVE_WCSNLEN
|
|
|
|
|
return wcsnlen(string, maxlen);
|
|
|
|
|
#else
|
|
|
|
|
size_t len = 0;
|
|
|
|
|
while (len < maxlen && *string++) {
|
|
|
|
|
++len;
|
|
|
|
|
}
|
|
|
|
|
return len;
|
Use C++ style comments consistently in SDL source code
Implemented using this script:
find . -type f -exec sed -i'' -e 's,/\* *\([^*]*\)\*/ *$,// \1,' -e 's, \+$,,' {} \;
git checkout \
core/linux/SDL_evdev_kbd_default_keymap.h \
events/imKStoUCS.* \
hidapi \
joystick/controller_type.c \
joystick/controller_type.h \
joystick/hidapi/steam/controller_constants.h \
joystick/hidapi/steam/controller_structs.h \
joystick/SDL_gamepad_db.h \
libm \
render/*/*Shader*.h \
render/vitagxm/SDL_render_vita_gxm_shaders.h \
render/metal/SDL_shaders_metal_*.h \
stdlib/SDL_malloc.c \
stdlib/SDL_qsort.c \
stdlib/SDL_strtokr.c \
test/ \
video/directx/SDL_d3d12_xbox_cmacros.h \
video/directx/d3d12.h \
video/directx/d3d12sdklayers.h \
video/khronos \
video/x11/edid-parse.c \
video/x11/xsettings-client.* \
video/yuv2rgb
sed -i'' -e 's,/\* *\([^*]*\)\*/ *$,// \1,' -e 's, \+$,,' hidapi/SDL_hidapi.c
2024-08-22 10:30:45 -07:00
|
|
|
#endif // HAVE_WCSNLEN
|
2023-07-17 18:10:57 -07:00
|
|
|
}
|
|
|
|
|
|
2023-05-23 11:29:41 -07:00
|
|
|
size_t SDL_wcslcpy(SDL_OUT_Z_CAP(maxlen) wchar_t *dst, const wchar_t *src, size_t maxlen)
|
2015-06-21 17:33:46 +02:00
|
|
|
{
|
2023-03-30 20:26:31 +02:00
|
|
|
#ifdef HAVE_WCSLCPY
|
2015-06-21 17:33:46 +02:00
|
|
|
return wcslcpy(dst, src, maxlen);
|
|
|
|
|
#else
|
|
|
|
|
size_t srclen = SDL_wcslen(src);
|
|
|
|
|
if (maxlen > 0) {
|
|
|
|
|
size_t len = SDL_min(srclen, maxlen - 1);
|
|
|
|
|
SDL_memcpy(dst, src, len * sizeof(wchar_t));
|
|
|
|
|
dst[len] = '\0';
|
|
|
|
|
}
|
|
|
|
|
return srclen;
|
Use C++ style comments consistently in SDL source code
Implemented using this script:
find . -type f -exec sed -i'' -e 's,/\* *\([^*]*\)\*/ *$,// \1,' -e 's, \+$,,' {} \;
git checkout \
core/linux/SDL_evdev_kbd_default_keymap.h \
events/imKStoUCS.* \
hidapi \
joystick/controller_type.c \
joystick/controller_type.h \
joystick/hidapi/steam/controller_constants.h \
joystick/hidapi/steam/controller_structs.h \
joystick/SDL_gamepad_db.h \
libm \
render/*/*Shader*.h \
render/vitagxm/SDL_render_vita_gxm_shaders.h \
render/metal/SDL_shaders_metal_*.h \
stdlib/SDL_malloc.c \
stdlib/SDL_qsort.c \
stdlib/SDL_strtokr.c \
test/ \
video/directx/SDL_d3d12_xbox_cmacros.h \
video/directx/d3d12.h \
video/directx/d3d12sdklayers.h \
video/khronos \
video/x11/edid-parse.c \
video/x11/xsettings-client.* \
video/yuv2rgb
sed -i'' -e 's,/\* *\([^*]*\)\*/ *$,// \1,' -e 's, \+$,,' hidapi/SDL_hidapi.c
2024-08-22 10:30:45 -07:00
|
|
|
#endif // HAVE_WCSLCPY
|
2015-06-21 17:33:46 +02:00
|
|
|
}
|
|
|
|
|
|
2023-05-23 11:29:41 -07:00
|
|
|
size_t SDL_wcslcat(SDL_INOUT_Z_CAP(maxlen) wchar_t *dst, const wchar_t *src, size_t maxlen)
|
2015-06-21 17:33:46 +02:00
|
|
|
{
|
2023-03-30 20:26:31 +02:00
|
|
|
#ifdef HAVE_WCSLCAT
|
2015-06-21 17:33:46 +02:00
|
|
|
return wcslcat(dst, src, maxlen);
|
|
|
|
|
#else
|
|
|
|
|
size_t dstlen = SDL_wcslen(dst);
|
|
|
|
|
size_t srclen = SDL_wcslen(src);
|
|
|
|
|
if (dstlen < maxlen) {
|
|
|
|
|
SDL_wcslcpy(dst + dstlen, src, maxlen - dstlen);
|
|
|
|
|
}
|
|
|
|
|
return dstlen + srclen;
|
Use C++ style comments consistently in SDL source code
Implemented using this script:
find . -type f -exec sed -i'' -e 's,/\* *\([^*]*\)\*/ *$,// \1,' -e 's, \+$,,' {} \;
git checkout \
core/linux/SDL_evdev_kbd_default_keymap.h \
events/imKStoUCS.* \
hidapi \
joystick/controller_type.c \
joystick/controller_type.h \
joystick/hidapi/steam/controller_constants.h \
joystick/hidapi/steam/controller_structs.h \
joystick/SDL_gamepad_db.h \
libm \
render/*/*Shader*.h \
render/vitagxm/SDL_render_vita_gxm_shaders.h \
render/metal/SDL_shaders_metal_*.h \
stdlib/SDL_malloc.c \
stdlib/SDL_qsort.c \
stdlib/SDL_strtokr.c \
test/ \
video/directx/SDL_d3d12_xbox_cmacros.h \
video/directx/d3d12.h \
video/directx/d3d12sdklayers.h \
video/khronos \
video/x11/edid-parse.c \
video/x11/xsettings-client.* \
video/yuv2rgb
sed -i'' -e 's,/\* *\([^*]*\)\*/ *$,// \1,' -e 's, \+$,,' hidapi/SDL_hidapi.c
2024-08-22 10:30:45 -07:00
|
|
|
#endif // HAVE_WCSLCAT
|
2015-06-21 17:33:46 +02:00
|
|
|
}
|
|
|
|
|
|
2023-05-23 11:29:41 -07:00
|
|
|
wchar_t *SDL_wcsdup(const wchar_t *string)
|
2019-11-20 16:42:50 -08:00
|
|
|
{
|
|
|
|
|
size_t len = ((SDL_wcslen(string) + 1) * sizeof(wchar_t));
|
|
|
|
|
wchar_t *newstr = (wchar_t *)SDL_malloc(len);
|
|
|
|
|
if (newstr) {
|
|
|
|
|
SDL_memcpy(newstr, string, len);
|
|
|
|
|
}
|
|
|
|
|
return newstr;
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-03 14:42:28 -08:00
|
|
|
wchar_t *SDL_wcsnstr(const wchar_t *haystack, const wchar_t *needle, size_t maxlen)
|
2019-11-20 16:42:50 -08:00
|
|
|
{
|
|
|
|
|
size_t length = SDL_wcslen(needle);
|
2023-12-03 14:42:28 -08:00
|
|
|
if (length == 0) {
|
|
|
|
|
return (wchar_t *)haystack;
|
|
|
|
|
}
|
|
|
|
|
while (maxlen >= length && *haystack) {
|
|
|
|
|
if (maxlen >= length && SDL_wcsncmp(haystack, needle, length) == 0) {
|
2019-11-20 16:42:50 -08:00
|
|
|
return (wchar_t *)haystack;
|
|
|
|
|
}
|
|
|
|
|
++haystack;
|
2023-12-03 14:42:28 -08:00
|
|
|
--maxlen;
|
2019-11-20 16:42:50 -08:00
|
|
|
}
|
|
|
|
|
return NULL;
|
2023-12-03 14:42:28 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
wchar_t *SDL_wcsstr(const wchar_t *haystack, const wchar_t *needle)
|
|
|
|
|
{
|
|
|
|
|
#ifdef HAVE_WCSSTR
|
|
|
|
|
return SDL_const_cast(wchar_t *, wcsstr(haystack, needle));
|
|
|
|
|
#else
|
|
|
|
|
return SDL_wcsnstr(haystack, needle, SDL_wcslen(haystack));
|
Use C++ style comments consistently in SDL source code
Implemented using this script:
find . -type f -exec sed -i'' -e 's,/\* *\([^*]*\)\*/ *$,// \1,' -e 's, \+$,,' {} \;
git checkout \
core/linux/SDL_evdev_kbd_default_keymap.h \
events/imKStoUCS.* \
hidapi \
joystick/controller_type.c \
joystick/controller_type.h \
joystick/hidapi/steam/controller_constants.h \
joystick/hidapi/steam/controller_structs.h \
joystick/SDL_gamepad_db.h \
libm \
render/*/*Shader*.h \
render/vitagxm/SDL_render_vita_gxm_shaders.h \
render/metal/SDL_shaders_metal_*.h \
stdlib/SDL_malloc.c \
stdlib/SDL_qsort.c \
stdlib/SDL_strtokr.c \
test/ \
video/directx/SDL_d3d12_xbox_cmacros.h \
video/directx/d3d12.h \
video/directx/d3d12sdklayers.h \
video/khronos \
video/x11/edid-parse.c \
video/x11/xsettings-client.* \
video/yuv2rgb
sed -i'' -e 's,/\* *\([^*]*\)\*/ *$,// \1,' -e 's, \+$,,' hidapi/SDL_hidapi.c
2024-08-22 10:30:45 -07:00
|
|
|
#endif // HAVE_WCSSTR
|
2019-11-20 16:42:50 -08:00
|
|
|
}
|
|
|
|
|
|
2022-11-30 12:51:59 -08:00
|
|
|
int SDL_wcscmp(const wchar_t *str1, const wchar_t *str2)
|
2017-08-13 20:37:49 -07:00
|
|
|
{
|
2023-03-30 20:26:31 +02:00
|
|
|
#ifdef HAVE_WCSCMP
|
2017-08-13 20:37:49 -07:00
|
|
|
return wcscmp(str1, str2);
|
|
|
|
|
#else
|
|
|
|
|
while (*str1 && *str2) {
|
2022-11-27 17:38:43 +01:00
|
|
|
if (*str1 != *str2) {
|
2017-08-13 20:37:49 -07:00
|
|
|
break;
|
2022-11-27 17:38:43 +01:00
|
|
|
}
|
2017-08-13 20:37:49 -07:00
|
|
|
++str1;
|
|
|
|
|
++str2;
|
|
|
|
|
}
|
2022-12-01 16:07:03 -05:00
|
|
|
return *str1 - *str2;
|
Use C++ style comments consistently in SDL source code
Implemented using this script:
find . -type f -exec sed -i'' -e 's,/\* *\([^*]*\)\*/ *$,// \1,' -e 's, \+$,,' {} \;
git checkout \
core/linux/SDL_evdev_kbd_default_keymap.h \
events/imKStoUCS.* \
hidapi \
joystick/controller_type.c \
joystick/controller_type.h \
joystick/hidapi/steam/controller_constants.h \
joystick/hidapi/steam/controller_structs.h \
joystick/SDL_gamepad_db.h \
libm \
render/*/*Shader*.h \
render/vitagxm/SDL_render_vita_gxm_shaders.h \
render/metal/SDL_shaders_metal_*.h \
stdlib/SDL_malloc.c \
stdlib/SDL_qsort.c \
stdlib/SDL_strtokr.c \
test/ \
video/directx/SDL_d3d12_xbox_cmacros.h \
video/directx/d3d12.h \
video/directx/d3d12sdklayers.h \
video/khronos \
video/x11/edid-parse.c \
video/x11/xsettings-client.* \
video/yuv2rgb
sed -i'' -e 's,/\* *\([^*]*\)\*/ *$,// \1,' -e 's, \+$,,' hidapi/SDL_hidapi.c
2024-08-22 10:30:45 -07:00
|
|
|
#endif // HAVE_WCSCMP
|
2017-08-13 20:37:49 -07:00
|
|
|
}
|
|
|
|
|
|
2022-11-30 12:51:59 -08:00
|
|
|
int SDL_wcsncmp(const wchar_t *str1, const wchar_t *str2, size_t maxlen)
|
2019-11-20 16:42:50 -08:00
|
|
|
{
|
2023-03-30 20:26:31 +02:00
|
|
|
#ifdef HAVE_WCSNCMP
|
2019-11-20 16:42:50 -08:00
|
|
|
return wcsncmp(str1, str2, maxlen);
|
|
|
|
|
#else
|
2020-07-24 22:24:03 -04:00
|
|
|
while (*str1 && *str2 && maxlen) {
|
2022-11-27 17:38:43 +01:00
|
|
|
if (*str1 != *str2) {
|
2019-11-20 16:42:50 -08:00
|
|
|
break;
|
2022-11-27 17:38:43 +01:00
|
|
|
}
|
2019-11-20 16:42:50 -08:00
|
|
|
++str1;
|
|
|
|
|
++str2;
|
2020-07-24 22:24:03 -04:00
|
|
|
--maxlen;
|
2019-11-20 16:42:50 -08:00
|
|
|
}
|
2020-07-24 22:24:03 -04:00
|
|
|
if (!maxlen) {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2022-12-01 16:07:03 -05:00
|
|
|
return *str1 - *str2;
|
2020-07-24 22:24:03 -04:00
|
|
|
|
Use C++ style comments consistently in SDL source code
Implemented using this script:
find . -type f -exec sed -i'' -e 's,/\* *\([^*]*\)\*/ *$,// \1,' -e 's, \+$,,' {} \;
git checkout \
core/linux/SDL_evdev_kbd_default_keymap.h \
events/imKStoUCS.* \
hidapi \
joystick/controller_type.c \
joystick/controller_type.h \
joystick/hidapi/steam/controller_constants.h \
joystick/hidapi/steam/controller_structs.h \
joystick/SDL_gamepad_db.h \
libm \
render/*/*Shader*.h \
render/vitagxm/SDL_render_vita_gxm_shaders.h \
render/metal/SDL_shaders_metal_*.h \
stdlib/SDL_malloc.c \
stdlib/SDL_qsort.c \
stdlib/SDL_strtokr.c \
test/ \
video/directx/SDL_d3d12_xbox_cmacros.h \
video/directx/d3d12.h \
video/directx/d3d12sdklayers.h \
video/khronos \
video/x11/edid-parse.c \
video/x11/xsettings-client.* \
video/yuv2rgb
sed -i'' -e 's,/\* *\([^*]*\)\*/ *$,// \1,' -e 's, \+$,,' hidapi/SDL_hidapi.c
2024-08-22 10:30:45 -07:00
|
|
|
#endif // HAVE_WCSNCMP
|
2019-11-20 16:42:50 -08:00
|
|
|
}
|
|
|
|
|
|
stdlib: Improve Unicode support and consistency in string comparison functions.
SDL_strcasecmp (even when calling into a C runtime) does not work with
Unicode chars, and depending on the user's locale, might not work with
even basic ASCII strings.
This implements the function from scratch, using "case-folding,"
which is a more robust method that deals with various languages. It
involves a hashtable of a few hundred codepoints that are "uppercase" and
how to map them to lowercase equivalents (possibly increasing the size of
the string in the process). The vast majority of human languages (and
Unicode) do not have letters with different cases, but still, this static
table takes about 10 kilobytes on a 64-bit machine.
Even this will fail in one known case: the Turkish 'i' folds differently
if you're writing in Turkish vs other languages. Generally this is seen as
unfortunate collateral damage in cases where you can't specify the language
in use.
In addition to case-folding the codepoints, the new functions also know how
to decode the various formats to turn them into codepoints in the first
place, instead of blindly stepping by one byte (or one wchar_t) per
character.
Also included is casefolding.txt from the Unicode Consortium and a perl
script to generate the hashtable from that text file, so we can trivially
update this if new languages are added in the future.
A simple test using the new function:
```c
#include <SDL3/SDL.h>
int main(void)
{
const char *a = "α ε η";
const char *b = "Α Ε Η";
SDL_Log(" strcasecmp(\"%s\", \"%s\") == %d\n", a, b, strcasecmp(a, b));
SDL_Log("SDL_strcasecmp(\"%s\", \"%s\") == %d\n", a, b, SDL_strcasecmp(a, b));
return 0;
}
```
Produces:
```
INFO: strcasecmp("α ε η", "Α Ε Η") == 32
INFO: SDL_strcasecmp("α ε η", "Α Ε Η") == 0
```
glibc strcasecmp() fails to compare a Greek lowercase string to its uppercase
equivalent, even with a UTF-8 locale, but SDL_strcasecmp() works.
Other SDL_stdinc.h functions are changed to be more consistent, which is to
say they now ignore any C runtime and often dictate that only English-based
low-ASCII works with them.
Fixes Issue #9313.
2024-03-26 13:22:38 -04:00
|
|
|
int SDL_wcscasecmp(const wchar_t *wstr1, const wchar_t *wstr2)
|
2020-11-24 12:43:01 -08:00
|
|
|
{
|
stdlib: Improve Unicode support and consistency in string comparison functions.
SDL_strcasecmp (even when calling into a C runtime) does not work with
Unicode chars, and depending on the user's locale, might not work with
even basic ASCII strings.
This implements the function from scratch, using "case-folding,"
which is a more robust method that deals with various languages. It
involves a hashtable of a few hundred codepoints that are "uppercase" and
how to map them to lowercase equivalents (possibly increasing the size of
the string in the process). The vast majority of human languages (and
Unicode) do not have letters with different cases, but still, this static
table takes about 10 kilobytes on a 64-bit machine.
Even this will fail in one known case: the Turkish 'i' folds differently
if you're writing in Turkish vs other languages. Generally this is seen as
unfortunate collateral damage in cases where you can't specify the language
in use.
In addition to case-folding the codepoints, the new functions also know how
to decode the various formats to turn them into codepoints in the first
place, instead of blindly stepping by one byte (or one wchar_t) per
character.
Also included is casefolding.txt from the Unicode Consortium and a perl
script to generate the hashtable from that text file, so we can trivially
update this if new languages are added in the future.
A simple test using the new function:
```c
#include <SDL3/SDL.h>
int main(void)
{
const char *a = "α ε η";
const char *b = "Α Ε Η";
SDL_Log(" strcasecmp(\"%s\", \"%s\") == %d\n", a, b, strcasecmp(a, b));
SDL_Log("SDL_strcasecmp(\"%s\", \"%s\") == %d\n", a, b, SDL_strcasecmp(a, b));
return 0;
}
```
Produces:
```
INFO: strcasecmp("α ε η", "Α Ε Η") == 32
INFO: SDL_strcasecmp("α ε η", "Α Ε Η") == 0
```
glibc strcasecmp() fails to compare a Greek lowercase string to its uppercase
equivalent, even with a UTF-8 locale, but SDL_strcasecmp() works.
Other SDL_stdinc.h functions are changed to be more consistent, which is to
say they now ignore any C runtime and often dictate that only English-based
low-ASCII works with them.
Fixes Issue #9313.
2024-03-26 13:22:38 -04:00
|
|
|
#if (SDL_SIZEOF_WCHAR_T == 2)
|
|
|
|
|
const Uint16 *str1 = (const Uint16 *) wstr1;
|
|
|
|
|
const Uint16 *str2 = (const Uint16 *) wstr2;
|
|
|
|
|
UNICODE_STRCASECMP(16, 2, 2, (void) str1start, (void) str2start); // always NULL-terminated, no need to adjust lengths.
|
|
|
|
|
#elif (SDL_SIZEOF_WCHAR_T == 4)
|
|
|
|
|
const Uint32 *str1 = (const Uint32 *) wstr1;
|
|
|
|
|
const Uint32 *str2 = (const Uint32 *) wstr2;
|
|
|
|
|
UNICODE_STRCASECMP(32, 1, 1, (void) str1start, (void) str2start); // always NULL-terminated, no need to adjust lengths.
|
2020-11-24 12:43:01 -08:00
|
|
|
#else
|
stdlib: Improve Unicode support and consistency in string comparison functions.
SDL_strcasecmp (even when calling into a C runtime) does not work with
Unicode chars, and depending on the user's locale, might not work with
even basic ASCII strings.
This implements the function from scratch, using "case-folding,"
which is a more robust method that deals with various languages. It
involves a hashtable of a few hundred codepoints that are "uppercase" and
how to map them to lowercase equivalents (possibly increasing the size of
the string in the process). The vast majority of human languages (and
Unicode) do not have letters with different cases, but still, this static
table takes about 10 kilobytes on a 64-bit machine.
Even this will fail in one known case: the Turkish 'i' folds differently
if you're writing in Turkish vs other languages. Generally this is seen as
unfortunate collateral damage in cases where you can't specify the language
in use.
In addition to case-folding the codepoints, the new functions also know how
to decode the various formats to turn them into codepoints in the first
place, instead of blindly stepping by one byte (or one wchar_t) per
character.
Also included is casefolding.txt from the Unicode Consortium and a perl
script to generate the hashtable from that text file, so we can trivially
update this if new languages are added in the future.
A simple test using the new function:
```c
#include <SDL3/SDL.h>
int main(void)
{
const char *a = "α ε η";
const char *b = "Α Ε Η";
SDL_Log(" strcasecmp(\"%s\", \"%s\") == %d\n", a, b, strcasecmp(a, b));
SDL_Log("SDL_strcasecmp(\"%s\", \"%s\") == %d\n", a, b, SDL_strcasecmp(a, b));
return 0;
}
```
Produces:
```
INFO: strcasecmp("α ε η", "Α Ε Η") == 32
INFO: SDL_strcasecmp("α ε η", "Α Ε Η") == 0
```
glibc strcasecmp() fails to compare a Greek lowercase string to its uppercase
equivalent, even with a UTF-8 locale, but SDL_strcasecmp() works.
Other SDL_stdinc.h functions are changed to be more consistent, which is to
say they now ignore any C runtime and often dictate that only English-based
low-ASCII works with them.
Fixes Issue #9313.
2024-03-26 13:22:38 -04:00
|
|
|
#error Unexpected wchar_t size
|
|
|
|
|
return -1;
|
2024-04-01 14:30:11 -07:00
|
|
|
#endif
|
2020-11-24 12:43:01 -08:00
|
|
|
}
|
|
|
|
|
|
stdlib: Improve Unicode support and consistency in string comparison functions.
SDL_strcasecmp (even when calling into a C runtime) does not work with
Unicode chars, and depending on the user's locale, might not work with
even basic ASCII strings.
This implements the function from scratch, using "case-folding,"
which is a more robust method that deals with various languages. It
involves a hashtable of a few hundred codepoints that are "uppercase" and
how to map them to lowercase equivalents (possibly increasing the size of
the string in the process). The vast majority of human languages (and
Unicode) do not have letters with different cases, but still, this static
table takes about 10 kilobytes on a 64-bit machine.
Even this will fail in one known case: the Turkish 'i' folds differently
if you're writing in Turkish vs other languages. Generally this is seen as
unfortunate collateral damage in cases where you can't specify the language
in use.
In addition to case-folding the codepoints, the new functions also know how
to decode the various formats to turn them into codepoints in the first
place, instead of blindly stepping by one byte (or one wchar_t) per
character.
Also included is casefolding.txt from the Unicode Consortium and a perl
script to generate the hashtable from that text file, so we can trivially
update this if new languages are added in the future.
A simple test using the new function:
```c
#include <SDL3/SDL.h>
int main(void)
{
const char *a = "α ε η";
const char *b = "Α Ε Η";
SDL_Log(" strcasecmp(\"%s\", \"%s\") == %d\n", a, b, strcasecmp(a, b));
SDL_Log("SDL_strcasecmp(\"%s\", \"%s\") == %d\n", a, b, SDL_strcasecmp(a, b));
return 0;
}
```
Produces:
```
INFO: strcasecmp("α ε η", "Α Ε Η") == 32
INFO: SDL_strcasecmp("α ε η", "Α Ε Η") == 0
```
glibc strcasecmp() fails to compare a Greek lowercase string to its uppercase
equivalent, even with a UTF-8 locale, but SDL_strcasecmp() works.
Other SDL_stdinc.h functions are changed to be more consistent, which is to
say they now ignore any C runtime and often dictate that only English-based
low-ASCII works with them.
Fixes Issue #9313.
2024-03-26 13:22:38 -04:00
|
|
|
int SDL_wcsncasecmp(const wchar_t *wstr1, const wchar_t *wstr2, size_t maxlen)
|
2020-11-24 12:43:01 -08:00
|
|
|
{
|
stdlib: Improve Unicode support and consistency in string comparison functions.
SDL_strcasecmp (even when calling into a C runtime) does not work with
Unicode chars, and depending on the user's locale, might not work with
even basic ASCII strings.
This implements the function from scratch, using "case-folding,"
which is a more robust method that deals with various languages. It
involves a hashtable of a few hundred codepoints that are "uppercase" and
how to map them to lowercase equivalents (possibly increasing the size of
the string in the process). The vast majority of human languages (and
Unicode) do not have letters with different cases, but still, this static
table takes about 10 kilobytes on a 64-bit machine.
Even this will fail in one known case: the Turkish 'i' folds differently
if you're writing in Turkish vs other languages. Generally this is seen as
unfortunate collateral damage in cases where you can't specify the language
in use.
In addition to case-folding the codepoints, the new functions also know how
to decode the various formats to turn them into codepoints in the first
place, instead of blindly stepping by one byte (or one wchar_t) per
character.
Also included is casefolding.txt from the Unicode Consortium and a perl
script to generate the hashtable from that text file, so we can trivially
update this if new languages are added in the future.
A simple test using the new function:
```c
#include <SDL3/SDL.h>
int main(void)
{
const char *a = "α ε η";
const char *b = "Α Ε Η";
SDL_Log(" strcasecmp(\"%s\", \"%s\") == %d\n", a, b, strcasecmp(a, b));
SDL_Log("SDL_strcasecmp(\"%s\", \"%s\") == %d\n", a, b, SDL_strcasecmp(a, b));
return 0;
}
```
Produces:
```
INFO: strcasecmp("α ε η", "Α Ε Η") == 32
INFO: SDL_strcasecmp("α ε η", "Α Ε Η") == 0
```
glibc strcasecmp() fails to compare a Greek lowercase string to its uppercase
equivalent, even with a UTF-8 locale, but SDL_strcasecmp() works.
Other SDL_stdinc.h functions are changed to be more consistent, which is to
say they now ignore any C runtime and often dictate that only English-based
low-ASCII works with them.
Fixes Issue #9313.
2024-03-26 13:22:38 -04:00
|
|
|
size_t slen1 = maxlen;
|
|
|
|
|
size_t slen2 = maxlen;
|
|
|
|
|
|
|
|
|
|
#if (SDL_SIZEOF_WCHAR_T == 2)
|
|
|
|
|
const Uint16 *str1 = (const Uint16 *) wstr1;
|
|
|
|
|
const Uint16 *str2 = (const Uint16 *) wstr2;
|
|
|
|
|
UNICODE_STRCASECMP(16, slen1, slen2, slen1 -= (size_t) (str1 - str1start), slen2 -= (size_t) (str2 - str2start));
|
|
|
|
|
#elif (SDL_SIZEOF_WCHAR_T == 4)
|
|
|
|
|
const Uint32 *str1 = (const Uint32 *) wstr1;
|
|
|
|
|
const Uint32 *str2 = (const Uint32 *) wstr2;
|
|
|
|
|
UNICODE_STRCASECMP(32, slen1, slen2, slen1 -= (size_t) (str1 - str1start), slen2 -= (size_t) (str2 - str2start));
|
2020-11-24 12:43:01 -08:00
|
|
|
#else
|
stdlib: Improve Unicode support and consistency in string comparison functions.
SDL_strcasecmp (even when calling into a C runtime) does not work with
Unicode chars, and depending on the user's locale, might not work with
even basic ASCII strings.
This implements the function from scratch, using "case-folding,"
which is a more robust method that deals with various languages. It
involves a hashtable of a few hundred codepoints that are "uppercase" and
how to map them to lowercase equivalents (possibly increasing the size of
the string in the process). The vast majority of human languages (and
Unicode) do not have letters with different cases, but still, this static
table takes about 10 kilobytes on a 64-bit machine.
Even this will fail in one known case: the Turkish 'i' folds differently
if you're writing in Turkish vs other languages. Generally this is seen as
unfortunate collateral damage in cases where you can't specify the language
in use.
In addition to case-folding the codepoints, the new functions also know how
to decode the various formats to turn them into codepoints in the first
place, instead of blindly stepping by one byte (or one wchar_t) per
character.
Also included is casefolding.txt from the Unicode Consortium and a perl
script to generate the hashtable from that text file, so we can trivially
update this if new languages are added in the future.
A simple test using the new function:
```c
#include <SDL3/SDL.h>
int main(void)
{
const char *a = "α ε η";
const char *b = "Α Ε Η";
SDL_Log(" strcasecmp(\"%s\", \"%s\") == %d\n", a, b, strcasecmp(a, b));
SDL_Log("SDL_strcasecmp(\"%s\", \"%s\") == %d\n", a, b, SDL_strcasecmp(a, b));
return 0;
}
```
Produces:
```
INFO: strcasecmp("α ε η", "Α Ε Η") == 32
INFO: SDL_strcasecmp("α ε η", "Α Ε Η") == 0
```
glibc strcasecmp() fails to compare a Greek lowercase string to its uppercase
equivalent, even with a UTF-8 locale, but SDL_strcasecmp() works.
Other SDL_stdinc.h functions are changed to be more consistent, which is to
say they now ignore any C runtime and often dictate that only English-based
low-ASCII works with them.
Fixes Issue #9313.
2024-03-26 13:22:38 -04:00
|
|
|
#error Unexpected wchar_t size
|
|
|
|
|
return -1;
|
2024-04-01 14:30:11 -07:00
|
|
|
#endif
|
2020-11-24 12:43:01 -08:00
|
|
|
}
|
|
|
|
|
|
2023-05-24 08:14:47 -07:00
|
|
|
long SDL_wcstol(const wchar_t *string, wchar_t **endp, int base)
|
|
|
|
|
{
|
|
|
|
|
#ifdef HAVE_WCSTOL
|
|
|
|
|
return wcstol(string, endp, base);
|
|
|
|
|
#else
|
|
|
|
|
long value = 0;
|
2024-09-10 21:50:48 +02:00
|
|
|
size_t len = SDL_ScanLongW(string, 0, base, &value);
|
2023-05-24 08:14:47 -07:00
|
|
|
if (endp) {
|
|
|
|
|
*endp = (wchar_t *)string + len;
|
|
|
|
|
}
|
|
|
|
|
return value;
|
Use C++ style comments consistently in SDL source code
Implemented using this script:
find . -type f -exec sed -i'' -e 's,/\* *\([^*]*\)\*/ *$,// \1,' -e 's, \+$,,' {} \;
git checkout \
core/linux/SDL_evdev_kbd_default_keymap.h \
events/imKStoUCS.* \
hidapi \
joystick/controller_type.c \
joystick/controller_type.h \
joystick/hidapi/steam/controller_constants.h \
joystick/hidapi/steam/controller_structs.h \
joystick/SDL_gamepad_db.h \
libm \
render/*/*Shader*.h \
render/vitagxm/SDL_render_vita_gxm_shaders.h \
render/metal/SDL_shaders_metal_*.h \
stdlib/SDL_malloc.c \
stdlib/SDL_qsort.c \
stdlib/SDL_strtokr.c \
test/ \
video/directx/SDL_d3d12_xbox_cmacros.h \
video/directx/d3d12.h \
video/directx/d3d12sdklayers.h \
video/khronos \
video/x11/edid-parse.c \
video/x11/xsettings-client.* \
video/yuv2rgb
sed -i'' -e 's,/\* *\([^*]*\)\*/ *$,// \1,' -e 's, \+$,,' hidapi/SDL_hidapi.c
2024-08-22 10:30:45 -07:00
|
|
|
#endif // HAVE_WCSTOL
|
2023-05-24 08:14:47 -07:00
|
|
|
}
|
|
|
|
|
|
2023-05-23 11:29:41 -07:00
|
|
|
size_t SDL_strlcpy(SDL_OUT_Z_CAP(maxlen) char *dst, const char *src, size_t maxlen)
|
2015-06-21 17:33:46 +02:00
|
|
|
{
|
2023-03-30 20:26:31 +02:00
|
|
|
#ifdef HAVE_STRLCPY
|
2015-06-21 17:33:46 +02:00
|
|
|
return strlcpy(dst, src, maxlen);
|
|
|
|
|
#else
|
|
|
|
|
size_t srclen = SDL_strlen(src);
|
|
|
|
|
if (maxlen > 0) {
|
|
|
|
|
size_t len = SDL_min(srclen, maxlen - 1);
|
|
|
|
|
SDL_memcpy(dst, src, len);
|
|
|
|
|
dst[len] = '\0';
|
|
|
|
|
}
|
|
|
|
|
return srclen;
|
Use C++ style comments consistently in SDL source code
Implemented using this script:
find . -type f -exec sed -i'' -e 's,/\* *\([^*]*\)\*/ *$,// \1,' -e 's, \+$,,' {} \;
git checkout \
core/linux/SDL_evdev_kbd_default_keymap.h \
events/imKStoUCS.* \
hidapi \
joystick/controller_type.c \
joystick/controller_type.h \
joystick/hidapi/steam/controller_constants.h \
joystick/hidapi/steam/controller_structs.h \
joystick/SDL_gamepad_db.h \
libm \
render/*/*Shader*.h \
render/vitagxm/SDL_render_vita_gxm_shaders.h \
render/metal/SDL_shaders_metal_*.h \
stdlib/SDL_malloc.c \
stdlib/SDL_qsort.c \
stdlib/SDL_strtokr.c \
test/ \
video/directx/SDL_d3d12_xbox_cmacros.h \
video/directx/d3d12.h \
video/directx/d3d12sdklayers.h \
video/khronos \
video/x11/edid-parse.c \
video/x11/xsettings-client.* \
video/yuv2rgb
sed -i'' -e 's,/\* *\([^*]*\)\*/ *$,// \1,' -e 's, \+$,,' hidapi/SDL_hidapi.c
2024-08-22 10:30:45 -07:00
|
|
|
#endif // HAVE_STRLCPY
|
2015-06-21 17:33:46 +02:00
|
|
|
}
|
|
|
|
|
|
2023-05-23 11:29:41 -07:00
|
|
|
size_t SDL_utf8strlcpy(SDL_OUT_Z_CAP(dst_bytes) char *dst, const char *src, size_t dst_bytes)
|
2015-06-21 17:33:46 +02:00
|
|
|
{
|
2024-09-09 10:41:49 -07:00
|
|
|
size_t bytes = 0;
|
|
|
|
|
|
|
|
|
|
if (dst_bytes > 0) {
|
|
|
|
|
size_t src_bytes = SDL_strlen(src);
|
|
|
|
|
size_t i = 0;
|
|
|
|
|
size_t trailing_bytes = 0;
|
|
|
|
|
|
|
|
|
|
bytes = SDL_min(src_bytes, dst_bytes - 1);
|
|
|
|
|
if (bytes) {
|
|
|
|
|
unsigned char c = (unsigned char)src[bytes - 1];
|
|
|
|
|
if (UTF8_IsLeadByte(c)) {
|
|
|
|
|
--bytes;
|
|
|
|
|
} else if (UTF8_IsTrailingByte(c)) {
|
|
|
|
|
for (i = bytes - 1; i != 0; --i) {
|
|
|
|
|
c = (unsigned char)src[i];
|
|
|
|
|
trailing_bytes = UTF8_GetTrailingBytes(c);
|
|
|
|
|
if (trailing_bytes) {
|
|
|
|
|
if ((bytes - i) != (trailing_bytes + 1)) {
|
|
|
|
|
bytes = i;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
SDL_memcpy(dst, src, bytes);
|
|
|
|
|
}
|
|
|
|
|
dst[bytes] = '\0';
|
|
|
|
|
}
|
2022-03-04 11:01:55 -08:00
|
|
|
|
2015-06-21 17:33:46 +02:00
|
|
|
return bytes;
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-23 11:29:41 -07:00
|
|
|
size_t SDL_utf8strlen(const char *str)
|
2017-05-29 03:01:05 -04:00
|
|
|
{
|
2024-08-22 17:33:49 -07:00
|
|
|
size_t result = 0;
|
2024-06-26 21:32:45 -04:00
|
|
|
while (SDL_StepUTF8(&str, NULL)) {
|
2024-08-22 17:33:49 -07:00
|
|
|
result++;
|
2017-05-29 03:01:05 -04:00
|
|
|
}
|
2024-08-22 17:33:49 -07:00
|
|
|
return result;
|
2017-05-29 03:01:05 -04:00
|
|
|
}
|
|
|
|
|
|
2023-05-23 11:29:41 -07:00
|
|
|
size_t SDL_utf8strnlen(const char *str, size_t bytes)
|
2022-05-05 02:23:05 +02:00
|
|
|
{
|
2024-08-22 17:33:49 -07:00
|
|
|
size_t result = 0;
|
2024-06-26 21:32:45 -04:00
|
|
|
while (SDL_StepUTF8(&str, &bytes)) {
|
2024-08-22 17:33:49 -07:00
|
|
|
result++;
|
2022-05-05 02:23:05 +02:00
|
|
|
}
|
2024-08-22 17:33:49 -07:00
|
|
|
return result;
|
2022-05-05 02:23:05 +02:00
|
|
|
}
|
|
|
|
|
|
2023-05-23 11:29:41 -07:00
|
|
|
size_t SDL_strlcat(SDL_INOUT_Z_CAP(maxlen) char *dst, const char *src, size_t maxlen)
|
2015-06-21 17:33:46 +02:00
|
|
|
{
|
2023-03-30 20:26:31 +02:00
|
|
|
#ifdef HAVE_STRLCAT
|
2015-06-21 17:33:46 +02:00
|
|
|
return strlcat(dst, src, maxlen);
|
|
|
|
|
#else
|
|
|
|
|
size_t dstlen = SDL_strlen(dst);
|
|
|
|
|
size_t srclen = SDL_strlen(src);
|
|
|
|
|
if (dstlen < maxlen) {
|
|
|
|
|
SDL_strlcpy(dst + dstlen, src, maxlen - dstlen);
|
|
|
|
|
}
|
|
|
|
|
return dstlen + srclen;
|
Use C++ style comments consistently in SDL source code
Implemented using this script:
find . -type f -exec sed -i'' -e 's,/\* *\([^*]*\)\*/ *$,// \1,' -e 's, \+$,,' {} \;
git checkout \
core/linux/SDL_evdev_kbd_default_keymap.h \
events/imKStoUCS.* \
hidapi \
joystick/controller_type.c \
joystick/controller_type.h \
joystick/hidapi/steam/controller_constants.h \
joystick/hidapi/steam/controller_structs.h \
joystick/SDL_gamepad_db.h \
libm \
render/*/*Shader*.h \
render/vitagxm/SDL_render_vita_gxm_shaders.h \
render/metal/SDL_shaders_metal_*.h \
stdlib/SDL_malloc.c \
stdlib/SDL_qsort.c \
stdlib/SDL_strtokr.c \
test/ \
video/directx/SDL_d3d12_xbox_cmacros.h \
video/directx/d3d12.h \
video/directx/d3d12sdklayers.h \
video/khronos \
video/x11/edid-parse.c \
video/x11/xsettings-client.* \
video/yuv2rgb
sed -i'' -e 's,/\* *\([^*]*\)\*/ *$,// \1,' -e 's, \+$,,' hidapi/SDL_hidapi.c
2024-08-22 10:30:45 -07:00
|
|
|
#endif // HAVE_STRLCAT
|
2015-06-21 17:33:46 +02:00
|
|
|
}
|
|
|
|
|
|
2023-05-23 11:29:41 -07:00
|
|
|
char *SDL_strdup(const char *string)
|
2015-06-21 17:33:46 +02:00
|
|
|
{
|
|
|
|
|
size_t len = SDL_strlen(string) + 1;
|
2018-08-09 16:00:17 -07:00
|
|
|
char *newstr = (char *)SDL_malloc(len);
|
2015-06-21 17:33:46 +02:00
|
|
|
if (newstr) {
|
2018-02-13 08:13:29 -08:00
|
|
|
SDL_memcpy(newstr, string, len);
|
2015-06-21 17:33:46 +02:00
|
|
|
}
|
|
|
|
|
return newstr;
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-15 09:12:39 -07:00
|
|
|
char *SDL_strndup(const char *string, size_t maxlen)
|
|
|
|
|
{
|
2023-07-17 18:10:57 -07:00
|
|
|
size_t len = SDL_strnlen(string, maxlen);
|
|
|
|
|
char *newstr = (char *)SDL_malloc(len + 1);
|
2023-07-15 09:12:39 -07:00
|
|
|
if (newstr) {
|
|
|
|
|
SDL_memcpy(newstr, string, len);
|
2023-07-17 18:10:57 -07:00
|
|
|
newstr[len] = '\0';
|
2023-07-15 09:12:39 -07:00
|
|
|
}
|
|
|
|
|
return newstr;
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-23 11:29:41 -07:00
|
|
|
char *SDL_strrev(char *string)
|
2015-06-21 17:33:46 +02:00
|
|
|
{
|
2023-03-30 20:26:31 +02:00
|
|
|
#ifdef HAVE__STRREV
|
2015-06-21 17:33:46 +02:00
|
|
|
return _strrev(string);
|
|
|
|
|
#else
|
|
|
|
|
size_t len = SDL_strlen(string);
|
|
|
|
|
char *a = &string[0];
|
|
|
|
|
char *b = &string[len - 1];
|
|
|
|
|
len /= 2;
|
|
|
|
|
while (len--) {
|
Use C++ style comments consistently in SDL source code
Implemented using this script:
find . -type f -exec sed -i'' -e 's,/\* *\([^*]*\)\*/ *$,// \1,' -e 's, \+$,,' {} \;
git checkout \
core/linux/SDL_evdev_kbd_default_keymap.h \
events/imKStoUCS.* \
hidapi \
joystick/controller_type.c \
joystick/controller_type.h \
joystick/hidapi/steam/controller_constants.h \
joystick/hidapi/steam/controller_structs.h \
joystick/SDL_gamepad_db.h \
libm \
render/*/*Shader*.h \
render/vitagxm/SDL_render_vita_gxm_shaders.h \
render/metal/SDL_shaders_metal_*.h \
stdlib/SDL_malloc.c \
stdlib/SDL_qsort.c \
stdlib/SDL_strtokr.c \
test/ \
video/directx/SDL_d3d12_xbox_cmacros.h \
video/directx/d3d12.h \
video/directx/d3d12sdklayers.h \
video/khronos \
video/x11/edid-parse.c \
video/x11/xsettings-client.* \
video/yuv2rgb
sed -i'' -e 's,/\* *\([^*]*\)\*/ *$,// \1,' -e 's, \+$,,' hidapi/SDL_hidapi.c
2024-08-22 10:30:45 -07:00
|
|
|
const char c = *a; // NOLINT(clang-analyzer-core.uninitialized.Assign)
|
2015-06-21 17:33:46 +02:00
|
|
|
*a++ = *b;
|
|
|
|
|
*b-- = c;
|
|
|
|
|
}
|
|
|
|
|
return string;
|
Use C++ style comments consistently in SDL source code
Implemented using this script:
find . -type f -exec sed -i'' -e 's,/\* *\([^*]*\)\*/ *$,// \1,' -e 's, \+$,,' {} \;
git checkout \
core/linux/SDL_evdev_kbd_default_keymap.h \
events/imKStoUCS.* \
hidapi \
joystick/controller_type.c \
joystick/controller_type.h \
joystick/hidapi/steam/controller_constants.h \
joystick/hidapi/steam/controller_structs.h \
joystick/SDL_gamepad_db.h \
libm \
render/*/*Shader*.h \
render/vitagxm/SDL_render_vita_gxm_shaders.h \
render/metal/SDL_shaders_metal_*.h \
stdlib/SDL_malloc.c \
stdlib/SDL_qsort.c \
stdlib/SDL_strtokr.c \
test/ \
video/directx/SDL_d3d12_xbox_cmacros.h \
video/directx/d3d12.h \
video/directx/d3d12sdklayers.h \
video/khronos \
video/x11/edid-parse.c \
video/x11/xsettings-client.* \
video/yuv2rgb
sed -i'' -e 's,/\* *\([^*]*\)\*/ *$,// \1,' -e 's, \+$,,' hidapi/SDL_hidapi.c
2024-08-22 10:30:45 -07:00
|
|
|
#endif // HAVE__STRREV
|
2015-06-21 17:33:46 +02:00
|
|
|
}
|
|
|
|
|
|
2023-05-23 11:29:41 -07:00
|
|
|
char *SDL_strupr(char *string)
|
2015-06-21 17:33:46 +02:00
|
|
|
{
|
|
|
|
|
char *bufp = string;
|
|
|
|
|
while (*bufp) {
|
2023-03-30 14:04:32 -07:00
|
|
|
*bufp = (char)SDL_toupper((unsigned char)*bufp);
|
2015-06-21 17:33:46 +02:00
|
|
|
++bufp;
|
|
|
|
|
}
|
|
|
|
|
return string;
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-23 11:29:41 -07:00
|
|
|
char *SDL_strlwr(char *string)
|
2015-06-21 17:33:46 +02:00
|
|
|
{
|
|
|
|
|
char *bufp = string;
|
|
|
|
|
while (*bufp) {
|
2023-03-30 14:04:32 -07:00
|
|
|
*bufp = (char)SDL_tolower((unsigned char)*bufp);
|
2015-06-21 17:33:46 +02:00
|
|
|
++bufp;
|
|
|
|
|
}
|
|
|
|
|
return string;
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-23 11:29:41 -07:00
|
|
|
char *SDL_strchr(const char *string, int c)
|
2015-06-21 17:33:46 +02:00
|
|
|
{
|
|
|
|
|
#ifdef HAVE_STRCHR
|
2022-11-27 17:38:43 +01:00
|
|
|
return SDL_const_cast(char *, strchr(string, c));
|
2015-06-21 17:33:46 +02:00
|
|
|
#elif defined(HAVE_INDEX)
|
2022-11-27 17:38:43 +01:00
|
|
|
return SDL_const_cast(char *, index(string, c));
|
2015-06-21 17:33:46 +02:00
|
|
|
#else
|
|
|
|
|
while (*string) {
|
|
|
|
|
if (*string == c) {
|
2022-11-27 17:38:43 +01:00
|
|
|
return (char *)string;
|
2015-06-21 17:33:46 +02:00
|
|
|
}
|
|
|
|
|
++string;
|
|
|
|
|
}
|
2022-07-04 16:42:46 +02:00
|
|
|
if (c == '\0') {
|
2022-11-27 17:38:43 +01:00
|
|
|
return (char *)string;
|
2022-07-04 16:42:46 +02:00
|
|
|
}
|
2015-06-21 17:33:46 +02:00
|
|
|
return NULL;
|
Use C++ style comments consistently in SDL source code
Implemented using this script:
find . -type f -exec sed -i'' -e 's,/\* *\([^*]*\)\*/ *$,// \1,' -e 's, \+$,,' {} \;
git checkout \
core/linux/SDL_evdev_kbd_default_keymap.h \
events/imKStoUCS.* \
hidapi \
joystick/controller_type.c \
joystick/controller_type.h \
joystick/hidapi/steam/controller_constants.h \
joystick/hidapi/steam/controller_structs.h \
joystick/SDL_gamepad_db.h \
libm \
render/*/*Shader*.h \
render/vitagxm/SDL_render_vita_gxm_shaders.h \
render/metal/SDL_shaders_metal_*.h \
stdlib/SDL_malloc.c \
stdlib/SDL_qsort.c \
stdlib/SDL_strtokr.c \
test/ \
video/directx/SDL_d3d12_xbox_cmacros.h \
video/directx/d3d12.h \
video/directx/d3d12sdklayers.h \
video/khronos \
video/x11/edid-parse.c \
video/x11/xsettings-client.* \
video/yuv2rgb
sed -i'' -e 's,/\* *\([^*]*\)\*/ *$,// \1,' -e 's, \+$,,' hidapi/SDL_hidapi.c
2024-08-22 10:30:45 -07:00
|
|
|
#endif // HAVE_STRCHR
|
2015-06-21 17:33:46 +02:00
|
|
|
}
|
|
|
|
|
|
2023-05-23 11:29:41 -07:00
|
|
|
char *SDL_strrchr(const char *string, int c)
|
2015-06-21 17:33:46 +02:00
|
|
|
{
|
|
|
|
|
#ifdef HAVE_STRRCHR
|
2022-11-27 17:38:43 +01:00
|
|
|
return SDL_const_cast(char *, strrchr(string, c));
|
2015-06-21 17:33:46 +02:00
|
|
|
#elif defined(HAVE_RINDEX)
|
2022-11-27 17:38:43 +01:00
|
|
|
return SDL_const_cast(char *, rindex(string, c));
|
2015-06-21 17:33:46 +02:00
|
|
|
#else
|
2022-07-04 16:42:46 +02:00
|
|
|
const char *bufp = string + SDL_strlen(string);
|
2015-06-21 17:33:46 +02:00
|
|
|
while (bufp >= string) {
|
|
|
|
|
if (*bufp == c) {
|
2022-11-27 17:38:43 +01:00
|
|
|
return (char *)bufp;
|
2015-06-21 17:33:46 +02:00
|
|
|
}
|
|
|
|
|
--bufp;
|
|
|
|
|
}
|
|
|
|
|
return NULL;
|
Use C++ style comments consistently in SDL source code
Implemented using this script:
find . -type f -exec sed -i'' -e 's,/\* *\([^*]*\)\*/ *$,// \1,' -e 's, \+$,,' {} \;
git checkout \
core/linux/SDL_evdev_kbd_default_keymap.h \
events/imKStoUCS.* \
hidapi \
joystick/controller_type.c \
joystick/controller_type.h \
joystick/hidapi/steam/controller_constants.h \
joystick/hidapi/steam/controller_structs.h \
joystick/SDL_gamepad_db.h \
libm \
render/*/*Shader*.h \
render/vitagxm/SDL_render_vita_gxm_shaders.h \
render/metal/SDL_shaders_metal_*.h \
stdlib/SDL_malloc.c \
stdlib/SDL_qsort.c \
stdlib/SDL_strtokr.c \
test/ \
video/directx/SDL_d3d12_xbox_cmacros.h \
video/directx/d3d12.h \
video/directx/d3d12sdklayers.h \
video/khronos \
video/x11/edid-parse.c \
video/x11/xsettings-client.* \
video/yuv2rgb
sed -i'' -e 's,/\* *\([^*]*\)\*/ *$,// \1,' -e 's, \+$,,' hidapi/SDL_hidapi.c
2024-08-22 10:30:45 -07:00
|
|
|
#endif // HAVE_STRRCHR
|
2015-06-21 17:33:46 +02:00
|
|
|
}
|
|
|
|
|
|
2023-12-03 14:42:28 -08:00
|
|
|
char *SDL_strnstr(const char *haystack, const char *needle, size_t maxlen)
|
2015-06-21 17:33:46 +02:00
|
|
|
{
|
2023-12-03 14:42:28 -08:00
|
|
|
#ifdef HAVE_STRNSTR
|
|
|
|
|
return SDL_const_cast(char *, strnstr(haystack, needle, maxlen));
|
2015-06-21 17:33:46 +02:00
|
|
|
#else
|
|
|
|
|
size_t length = SDL_strlen(needle);
|
2023-12-03 14:42:28 -08:00
|
|
|
if (length == 0) {
|
|
|
|
|
return (char *)haystack;
|
|
|
|
|
}
|
|
|
|
|
while (maxlen >= length && *haystack) {
|
2015-06-21 17:33:46 +02:00
|
|
|
if (SDL_strncmp(haystack, needle, length) == 0) {
|
2022-11-27 17:38:43 +01:00
|
|
|
return (char *)haystack;
|
2015-06-21 17:33:46 +02:00
|
|
|
}
|
|
|
|
|
++haystack;
|
2023-12-03 14:42:28 -08:00
|
|
|
--maxlen;
|
2015-06-21 17:33:46 +02:00
|
|
|
}
|
|
|
|
|
return NULL;
|
Use C++ style comments consistently in SDL source code
Implemented using this script:
find . -type f -exec sed -i'' -e 's,/\* *\([^*]*\)\*/ *$,// \1,' -e 's, \+$,,' {} \;
git checkout \
core/linux/SDL_evdev_kbd_default_keymap.h \
events/imKStoUCS.* \
hidapi \
joystick/controller_type.c \
joystick/controller_type.h \
joystick/hidapi/steam/controller_constants.h \
joystick/hidapi/steam/controller_structs.h \
joystick/SDL_gamepad_db.h \
libm \
render/*/*Shader*.h \
render/vitagxm/SDL_render_vita_gxm_shaders.h \
render/metal/SDL_shaders_metal_*.h \
stdlib/SDL_malloc.c \
stdlib/SDL_qsort.c \
stdlib/SDL_strtokr.c \
test/ \
video/directx/SDL_d3d12_xbox_cmacros.h \
video/directx/d3d12.h \
video/directx/d3d12sdklayers.h \
video/khronos \
video/x11/edid-parse.c \
video/x11/xsettings-client.* \
video/yuv2rgb
sed -i'' -e 's,/\* *\([^*]*\)\*/ *$,// \1,' -e 's, \+$,,' hidapi/SDL_hidapi.c
2024-08-22 10:30:45 -07:00
|
|
|
#endif // HAVE_STRSTR
|
2015-06-21 17:33:46 +02:00
|
|
|
}
|
|
|
|
|
|
2023-12-03 14:42:28 -08:00
|
|
|
char *SDL_strstr(const char *haystack, const char *needle)
|
|
|
|
|
{
|
|
|
|
|
#ifdef HAVE_STRSTR
|
|
|
|
|
return SDL_const_cast(char *, strstr(haystack, needle));
|
|
|
|
|
#else
|
|
|
|
|
return SDL_strnstr(haystack, needle, SDL_strlen(haystack));
|
Use C++ style comments consistently in SDL source code
Implemented using this script:
find . -type f -exec sed -i'' -e 's,/\* *\([^*]*\)\*/ *$,// \1,' -e 's, \+$,,' {} \;
git checkout \
core/linux/SDL_evdev_kbd_default_keymap.h \
events/imKStoUCS.* \
hidapi \
joystick/controller_type.c \
joystick/controller_type.h \
joystick/hidapi/steam/controller_constants.h \
joystick/hidapi/steam/controller_structs.h \
joystick/SDL_gamepad_db.h \
libm \
render/*/*Shader*.h \
render/vitagxm/SDL_render_vita_gxm_shaders.h \
render/metal/SDL_shaders_metal_*.h \
stdlib/SDL_malloc.c \
stdlib/SDL_qsort.c \
stdlib/SDL_strtokr.c \
test/ \
video/directx/SDL_d3d12_xbox_cmacros.h \
video/directx/d3d12.h \
video/directx/d3d12sdklayers.h \
video/khronos \
video/x11/edid-parse.c \
video/x11/xsettings-client.* \
video/yuv2rgb
sed -i'' -e 's,/\* *\([^*]*\)\*/ *$,// \1,' -e 's, \+$,,' hidapi/SDL_hidapi.c
2024-08-22 10:30:45 -07:00
|
|
|
#endif // HAVE_STRSTR
|
2023-12-03 14:42:28 -08:00
|
|
|
}
|
|
|
|
|
|
2023-05-23 11:29:41 -07:00
|
|
|
char *SDL_strcasestr(const char *haystack, const char *needle)
|
2022-11-05 15:58:30 -07:00
|
|
|
{
|
stdlib: Improve Unicode support and consistency in string comparison functions.
SDL_strcasecmp (even when calling into a C runtime) does not work with
Unicode chars, and depending on the user's locale, might not work with
even basic ASCII strings.
This implements the function from scratch, using "case-folding,"
which is a more robust method that deals with various languages. It
involves a hashtable of a few hundred codepoints that are "uppercase" and
how to map them to lowercase equivalents (possibly increasing the size of
the string in the process). The vast majority of human languages (and
Unicode) do not have letters with different cases, but still, this static
table takes about 10 kilobytes on a 64-bit machine.
Even this will fail in one known case: the Turkish 'i' folds differently
if you're writing in Turkish vs other languages. Generally this is seen as
unfortunate collateral damage in cases where you can't specify the language
in use.
In addition to case-folding the codepoints, the new functions also know how
to decode the various formats to turn them into codepoints in the first
place, instead of blindly stepping by one byte (or one wchar_t) per
character.
Also included is casefolding.txt from the Unicode Consortium and a perl
script to generate the hashtable from that text file, so we can trivially
update this if new languages are added in the future.
A simple test using the new function:
```c
#include <SDL3/SDL.h>
int main(void)
{
const char *a = "α ε η";
const char *b = "Α Ε Η";
SDL_Log(" strcasecmp(\"%s\", \"%s\") == %d\n", a, b, strcasecmp(a, b));
SDL_Log("SDL_strcasecmp(\"%s\", \"%s\") == %d\n", a, b, SDL_strcasecmp(a, b));
return 0;
}
```
Produces:
```
INFO: strcasecmp("α ε η", "Α Ε Η") == 32
INFO: SDL_strcasecmp("α ε η", "Α Ε Η") == 0
```
glibc strcasecmp() fails to compare a Greek lowercase string to its uppercase
equivalent, even with a UTF-8 locale, but SDL_strcasecmp() works.
Other SDL_stdinc.h functions are changed to be more consistent, which is to
say they now ignore any C runtime and often dictate that only English-based
low-ASCII works with them.
Fixes Issue #9313.
2024-03-26 13:22:38 -04:00
|
|
|
const size_t length = SDL_strlen(needle);
|
|
|
|
|
do {
|
2022-11-05 15:58:30 -07:00
|
|
|
if (SDL_strncasecmp(haystack, needle, length) == 0) {
|
2022-11-27 17:38:43 +01:00
|
|
|
return (char *)haystack;
|
2022-11-05 15:58:30 -07:00
|
|
|
}
|
2024-06-26 21:32:45 -04:00
|
|
|
} while (SDL_StepUTF8(&haystack, NULL)); // move ahead by a full codepoint at a time, regardless of bytes.
|
stdlib: Improve Unicode support and consistency in string comparison functions.
SDL_strcasecmp (even when calling into a C runtime) does not work with
Unicode chars, and depending on the user's locale, might not work with
even basic ASCII strings.
This implements the function from scratch, using "case-folding,"
which is a more robust method that deals with various languages. It
involves a hashtable of a few hundred codepoints that are "uppercase" and
how to map them to lowercase equivalents (possibly increasing the size of
the string in the process). The vast majority of human languages (and
Unicode) do not have letters with different cases, but still, this static
table takes about 10 kilobytes on a 64-bit machine.
Even this will fail in one known case: the Turkish 'i' folds differently
if you're writing in Turkish vs other languages. Generally this is seen as
unfortunate collateral damage in cases where you can't specify the language
in use.
In addition to case-folding the codepoints, the new functions also know how
to decode the various formats to turn them into codepoints in the first
place, instead of blindly stepping by one byte (or one wchar_t) per
character.
Also included is casefolding.txt from the Unicode Consortium and a perl
script to generate the hashtable from that text file, so we can trivially
update this if new languages are added in the future.
A simple test using the new function:
```c
#include <SDL3/SDL.h>
int main(void)
{
const char *a = "α ε η";
const char *b = "Α Ε Η";
SDL_Log(" strcasecmp(\"%s\", \"%s\") == %d\n", a, b, strcasecmp(a, b));
SDL_Log("SDL_strcasecmp(\"%s\", \"%s\") == %d\n", a, b, SDL_strcasecmp(a, b));
return 0;
}
```
Produces:
```
INFO: strcasecmp("α ε η", "Α Ε Η") == 32
INFO: SDL_strcasecmp("α ε η", "Α Ε Η") == 0
```
glibc strcasecmp() fails to compare a Greek lowercase string to its uppercase
equivalent, even with a UTF-8 locale, but SDL_strcasecmp() works.
Other SDL_stdinc.h functions are changed to be more consistent, which is to
say they now ignore any C runtime and often dictate that only English-based
low-ASCII works with them.
Fixes Issue #9313.
2024-03-26 13:22:38 -04:00
|
|
|
|
2022-11-05 15:58:30 -07:00
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2015-06-21 17:33:46 +02:00
|
|
|
#if !defined(HAVE__LTOA) || !defined(HAVE__I64TOA) || \
|
|
|
|
|
!defined(HAVE__ULTOA) || !defined(HAVE__UI64TOA)
|
|
|
|
|
static const char ntoa_table[] = {
|
|
|
|
|
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
|
|
|
|
|
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
|
|
|
|
|
'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T',
|
|
|
|
|
'U', 'V', 'W', 'X', 'Y', 'Z'
|
|
|
|
|
};
|
Use C++ style comments consistently in SDL source code
Implemented using this script:
find . -type f -exec sed -i'' -e 's,/\* *\([^*]*\)\*/ *$,// \1,' -e 's, \+$,,' {} \;
git checkout \
core/linux/SDL_evdev_kbd_default_keymap.h \
events/imKStoUCS.* \
hidapi \
joystick/controller_type.c \
joystick/controller_type.h \
joystick/hidapi/steam/controller_constants.h \
joystick/hidapi/steam/controller_structs.h \
joystick/SDL_gamepad_db.h \
libm \
render/*/*Shader*.h \
render/vitagxm/SDL_render_vita_gxm_shaders.h \
render/metal/SDL_shaders_metal_*.h \
stdlib/SDL_malloc.c \
stdlib/SDL_qsort.c \
stdlib/SDL_strtokr.c \
test/ \
video/directx/SDL_d3d12_xbox_cmacros.h \
video/directx/d3d12.h \
video/directx/d3d12sdklayers.h \
video/khronos \
video/x11/edid-parse.c \
video/x11/xsettings-client.* \
video/yuv2rgb
sed -i'' -e 's,/\* *\([^*]*\)\*/ *$,// \1,' -e 's, \+$,,' hidapi/SDL_hidapi.c
2024-08-22 10:30:45 -07:00
|
|
|
#endif // ntoa() conversion table
|
2015-06-21 17:33:46 +02:00
|
|
|
|
2023-05-23 11:29:41 -07:00
|
|
|
char *SDL_itoa(int value, char *string, int radix)
|
2015-06-21 17:33:46 +02:00
|
|
|
{
|
|
|
|
|
#ifdef HAVE_ITOA
|
|
|
|
|
return itoa(value, string, radix);
|
|
|
|
|
#else
|
|
|
|
|
return SDL_ltoa((long)value, string, radix);
|
Use C++ style comments consistently in SDL source code
Implemented using this script:
find . -type f -exec sed -i'' -e 's,/\* *\([^*]*\)\*/ *$,// \1,' -e 's, \+$,,' {} \;
git checkout \
core/linux/SDL_evdev_kbd_default_keymap.h \
events/imKStoUCS.* \
hidapi \
joystick/controller_type.c \
joystick/controller_type.h \
joystick/hidapi/steam/controller_constants.h \
joystick/hidapi/steam/controller_structs.h \
joystick/SDL_gamepad_db.h \
libm \
render/*/*Shader*.h \
render/vitagxm/SDL_render_vita_gxm_shaders.h \
render/metal/SDL_shaders_metal_*.h \
stdlib/SDL_malloc.c \
stdlib/SDL_qsort.c \
stdlib/SDL_strtokr.c \
test/ \
video/directx/SDL_d3d12_xbox_cmacros.h \
video/directx/d3d12.h \
video/directx/d3d12sdklayers.h \
video/khronos \
video/x11/edid-parse.c \
video/x11/xsettings-client.* \
video/yuv2rgb
sed -i'' -e 's,/\* *\([^*]*\)\*/ *$,// \1,' -e 's, \+$,,' hidapi/SDL_hidapi.c
2024-08-22 10:30:45 -07:00
|
|
|
#endif // HAVE_ITOA
|
2015-06-21 17:33:46 +02:00
|
|
|
}
|
|
|
|
|
|
2023-05-23 11:29:41 -07:00
|
|
|
char *SDL_uitoa(unsigned int value, char *string, int radix)
|
2015-06-21 17:33:46 +02:00
|
|
|
{
|
|
|
|
|
#ifdef HAVE__UITOA
|
|
|
|
|
return _uitoa(value, string, radix);
|
|
|
|
|
#else
|
|
|
|
|
return SDL_ultoa((unsigned long)value, string, radix);
|
Use C++ style comments consistently in SDL source code
Implemented using this script:
find . -type f -exec sed -i'' -e 's,/\* *\([^*]*\)\*/ *$,// \1,' -e 's, \+$,,' {} \;
git checkout \
core/linux/SDL_evdev_kbd_default_keymap.h \
events/imKStoUCS.* \
hidapi \
joystick/controller_type.c \
joystick/controller_type.h \
joystick/hidapi/steam/controller_constants.h \
joystick/hidapi/steam/controller_structs.h \
joystick/SDL_gamepad_db.h \
libm \
render/*/*Shader*.h \
render/vitagxm/SDL_render_vita_gxm_shaders.h \
render/metal/SDL_shaders_metal_*.h \
stdlib/SDL_malloc.c \
stdlib/SDL_qsort.c \
stdlib/SDL_strtokr.c \
test/ \
video/directx/SDL_d3d12_xbox_cmacros.h \
video/directx/d3d12.h \
video/directx/d3d12sdklayers.h \
video/khronos \
video/x11/edid-parse.c \
video/x11/xsettings-client.* \
video/yuv2rgb
sed -i'' -e 's,/\* *\([^*]*\)\*/ *$,// \1,' -e 's, \+$,,' hidapi/SDL_hidapi.c
2024-08-22 10:30:45 -07:00
|
|
|
#endif // HAVE__UITOA
|
2015-06-21 17:33:46 +02:00
|
|
|
}
|
|
|
|
|
|
2023-05-23 11:29:41 -07:00
|
|
|
char *SDL_ltoa(long value, char *string, int radix)
|
2015-06-21 17:33:46 +02:00
|
|
|
{
|
2023-03-30 20:26:31 +02:00
|
|
|
#ifdef HAVE__LTOA
|
2015-06-21 17:33:46 +02:00
|
|
|
return _ltoa(value, string, radix);
|
|
|
|
|
#else
|
|
|
|
|
char *bufp = string;
|
|
|
|
|
|
|
|
|
|
if (value < 0) {
|
|
|
|
|
*bufp++ = '-';
|
|
|
|
|
SDL_ultoa(-value, bufp, radix);
|
|
|
|
|
} else {
|
|
|
|
|
SDL_ultoa(value, bufp, radix);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return string;
|
Use C++ style comments consistently in SDL source code
Implemented using this script:
find . -type f -exec sed -i'' -e 's,/\* *\([^*]*\)\*/ *$,// \1,' -e 's, \+$,,' {} \;
git checkout \
core/linux/SDL_evdev_kbd_default_keymap.h \
events/imKStoUCS.* \
hidapi \
joystick/controller_type.c \
joystick/controller_type.h \
joystick/hidapi/steam/controller_constants.h \
joystick/hidapi/steam/controller_structs.h \
joystick/SDL_gamepad_db.h \
libm \
render/*/*Shader*.h \
render/vitagxm/SDL_render_vita_gxm_shaders.h \
render/metal/SDL_shaders_metal_*.h \
stdlib/SDL_malloc.c \
stdlib/SDL_qsort.c \
stdlib/SDL_strtokr.c \
test/ \
video/directx/SDL_d3d12_xbox_cmacros.h \
video/directx/d3d12.h \
video/directx/d3d12sdklayers.h \
video/khronos \
video/x11/edid-parse.c \
video/x11/xsettings-client.* \
video/yuv2rgb
sed -i'' -e 's,/\* *\([^*]*\)\*/ *$,// \1,' -e 's, \+$,,' hidapi/SDL_hidapi.c
2024-08-22 10:30:45 -07:00
|
|
|
#endif // HAVE__LTOA
|
2015-06-21 17:33:46 +02:00
|
|
|
}
|
|
|
|
|
|
2023-05-23 11:29:41 -07:00
|
|
|
char *SDL_ultoa(unsigned long value, char *string, int radix)
|
2015-06-21 17:33:46 +02:00
|
|
|
{
|
2023-03-30 20:26:31 +02:00
|
|
|
#ifdef HAVE__ULTOA
|
2015-06-21 17:33:46 +02:00
|
|
|
return _ultoa(value, string, radix);
|
|
|
|
|
#else
|
|
|
|
|
char *bufp = string;
|
|
|
|
|
|
|
|
|
|
if (value) {
|
|
|
|
|
while (value > 0) {
|
|
|
|
|
*bufp++ = ntoa_table[value % radix];
|
|
|
|
|
value /= radix;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
*bufp++ = '0';
|
|
|
|
|
}
|
|
|
|
|
*bufp = '\0';
|
|
|
|
|
|
Use C++ style comments consistently in SDL source code
Implemented using this script:
find . -type f -exec sed -i'' -e 's,/\* *\([^*]*\)\*/ *$,// \1,' -e 's, \+$,,' {} \;
git checkout \
core/linux/SDL_evdev_kbd_default_keymap.h \
events/imKStoUCS.* \
hidapi \
joystick/controller_type.c \
joystick/controller_type.h \
joystick/hidapi/steam/controller_constants.h \
joystick/hidapi/steam/controller_structs.h \
joystick/SDL_gamepad_db.h \
libm \
render/*/*Shader*.h \
render/vitagxm/SDL_render_vita_gxm_shaders.h \
render/metal/SDL_shaders_metal_*.h \
stdlib/SDL_malloc.c \
stdlib/SDL_qsort.c \
stdlib/SDL_strtokr.c \
test/ \
video/directx/SDL_d3d12_xbox_cmacros.h \
video/directx/d3d12.h \
video/directx/d3d12sdklayers.h \
video/khronos \
video/x11/edid-parse.c \
video/x11/xsettings-client.* \
video/yuv2rgb
sed -i'' -e 's,/\* *\([^*]*\)\*/ *$,// \1,' -e 's, \+$,,' hidapi/SDL_hidapi.c
2024-08-22 10:30:45 -07:00
|
|
|
// The numbers went into the string backwards. :)
|
2015-06-21 17:33:46 +02:00
|
|
|
SDL_strrev(string);
|
|
|
|
|
|
|
|
|
|
return string;
|
Use C++ style comments consistently in SDL source code
Implemented using this script:
find . -type f -exec sed -i'' -e 's,/\* *\([^*]*\)\*/ *$,// \1,' -e 's, \+$,,' {} \;
git checkout \
core/linux/SDL_evdev_kbd_default_keymap.h \
events/imKStoUCS.* \
hidapi \
joystick/controller_type.c \
joystick/controller_type.h \
joystick/hidapi/steam/controller_constants.h \
joystick/hidapi/steam/controller_structs.h \
joystick/SDL_gamepad_db.h \
libm \
render/*/*Shader*.h \
render/vitagxm/SDL_render_vita_gxm_shaders.h \
render/metal/SDL_shaders_metal_*.h \
stdlib/SDL_malloc.c \
stdlib/SDL_qsort.c \
stdlib/SDL_strtokr.c \
test/ \
video/directx/SDL_d3d12_xbox_cmacros.h \
video/directx/d3d12.h \
video/directx/d3d12sdklayers.h \
video/khronos \
video/x11/edid-parse.c \
video/x11/xsettings-client.* \
video/yuv2rgb
sed -i'' -e 's,/\* *\([^*]*\)\*/ *$,// \1,' -e 's, \+$,,' hidapi/SDL_hidapi.c
2024-08-22 10:30:45 -07:00
|
|
|
#endif // HAVE__ULTOA
|
2015-06-21 17:33:46 +02:00
|
|
|
}
|
|
|
|
|
|
2024-09-09 14:54:50 -07:00
|
|
|
char *SDL_lltoa(long long value, char *string, int radix)
|
2015-06-21 17:33:46 +02:00
|
|
|
{
|
2023-03-30 20:26:31 +02:00
|
|
|
#ifdef HAVE__I64TOA
|
2015-06-21 17:33:46 +02:00
|
|
|
return _i64toa(value, string, radix);
|
|
|
|
|
#else
|
|
|
|
|
char *bufp = string;
|
|
|
|
|
|
|
|
|
|
if (value < 0) {
|
|
|
|
|
*bufp++ = '-';
|
|
|
|
|
SDL_ulltoa(-value, bufp, radix);
|
|
|
|
|
} else {
|
|
|
|
|
SDL_ulltoa(value, bufp, radix);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return string;
|
Use C++ style comments consistently in SDL source code
Implemented using this script:
find . -type f -exec sed -i'' -e 's,/\* *\([^*]*\)\*/ *$,// \1,' -e 's, \+$,,' {} \;
git checkout \
core/linux/SDL_evdev_kbd_default_keymap.h \
events/imKStoUCS.* \
hidapi \
joystick/controller_type.c \
joystick/controller_type.h \
joystick/hidapi/steam/controller_constants.h \
joystick/hidapi/steam/controller_structs.h \
joystick/SDL_gamepad_db.h \
libm \
render/*/*Shader*.h \
render/vitagxm/SDL_render_vita_gxm_shaders.h \
render/metal/SDL_shaders_metal_*.h \
stdlib/SDL_malloc.c \
stdlib/SDL_qsort.c \
stdlib/SDL_strtokr.c \
test/ \
video/directx/SDL_d3d12_xbox_cmacros.h \
video/directx/d3d12.h \
video/directx/d3d12sdklayers.h \
video/khronos \
video/x11/edid-parse.c \
video/x11/xsettings-client.* \
video/yuv2rgb
sed -i'' -e 's,/\* *\([^*]*\)\*/ *$,// \1,' -e 's, \+$,,' hidapi/SDL_hidapi.c
2024-08-22 10:30:45 -07:00
|
|
|
#endif // HAVE__I64TOA
|
2015-06-21 17:33:46 +02:00
|
|
|
}
|
|
|
|
|
|
2024-09-09 14:54:50 -07:00
|
|
|
char *SDL_ulltoa(unsigned long long value, char *string, int radix)
|
2015-06-21 17:33:46 +02:00
|
|
|
{
|
2023-03-30 20:26:31 +02:00
|
|
|
#ifdef HAVE__UI64TOA
|
2015-06-21 17:33:46 +02:00
|
|
|
return _ui64toa(value, string, radix);
|
|
|
|
|
#else
|
|
|
|
|
char *bufp = string;
|
|
|
|
|
|
|
|
|
|
if (value) {
|
|
|
|
|
while (value > 0) {
|
|
|
|
|
*bufp++ = ntoa_table[value % radix];
|
|
|
|
|
value /= radix;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
*bufp++ = '0';
|
|
|
|
|
}
|
|
|
|
|
*bufp = '\0';
|
|
|
|
|
|
Use C++ style comments consistently in SDL source code
Implemented using this script:
find . -type f -exec sed -i'' -e 's,/\* *\([^*]*\)\*/ *$,// \1,' -e 's, \+$,,' {} \;
git checkout \
core/linux/SDL_evdev_kbd_default_keymap.h \
events/imKStoUCS.* \
hidapi \
joystick/controller_type.c \
joystick/controller_type.h \
joystick/hidapi/steam/controller_constants.h \
joystick/hidapi/steam/controller_structs.h \
joystick/SDL_gamepad_db.h \
libm \
render/*/*Shader*.h \
render/vitagxm/SDL_render_vita_gxm_shaders.h \
render/metal/SDL_shaders_metal_*.h \
stdlib/SDL_malloc.c \
stdlib/SDL_qsort.c \
stdlib/SDL_strtokr.c \
test/ \
video/directx/SDL_d3d12_xbox_cmacros.h \
video/directx/d3d12.h \
video/directx/d3d12sdklayers.h \
video/khronos \
video/x11/edid-parse.c \
video/x11/xsettings-client.* \
video/yuv2rgb
sed -i'' -e 's,/\* *\([^*]*\)\*/ *$,// \1,' -e 's, \+$,,' hidapi/SDL_hidapi.c
2024-08-22 10:30:45 -07:00
|
|
|
// The numbers went into the string backwards. :)
|
2015-06-21 17:33:46 +02:00
|
|
|
SDL_strrev(string);
|
|
|
|
|
|
|
|
|
|
return string;
|
Use C++ style comments consistently in SDL source code
Implemented using this script:
find . -type f -exec sed -i'' -e 's,/\* *\([^*]*\)\*/ *$,// \1,' -e 's, \+$,,' {} \;
git checkout \
core/linux/SDL_evdev_kbd_default_keymap.h \
events/imKStoUCS.* \
hidapi \
joystick/controller_type.c \
joystick/controller_type.h \
joystick/hidapi/steam/controller_constants.h \
joystick/hidapi/steam/controller_structs.h \
joystick/SDL_gamepad_db.h \
libm \
render/*/*Shader*.h \
render/vitagxm/SDL_render_vita_gxm_shaders.h \
render/metal/SDL_shaders_metal_*.h \
stdlib/SDL_malloc.c \
stdlib/SDL_qsort.c \
stdlib/SDL_strtokr.c \
test/ \
video/directx/SDL_d3d12_xbox_cmacros.h \
video/directx/d3d12.h \
video/directx/d3d12sdklayers.h \
video/khronos \
video/x11/edid-parse.c \
video/x11/xsettings-client.* \
video/yuv2rgb
sed -i'' -e 's,/\* *\([^*]*\)\*/ *$,// \1,' -e 's, \+$,,' hidapi/SDL_hidapi.c
2024-08-22 10:30:45 -07:00
|
|
|
#endif // HAVE__UI64TOA
|
2015-06-21 17:33:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int SDL_atoi(const char *string)
|
|
|
|
|
{
|
|
|
|
|
#ifdef HAVE_ATOI
|
|
|
|
|
return atoi(string);
|
|
|
|
|
#else
|
2021-10-04 21:32:00 +03:00
|
|
|
return SDL_strtol(string, NULL, 10);
|
Use C++ style comments consistently in SDL source code
Implemented using this script:
find . -type f -exec sed -i'' -e 's,/\* *\([^*]*\)\*/ *$,// \1,' -e 's, \+$,,' {} \;
git checkout \
core/linux/SDL_evdev_kbd_default_keymap.h \
events/imKStoUCS.* \
hidapi \
joystick/controller_type.c \
joystick/controller_type.h \
joystick/hidapi/steam/controller_constants.h \
joystick/hidapi/steam/controller_structs.h \
joystick/SDL_gamepad_db.h \
libm \
render/*/*Shader*.h \
render/vitagxm/SDL_render_vita_gxm_shaders.h \
render/metal/SDL_shaders_metal_*.h \
stdlib/SDL_malloc.c \
stdlib/SDL_qsort.c \
stdlib/SDL_strtokr.c \
test/ \
video/directx/SDL_d3d12_xbox_cmacros.h \
video/directx/d3d12.h \
video/directx/d3d12sdklayers.h \
video/khronos \
video/x11/edid-parse.c \
video/x11/xsettings-client.* \
video/yuv2rgb
sed -i'' -e 's,/\* *\([^*]*\)\*/ *$,// \1,' -e 's, \+$,,' hidapi/SDL_hidapi.c
2024-08-22 10:30:45 -07:00
|
|
|
#endif // HAVE_ATOI
|
2015-06-21 17:33:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
double SDL_atof(const char *string)
|
|
|
|
|
{
|
|
|
|
|
#ifdef HAVE_ATOF
|
2019-10-30 15:13:55 +01:00
|
|
|
return atof(string);
|
2015-06-21 17:33:46 +02:00
|
|
|
#else
|
|
|
|
|
return SDL_strtod(string, NULL);
|
Use C++ style comments consistently in SDL source code
Implemented using this script:
find . -type f -exec sed -i'' -e 's,/\* *\([^*]*\)\*/ *$,// \1,' -e 's, \+$,,' {} \;
git checkout \
core/linux/SDL_evdev_kbd_default_keymap.h \
events/imKStoUCS.* \
hidapi \
joystick/controller_type.c \
joystick/controller_type.h \
joystick/hidapi/steam/controller_constants.h \
joystick/hidapi/steam/controller_structs.h \
joystick/SDL_gamepad_db.h \
libm \
render/*/*Shader*.h \
render/vitagxm/SDL_render_vita_gxm_shaders.h \
render/metal/SDL_shaders_metal_*.h \
stdlib/SDL_malloc.c \
stdlib/SDL_qsort.c \
stdlib/SDL_strtokr.c \
test/ \
video/directx/SDL_d3d12_xbox_cmacros.h \
video/directx/d3d12.h \
video/directx/d3d12sdklayers.h \
video/khronos \
video/x11/edid-parse.c \
video/x11/xsettings-client.* \
video/yuv2rgb
sed -i'' -e 's,/\* *\([^*]*\)\*/ *$,// \1,' -e 's, \+$,,' hidapi/SDL_hidapi.c
2024-08-22 10:30:45 -07:00
|
|
|
#endif // HAVE_ATOF
|
2015-06-21 17:33:46 +02:00
|
|
|
}
|
|
|
|
|
|
2022-11-30 12:51:59 -08:00
|
|
|
long SDL_strtol(const char *string, char **endp, int base)
|
2015-06-21 17:33:46 +02:00
|
|
|
{
|
2023-03-30 20:26:31 +02:00
|
|
|
#ifdef HAVE_STRTOL
|
2015-06-21 17:33:46 +02:00
|
|
|
return strtol(string, endp, base);
|
|
|
|
|
#else
|
2017-08-12 00:01:24 -07:00
|
|
|
long value = 0;
|
2024-09-12 00:02:47 +02:00
|
|
|
size_t len = SDL_ScanLong(string, 0, base, &value);
|
2015-06-21 17:33:46 +02:00
|
|
|
if (endp) {
|
2022-11-30 12:51:59 -08:00
|
|
|
*endp = (char *)string + len;
|
2015-06-21 17:33:46 +02:00
|
|
|
}
|
|
|
|
|
return value;
|
Use C++ style comments consistently in SDL source code
Implemented using this script:
find . -type f -exec sed -i'' -e 's,/\* *\([^*]*\)\*/ *$,// \1,' -e 's, \+$,,' {} \;
git checkout \
core/linux/SDL_evdev_kbd_default_keymap.h \
events/imKStoUCS.* \
hidapi \
joystick/controller_type.c \
joystick/controller_type.h \
joystick/hidapi/steam/controller_constants.h \
joystick/hidapi/steam/controller_structs.h \
joystick/SDL_gamepad_db.h \
libm \
render/*/*Shader*.h \
render/vitagxm/SDL_render_vita_gxm_shaders.h \
render/metal/SDL_shaders_metal_*.h \
stdlib/SDL_malloc.c \
stdlib/SDL_qsort.c \
stdlib/SDL_strtokr.c \
test/ \
video/directx/SDL_d3d12_xbox_cmacros.h \
video/directx/d3d12.h \
video/directx/d3d12sdklayers.h \
video/khronos \
video/x11/edid-parse.c \
video/x11/xsettings-client.* \
video/yuv2rgb
sed -i'' -e 's,/\* *\([^*]*\)\*/ *$,// \1,' -e 's, \+$,,' hidapi/SDL_hidapi.c
2024-08-22 10:30:45 -07:00
|
|
|
#endif // HAVE_STRTOL
|
2015-06-21 17:33:46 +02:00
|
|
|
}
|
|
|
|
|
|
2024-09-09 14:54:50 -07:00
|
|
|
unsigned long SDL_strtoul(const char *string, char **endp, int base)
|
2015-06-21 17:33:46 +02:00
|
|
|
{
|
2023-03-30 20:26:31 +02:00
|
|
|
#ifdef HAVE_STRTOUL
|
2015-06-21 17:33:46 +02:00
|
|
|
return strtoul(string, endp, base);
|
|
|
|
|
#else
|
2017-08-12 00:01:24 -07:00
|
|
|
unsigned long value = 0;
|
2024-09-12 00:02:47 +02:00
|
|
|
size_t len = SDL_ScanUnsignedLong(string, 0, base, &value);
|
2015-06-21 17:33:46 +02:00
|
|
|
if (endp) {
|
2022-11-30 12:51:59 -08:00
|
|
|
*endp = (char *)string + len;
|
2015-06-21 17:33:46 +02:00
|
|
|
}
|
|
|
|
|
return value;
|
Use C++ style comments consistently in SDL source code
Implemented using this script:
find . -type f -exec sed -i'' -e 's,/\* *\([^*]*\)\*/ *$,// \1,' -e 's, \+$,,' {} \;
git checkout \
core/linux/SDL_evdev_kbd_default_keymap.h \
events/imKStoUCS.* \
hidapi \
joystick/controller_type.c \
joystick/controller_type.h \
joystick/hidapi/steam/controller_constants.h \
joystick/hidapi/steam/controller_structs.h \
joystick/SDL_gamepad_db.h \
libm \
render/*/*Shader*.h \
render/vitagxm/SDL_render_vita_gxm_shaders.h \
render/metal/SDL_shaders_metal_*.h \
stdlib/SDL_malloc.c \
stdlib/SDL_qsort.c \
stdlib/SDL_strtokr.c \
test/ \
video/directx/SDL_d3d12_xbox_cmacros.h \
video/directx/d3d12.h \
video/directx/d3d12sdklayers.h \
video/khronos \
video/x11/edid-parse.c \
video/x11/xsettings-client.* \
video/yuv2rgb
sed -i'' -e 's,/\* *\([^*]*\)\*/ *$,// \1,' -e 's, \+$,,' hidapi/SDL_hidapi.c
2024-08-22 10:30:45 -07:00
|
|
|
#endif // HAVE_STRTOUL
|
2015-06-21 17:33:46 +02:00
|
|
|
}
|
|
|
|
|
|
2024-09-09 14:54:50 -07:00
|
|
|
long long SDL_strtoll(const char *string, char **endp, int base)
|
2015-06-21 17:33:46 +02:00
|
|
|
{
|
2023-03-30 20:26:31 +02:00
|
|
|
#ifdef HAVE_STRTOLL
|
2015-06-21 17:33:46 +02:00
|
|
|
return strtoll(string, endp, base);
|
|
|
|
|
#else
|
2024-09-09 14:54:50 -07:00
|
|
|
long long value = 0;
|
2024-09-12 00:02:47 +02:00
|
|
|
size_t len = SDL_ScanLongLong(string, 0, base, &value);
|
2015-06-21 17:33:46 +02:00
|
|
|
if (endp) {
|
2022-11-30 12:51:59 -08:00
|
|
|
*endp = (char *)string + len;
|
2015-06-21 17:33:46 +02:00
|
|
|
}
|
|
|
|
|
return value;
|
Use C++ style comments consistently in SDL source code
Implemented using this script:
find . -type f -exec sed -i'' -e 's,/\* *\([^*]*\)\*/ *$,// \1,' -e 's, \+$,,' {} \;
git checkout \
core/linux/SDL_evdev_kbd_default_keymap.h \
events/imKStoUCS.* \
hidapi \
joystick/controller_type.c \
joystick/controller_type.h \
joystick/hidapi/steam/controller_constants.h \
joystick/hidapi/steam/controller_structs.h \
joystick/SDL_gamepad_db.h \
libm \
render/*/*Shader*.h \
render/vitagxm/SDL_render_vita_gxm_shaders.h \
render/metal/SDL_shaders_metal_*.h \
stdlib/SDL_malloc.c \
stdlib/SDL_qsort.c \
stdlib/SDL_strtokr.c \
test/ \
video/directx/SDL_d3d12_xbox_cmacros.h \
video/directx/d3d12.h \
video/directx/d3d12sdklayers.h \
video/khronos \
video/x11/edid-parse.c \
video/x11/xsettings-client.* \
video/yuv2rgb
sed -i'' -e 's,/\* *\([^*]*\)\*/ *$,// \1,' -e 's, \+$,,' hidapi/SDL_hidapi.c
2024-08-22 10:30:45 -07:00
|
|
|
#endif // HAVE_STRTOLL
|
2015-06-21 17:33:46 +02:00
|
|
|
}
|
|
|
|
|
|
2024-09-09 14:54:50 -07:00
|
|
|
unsigned long long SDL_strtoull(const char *string, char **endp, int base)
|
2015-06-21 17:33:46 +02:00
|
|
|
{
|
2023-03-30 20:26:31 +02:00
|
|
|
#ifdef HAVE_STRTOULL
|
2015-06-21 17:33:46 +02:00
|
|
|
return strtoull(string, endp, base);
|
|
|
|
|
#else
|
2024-09-09 14:54:50 -07:00
|
|
|
unsigned long long value = 0;
|
2024-09-11 22:32:45 +02:00
|
|
|
size_t len = SDL_ScanUnsignedLongLong(string, 0, base, &value);
|
2015-06-21 17:33:46 +02:00
|
|
|
if (endp) {
|
2022-11-30 12:51:59 -08:00
|
|
|
*endp = (char *)string + len;
|
2015-06-21 17:33:46 +02:00
|
|
|
}
|
|
|
|
|
return value;
|
Use C++ style comments consistently in SDL source code
Implemented using this script:
find . -type f -exec sed -i'' -e 's,/\* *\([^*]*\)\*/ *$,// \1,' -e 's, \+$,,' {} \;
git checkout \
core/linux/SDL_evdev_kbd_default_keymap.h \
events/imKStoUCS.* \
hidapi \
joystick/controller_type.c \
joystick/controller_type.h \
joystick/hidapi/steam/controller_constants.h \
joystick/hidapi/steam/controller_structs.h \
joystick/SDL_gamepad_db.h \
libm \
render/*/*Shader*.h \
render/vitagxm/SDL_render_vita_gxm_shaders.h \
render/metal/SDL_shaders_metal_*.h \
stdlib/SDL_malloc.c \
stdlib/SDL_qsort.c \
stdlib/SDL_strtokr.c \
test/ \
video/directx/SDL_d3d12_xbox_cmacros.h \
video/directx/d3d12.h \
video/directx/d3d12sdklayers.h \
video/khronos \
video/x11/edid-parse.c \
video/x11/xsettings-client.* \
video/yuv2rgb
sed -i'' -e 's,/\* *\([^*]*\)\*/ *$,// \1,' -e 's, \+$,,' hidapi/SDL_hidapi.c
2024-08-22 10:30:45 -07:00
|
|
|
#endif // HAVE_STRTOULL
|
2015-06-21 17:33:46 +02:00
|
|
|
}
|
|
|
|
|
|
stdlib: Improve Unicode support and consistency in string comparison functions.
SDL_strcasecmp (even when calling into a C runtime) does not work with
Unicode chars, and depending on the user's locale, might not work with
even basic ASCII strings.
This implements the function from scratch, using "case-folding,"
which is a more robust method that deals with various languages. It
involves a hashtable of a few hundred codepoints that are "uppercase" and
how to map them to lowercase equivalents (possibly increasing the size of
the string in the process). The vast majority of human languages (and
Unicode) do not have letters with different cases, but still, this static
table takes about 10 kilobytes on a 64-bit machine.
Even this will fail in one known case: the Turkish 'i' folds differently
if you're writing in Turkish vs other languages. Generally this is seen as
unfortunate collateral damage in cases where you can't specify the language
in use.
In addition to case-folding the codepoints, the new functions also know how
to decode the various formats to turn them into codepoints in the first
place, instead of blindly stepping by one byte (or one wchar_t) per
character.
Also included is casefolding.txt from the Unicode Consortium and a perl
script to generate the hashtable from that text file, so we can trivially
update this if new languages are added in the future.
A simple test using the new function:
```c
#include <SDL3/SDL.h>
int main(void)
{
const char *a = "α ε η";
const char *b = "Α Ε Η";
SDL_Log(" strcasecmp(\"%s\", \"%s\") == %d\n", a, b, strcasecmp(a, b));
SDL_Log("SDL_strcasecmp(\"%s\", \"%s\") == %d\n", a, b, SDL_strcasecmp(a, b));
return 0;
}
```
Produces:
```
INFO: strcasecmp("α ε η", "Α Ε Η") == 32
INFO: SDL_strcasecmp("α ε η", "Α Ε Η") == 0
```
glibc strcasecmp() fails to compare a Greek lowercase string to its uppercase
equivalent, even with a UTF-8 locale, but SDL_strcasecmp() works.
Other SDL_stdinc.h functions are changed to be more consistent, which is to
say they now ignore any C runtime and often dictate that only English-based
low-ASCII works with them.
Fixes Issue #9313.
2024-03-26 13:22:38 -04:00
|
|
|
double SDL_strtod(const char *string, char **endp)
|
2015-06-21 17:33:46 +02:00
|
|
|
{
|
2023-03-30 20:26:31 +02:00
|
|
|
#ifdef HAVE_STRTOD
|
2015-06-21 17:33:46 +02:00
|
|
|
return strtod(string, endp);
|
|
|
|
|
#else
|
2024-09-12 01:23:56 +02:00
|
|
|
double value;
|
|
|
|
|
size_t len = SDL_ScanFloat(string, &value);
|
2015-06-21 17:33:46 +02:00
|
|
|
if (endp) {
|
2022-11-30 12:51:59 -08:00
|
|
|
*endp = (char *)string + len;
|
2015-06-21 17:33:46 +02:00
|
|
|
}
|
|
|
|
|
return value;
|
Use C++ style comments consistently in SDL source code
Implemented using this script:
find . -type f -exec sed -i'' -e 's,/\* *\([^*]*\)\*/ *$,// \1,' -e 's, \+$,,' {} \;
git checkout \
core/linux/SDL_evdev_kbd_default_keymap.h \
events/imKStoUCS.* \
hidapi \
joystick/controller_type.c \
joystick/controller_type.h \
joystick/hidapi/steam/controller_constants.h \
joystick/hidapi/steam/controller_structs.h \
joystick/SDL_gamepad_db.h \
libm \
render/*/*Shader*.h \
render/vitagxm/SDL_render_vita_gxm_shaders.h \
render/metal/SDL_shaders_metal_*.h \
stdlib/SDL_malloc.c \
stdlib/SDL_qsort.c \
stdlib/SDL_strtokr.c \
test/ \
video/directx/SDL_d3d12_xbox_cmacros.h \
video/directx/d3d12.h \
video/directx/d3d12sdklayers.h \
video/khronos \
video/x11/edid-parse.c \
video/x11/xsettings-client.* \
video/yuv2rgb
sed -i'' -e 's,/\* *\([^*]*\)\*/ *$,// \1,' -e 's, \+$,,' hidapi/SDL_hidapi.c
2024-08-22 10:30:45 -07:00
|
|
|
#endif // HAVE_STRTOD
|
2015-06-21 17:33:46 +02:00
|
|
|
}
|
|
|
|
|
|
2022-11-30 12:51:59 -08:00
|
|
|
int SDL_strcmp(const char *str1, const char *str2)
|
2015-06-21 17:33:46 +02:00
|
|
|
{
|
2023-03-30 20:26:31 +02:00
|
|
|
#ifdef HAVE_STRCMP
|
2015-06-21 17:33:46 +02:00
|
|
|
return strcmp(str1, str2);
|
|
|
|
|
#else
|
2022-02-05 11:01:25 +01:00
|
|
|
int result;
|
|
|
|
|
|
2022-11-27 17:38:43 +01:00
|
|
|
while (1) {
|
2022-12-01 16:07:03 -05:00
|
|
|
result = ((unsigned char)*str1 - (unsigned char)*str2);
|
2022-11-30 12:51:59 -08:00
|
|
|
if (result != 0 || (*str1 == '\0' /* && *str2 == '\0'*/)) {
|
2015-06-21 17:33:46 +02:00
|
|
|
break;
|
2022-11-27 17:38:43 +01:00
|
|
|
}
|
2015-06-21 17:33:46 +02:00
|
|
|
++str1;
|
|
|
|
|
++str2;
|
|
|
|
|
}
|
2022-02-05 11:01:25 +01:00
|
|
|
return result;
|
Use C++ style comments consistently in SDL source code
Implemented using this script:
find . -type f -exec sed -i'' -e 's,/\* *\([^*]*\)\*/ *$,// \1,' -e 's, \+$,,' {} \;
git checkout \
core/linux/SDL_evdev_kbd_default_keymap.h \
events/imKStoUCS.* \
hidapi \
joystick/controller_type.c \
joystick/controller_type.h \
joystick/hidapi/steam/controller_constants.h \
joystick/hidapi/steam/controller_structs.h \
joystick/SDL_gamepad_db.h \
libm \
render/*/*Shader*.h \
render/vitagxm/SDL_render_vita_gxm_shaders.h \
render/metal/SDL_shaders_metal_*.h \
stdlib/SDL_malloc.c \
stdlib/SDL_qsort.c \
stdlib/SDL_strtokr.c \
test/ \
video/directx/SDL_d3d12_xbox_cmacros.h \
video/directx/d3d12.h \
video/directx/d3d12sdklayers.h \
video/khronos \
video/x11/edid-parse.c \
video/x11/xsettings-client.* \
video/yuv2rgb
sed -i'' -e 's,/\* *\([^*]*\)\*/ *$,// \1,' -e 's, \+$,,' hidapi/SDL_hidapi.c
2024-08-22 10:30:45 -07:00
|
|
|
#endif // HAVE_STRCMP
|
2015-06-21 17:33:46 +02:00
|
|
|
}
|
|
|
|
|
|
2022-11-30 12:51:59 -08:00
|
|
|
int SDL_strncmp(const char *str1, const char *str2, size_t maxlen)
|
2015-06-21 17:33:46 +02:00
|
|
|
{
|
2023-03-30 20:26:31 +02:00
|
|
|
#ifdef HAVE_STRNCMP
|
2015-06-21 17:33:46 +02:00
|
|
|
return strncmp(str1, str2, maxlen);
|
|
|
|
|
#else
|
2023-03-30 14:02:02 -07:00
|
|
|
int result = 0;
|
2022-02-05 11:01:25 +01:00
|
|
|
|
|
|
|
|
while (maxlen) {
|
2022-11-30 12:51:59 -08:00
|
|
|
result = (int)(unsigned char)*str1 - (unsigned char)*str2;
|
|
|
|
|
if (result != 0 || *str1 == '\0' /* && *str2 == '\0'*/) {
|
2015-06-21 17:33:46 +02:00
|
|
|
break;
|
2022-11-27 17:38:43 +01:00
|
|
|
}
|
2015-06-21 17:33:46 +02:00
|
|
|
++str1;
|
|
|
|
|
++str2;
|
|
|
|
|
--maxlen;
|
|
|
|
|
}
|
2022-02-05 11:01:25 +01:00
|
|
|
return result;
|
Use C++ style comments consistently in SDL source code
Implemented using this script:
find . -type f -exec sed -i'' -e 's,/\* *\([^*]*\)\*/ *$,// \1,' -e 's, \+$,,' {} \;
git checkout \
core/linux/SDL_evdev_kbd_default_keymap.h \
events/imKStoUCS.* \
hidapi \
joystick/controller_type.c \
joystick/controller_type.h \
joystick/hidapi/steam/controller_constants.h \
joystick/hidapi/steam/controller_structs.h \
joystick/SDL_gamepad_db.h \
libm \
render/*/*Shader*.h \
render/vitagxm/SDL_render_vita_gxm_shaders.h \
render/metal/SDL_shaders_metal_*.h \
stdlib/SDL_malloc.c \
stdlib/SDL_qsort.c \
stdlib/SDL_strtokr.c \
test/ \
video/directx/SDL_d3d12_xbox_cmacros.h \
video/directx/d3d12.h \
video/directx/d3d12sdklayers.h \
video/khronos \
video/x11/edid-parse.c \
video/x11/xsettings-client.* \
video/yuv2rgb
sed -i'' -e 's,/\* *\([^*]*\)\*/ *$,// \1,' -e 's, \+$,,' hidapi/SDL_hidapi.c
2024-08-22 10:30:45 -07:00
|
|
|
#endif // HAVE_STRNCMP
|
2015-06-21 17:33:46 +02:00
|
|
|
}
|
|
|
|
|
|
2022-11-30 12:51:59 -08:00
|
|
|
int SDL_strcasecmp(const char *str1, const char *str2)
|
2015-06-21 17:33:46 +02:00
|
|
|
{
|
stdlib: Improve Unicode support and consistency in string comparison functions.
SDL_strcasecmp (even when calling into a C runtime) does not work with
Unicode chars, and depending on the user's locale, might not work with
even basic ASCII strings.
This implements the function from scratch, using "case-folding,"
which is a more robust method that deals with various languages. It
involves a hashtable of a few hundred codepoints that are "uppercase" and
how to map them to lowercase equivalents (possibly increasing the size of
the string in the process). The vast majority of human languages (and
Unicode) do not have letters with different cases, but still, this static
table takes about 10 kilobytes on a 64-bit machine.
Even this will fail in one known case: the Turkish 'i' folds differently
if you're writing in Turkish vs other languages. Generally this is seen as
unfortunate collateral damage in cases where you can't specify the language
in use.
In addition to case-folding the codepoints, the new functions also know how
to decode the various formats to turn them into codepoints in the first
place, instead of blindly stepping by one byte (or one wchar_t) per
character.
Also included is casefolding.txt from the Unicode Consortium and a perl
script to generate the hashtable from that text file, so we can trivially
update this if new languages are added in the future.
A simple test using the new function:
```c
#include <SDL3/SDL.h>
int main(void)
{
const char *a = "α ε η";
const char *b = "Α Ε Η";
SDL_Log(" strcasecmp(\"%s\", \"%s\") == %d\n", a, b, strcasecmp(a, b));
SDL_Log("SDL_strcasecmp(\"%s\", \"%s\") == %d\n", a, b, SDL_strcasecmp(a, b));
return 0;
}
```
Produces:
```
INFO: strcasecmp("α ε η", "Α Ε Η") == 32
INFO: SDL_strcasecmp("α ε η", "Α Ε Η") == 0
```
glibc strcasecmp() fails to compare a Greek lowercase string to its uppercase
equivalent, even with a UTF-8 locale, but SDL_strcasecmp() works.
Other SDL_stdinc.h functions are changed to be more consistent, which is to
say they now ignore any C runtime and often dictate that only English-based
low-ASCII works with them.
Fixes Issue #9313.
2024-03-26 13:22:38 -04:00
|
|
|
UNICODE_STRCASECMP(8, 4, 4, (void) str1start, (void) str2start); // always NULL-terminated, no need to adjust lengths.
|
2015-06-21 17:33:46 +02:00
|
|
|
}
|
|
|
|
|
|
2022-11-30 12:51:59 -08:00
|
|
|
int SDL_strncasecmp(const char *str1, const char *str2, size_t maxlen)
|
2015-06-21 17:33:46 +02:00
|
|
|
{
|
stdlib: Improve Unicode support and consistency in string comparison functions.
SDL_strcasecmp (even when calling into a C runtime) does not work with
Unicode chars, and depending on the user's locale, might not work with
even basic ASCII strings.
This implements the function from scratch, using "case-folding,"
which is a more robust method that deals with various languages. It
involves a hashtable of a few hundred codepoints that are "uppercase" and
how to map them to lowercase equivalents (possibly increasing the size of
the string in the process). The vast majority of human languages (and
Unicode) do not have letters with different cases, but still, this static
table takes about 10 kilobytes on a 64-bit machine.
Even this will fail in one known case: the Turkish 'i' folds differently
if you're writing in Turkish vs other languages. Generally this is seen as
unfortunate collateral damage in cases where you can't specify the language
in use.
In addition to case-folding the codepoints, the new functions also know how
to decode the various formats to turn them into codepoints in the first
place, instead of blindly stepping by one byte (or one wchar_t) per
character.
Also included is casefolding.txt from the Unicode Consortium and a perl
script to generate the hashtable from that text file, so we can trivially
update this if new languages are added in the future.
A simple test using the new function:
```c
#include <SDL3/SDL.h>
int main(void)
{
const char *a = "α ε η";
const char *b = "Α Ε Η";
SDL_Log(" strcasecmp(\"%s\", \"%s\") == %d\n", a, b, strcasecmp(a, b));
SDL_Log("SDL_strcasecmp(\"%s\", \"%s\") == %d\n", a, b, SDL_strcasecmp(a, b));
return 0;
}
```
Produces:
```
INFO: strcasecmp("α ε η", "Α Ε Η") == 32
INFO: SDL_strcasecmp("α ε η", "Α Ε Η") == 0
```
glibc strcasecmp() fails to compare a Greek lowercase string to its uppercase
equivalent, even with a UTF-8 locale, but SDL_strcasecmp() works.
Other SDL_stdinc.h functions are changed to be more consistent, which is to
say they now ignore any C runtime and often dictate that only English-based
low-ASCII works with them.
Fixes Issue #9313.
2024-03-26 13:22:38 -04:00
|
|
|
size_t slen1 = maxlen;
|
|
|
|
|
size_t slen2 = maxlen;
|
|
|
|
|
UNICODE_STRCASECMP(8, slen1, slen2, slen1 -= (size_t) (str1 - ((const char *) str1start)), slen2 -= (size_t) (str2 - ((const char *) str2start)));
|
2015-06-21 17:33:46 +02:00
|
|
|
}
|
|
|
|
|
|
2022-11-30 12:51:59 -08:00
|
|
|
int SDL_sscanf(const char *text, SDL_SCANF_FORMAT_STRING const char *fmt, ...)
|
2015-06-21 17:33:46 +02:00
|
|
|
{
|
|
|
|
|
int rc;
|
|
|
|
|
va_list ap;
|
|
|
|
|
va_start(ap, fmt);
|
|
|
|
|
rc = SDL_vsscanf(text, fmt, ap);
|
|
|
|
|
va_end(ap);
|
|
|
|
|
return rc;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#ifdef HAVE_VSSCANF
|
2022-11-30 12:51:59 -08:00
|
|
|
int SDL_vsscanf(const char *text, const char *fmt, va_list ap)
|
2015-06-21 17:33:46 +02:00
|
|
|
{
|
|
|
|
|
return vsscanf(text, fmt, ap);
|
|
|
|
|
}
|
|
|
|
|
#else
|
2024-08-22 09:21:26 -07:00
|
|
|
static bool CharacterMatchesSet(char c, const char *set, size_t set_len)
|
2023-10-24 16:41:19 -07:00
|
|
|
{
|
2024-08-22 09:21:26 -07:00
|
|
|
bool invert = false;
|
|
|
|
|
bool result = false;
|
2023-10-24 16:41:19 -07:00
|
|
|
|
|
|
|
|
if (*set == '^') {
|
2024-08-22 09:21:26 -07:00
|
|
|
invert = true;
|
2023-10-24 16:41:19 -07:00
|
|
|
++set;
|
|
|
|
|
--set_len;
|
|
|
|
|
}
|
|
|
|
|
while (set_len > 0 && !result) {
|
|
|
|
|
if (set_len >= 3 && set[1] == '-') {
|
|
|
|
|
char low_char = SDL_min(set[0], set[2]);
|
|
|
|
|
char high_char = SDL_max(set[0], set[2]);
|
|
|
|
|
if (c >= low_char && c <= high_char) {
|
2024-08-22 09:21:26 -07:00
|
|
|
result = true;
|
2023-10-24 16:41:19 -07:00
|
|
|
}
|
|
|
|
|
set += 3;
|
|
|
|
|
set_len -= 3;
|
|
|
|
|
} else {
|
|
|
|
|
if (c == *set) {
|
2024-08-22 09:21:26 -07:00
|
|
|
result = true;
|
2023-10-24 16:41:19 -07:00
|
|
|
}
|
|
|
|
|
++set;
|
|
|
|
|
--set_len;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (invert) {
|
2023-11-03 09:27:29 -07:00
|
|
|
result = !result;
|
2023-10-24 16:41:19 -07:00
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
Use C++ style comments consistently in SDL source code
Implemented using this script:
find . -type f -exec sed -i'' -e 's,/\* *\([^*]*\)\*/ *$,// \1,' -e 's, \+$,,' {} \;
git checkout \
core/linux/SDL_evdev_kbd_default_keymap.h \
events/imKStoUCS.* \
hidapi \
joystick/controller_type.c \
joystick/controller_type.h \
joystick/hidapi/steam/controller_constants.h \
joystick/hidapi/steam/controller_structs.h \
joystick/SDL_gamepad_db.h \
libm \
render/*/*Shader*.h \
render/vitagxm/SDL_render_vita_gxm_shaders.h \
render/metal/SDL_shaders_metal_*.h \
stdlib/SDL_malloc.c \
stdlib/SDL_qsort.c \
stdlib/SDL_strtokr.c \
test/ \
video/directx/SDL_d3d12_xbox_cmacros.h \
video/directx/d3d12.h \
video/directx/d3d12sdklayers.h \
video/khronos \
video/x11/edid-parse.c \
video/x11/xsettings-client.* \
video/yuv2rgb
sed -i'' -e 's,/\* *\([^*]*\)\*/ *$,// \1,' -e 's, \+$,,' hidapi/SDL_hidapi.c
2024-08-22 10:30:45 -07:00
|
|
|
// NOLINTNEXTLINE(readability-non-const-parameter)
|
2023-12-06 01:32:00 +03:00
|
|
|
int SDL_vsscanf(const char *text, SDL_SCANF_FORMAT_STRING const char *fmt, va_list ap)
|
2015-06-21 17:33:46 +02:00
|
|
|
{
|
2024-08-22 17:33:49 -07:00
|
|
|
int result = 0;
|
2015-06-21 17:33:46 +02:00
|
|
|
|
2023-11-09 22:29:15 +01:00
|
|
|
if (!text || !*text) {
|
2017-08-11 19:36:12 -07:00
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
2015-06-21 17:33:46 +02:00
|
|
|
while (*fmt) {
|
|
|
|
|
if (*fmt == ' ') {
|
2022-11-30 12:51:59 -08:00
|
|
|
while (SDL_isspace((unsigned char)*text)) {
|
2015-06-21 17:33:46 +02:00
|
|
|
++text;
|
|
|
|
|
}
|
|
|
|
|
++fmt;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
if (*fmt == '%') {
|
2024-08-22 09:21:26 -07:00
|
|
|
bool done = false;
|
2015-06-21 17:33:46 +02:00
|
|
|
long count = 0;
|
|
|
|
|
int radix = 10;
|
|
|
|
|
enum
|
|
|
|
|
{
|
|
|
|
|
DO_SHORT,
|
|
|
|
|
DO_INT,
|
|
|
|
|
DO_LONG,
|
2022-09-19 15:42:11 -07:00
|
|
|
DO_LONGLONG,
|
|
|
|
|
DO_SIZE_T
|
2015-06-21 17:33:46 +02:00
|
|
|
} inttype = DO_INT;
|
2017-08-11 19:36:12 -07:00
|
|
|
size_t advance;
|
2024-08-22 09:21:26 -07:00
|
|
|
bool suppress = false;
|
2015-06-21 17:33:46 +02:00
|
|
|
|
|
|
|
|
++fmt;
|
|
|
|
|
if (*fmt == '%') {
|
|
|
|
|
if (*text == '%') {
|
|
|
|
|
++text;
|
|
|
|
|
++fmt;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
if (*fmt == '*') {
|
2024-08-22 09:21:26 -07:00
|
|
|
suppress = true;
|
2015-06-21 17:33:46 +02:00
|
|
|
++fmt;
|
|
|
|
|
}
|
2022-06-18 07:02:38 -07:00
|
|
|
fmt += SDL_ScanLong(fmt, 0, 10, &count);
|
2015-06-21 17:33:46 +02:00
|
|
|
|
|
|
|
|
if (*fmt == 'c') {
|
|
|
|
|
if (!count) {
|
|
|
|
|
count = 1;
|
|
|
|
|
}
|
|
|
|
|
if (suppress) {
|
|
|
|
|
while (count--) {
|
|
|
|
|
++text;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
char *valuep = va_arg(ap, char *);
|
|
|
|
|
while (count--) {
|
|
|
|
|
*valuep++ = *text++;
|
|
|
|
|
}
|
2024-08-22 17:33:49 -07:00
|
|
|
++result;
|
2015-06-21 17:33:46 +02:00
|
|
|
}
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-30 12:51:59 -08:00
|
|
|
while (SDL_isspace((unsigned char)*text)) {
|
2015-06-21 17:33:46 +02:00
|
|
|
++text;
|
|
|
|
|
}
|
|
|
|
|
|
Use C++ style comments consistently in SDL source code
Implemented using this script:
find . -type f -exec sed -i'' -e 's,/\* *\([^*]*\)\*/ *$,// \1,' -e 's, \+$,,' {} \;
git checkout \
core/linux/SDL_evdev_kbd_default_keymap.h \
events/imKStoUCS.* \
hidapi \
joystick/controller_type.c \
joystick/controller_type.h \
joystick/hidapi/steam/controller_constants.h \
joystick/hidapi/steam/controller_structs.h \
joystick/SDL_gamepad_db.h \
libm \
render/*/*Shader*.h \
render/vitagxm/SDL_render_vita_gxm_shaders.h \
render/metal/SDL_shaders_metal_*.h \
stdlib/SDL_malloc.c \
stdlib/SDL_qsort.c \
stdlib/SDL_strtokr.c \
test/ \
video/directx/SDL_d3d12_xbox_cmacros.h \
video/directx/d3d12.h \
video/directx/d3d12sdklayers.h \
video/khronos \
video/x11/edid-parse.c \
video/x11/xsettings-client.* \
video/yuv2rgb
sed -i'' -e 's,/\* *\([^*]*\)\*/ *$,// \1,' -e 's, \+$,,' hidapi/SDL_hidapi.c
2024-08-22 10:30:45 -07:00
|
|
|
// FIXME: implement more of the format specifiers
|
2015-06-21 17:33:46 +02:00
|
|
|
while (!done) {
|
|
|
|
|
switch (*fmt) {
|
|
|
|
|
case '*':
|
2024-08-22 09:21:26 -07:00
|
|
|
suppress = true;
|
2015-06-21 17:33:46 +02:00
|
|
|
break;
|
|
|
|
|
case 'h':
|
2023-08-10 23:43:58 +02:00
|
|
|
if (inttype == DO_INT) {
|
|
|
|
|
inttype = DO_SHORT;
|
|
|
|
|
} else if (inttype > DO_SHORT) {
|
2015-06-21 17:33:46 +02:00
|
|
|
++inttype;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case 'l':
|
|
|
|
|
if (inttype < DO_LONGLONG) {
|
|
|
|
|
++inttype;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case 'I':
|
|
|
|
|
if (SDL_strncmp(fmt, "I64", 3) == 0) {
|
|
|
|
|
fmt += 2;
|
|
|
|
|
inttype = DO_LONGLONG;
|
|
|
|
|
}
|
|
|
|
|
break;
|
2022-09-19 15:42:11 -07:00
|
|
|
case 'z':
|
|
|
|
|
inttype = DO_SIZE_T;
|
|
|
|
|
break;
|
2015-06-21 17:33:46 +02:00
|
|
|
case 'i':
|
2022-11-30 12:51:59 -08:00
|
|
|
{
|
|
|
|
|
int index = 0;
|
|
|
|
|
if (text[index] == '-') {
|
|
|
|
|
++index;
|
|
|
|
|
}
|
|
|
|
|
if (text[index] == '0') {
|
|
|
|
|
if (SDL_tolower((unsigned char)text[index + 1]) == 'x') {
|
|
|
|
|
radix = 16;
|
|
|
|
|
} else {
|
|
|
|
|
radix = 8;
|
2015-06-21 17:33:46 +02:00
|
|
|
}
|
|
|
|
|
}
|
2022-11-30 12:51:59 -08:00
|
|
|
}
|
Add and use `SDL_FALLTHROUGH` for fallthroughs
Case fallthrough warnings can be suppressed using the __fallthrough__
compiler attribute. Unfortunately, not all compilers have this
attribute, or even have __has_attribute to check if they have the
__fallthrough__ attribute. [[fallthrough]] is also available in C++17
and the next C2x, but not everyone uses C++17 or C2x.
So define the SDL_FALLTHROUGH macro to deal with those problems - if we
are using C++17 or C2x, it expands to [[fallthrough]]; else if the
compiler has __has_attribute and has the __fallthrough__ attribute, then
it expands to __attribute__((__fallthrough__)); else it expands to an
empty statement, with a /* fallthrough */ comment (it's a do {} while
(0) statement, because users of this macro need to use a semicolon,
because [[fallthrough]] and __attribute__((__fallthrough__)) require a
semicolon).
Clang before Clang 10 and GCC before GCC 7 have problems with using
__attribute__ as a sole statement and warn about a "declaration not
declaring anything", so fall back to using the /* fallthrough */ comment
if we are using those older compiler versions.
Applications using SDL are also free to use this macro (because it is
defined in begin_code.h).
All existing /* fallthrough */ comments have been replaced with this
macro. Some of them were unnecessary because they were the last case in
a switch; using SDL_FALLTHROUGH in those cases would result in a compile
error on compilers that support __fallthrough__, for having a
__attribute__((__fallthrough__)) statement that didn't immediately
precede a case label.
2021-09-27 14:38:12 -07:00
|
|
|
SDL_FALLTHROUGH;
|
2015-06-21 17:33:46 +02:00
|
|
|
case 'd':
|
|
|
|
|
if (inttype == DO_LONGLONG) {
|
2024-09-09 14:54:50 -07:00
|
|
|
long long value = 0;
|
2022-06-18 07:02:38 -07:00
|
|
|
advance = SDL_ScanLongLong(text, count, radix, &value);
|
2017-08-12 00:01:24 -07:00
|
|
|
text += advance;
|
|
|
|
|
if (advance && !suppress) {
|
2015-06-21 17:33:46 +02:00
|
|
|
Sint64 *valuep = va_arg(ap, Sint64 *);
|
|
|
|
|
*valuep = value;
|
2024-08-22 17:33:49 -07:00
|
|
|
++result;
|
2015-06-21 17:33:46 +02:00
|
|
|
}
|
2022-09-19 15:42:11 -07:00
|
|
|
} else if (inttype == DO_SIZE_T) {
|
2024-09-09 14:54:50 -07:00
|
|
|
long long value = 0;
|
2022-09-19 15:42:11 -07:00
|
|
|
advance = SDL_ScanLongLong(text, count, radix, &value);
|
|
|
|
|
text += advance;
|
|
|
|
|
if (advance && !suppress) {
|
|
|
|
|
size_t *valuep = va_arg(ap, size_t *);
|
|
|
|
|
*valuep = (size_t)value;
|
2024-08-22 17:33:49 -07:00
|
|
|
++result;
|
2022-09-19 15:42:11 -07:00
|
|
|
}
|
2015-06-21 17:33:46 +02:00
|
|
|
} else {
|
2022-08-12 20:51:28 -07:00
|
|
|
long value = 0;
|
2022-06-18 07:02:38 -07:00
|
|
|
advance = SDL_ScanLong(text, count, radix, &value);
|
2017-08-11 19:36:12 -07:00
|
|
|
text += advance;
|
|
|
|
|
if (advance && !suppress) {
|
2015-06-21 17:33:46 +02:00
|
|
|
switch (inttype) {
|
|
|
|
|
case DO_SHORT:
|
2022-11-30 12:51:59 -08:00
|
|
|
{
|
|
|
|
|
short *valuep = va_arg(ap, short *);
|
|
|
|
|
*valuep = (short)value;
|
|
|
|
|
} break;
|
2015-06-21 17:33:46 +02:00
|
|
|
case DO_INT:
|
2022-11-30 12:51:59 -08:00
|
|
|
{
|
|
|
|
|
int *valuep = va_arg(ap, int *);
|
|
|
|
|
*valuep = (int)value;
|
|
|
|
|
} break;
|
2015-06-21 17:33:46 +02:00
|
|
|
case DO_LONG:
|
2022-11-30 12:51:59 -08:00
|
|
|
{
|
|
|
|
|
long *valuep = va_arg(ap, long *);
|
|
|
|
|
*valuep = value;
|
|
|
|
|
} break;
|
2015-06-21 17:33:46 +02:00
|
|
|
case DO_LONGLONG:
|
2022-09-19 15:42:11 -07:00
|
|
|
case DO_SIZE_T:
|
Use C++ style comments consistently in SDL source code
Implemented using this script:
find . -type f -exec sed -i'' -e 's,/\* *\([^*]*\)\*/ *$,// \1,' -e 's, \+$,,' {} \;
git checkout \
core/linux/SDL_evdev_kbd_default_keymap.h \
events/imKStoUCS.* \
hidapi \
joystick/controller_type.c \
joystick/controller_type.h \
joystick/hidapi/steam/controller_constants.h \
joystick/hidapi/steam/controller_structs.h \
joystick/SDL_gamepad_db.h \
libm \
render/*/*Shader*.h \
render/vitagxm/SDL_render_vita_gxm_shaders.h \
render/metal/SDL_shaders_metal_*.h \
stdlib/SDL_malloc.c \
stdlib/SDL_qsort.c \
stdlib/SDL_strtokr.c \
test/ \
video/directx/SDL_d3d12_xbox_cmacros.h \
video/directx/d3d12.h \
video/directx/d3d12sdklayers.h \
video/khronos \
video/x11/edid-parse.c \
video/x11/xsettings-client.* \
video/yuv2rgb
sed -i'' -e 's,/\* *\([^*]*\)\*/ *$,// \1,' -e 's, \+$,,' hidapi/SDL_hidapi.c
2024-08-22 10:30:45 -07:00
|
|
|
// Handled above
|
2015-06-21 17:33:46 +02:00
|
|
|
break;
|
|
|
|
|
}
|
2024-08-22 17:33:49 -07:00
|
|
|
++result;
|
2015-06-21 17:33:46 +02:00
|
|
|
}
|
|
|
|
|
}
|
2024-08-22 09:21:26 -07:00
|
|
|
done = true;
|
2015-06-21 17:33:46 +02:00
|
|
|
break;
|
|
|
|
|
case 'o':
|
|
|
|
|
if (radix == 10) {
|
|
|
|
|
radix = 8;
|
|
|
|
|
}
|
Add and use `SDL_FALLTHROUGH` for fallthroughs
Case fallthrough warnings can be suppressed using the __fallthrough__
compiler attribute. Unfortunately, not all compilers have this
attribute, or even have __has_attribute to check if they have the
__fallthrough__ attribute. [[fallthrough]] is also available in C++17
and the next C2x, but not everyone uses C++17 or C2x.
So define the SDL_FALLTHROUGH macro to deal with those problems - if we
are using C++17 or C2x, it expands to [[fallthrough]]; else if the
compiler has __has_attribute and has the __fallthrough__ attribute, then
it expands to __attribute__((__fallthrough__)); else it expands to an
empty statement, with a /* fallthrough */ comment (it's a do {} while
(0) statement, because users of this macro need to use a semicolon,
because [[fallthrough]] and __attribute__((__fallthrough__)) require a
semicolon).
Clang before Clang 10 and GCC before GCC 7 have problems with using
__attribute__ as a sole statement and warn about a "declaration not
declaring anything", so fall back to using the /* fallthrough */ comment
if we are using those older compiler versions.
Applications using SDL are also free to use this macro (because it is
defined in begin_code.h).
All existing /* fallthrough */ comments have been replaced with this
macro. Some of them were unnecessary because they were the last case in
a switch; using SDL_FALLTHROUGH in those cases would result in a compile
error on compilers that support __fallthrough__, for having a
__attribute__((__fallthrough__)) statement that didn't immediately
precede a case label.
2021-09-27 14:38:12 -07:00
|
|
|
SDL_FALLTHROUGH;
|
2015-06-21 17:33:46 +02:00
|
|
|
case 'x':
|
|
|
|
|
case 'X':
|
|
|
|
|
if (radix == 10) {
|
|
|
|
|
radix = 16;
|
|
|
|
|
}
|
Add and use `SDL_FALLTHROUGH` for fallthroughs
Case fallthrough warnings can be suppressed using the __fallthrough__
compiler attribute. Unfortunately, not all compilers have this
attribute, or even have __has_attribute to check if they have the
__fallthrough__ attribute. [[fallthrough]] is also available in C++17
and the next C2x, but not everyone uses C++17 or C2x.
So define the SDL_FALLTHROUGH macro to deal with those problems - if we
are using C++17 or C2x, it expands to [[fallthrough]]; else if the
compiler has __has_attribute and has the __fallthrough__ attribute, then
it expands to __attribute__((__fallthrough__)); else it expands to an
empty statement, with a /* fallthrough */ comment (it's a do {} while
(0) statement, because users of this macro need to use a semicolon,
because [[fallthrough]] and __attribute__((__fallthrough__)) require a
semicolon).
Clang before Clang 10 and GCC before GCC 7 have problems with using
__attribute__ as a sole statement and warn about a "declaration not
declaring anything", so fall back to using the /* fallthrough */ comment
if we are using those older compiler versions.
Applications using SDL are also free to use this macro (because it is
defined in begin_code.h).
All existing /* fallthrough */ comments have been replaced with this
macro. Some of them were unnecessary because they were the last case in
a switch; using SDL_FALLTHROUGH in those cases would result in a compile
error on compilers that support __fallthrough__, for having a
__attribute__((__fallthrough__)) statement that didn't immediately
precede a case label.
2021-09-27 14:38:12 -07:00
|
|
|
SDL_FALLTHROUGH;
|
2015-06-21 17:33:46 +02:00
|
|
|
case 'u':
|
|
|
|
|
if (inttype == DO_LONGLONG) {
|
2024-09-09 14:54:50 -07:00
|
|
|
unsigned long long value = 0;
|
2022-06-18 07:02:38 -07:00
|
|
|
advance = SDL_ScanUnsignedLongLong(text, count, radix, &value);
|
2017-08-11 19:36:12 -07:00
|
|
|
text += advance;
|
|
|
|
|
if (advance && !suppress) {
|
2015-06-21 17:33:46 +02:00
|
|
|
Uint64 *valuep = va_arg(ap, Uint64 *);
|
|
|
|
|
*valuep = value;
|
2024-08-22 17:33:49 -07:00
|
|
|
++result;
|
2015-06-21 17:33:46 +02:00
|
|
|
}
|
2022-09-19 15:42:11 -07:00
|
|
|
} else if (inttype == DO_SIZE_T) {
|
2024-09-09 14:54:50 -07:00
|
|
|
unsigned long long value = 0;
|
2022-09-19 15:42:11 -07:00
|
|
|
advance = SDL_ScanUnsignedLongLong(text, count, radix, &value);
|
|
|
|
|
text += advance;
|
|
|
|
|
if (advance && !suppress) {
|
|
|
|
|
size_t *valuep = va_arg(ap, size_t *);
|
|
|
|
|
*valuep = (size_t)value;
|
2024-08-22 17:33:49 -07:00
|
|
|
++result;
|
2022-09-19 15:42:11 -07:00
|
|
|
}
|
2015-06-21 17:33:46 +02:00
|
|
|
} else {
|
2017-09-04 22:14:57 -07:00
|
|
|
unsigned long value = 0;
|
2022-06-18 07:02:38 -07:00
|
|
|
advance = SDL_ScanUnsignedLong(text, count, radix, &value);
|
2017-08-11 19:36:12 -07:00
|
|
|
text += advance;
|
|
|
|
|
if (advance && !suppress) {
|
2015-06-21 17:33:46 +02:00
|
|
|
switch (inttype) {
|
|
|
|
|
case DO_SHORT:
|
2022-11-30 12:51:59 -08:00
|
|
|
{
|
|
|
|
|
short *valuep = va_arg(ap, short *);
|
|
|
|
|
*valuep = (short)value;
|
|
|
|
|
} break;
|
2015-06-21 17:33:46 +02:00
|
|
|
case DO_INT:
|
2022-11-30 12:51:59 -08:00
|
|
|
{
|
|
|
|
|
int *valuep = va_arg(ap, int *);
|
|
|
|
|
*valuep = (int)value;
|
|
|
|
|
} break;
|
2015-06-21 17:33:46 +02:00
|
|
|
case DO_LONG:
|
2022-11-30 12:51:59 -08:00
|
|
|
{
|
|
|
|
|
long *valuep = va_arg(ap, long *);
|
|
|
|
|
*valuep = value;
|
|
|
|
|
} break;
|
2015-06-21 17:33:46 +02:00
|
|
|
case DO_LONGLONG:
|
2022-09-19 15:42:11 -07:00
|
|
|
case DO_SIZE_T:
|
Use C++ style comments consistently in SDL source code
Implemented using this script:
find . -type f -exec sed -i'' -e 's,/\* *\([^*]*\)\*/ *$,// \1,' -e 's, \+$,,' {} \;
git checkout \
core/linux/SDL_evdev_kbd_default_keymap.h \
events/imKStoUCS.* \
hidapi \
joystick/controller_type.c \
joystick/controller_type.h \
joystick/hidapi/steam/controller_constants.h \
joystick/hidapi/steam/controller_structs.h \
joystick/SDL_gamepad_db.h \
libm \
render/*/*Shader*.h \
render/vitagxm/SDL_render_vita_gxm_shaders.h \
render/metal/SDL_shaders_metal_*.h \
stdlib/SDL_malloc.c \
stdlib/SDL_qsort.c \
stdlib/SDL_strtokr.c \
test/ \
video/directx/SDL_d3d12_xbox_cmacros.h \
video/directx/d3d12.h \
video/directx/d3d12sdklayers.h \
video/khronos \
video/x11/edid-parse.c \
video/x11/xsettings-client.* \
video/yuv2rgb
sed -i'' -e 's,/\* *\([^*]*\)\*/ *$,// \1,' -e 's, \+$,,' hidapi/SDL_hidapi.c
2024-08-22 10:30:45 -07:00
|
|
|
// Handled above
|
2015-06-21 17:33:46 +02:00
|
|
|
break;
|
|
|
|
|
}
|
2024-08-22 17:33:49 -07:00
|
|
|
++result;
|
2015-06-21 17:33:46 +02:00
|
|
|
}
|
|
|
|
|
}
|
2024-08-22 09:21:26 -07:00
|
|
|
done = true;
|
2015-06-21 17:33:46 +02:00
|
|
|
break;
|
|
|
|
|
case 'p':
|
2022-11-30 12:51:59 -08:00
|
|
|
{
|
|
|
|
|
uintptr_t value = 0;
|
2024-09-12 00:26:36 +02:00
|
|
|
advance = SDL_ScanUintPtrT(text, &value);
|
2022-11-30 12:51:59 -08:00
|
|
|
text += advance;
|
|
|
|
|
if (advance && !suppress) {
|
|
|
|
|
void **valuep = va_arg(ap, void **);
|
|
|
|
|
*valuep = (void *)value;
|
2024-08-22 17:33:49 -07:00
|
|
|
++result;
|
2015-06-21 17:33:46 +02:00
|
|
|
}
|
2022-11-30 12:51:59 -08:00
|
|
|
}
|
2024-08-22 09:21:26 -07:00
|
|
|
done = true;
|
2015-06-21 17:33:46 +02:00
|
|
|
break;
|
|
|
|
|
case 'f':
|
2022-11-30 12:51:59 -08:00
|
|
|
{
|
|
|
|
|
double value = 0.0;
|
|
|
|
|
advance = SDL_ScanFloat(text, &value);
|
|
|
|
|
text += advance;
|
|
|
|
|
if (advance && !suppress) {
|
|
|
|
|
float *valuep = va_arg(ap, float *);
|
|
|
|
|
*valuep = (float)value;
|
2024-08-22 17:33:49 -07:00
|
|
|
++result;
|
2015-06-21 17:33:46 +02:00
|
|
|
}
|
2022-11-30 12:51:59 -08:00
|
|
|
}
|
2024-08-22 09:21:26 -07:00
|
|
|
done = true;
|
2015-06-21 17:33:46 +02:00
|
|
|
break;
|
|
|
|
|
case 's':
|
|
|
|
|
if (suppress) {
|
2022-11-30 12:51:59 -08:00
|
|
|
while (!SDL_isspace((unsigned char)*text)) {
|
2015-06-21 17:33:46 +02:00
|
|
|
++text;
|
|
|
|
|
if (count) {
|
|
|
|
|
if (--count == 0) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
char *valuep = va_arg(ap, char *);
|
2022-11-30 12:51:59 -08:00
|
|
|
while (!SDL_isspace((unsigned char)*text)) {
|
2015-06-21 17:33:46 +02:00
|
|
|
*valuep++ = *text++;
|
|
|
|
|
if (count) {
|
|
|
|
|
if (--count == 0) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
*valuep = '\0';
|
2024-08-22 17:33:49 -07:00
|
|
|
++result;
|
2015-06-21 17:33:46 +02:00
|
|
|
}
|
2024-08-22 09:21:26 -07:00
|
|
|
done = true;
|
2015-06-21 17:33:46 +02:00
|
|
|
break;
|
2023-10-24 16:41:19 -07:00
|
|
|
case '[':
|
|
|
|
|
{
|
|
|
|
|
const char *set = fmt + 1;
|
|
|
|
|
while (*fmt && *fmt != ']') {
|
|
|
|
|
++fmt;
|
|
|
|
|
}
|
|
|
|
|
if (*fmt) {
|
|
|
|
|
size_t set_len = (fmt - set);
|
|
|
|
|
if (suppress) {
|
|
|
|
|
while (CharacterMatchesSet(*text, set, set_len)) {
|
|
|
|
|
++text;
|
|
|
|
|
if (count) {
|
|
|
|
|
if (--count == 0) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
2024-08-22 09:21:26 -07:00
|
|
|
bool had_match = false;
|
2023-10-24 16:41:19 -07:00
|
|
|
char *valuep = va_arg(ap, char *);
|
|
|
|
|
while (CharacterMatchesSet(*text, set, set_len)) {
|
2024-08-22 09:21:26 -07:00
|
|
|
had_match = true;
|
2023-10-24 16:41:19 -07:00
|
|
|
*valuep++ = *text++;
|
|
|
|
|
if (count) {
|
|
|
|
|
if (--count == 0) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
*valuep = '\0';
|
|
|
|
|
if (had_match) {
|
2024-08-22 17:33:49 -07:00
|
|
|
++result;
|
2023-10-24 16:41:19 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-08-22 09:21:26 -07:00
|
|
|
done = true;
|
2023-10-24 16:41:19 -07:00
|
|
|
break;
|
2015-06-21 17:33:46 +02:00
|
|
|
default:
|
2024-08-22 09:21:26 -07:00
|
|
|
done = true;
|
2015-06-21 17:33:46 +02:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
++fmt;
|
|
|
|
|
}
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
if (*text == *fmt) {
|
|
|
|
|
++text;
|
|
|
|
|
++fmt;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
Use C++ style comments consistently in SDL source code
Implemented using this script:
find . -type f -exec sed -i'' -e 's,/\* *\([^*]*\)\*/ *$,// \1,' -e 's, \+$,,' {} \;
git checkout \
core/linux/SDL_evdev_kbd_default_keymap.h \
events/imKStoUCS.* \
hidapi \
joystick/controller_type.c \
joystick/controller_type.h \
joystick/hidapi/steam/controller_constants.h \
joystick/hidapi/steam/controller_structs.h \
joystick/SDL_gamepad_db.h \
libm \
render/*/*Shader*.h \
render/vitagxm/SDL_render_vita_gxm_shaders.h \
render/metal/SDL_shaders_metal_*.h \
stdlib/SDL_malloc.c \
stdlib/SDL_qsort.c \
stdlib/SDL_strtokr.c \
test/ \
video/directx/SDL_d3d12_xbox_cmacros.h \
video/directx/d3d12.h \
video/directx/d3d12sdklayers.h \
video/khronos \
video/x11/edid-parse.c \
video/x11/xsettings-client.* \
video/yuv2rgb
sed -i'' -e 's,/\* *\([^*]*\)\*/ *$,// \1,' -e 's, \+$,,' hidapi/SDL_hidapi.c
2024-08-22 10:30:45 -07:00
|
|
|
// Text didn't match format specifier
|
2015-06-21 17:33:46 +02:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-22 17:33:49 -07:00
|
|
|
return result;
|
2015-06-21 17:33:46 +02:00
|
|
|
}
|
Use C++ style comments consistently in SDL source code
Implemented using this script:
find . -type f -exec sed -i'' -e 's,/\* *\([^*]*\)\*/ *$,// \1,' -e 's, \+$,,' {} \;
git checkout \
core/linux/SDL_evdev_kbd_default_keymap.h \
events/imKStoUCS.* \
hidapi \
joystick/controller_type.c \
joystick/controller_type.h \
joystick/hidapi/steam/controller_constants.h \
joystick/hidapi/steam/controller_structs.h \
joystick/SDL_gamepad_db.h \
libm \
render/*/*Shader*.h \
render/vitagxm/SDL_render_vita_gxm_shaders.h \
render/metal/SDL_shaders_metal_*.h \
stdlib/SDL_malloc.c \
stdlib/SDL_qsort.c \
stdlib/SDL_strtokr.c \
test/ \
video/directx/SDL_d3d12_xbox_cmacros.h \
video/directx/d3d12.h \
video/directx/d3d12sdklayers.h \
video/khronos \
video/x11/edid-parse.c \
video/x11/xsettings-client.* \
video/yuv2rgb
sed -i'' -e 's,/\* *\([^*]*\)\*/ *$,// \1,' -e 's, \+$,,' hidapi/SDL_hidapi.c
2024-08-22 10:30:45 -07:00
|
|
|
#endif // HAVE_VSSCANF
|
2015-06-21 17:33:46 +02:00
|
|
|
|
2022-11-30 12:51:59 -08:00
|
|
|
int SDL_snprintf(SDL_OUT_Z_CAP(maxlen) char *text, size_t maxlen, SDL_PRINTF_FORMAT_STRING const char *fmt, ...)
|
2015-06-21 17:33:46 +02:00
|
|
|
{
|
|
|
|
|
va_list ap;
|
2024-08-22 17:33:49 -07:00
|
|
|
int result;
|
2015-06-21 17:33:46 +02:00
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
2024-08-22 17:33:49 -07:00
|
|
|
result = SDL_vsnprintf(text, maxlen, fmt, ap);
|
2015-06-21 17:33:46 +02:00
|
|
|
va_end(ap);
|
|
|
|
|
|
2024-08-22 17:33:49 -07:00
|
|
|
return result;
|
2015-06-21 17:33:46 +02:00
|
|
|
}
|
|
|
|
|
|
2023-05-24 09:41:22 -07:00
|
|
|
int SDL_swprintf(SDL_OUT_Z_CAP(maxlen) wchar_t *text, size_t maxlen, SDL_PRINTF_FORMAT_STRING const wchar_t *fmt, ...)
|
|
|
|
|
{
|
|
|
|
|
va_list ap;
|
2024-08-22 17:33:49 -07:00
|
|
|
int result;
|
2023-05-24 09:41:22 -07:00
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
2024-08-22 17:33:49 -07:00
|
|
|
result = SDL_vswprintf(text, maxlen, fmt, ap);
|
2023-05-24 09:41:22 -07:00
|
|
|
va_end(ap);
|
|
|
|
|
|
2024-08-22 17:33:49 -07:00
|
|
|
return result;
|
2023-05-24 09:41:22 -07:00
|
|
|
}
|
|
|
|
|
|
2018-05-10 09:02:39 +03:00
|
|
|
#if defined(HAVE_LIBC) && defined(__WATCOMC__)
|
Use C++ style comments consistently in SDL source code
Implemented using this script:
find . -type f -exec sed -i'' -e 's,/\* *\([^*]*\)\*/ *$,// \1,' -e 's, \+$,,' {} \;
git checkout \
core/linux/SDL_evdev_kbd_default_keymap.h \
events/imKStoUCS.* \
hidapi \
joystick/controller_type.c \
joystick/controller_type.h \
joystick/hidapi/steam/controller_constants.h \
joystick/hidapi/steam/controller_structs.h \
joystick/SDL_gamepad_db.h \
libm \
render/*/*Shader*.h \
render/vitagxm/SDL_render_vita_gxm_shaders.h \
render/metal/SDL_shaders_metal_*.h \
stdlib/SDL_malloc.c \
stdlib/SDL_qsort.c \
stdlib/SDL_strtokr.c \
test/ \
video/directx/SDL_d3d12_xbox_cmacros.h \
video/directx/d3d12.h \
video/directx/d3d12sdklayers.h \
video/khronos \
video/x11/edid-parse.c \
video/x11/xsettings-client.* \
video/yuv2rgb
sed -i'' -e 's,/\* *\([^*]*\)\*/ *$,// \1,' -e 's, \+$,,' hidapi/SDL_hidapi.c
2024-08-22 10:30:45 -07:00
|
|
|
// _vsnprintf() doesn't ensure nul termination
|
2018-05-10 09:02:39 +03:00
|
|
|
int SDL_vsnprintf(SDL_OUT_Z_CAP(maxlen) char *text, size_t maxlen, const char *fmt, va_list ap)
|
|
|
|
|
{
|
2024-08-22 17:33:49 -07:00
|
|
|
int result;
|
2022-11-27 17:38:43 +01:00
|
|
|
if (!fmt) {
|
|
|
|
|
fmt = "";
|
|
|
|
|
}
|
2024-08-22 17:33:49 -07:00
|
|
|
result = _vsnprintf(text, maxlen, fmt, ap);
|
2022-11-27 17:38:43 +01:00
|
|
|
if (maxlen > 0) {
|
|
|
|
|
text[maxlen - 1] = '\0';
|
|
|
|
|
}
|
2024-08-22 17:33:49 -07:00
|
|
|
if (result < 0) {
|
|
|
|
|
result = (int)maxlen;
|
2022-11-27 17:38:43 +01:00
|
|
|
}
|
2024-08-22 17:33:49 -07:00
|
|
|
return result;
|
2018-05-10 09:02:39 +03:00
|
|
|
}
|
|
|
|
|
#elif defined(HAVE_VSNPRINTF)
|
2015-06-21 17:33:46 +02:00
|
|
|
int SDL_vsnprintf(SDL_OUT_Z_CAP(maxlen) char *text, size_t maxlen, const char *fmt, va_list ap)
|
|
|
|
|
{
|
|
|
|
|
if (!fmt) {
|
|
|
|
|
fmt = "";
|
|
|
|
|
}
|
|
|
|
|
return vsnprintf(text, maxlen, fmt, ap);
|
|
|
|
|
}
|
|
|
|
|
#else
|
2022-11-30 12:51:59 -08:00
|
|
|
#define TEXT_AND_LEN_ARGS (length < maxlen) ? &text[length] : NULL, (length < maxlen) ? (maxlen - length) : 0
|
2021-09-22 11:42:10 -07:00
|
|
|
|
Use C++ style comments consistently in SDL source code
Implemented using this script:
find . -type f -exec sed -i'' -e 's,/\* *\([^*]*\)\*/ *$,// \1,' -e 's, \+$,,' {} \;
git checkout \
core/linux/SDL_evdev_kbd_default_keymap.h \
events/imKStoUCS.* \
hidapi \
joystick/controller_type.c \
joystick/controller_type.h \
joystick/hidapi/steam/controller_constants.h \
joystick/hidapi/steam/controller_structs.h \
joystick/SDL_gamepad_db.h \
libm \
render/*/*Shader*.h \
render/vitagxm/SDL_render_vita_gxm_shaders.h \
render/metal/SDL_shaders_metal_*.h \
stdlib/SDL_malloc.c \
stdlib/SDL_qsort.c \
stdlib/SDL_strtokr.c \
test/ \
video/directx/SDL_d3d12_xbox_cmacros.h \
video/directx/d3d12.h \
video/directx/d3d12sdklayers.h \
video/khronos \
video/x11/edid-parse.c \
video/x11/xsettings-client.* \
video/yuv2rgb
sed -i'' -e 's,/\* *\([^*]*\)\*/ *$,// \1,' -e 's, \+$,,' hidapi/SDL_hidapi.c
2024-08-22 10:30:45 -07:00
|
|
|
// FIXME: implement more of the format specifiers
|
2015-06-21 17:33:46 +02:00
|
|
|
typedef enum
|
|
|
|
|
{
|
|
|
|
|
SDL_CASE_NOCHANGE,
|
|
|
|
|
SDL_CASE_LOWER,
|
|
|
|
|
SDL_CASE_UPPER
|
|
|
|
|
} SDL_letter_case;
|
|
|
|
|
|
|
|
|
|
typedef struct
|
|
|
|
|
{
|
2024-08-22 09:21:26 -07:00
|
|
|
bool left_justify;
|
|
|
|
|
bool force_sign;
|
|
|
|
|
bool force_type; // for now: used only by float printer, ignored otherwise.
|
|
|
|
|
bool pad_zeroes;
|
2015-06-21 17:33:46 +02:00
|
|
|
SDL_letter_case force_case;
|
|
|
|
|
int width;
|
|
|
|
|
int radix;
|
|
|
|
|
int precision;
|
|
|
|
|
} SDL_FormatInfo;
|
|
|
|
|
|
2023-05-23 11:29:41 -07:00
|
|
|
static size_t SDL_PrintString(char *text, size_t maxlen, SDL_FormatInfo *info, const char *string)
|
2015-06-21 17:33:46 +02:00
|
|
|
{
|
2024-08-04 19:21:06 -07:00
|
|
|
const char fill = (info && info->pad_zeroes) ? '0' : ' ';
|
|
|
|
|
size_t width = 0;
|
|
|
|
|
size_t filllen = 0;
|
2015-06-21 17:33:46 +02:00
|
|
|
size_t length = 0;
|
2018-09-26 10:40:02 +03:00
|
|
|
size_t slen, sz;
|
2015-06-21 17:33:46 +02:00
|
|
|
|
2023-11-09 22:29:15 +01:00
|
|
|
if (!string) {
|
2017-02-14 02:49:08 -05:00
|
|
|
string = "(null)";
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-26 10:40:02 +03:00
|
|
|
sz = SDL_strlen(string);
|
|
|
|
|
if (info && info->width > 0 && (size_t)info->width > sz) {
|
2024-08-04 19:21:06 -07:00
|
|
|
width = info->width - sz;
|
2022-11-27 17:38:43 +01:00
|
|
|
if (info->precision >= 0 && (size_t)info->precision < sz) {
|
2018-09-26 10:40:02 +03:00
|
|
|
width += sz - (size_t)info->precision;
|
2022-11-27 17:38:43 +01:00
|
|
|
}
|
2019-09-26 12:55:05 -04:00
|
|
|
|
|
|
|
|
filllen = SDL_min(width, maxlen);
|
2024-08-04 19:21:06 -07:00
|
|
|
if (!info->left_justify) {
|
|
|
|
|
SDL_memset(text, fill, filllen);
|
|
|
|
|
text += filllen;
|
|
|
|
|
maxlen -= filllen;
|
|
|
|
|
length += width;
|
|
|
|
|
filllen = 0;
|
|
|
|
|
}
|
2015-06-21 17:33:46 +02:00
|
|
|
}
|
|
|
|
|
|
2021-09-22 11:42:10 -07:00
|
|
|
SDL_strlcpy(text, string, maxlen);
|
|
|
|
|
length += sz;
|
2015-06-21 17:33:46 +02:00
|
|
|
|
2024-08-04 19:21:06 -07:00
|
|
|
if (filllen > 0) {
|
|
|
|
|
SDL_memset(text + sz, fill, filllen);
|
|
|
|
|
length += width;
|
|
|
|
|
}
|
|
|
|
|
|
2015-06-21 17:33:46 +02:00
|
|
|
if (info) {
|
2018-09-27 01:10:50 +03:00
|
|
|
if (info->precision >= 0 && (size_t)info->precision < sz) {
|
|
|
|
|
slen = (size_t)info->precision;
|
2018-09-26 10:40:02 +03:00
|
|
|
if (slen < maxlen) {
|
2021-09-22 11:42:10 -07:00
|
|
|
text[slen] = '\0';
|
2018-09-26 10:40:02 +03:00
|
|
|
}
|
2021-09-22 11:42:10 -07:00
|
|
|
length -= (sz - slen);
|
2018-09-26 10:40:02 +03:00
|
|
|
}
|
2021-09-22 11:42:10 -07:00
|
|
|
if (maxlen > 1) {
|
|
|
|
|
if (info->force_case == SDL_CASE_LOWER) {
|
|
|
|
|
SDL_strlwr(text);
|
|
|
|
|
} else if (info->force_case == SDL_CASE_UPPER) {
|
|
|
|
|
SDL_strupr(text);
|
|
|
|
|
}
|
2015-06-21 17:33:46 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return length;
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-26 13:57:49 -07:00
|
|
|
static size_t SDL_PrintStringW(char *text, size_t maxlen, SDL_FormatInfo *info, const wchar_t *wide_string)
|
|
|
|
|
{
|
|
|
|
|
size_t length = 0;
|
|
|
|
|
if (wide_string) {
|
|
|
|
|
char *string = SDL_iconv_string("UTF-8", "WCHAR_T", (char *)(wide_string), (SDL_wcslen(wide_string) + 1) * sizeof(*wide_string));
|
|
|
|
|
length = SDL_PrintString(TEXT_AND_LEN_ARGS, info, string);
|
|
|
|
|
SDL_free(string);
|
|
|
|
|
} else {
|
|
|
|
|
length = SDL_PrintString(TEXT_AND_LEN_ARGS, info, NULL);
|
|
|
|
|
}
|
|
|
|
|
return length;
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-30 12:51:59 -08:00
|
|
|
static void SDL_IntPrecisionAdjust(char *num, size_t maxlen, SDL_FormatInfo *info)
|
Use C++ style comments consistently in SDL source code
Implemented using this script:
find . -type f -exec sed -i'' -e 's,/\* *\([^*]*\)\*/ *$,// \1,' -e 's, \+$,,' {} \;
git checkout \
core/linux/SDL_evdev_kbd_default_keymap.h \
events/imKStoUCS.* \
hidapi \
joystick/controller_type.c \
joystick/controller_type.h \
joystick/hidapi/steam/controller_constants.h \
joystick/hidapi/steam/controller_structs.h \
joystick/SDL_gamepad_db.h \
libm \
render/*/*Shader*.h \
render/vitagxm/SDL_render_vita_gxm_shaders.h \
render/metal/SDL_shaders_metal_*.h \
stdlib/SDL_malloc.c \
stdlib/SDL_qsort.c \
stdlib/SDL_strtokr.c \
test/ \
video/directx/SDL_d3d12_xbox_cmacros.h \
video/directx/d3d12.h \
video/directx/d3d12sdklayers.h \
video/khronos \
video/x11/edid-parse.c \
video/x11/xsettings-client.* \
video/yuv2rgb
sed -i'' -e 's,/\* *\([^*]*\)\*/ *$,// \1,' -e 's, \+$,,' hidapi/SDL_hidapi.c
2024-08-22 10:30:45 -07:00
|
|
|
{ // left-pad num with zeroes.
|
2018-09-29 00:51:24 +03:00
|
|
|
size_t sz, pad, have_sign;
|
2018-09-27 00:32:15 +03:00
|
|
|
|
2023-11-09 22:29:15 +01:00
|
|
|
if (!info) {
|
2018-09-27 00:32:15 +03:00
|
|
|
return;
|
2022-11-27 17:38:43 +01:00
|
|
|
}
|
2018-09-27 00:32:15 +03:00
|
|
|
|
2018-09-29 00:51:24 +03:00
|
|
|
have_sign = 0;
|
|
|
|
|
if (*num == '-' || *num == '+') {
|
|
|
|
|
have_sign = 1;
|
2018-09-27 00:32:15 +03:00
|
|
|
++num;
|
2018-09-29 00:51:24 +03:00
|
|
|
--maxlen;
|
|
|
|
|
}
|
2018-09-27 00:32:15 +03:00
|
|
|
sz = SDL_strlen(num);
|
2018-09-29 00:51:24 +03:00
|
|
|
if (info->precision > 0 && sz < (size_t)info->precision) {
|
2018-09-27 00:32:15 +03:00
|
|
|
pad = (size_t)info->precision - sz;
|
Use C++ style comments consistently in SDL source code
Implemented using this script:
find . -type f -exec sed -i'' -e 's,/\* *\([^*]*\)\*/ *$,// \1,' -e 's, \+$,,' {} \;
git checkout \
core/linux/SDL_evdev_kbd_default_keymap.h \
events/imKStoUCS.* \
hidapi \
joystick/controller_type.c \
joystick/controller_type.h \
joystick/hidapi/steam/controller_constants.h \
joystick/hidapi/steam/controller_structs.h \
joystick/SDL_gamepad_db.h \
libm \
render/*/*Shader*.h \
render/vitagxm/SDL_render_vita_gxm_shaders.h \
render/metal/SDL_shaders_metal_*.h \
stdlib/SDL_malloc.c \
stdlib/SDL_qsort.c \
stdlib/SDL_strtokr.c \
test/ \
video/directx/SDL_d3d12_xbox_cmacros.h \
video/directx/d3d12.h \
video/directx/d3d12sdklayers.h \
video/khronos \
video/x11/edid-parse.c \
video/x11/xsettings-client.* \
video/yuv2rgb
sed -i'' -e 's,/\* *\([^*]*\)\*/ *$,// \1,' -e 's, \+$,,' hidapi/SDL_hidapi.c
2024-08-22 10:30:45 -07:00
|
|
|
if (pad + sz + 1 <= maxlen) { // otherwise ignore the precision
|
2018-09-27 00:32:15 +03:00
|
|
|
SDL_memmove(num + pad, num, sz + 1);
|
2018-09-27 01:00:50 +03:00
|
|
|
SDL_memset(num, '0', pad);
|
2018-09-27 00:32:15 +03:00
|
|
|
}
|
|
|
|
|
}
|
Use C++ style comments consistently in SDL source code
Implemented using this script:
find . -type f -exec sed -i'' -e 's,/\* *\([^*]*\)\*/ *$,// \1,' -e 's, \+$,,' {} \;
git checkout \
core/linux/SDL_evdev_kbd_default_keymap.h \
events/imKStoUCS.* \
hidapi \
joystick/controller_type.c \
joystick/controller_type.h \
joystick/hidapi/steam/controller_constants.h \
joystick/hidapi/steam/controller_structs.h \
joystick/SDL_gamepad_db.h \
libm \
render/*/*Shader*.h \
render/vitagxm/SDL_render_vita_gxm_shaders.h \
render/metal/SDL_shaders_metal_*.h \
stdlib/SDL_malloc.c \
stdlib/SDL_qsort.c \
stdlib/SDL_strtokr.c \
test/ \
video/directx/SDL_d3d12_xbox_cmacros.h \
video/directx/d3d12.h \
video/directx/d3d12sdklayers.h \
video/khronos \
video/x11/edid-parse.c \
video/x11/xsettings-client.* \
video/yuv2rgb
sed -i'' -e 's,/\* *\([^*]*\)\*/ *$,// \1,' -e 's, \+$,,' hidapi/SDL_hidapi.c
2024-08-22 10:30:45 -07:00
|
|
|
info->precision = -1; // so that SDL_PrintString() doesn't make a mess.
|
2018-09-29 00:51:24 +03:00
|
|
|
|
|
|
|
|
if (info->pad_zeroes && info->width > 0 && (size_t)info->width > sz + have_sign) {
|
2022-11-30 12:51:59 -08:00
|
|
|
/* handle here: spaces are added before the sign
|
|
|
|
|
but zeroes must be placed _after_ the sign. */
|
Use C++ style comments consistently in SDL source code
Implemented using this script:
find . -type f -exec sed -i'' -e 's,/\* *\([^*]*\)\*/ *$,// \1,' -e 's, \+$,,' {} \;
git checkout \
core/linux/SDL_evdev_kbd_default_keymap.h \
events/imKStoUCS.* \
hidapi \
joystick/controller_type.c \
joystick/controller_type.h \
joystick/hidapi/steam/controller_constants.h \
joystick/hidapi/steam/controller_structs.h \
joystick/SDL_gamepad_db.h \
libm \
render/*/*Shader*.h \
render/vitagxm/SDL_render_vita_gxm_shaders.h \
render/metal/SDL_shaders_metal_*.h \
stdlib/SDL_malloc.c \
stdlib/SDL_qsort.c \
stdlib/SDL_strtokr.c \
test/ \
video/directx/SDL_d3d12_xbox_cmacros.h \
video/directx/d3d12.h \
video/directx/d3d12sdklayers.h \
video/khronos \
video/x11/edid-parse.c \
video/x11/xsettings-client.* \
video/yuv2rgb
sed -i'' -e 's,/\* *\([^*]*\)\*/ *$,// \1,' -e 's, \+$,,' hidapi/SDL_hidapi.c
2024-08-22 10:30:45 -07:00
|
|
|
// sz hasn't changed: we ignore pad_zeroes if a precision is given.
|
2018-09-29 00:51:24 +03:00
|
|
|
pad = (size_t)info->width - sz - have_sign;
|
|
|
|
|
if (pad + sz + 1 <= maxlen) {
|
|
|
|
|
SDL_memmove(num + pad, num, sz + 1);
|
|
|
|
|
SDL_memset(num, '0', pad);
|
|
|
|
|
}
|
Use C++ style comments consistently in SDL source code
Implemented using this script:
find . -type f -exec sed -i'' -e 's,/\* *\([^*]*\)\*/ *$,// \1,' -e 's, \+$,,' {} \;
git checkout \
core/linux/SDL_evdev_kbd_default_keymap.h \
events/imKStoUCS.* \
hidapi \
joystick/controller_type.c \
joystick/controller_type.h \
joystick/hidapi/steam/controller_constants.h \
joystick/hidapi/steam/controller_structs.h \
joystick/SDL_gamepad_db.h \
libm \
render/*/*Shader*.h \
render/vitagxm/SDL_render_vita_gxm_shaders.h \
render/metal/SDL_shaders_metal_*.h \
stdlib/SDL_malloc.c \
stdlib/SDL_qsort.c \
stdlib/SDL_strtokr.c \
test/ \
video/directx/SDL_d3d12_xbox_cmacros.h \
video/directx/d3d12.h \
video/directx/d3d12sdklayers.h \
video/khronos \
video/x11/edid-parse.c \
video/x11/xsettings-client.* \
video/yuv2rgb
sed -i'' -e 's,/\* *\([^*]*\)\*/ *$,// \1,' -e 's, \+$,,' hidapi/SDL_hidapi.c
2024-08-22 10:30:45 -07:00
|
|
|
info->width = 0; // so that SDL_PrintString() doesn't make a mess.
|
2018-09-29 00:51:24 +03:00
|
|
|
}
|
2018-09-27 00:32:15 +03:00
|
|
|
}
|
|
|
|
|
|
2023-05-23 11:29:41 -07:00
|
|
|
static size_t SDL_PrintLong(char *text, size_t maxlen, SDL_FormatInfo *info, long value)
|
2015-06-21 17:33:46 +02:00
|
|
|
{
|
2018-09-29 01:24:10 +03:00
|
|
|
char num[130], *p = num;
|
|
|
|
|
|
|
|
|
|
if (info->force_sign && value >= 0L) {
|
|
|
|
|
*p++ = '+';
|
|
|
|
|
}
|
2015-06-21 17:33:46 +02:00
|
|
|
|
2018-09-29 01:24:10 +03:00
|
|
|
SDL_ltoa(value, p, info ? info->radix : 10);
|
2021-09-22 11:42:10 -07:00
|
|
|
SDL_IntPrecisionAdjust(num, sizeof(num), info);
|
2015-06-21 17:33:46 +02:00
|
|
|
return SDL_PrintString(text, maxlen, info, num);
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-23 11:29:41 -07:00
|
|
|
static size_t SDL_PrintUnsignedLong(char *text, size_t maxlen, SDL_FormatInfo *info, unsigned long value)
|
2015-06-21 17:33:46 +02:00
|
|
|
{
|
|
|
|
|
char num[130];
|
|
|
|
|
|
|
|
|
|
SDL_ultoa(value, num, info ? info->radix : 10);
|
2021-09-22 11:42:10 -07:00
|
|
|
SDL_IntPrecisionAdjust(num, sizeof(num), info);
|
2015-06-21 17:33:46 +02:00
|
|
|
return SDL_PrintString(text, maxlen, info, num);
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-09 14:54:50 -07:00
|
|
|
static size_t SDL_PrintLongLong(char *text, size_t maxlen, SDL_FormatInfo *info, long long value)
|
2015-06-21 17:33:46 +02:00
|
|
|
{
|
2018-09-29 01:24:10 +03:00
|
|
|
char num[130], *p = num;
|
|
|
|
|
|
|
|
|
|
if (info->force_sign && value >= (Sint64)0) {
|
|
|
|
|
*p++ = '+';
|
|
|
|
|
}
|
2015-06-21 17:33:46 +02:00
|
|
|
|
2018-09-29 01:24:10 +03:00
|
|
|
SDL_lltoa(value, p, info ? info->radix : 10);
|
2021-09-22 11:42:10 -07:00
|
|
|
SDL_IntPrecisionAdjust(num, sizeof(num), info);
|
2015-06-21 17:33:46 +02:00
|
|
|
return SDL_PrintString(text, maxlen, info, num);
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-09 14:54:50 -07:00
|
|
|
static size_t SDL_PrintUnsignedLongLong(char *text, size_t maxlen, SDL_FormatInfo *info, unsigned long long value)
|
2015-06-21 17:33:46 +02:00
|
|
|
{
|
|
|
|
|
char num[130];
|
|
|
|
|
|
|
|
|
|
SDL_ulltoa(value, num, info ? info->radix : 10);
|
2021-09-22 11:42:10 -07:00
|
|
|
SDL_IntPrecisionAdjust(num, sizeof(num), info);
|
2015-06-21 17:33:46 +02:00
|
|
|
return SDL_PrintString(text, maxlen, info, num);
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-22 09:21:26 -07:00
|
|
|
static size_t SDL_PrintFloat(char *text, size_t maxlen, SDL_FormatInfo *info, double arg, bool g)
|
2015-06-21 17:33:46 +02:00
|
|
|
{
|
2023-01-02 21:57:42 -08:00
|
|
|
char num[327];
|
2021-09-22 11:42:10 -07:00
|
|
|
size_t length = 0;
|
2023-01-02 21:57:42 -08:00
|
|
|
size_t integer_length;
|
|
|
|
|
int precision = info->precision;
|
2015-06-21 17:33:46 +02:00
|
|
|
|
Use C++ style comments consistently in SDL source code
Implemented using this script:
find . -type f -exec sed -i'' -e 's,/\* *\([^*]*\)\*/ *$,// \1,' -e 's, \+$,,' {} \;
git checkout \
core/linux/SDL_evdev_kbd_default_keymap.h \
events/imKStoUCS.* \
hidapi \
joystick/controller_type.c \
joystick/controller_type.h \
joystick/hidapi/steam/controller_constants.h \
joystick/hidapi/steam/controller_structs.h \
joystick/SDL_gamepad_db.h \
libm \
render/*/*Shader*.h \
render/vitagxm/SDL_render_vita_gxm_shaders.h \
render/metal/SDL_shaders_metal_*.h \
stdlib/SDL_malloc.c \
stdlib/SDL_qsort.c \
stdlib/SDL_strtokr.c \
test/ \
video/directx/SDL_d3d12_xbox_cmacros.h \
video/directx/d3d12.h \
video/directx/d3d12sdklayers.h \
video/khronos \
video/x11/edid-parse.c \
video/x11/xsettings-client.* \
video/yuv2rgb
sed -i'' -e 's,/\* *\([^*]*\)\*/ *$,// \1,' -e 's, \+$,,' hidapi/SDL_hidapi.c
2024-08-22 10:30:45 -07:00
|
|
|
// This isn't especially accurate, but hey, it's easy. :)
|
2024-09-09 14:54:50 -07:00
|
|
|
unsigned long long value;
|
2015-06-21 17:33:46 +02:00
|
|
|
|
2024-09-12 01:23:56 +02:00
|
|
|
if (arg < 0.0 || (arg == 0.0 && 1.0 / arg < 0.0)) { // additional check for signed zero
|
2023-01-02 21:57:42 -08:00
|
|
|
num[length++] = '-';
|
2021-11-07 12:26:39 -05:00
|
|
|
arg = -arg;
|
|
|
|
|
} else if (info->force_sign) {
|
2023-01-02 21:57:42 -08:00
|
|
|
num[length++] = '+';
|
2021-11-07 12:26:39 -05:00
|
|
|
}
|
2024-09-09 14:54:50 -07:00
|
|
|
value = (unsigned long long)arg;
|
2023-01-02 21:57:42 -08:00
|
|
|
integer_length = SDL_PrintUnsignedLongLong(&num[length], sizeof(num) - length, NULL, value);
|
|
|
|
|
length += integer_length;
|
2021-11-07 12:26:39 -05:00
|
|
|
arg -= value;
|
2023-01-02 21:57:42 -08:00
|
|
|
if (precision < 0) {
|
|
|
|
|
precision = 6;
|
|
|
|
|
}
|
|
|
|
|
if (g) {
|
Use C++ style comments consistently in SDL source code
Implemented using this script:
find . -type f -exec sed -i'' -e 's,/\* *\([^*]*\)\*/ *$,// \1,' -e 's, \+$,,' {} \;
git checkout \
core/linux/SDL_evdev_kbd_default_keymap.h \
events/imKStoUCS.* \
hidapi \
joystick/controller_type.c \
joystick/controller_type.h \
joystick/hidapi/steam/controller_constants.h \
joystick/hidapi/steam/controller_structs.h \
joystick/SDL_gamepad_db.h \
libm \
render/*/*Shader*.h \
render/vitagxm/SDL_render_vita_gxm_shaders.h \
render/metal/SDL_shaders_metal_*.h \
stdlib/SDL_malloc.c \
stdlib/SDL_qsort.c \
stdlib/SDL_strtokr.c \
test/ \
video/directx/SDL_d3d12_xbox_cmacros.h \
video/directx/d3d12.h \
video/directx/d3d12sdklayers.h \
video/khronos \
video/x11/edid-parse.c \
video/x11/xsettings-client.* \
video/yuv2rgb
sed -i'' -e 's,/\* *\([^*]*\)\*/ *$,// \1,' -e 's, \+$,,' hidapi/SDL_hidapi.c
2024-08-22 10:30:45 -07:00
|
|
|
// The precision includes the integer portion
|
2023-01-02 21:57:42 -08:00
|
|
|
precision -= SDL_min((int)integer_length, precision);
|
|
|
|
|
}
|
|
|
|
|
if (info->force_type || precision > 0) {
|
|
|
|
|
const char decimal_separator = '.';
|
|
|
|
|
double integer_value;
|
|
|
|
|
|
|
|
|
|
SDL_assert(length < sizeof(num));
|
|
|
|
|
num[length++] = decimal_separator;
|
|
|
|
|
while (precision > 1) {
|
|
|
|
|
arg *= 10.0;
|
|
|
|
|
arg = SDL_modf(arg, &integer_value);
|
|
|
|
|
SDL_assert(length < sizeof(num));
|
2023-03-30 14:04:32 -07:00
|
|
|
num[length++] = '0' + (char)integer_value;
|
2023-01-02 21:57:42 -08:00
|
|
|
--precision;
|
2015-06-21 17:33:46 +02:00
|
|
|
}
|
2023-01-02 21:57:42 -08:00
|
|
|
if (precision == 1) {
|
|
|
|
|
arg *= 10.0;
|
|
|
|
|
integer_value = SDL_round(arg);
|
|
|
|
|
if (integer_value == 10.0) {
|
Use C++ style comments consistently in SDL source code
Implemented using this script:
find . -type f -exec sed -i'' -e 's,/\* *\([^*]*\)\*/ *$,// \1,' -e 's, \+$,,' {} \;
git checkout \
core/linux/SDL_evdev_kbd_default_keymap.h \
events/imKStoUCS.* \
hidapi \
joystick/controller_type.c \
joystick/controller_type.h \
joystick/hidapi/steam/controller_constants.h \
joystick/hidapi/steam/controller_structs.h \
joystick/SDL_gamepad_db.h \
libm \
render/*/*Shader*.h \
render/vitagxm/SDL_render_vita_gxm_shaders.h \
render/metal/SDL_shaders_metal_*.h \
stdlib/SDL_malloc.c \
stdlib/SDL_qsort.c \
stdlib/SDL_strtokr.c \
test/ \
video/directx/SDL_d3d12_xbox_cmacros.h \
video/directx/d3d12.h \
video/directx/d3d12sdklayers.h \
video/khronos \
video/x11/edid-parse.c \
video/x11/xsettings-client.* \
video/yuv2rgb
sed -i'' -e 's,/\* *\([^*]*\)\*/ *$,// \1,' -e 's, \+$,,' hidapi/SDL_hidapi.c
2024-08-22 10:30:45 -07:00
|
|
|
// Carry the one...
|
2023-01-02 21:57:42 -08:00
|
|
|
size_t i;
|
|
|
|
|
|
|
|
|
|
for (i = length; i--; ) {
|
|
|
|
|
if (num[i] == decimal_separator) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
if (num[i] == '9') {
|
|
|
|
|
num[i] = '0';
|
2023-08-07 16:33:44 +01:00
|
|
|
if (i == 0 || num[i - 1] == '-' || num[i - 1] == '+') {
|
|
|
|
|
SDL_memmove(&num[i+1], &num[i], length - i);
|
|
|
|
|
num[i] = '1';
|
|
|
|
|
++length;
|
|
|
|
|
break;
|
|
|
|
|
}
|
2023-01-02 21:57:42 -08:00
|
|
|
} else {
|
|
|
|
|
++num[i];
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
SDL_assert(length < sizeof(num));
|
|
|
|
|
num[length++] = '0';
|
|
|
|
|
} else {
|
|
|
|
|
SDL_assert(length < sizeof(num));
|
2023-03-30 14:04:32 -07:00
|
|
|
num[length++] = '0' + (char)integer_value;
|
2023-01-02 21:57:42 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (g) {
|
Use C++ style comments consistently in SDL source code
Implemented using this script:
find . -type f -exec sed -i'' -e 's,/\* *\([^*]*\)\*/ *$,// \1,' -e 's, \+$,,' {} \;
git checkout \
core/linux/SDL_evdev_kbd_default_keymap.h \
events/imKStoUCS.* \
hidapi \
joystick/controller_type.c \
joystick/controller_type.h \
joystick/hidapi/steam/controller_constants.h \
joystick/hidapi/steam/controller_structs.h \
joystick/SDL_gamepad_db.h \
libm \
render/*/*Shader*.h \
render/vitagxm/SDL_render_vita_gxm_shaders.h \
render/metal/SDL_shaders_metal_*.h \
stdlib/SDL_malloc.c \
stdlib/SDL_qsort.c \
stdlib/SDL_strtokr.c \
test/ \
video/directx/SDL_d3d12_xbox_cmacros.h \
video/directx/d3d12.h \
video/directx/d3d12sdklayers.h \
video/khronos \
video/x11/edid-parse.c \
video/x11/xsettings-client.* \
video/yuv2rgb
sed -i'' -e 's,/\* *\([^*]*\)\*/ *$,// \1,' -e 's, \+$,,' hidapi/SDL_hidapi.c
2024-08-22 10:30:45 -07:00
|
|
|
// Trim trailing zeroes and decimal separator
|
2023-01-02 21:57:42 -08:00
|
|
|
size_t i;
|
|
|
|
|
|
|
|
|
|
for (i = length - 1; num[i] != decimal_separator; --i) {
|
|
|
|
|
if (num[i] == '0') {
|
|
|
|
|
--length;
|
|
|
|
|
} else {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (num[i] == decimal_separator) {
|
|
|
|
|
--length;
|
|
|
|
|
}
|
2015-06-21 17:33:46 +02:00
|
|
|
}
|
|
|
|
|
}
|
2023-01-02 21:57:42 -08:00
|
|
|
num[length] = '\0';
|
|
|
|
|
|
|
|
|
|
info->precision = -1;
|
|
|
|
|
length = SDL_PrintString(text, maxlen, info, num);
|
2015-06-21 17:33:46 +02:00
|
|
|
|
2021-09-22 11:42:10 -07:00
|
|
|
if (info->width > 0 && (size_t)info->width > length) {
|
2019-09-26 12:55:05 -04:00
|
|
|
const char fill = info->pad_zeroes ? '0' : ' ';
|
2021-09-22 11:42:10 -07:00
|
|
|
size_t width = info->width - length;
|
|
|
|
|
size_t filllen, movelen;
|
2019-09-26 12:55:05 -04:00
|
|
|
|
2021-09-22 11:42:10 -07:00
|
|
|
filllen = SDL_min(width, maxlen);
|
|
|
|
|
movelen = SDL_min(length, (maxlen - filllen));
|
|
|
|
|
SDL_memmove(&text[filllen], text, movelen);
|
|
|
|
|
SDL_memset(text, fill, filllen);
|
|
|
|
|
length += width;
|
2015-06-21 17:33:46 +02:00
|
|
|
}
|
2022-12-29 20:42:07 -08:00
|
|
|
return length;
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-05 13:33:11 -07:00
|
|
|
static size_t SDL_PrintPointer(char *text, size_t maxlen, SDL_FormatInfo *info, const void *value)
|
|
|
|
|
{
|
|
|
|
|
char num[130];
|
|
|
|
|
size_t length;
|
|
|
|
|
|
|
|
|
|
if (!value) {
|
|
|
|
|
return SDL_PrintString(text, maxlen, info, NULL);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SDL_ulltoa((unsigned long long)(uintptr_t)value, num, 16);
|
|
|
|
|
length = SDL_PrintString(text, maxlen, info, "0x");
|
|
|
|
|
return length + SDL_PrintString(TEXT_AND_LEN_ARGS, info, num);
|
|
|
|
|
}
|
|
|
|
|
|
Use C++ style comments consistently in SDL source code
Implemented using this script:
find . -type f -exec sed -i'' -e 's,/\* *\([^*]*\)\*/ *$,// \1,' -e 's, \+$,,' {} \;
git checkout \
core/linux/SDL_evdev_kbd_default_keymap.h \
events/imKStoUCS.* \
hidapi \
joystick/controller_type.c \
joystick/controller_type.h \
joystick/hidapi/steam/controller_constants.h \
joystick/hidapi/steam/controller_structs.h \
joystick/SDL_gamepad_db.h \
libm \
render/*/*Shader*.h \
render/vitagxm/SDL_render_vita_gxm_shaders.h \
render/metal/SDL_shaders_metal_*.h \
stdlib/SDL_malloc.c \
stdlib/SDL_qsort.c \
stdlib/SDL_strtokr.c \
test/ \
video/directx/SDL_d3d12_xbox_cmacros.h \
video/directx/d3d12.h \
video/directx/d3d12sdklayers.h \
video/khronos \
video/x11/edid-parse.c \
video/x11/xsettings-client.* \
video/yuv2rgb
sed -i'' -e 's,/\* *\([^*]*\)\*/ *$,// \1,' -e 's, \+$,,' hidapi/SDL_hidapi.c
2024-08-22 10:30:45 -07:00
|
|
|
// NOLINTNEXTLINE(readability-non-const-parameter)
|
2023-12-04 21:21:51 -08:00
|
|
|
int SDL_vsnprintf(SDL_OUT_Z_CAP(maxlen) char *text, size_t maxlen, SDL_PRINTF_FORMAT_STRING const char *fmt, va_list ap)
|
2015-06-21 17:33:46 +02:00
|
|
|
{
|
2021-09-22 11:42:10 -07:00
|
|
|
size_t length = 0;
|
2015-06-21 17:33:46 +02:00
|
|
|
|
2021-09-22 11:42:10 -07:00
|
|
|
if (!text) {
|
|
|
|
|
maxlen = 0;
|
|
|
|
|
}
|
2015-06-21 17:33:46 +02:00
|
|
|
if (!fmt) {
|
|
|
|
|
fmt = "";
|
|
|
|
|
}
|
2021-09-22 11:42:10 -07:00
|
|
|
while (*fmt) {
|
2015-06-21 17:33:46 +02:00
|
|
|
if (*fmt == '%') {
|
2024-08-22 09:21:26 -07:00
|
|
|
bool done = false;
|
|
|
|
|
bool check_flag;
|
2015-06-21 17:33:46 +02:00
|
|
|
SDL_FormatInfo info;
|
|
|
|
|
enum
|
|
|
|
|
{
|
|
|
|
|
DO_INT,
|
|
|
|
|
DO_LONG,
|
2022-09-19 15:42:11 -07:00
|
|
|
DO_LONGLONG,
|
|
|
|
|
DO_SIZE_T
|
2015-06-21 17:33:46 +02:00
|
|
|
} inttype = DO_INT;
|
|
|
|
|
|
|
|
|
|
SDL_zero(info);
|
|
|
|
|
info.radix = 10;
|
|
|
|
|
info.precision = -1;
|
|
|
|
|
|
2024-08-22 09:21:26 -07:00
|
|
|
check_flag = true;
|
2015-06-21 17:33:46 +02:00
|
|
|
while (check_flag) {
|
|
|
|
|
++fmt;
|
|
|
|
|
switch (*fmt) {
|
|
|
|
|
case '-':
|
2024-08-22 09:21:26 -07:00
|
|
|
info.left_justify = true;
|
2015-06-21 17:33:46 +02:00
|
|
|
break;
|
|
|
|
|
case '+':
|
2024-08-22 09:21:26 -07:00
|
|
|
info.force_sign = true;
|
2015-06-21 17:33:46 +02:00
|
|
|
break;
|
|
|
|
|
case '#':
|
2024-08-22 09:21:26 -07:00
|
|
|
info.force_type = true;
|
2015-06-21 17:33:46 +02:00
|
|
|
break;
|
|
|
|
|
case '0':
|
2024-08-22 09:21:26 -07:00
|
|
|
info.pad_zeroes = true;
|
2015-06-21 17:33:46 +02:00
|
|
|
break;
|
|
|
|
|
default:
|
2024-08-22 09:21:26 -07:00
|
|
|
check_flag = false;
|
2015-06-21 17:33:46 +02:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (*fmt >= '0' && *fmt <= '9') {
|
|
|
|
|
info.width = SDL_strtol(fmt, (char **)&fmt, 0);
|
2022-11-27 17:38:43 +01:00
|
|
|
} else if (*fmt == '*') {
|
2018-09-26 10:38:40 +03:00
|
|
|
++fmt;
|
|
|
|
|
info.width = va_arg(ap, int);
|
|
|
|
|
}
|
2015-06-21 17:33:46 +02:00
|
|
|
|
|
|
|
|
if (*fmt == '.') {
|
|
|
|
|
++fmt;
|
|
|
|
|
if (*fmt >= '0' && *fmt <= '9') {
|
|
|
|
|
info.precision = SDL_strtol(fmt, (char **)&fmt, 0);
|
2018-09-26 10:38:40 +03:00
|
|
|
} else if (*fmt == '*') {
|
|
|
|
|
++fmt;
|
|
|
|
|
info.precision = va_arg(ap, int);
|
2015-06-21 17:33:46 +02:00
|
|
|
} else {
|
|
|
|
|
info.precision = 0;
|
|
|
|
|
}
|
2018-09-26 17:11:40 +03:00
|
|
|
if (info.precision < 0) {
|
|
|
|
|
info.precision = 0;
|
|
|
|
|
}
|
2015-06-21 17:33:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
while (!done) {
|
|
|
|
|
switch (*fmt) {
|
|
|
|
|
case '%':
|
2021-09-22 11:42:10 -07:00
|
|
|
if (length < maxlen) {
|
|
|
|
|
text[length] = '%';
|
2015-06-21 17:33:46 +02:00
|
|
|
}
|
2021-09-22 11:42:10 -07:00
|
|
|
++length;
|
2024-08-22 09:21:26 -07:00
|
|
|
done = true;
|
2015-06-21 17:33:46 +02:00
|
|
|
break;
|
|
|
|
|
case 'c':
|
Use C++ style comments consistently in SDL source code
Implemented using this script:
find . -type f -exec sed -i'' -e 's,/\* *\([^*]*\)\*/ *$,// \1,' -e 's, \+$,,' {} \;
git checkout \
core/linux/SDL_evdev_kbd_default_keymap.h \
events/imKStoUCS.* \
hidapi \
joystick/controller_type.c \
joystick/controller_type.h \
joystick/hidapi/steam/controller_constants.h \
joystick/hidapi/steam/controller_structs.h \
joystick/SDL_gamepad_db.h \
libm \
render/*/*Shader*.h \
render/vitagxm/SDL_render_vita_gxm_shaders.h \
render/metal/SDL_shaders_metal_*.h \
stdlib/SDL_malloc.c \
stdlib/SDL_qsort.c \
stdlib/SDL_strtokr.c \
test/ \
video/directx/SDL_d3d12_xbox_cmacros.h \
video/directx/d3d12.h \
video/directx/d3d12sdklayers.h \
video/khronos \
video/x11/edid-parse.c \
video/x11/xsettings-client.* \
video/yuv2rgb
sed -i'' -e 's,/\* *\([^*]*\)\*/ *$,// \1,' -e 's, \+$,,' hidapi/SDL_hidapi.c
2024-08-22 10:30:45 -07:00
|
|
|
// char is promoted to int when passed through (...)
|
2021-09-22 11:42:10 -07:00
|
|
|
if (length < maxlen) {
|
2022-11-30 12:51:59 -08:00
|
|
|
text[length] = (char)va_arg(ap, int);
|
2015-06-21 17:33:46 +02:00
|
|
|
}
|
2021-09-22 11:42:10 -07:00
|
|
|
++length;
|
2024-08-22 09:21:26 -07:00
|
|
|
done = true;
|
2015-06-21 17:33:46 +02:00
|
|
|
break;
|
|
|
|
|
case 'h':
|
Use C++ style comments consistently in SDL source code
Implemented using this script:
find . -type f -exec sed -i'' -e 's,/\* *\([^*]*\)\*/ *$,// \1,' -e 's, \+$,,' {} \;
git checkout \
core/linux/SDL_evdev_kbd_default_keymap.h \
events/imKStoUCS.* \
hidapi \
joystick/controller_type.c \
joystick/controller_type.h \
joystick/hidapi/steam/controller_constants.h \
joystick/hidapi/steam/controller_structs.h \
joystick/SDL_gamepad_db.h \
libm \
render/*/*Shader*.h \
render/vitagxm/SDL_render_vita_gxm_shaders.h \
render/metal/SDL_shaders_metal_*.h \
stdlib/SDL_malloc.c \
stdlib/SDL_qsort.c \
stdlib/SDL_strtokr.c \
test/ \
video/directx/SDL_d3d12_xbox_cmacros.h \
video/directx/d3d12.h \
video/directx/d3d12sdklayers.h \
video/khronos \
video/x11/edid-parse.c \
video/x11/xsettings-client.* \
video/yuv2rgb
sed -i'' -e 's,/\* *\([^*]*\)\*/ *$,// \1,' -e 's, \+$,,' hidapi/SDL_hidapi.c
2024-08-22 10:30:45 -07:00
|
|
|
// short is promoted to int when passed through (...)
|
2015-06-21 17:33:46 +02:00
|
|
|
break;
|
|
|
|
|
case 'l':
|
|
|
|
|
if (inttype < DO_LONGLONG) {
|
|
|
|
|
++inttype;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case 'I':
|
|
|
|
|
if (SDL_strncmp(fmt, "I64", 3) == 0) {
|
|
|
|
|
fmt += 2;
|
|
|
|
|
inttype = DO_LONGLONG;
|
|
|
|
|
}
|
|
|
|
|
break;
|
2022-09-19 15:42:11 -07:00
|
|
|
case 'z':
|
|
|
|
|
inttype = DO_SIZE_T;
|
|
|
|
|
break;
|
2015-06-21 17:33:46 +02:00
|
|
|
case 'i':
|
|
|
|
|
case 'd':
|
2018-09-27 09:37:36 +03:00
|
|
|
if (info.precision >= 0) {
|
2024-08-22 09:21:26 -07:00
|
|
|
info.pad_zeroes = false;
|
2018-09-27 09:37:36 +03:00
|
|
|
}
|
2015-06-21 17:33:46 +02:00
|
|
|
switch (inttype) {
|
|
|
|
|
case DO_INT:
|
2021-09-22 11:42:10 -07:00
|
|
|
length += SDL_PrintLong(TEXT_AND_LEN_ARGS, &info,
|
2022-11-30 12:51:59 -08:00
|
|
|
(long)va_arg(ap, int));
|
2015-06-21 17:33:46 +02:00
|
|
|
break;
|
|
|
|
|
case DO_LONG:
|
2021-09-22 11:42:10 -07:00
|
|
|
length += SDL_PrintLong(TEXT_AND_LEN_ARGS, &info,
|
2022-11-30 12:51:59 -08:00
|
|
|
va_arg(ap, long));
|
2015-06-21 17:33:46 +02:00
|
|
|
break;
|
|
|
|
|
case DO_LONGLONG:
|
2021-09-22 11:42:10 -07:00
|
|
|
length += SDL_PrintLongLong(TEXT_AND_LEN_ARGS, &info,
|
2024-09-09 14:54:50 -07:00
|
|
|
va_arg(ap, long long));
|
2015-06-21 17:33:46 +02:00
|
|
|
break;
|
2022-09-19 15:42:11 -07:00
|
|
|
case DO_SIZE_T:
|
|
|
|
|
length += SDL_PrintLongLong(TEXT_AND_LEN_ARGS, &info,
|
|
|
|
|
va_arg(ap, size_t));
|
|
|
|
|
break;
|
2015-06-21 17:33:46 +02:00
|
|
|
}
|
2024-08-22 09:21:26 -07:00
|
|
|
done = true;
|
2015-06-21 17:33:46 +02:00
|
|
|
break;
|
|
|
|
|
case 'p':
|
2023-07-05 13:33:11 -07:00
|
|
|
info.force_case = SDL_CASE_LOWER;
|
|
|
|
|
length += SDL_PrintPointer(TEXT_AND_LEN_ARGS, &info, va_arg(ap, void *));
|
2024-08-22 09:21:26 -07:00
|
|
|
done = true;
|
2023-07-05 13:33:11 -07:00
|
|
|
break;
|
2015-06-21 17:33:46 +02:00
|
|
|
case 'x':
|
|
|
|
|
info.force_case = SDL_CASE_LOWER;
|
Add and use `SDL_FALLTHROUGH` for fallthroughs
Case fallthrough warnings can be suppressed using the __fallthrough__
compiler attribute. Unfortunately, not all compilers have this
attribute, or even have __has_attribute to check if they have the
__fallthrough__ attribute. [[fallthrough]] is also available in C++17
and the next C2x, but not everyone uses C++17 or C2x.
So define the SDL_FALLTHROUGH macro to deal with those problems - if we
are using C++17 or C2x, it expands to [[fallthrough]]; else if the
compiler has __has_attribute and has the __fallthrough__ attribute, then
it expands to __attribute__((__fallthrough__)); else it expands to an
empty statement, with a /* fallthrough */ comment (it's a do {} while
(0) statement, because users of this macro need to use a semicolon,
because [[fallthrough]] and __attribute__((__fallthrough__)) require a
semicolon).
Clang before Clang 10 and GCC before GCC 7 have problems with using
__attribute__ as a sole statement and warn about a "declaration not
declaring anything", so fall back to using the /* fallthrough */ comment
if we are using those older compiler versions.
Applications using SDL are also free to use this macro (because it is
defined in begin_code.h).
All existing /* fallthrough */ comments have been replaced with this
macro. Some of them were unnecessary because they were the last case in
a switch; using SDL_FALLTHROUGH in those cases would result in a compile
error on compilers that support __fallthrough__, for having a
__attribute__((__fallthrough__)) statement that didn't immediately
precede a case label.
2021-09-27 14:38:12 -07:00
|
|
|
SDL_FALLTHROUGH;
|
2015-06-21 17:33:46 +02:00
|
|
|
case 'X':
|
|
|
|
|
if (info.force_case == SDL_CASE_NOCHANGE) {
|
|
|
|
|
info.force_case = SDL_CASE_UPPER;
|
|
|
|
|
}
|
|
|
|
|
if (info.radix == 10) {
|
|
|
|
|
info.radix = 16;
|
|
|
|
|
}
|
Add and use `SDL_FALLTHROUGH` for fallthroughs
Case fallthrough warnings can be suppressed using the __fallthrough__
compiler attribute. Unfortunately, not all compilers have this
attribute, or even have __has_attribute to check if they have the
__fallthrough__ attribute. [[fallthrough]] is also available in C++17
and the next C2x, but not everyone uses C++17 or C2x.
So define the SDL_FALLTHROUGH macro to deal with those problems - if we
are using C++17 or C2x, it expands to [[fallthrough]]; else if the
compiler has __has_attribute and has the __fallthrough__ attribute, then
it expands to __attribute__((__fallthrough__)); else it expands to an
empty statement, with a /* fallthrough */ comment (it's a do {} while
(0) statement, because users of this macro need to use a semicolon,
because [[fallthrough]] and __attribute__((__fallthrough__)) require a
semicolon).
Clang before Clang 10 and GCC before GCC 7 have problems with using
__attribute__ as a sole statement and warn about a "declaration not
declaring anything", so fall back to using the /* fallthrough */ comment
if we are using those older compiler versions.
Applications using SDL are also free to use this macro (because it is
defined in begin_code.h).
All existing /* fallthrough */ comments have been replaced with this
macro. Some of them were unnecessary because they were the last case in
a switch; using SDL_FALLTHROUGH in those cases would result in a compile
error on compilers that support __fallthrough__, for having a
__attribute__((__fallthrough__)) statement that didn't immediately
precede a case label.
2021-09-27 14:38:12 -07:00
|
|
|
SDL_FALLTHROUGH;
|
2015-06-21 17:33:46 +02:00
|
|
|
case 'o':
|
|
|
|
|
if (info.radix == 10) {
|
|
|
|
|
info.radix = 8;
|
|
|
|
|
}
|
Add and use `SDL_FALLTHROUGH` for fallthroughs
Case fallthrough warnings can be suppressed using the __fallthrough__
compiler attribute. Unfortunately, not all compilers have this
attribute, or even have __has_attribute to check if they have the
__fallthrough__ attribute. [[fallthrough]] is also available in C++17
and the next C2x, but not everyone uses C++17 or C2x.
So define the SDL_FALLTHROUGH macro to deal with those problems - if we
are using C++17 or C2x, it expands to [[fallthrough]]; else if the
compiler has __has_attribute and has the __fallthrough__ attribute, then
it expands to __attribute__((__fallthrough__)); else it expands to an
empty statement, with a /* fallthrough */ comment (it's a do {} while
(0) statement, because users of this macro need to use a semicolon,
because [[fallthrough]] and __attribute__((__fallthrough__)) require a
semicolon).
Clang before Clang 10 and GCC before GCC 7 have problems with using
__attribute__ as a sole statement and warn about a "declaration not
declaring anything", so fall back to using the /* fallthrough */ comment
if we are using those older compiler versions.
Applications using SDL are also free to use this macro (because it is
defined in begin_code.h).
All existing /* fallthrough */ comments have been replaced with this
macro. Some of them were unnecessary because they were the last case in
a switch; using SDL_FALLTHROUGH in those cases would result in a compile
error on compilers that support __fallthrough__, for having a
__attribute__((__fallthrough__)) statement that didn't immediately
precede a case label.
2021-09-27 14:38:12 -07:00
|
|
|
SDL_FALLTHROUGH;
|
2015-06-21 17:33:46 +02:00
|
|
|
case 'u':
|
2024-08-22 09:21:26 -07:00
|
|
|
info.force_sign = false;
|
2018-09-27 09:37:36 +03:00
|
|
|
if (info.precision >= 0) {
|
2024-08-22 09:21:26 -07:00
|
|
|
info.pad_zeroes = false;
|
2018-09-27 09:37:36 +03:00
|
|
|
}
|
2015-06-21 17:33:46 +02:00
|
|
|
switch (inttype) {
|
|
|
|
|
case DO_INT:
|
2021-09-22 11:42:10 -07:00
|
|
|
length += SDL_PrintUnsignedLong(TEXT_AND_LEN_ARGS, &info,
|
2024-09-09 14:54:50 -07:00
|
|
|
va_arg(ap, unsigned int));
|
2015-06-21 17:33:46 +02:00
|
|
|
break;
|
|
|
|
|
case DO_LONG:
|
2021-09-22 11:42:10 -07:00
|
|
|
length += SDL_PrintUnsignedLong(TEXT_AND_LEN_ARGS, &info,
|
2022-11-30 12:51:59 -08:00
|
|
|
va_arg(ap, unsigned long));
|
2015-06-21 17:33:46 +02:00
|
|
|
break;
|
|
|
|
|
case DO_LONGLONG:
|
2021-09-22 11:42:10 -07:00
|
|
|
length += SDL_PrintUnsignedLongLong(TEXT_AND_LEN_ARGS, &info,
|
2024-09-09 14:54:50 -07:00
|
|
|
va_arg(ap, unsigned long long));
|
2015-06-21 17:33:46 +02:00
|
|
|
break;
|
2022-09-19 15:42:11 -07:00
|
|
|
case DO_SIZE_T:
|
|
|
|
|
length += SDL_PrintUnsignedLongLong(TEXT_AND_LEN_ARGS, &info,
|
|
|
|
|
va_arg(ap, size_t));
|
|
|
|
|
break;
|
2015-06-21 17:33:46 +02:00
|
|
|
}
|
2024-08-22 09:21:26 -07:00
|
|
|
done = true;
|
2015-06-21 17:33:46 +02:00
|
|
|
break;
|
|
|
|
|
case 'f':
|
2024-08-22 09:21:26 -07:00
|
|
|
length += SDL_PrintFloat(TEXT_AND_LEN_ARGS, &info, va_arg(ap, double), false);
|
|
|
|
|
done = true;
|
2015-06-21 17:33:46 +02:00
|
|
|
break;
|
2022-12-29 20:42:07 -08:00
|
|
|
case 'g':
|
2024-08-22 09:21:26 -07:00
|
|
|
length += SDL_PrintFloat(TEXT_AND_LEN_ARGS, &info, va_arg(ap, double), true);
|
|
|
|
|
done = true;
|
2022-12-29 20:42:07 -08:00
|
|
|
break;
|
2017-01-18 11:57:27 -08:00
|
|
|
case 'S':
|
2024-08-22 09:21:26 -07:00
|
|
|
info.pad_zeroes = false;
|
2023-05-26 13:57:49 -07:00
|
|
|
length += SDL_PrintStringW(TEXT_AND_LEN_ARGS, &info, va_arg(ap, wchar_t *));
|
2024-08-22 09:21:26 -07:00
|
|
|
done = true;
|
2023-05-26 13:57:49 -07:00
|
|
|
break;
|
2015-06-21 17:33:46 +02:00
|
|
|
case 's':
|
2024-08-22 09:21:26 -07:00
|
|
|
info.pad_zeroes = false;
|
2023-05-26 13:57:49 -07:00
|
|
|
if (inttype > DO_INT) {
|
|
|
|
|
length += SDL_PrintStringW(TEXT_AND_LEN_ARGS, &info, va_arg(ap, wchar_t *));
|
|
|
|
|
} else {
|
|
|
|
|
length += SDL_PrintString(TEXT_AND_LEN_ARGS, &info, va_arg(ap, char *));
|
|
|
|
|
}
|
2024-08-22 09:21:26 -07:00
|
|
|
done = true;
|
2015-06-21 17:33:46 +02:00
|
|
|
break;
|
|
|
|
|
default:
|
2024-08-22 09:21:26 -07:00
|
|
|
done = true;
|
2015-06-21 17:33:46 +02:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
++fmt;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
2021-09-22 11:42:10 -07:00
|
|
|
if (length < maxlen) {
|
2021-11-10 09:48:49 -08:00
|
|
|
text[length] = *fmt;
|
2021-09-22 11:42:10 -07:00
|
|
|
}
|
2021-11-10 09:48:49 -08:00
|
|
|
++fmt;
|
2021-09-22 11:42:10 -07:00
|
|
|
++length;
|
2015-06-21 17:33:46 +02:00
|
|
|
}
|
|
|
|
|
}
|
2021-09-22 11:42:10 -07:00
|
|
|
if (length < maxlen) {
|
|
|
|
|
text[length] = '\0';
|
|
|
|
|
} else if (maxlen > 0) {
|
|
|
|
|
text[maxlen - 1] = '\0';
|
2015-06-21 17:33:46 +02:00
|
|
|
}
|
2021-09-22 11:42:10 -07:00
|
|
|
return (int)length;
|
2015-06-21 17:33:46 +02:00
|
|
|
}
|
2021-09-22 11:42:10 -07:00
|
|
|
|
|
|
|
|
#undef TEXT_AND_LEN_ARGS
|
Use C++ style comments consistently in SDL source code
Implemented using this script:
find . -type f -exec sed -i'' -e 's,/\* *\([^*]*\)\*/ *$,// \1,' -e 's, \+$,,' {} \;
git checkout \
core/linux/SDL_evdev_kbd_default_keymap.h \
events/imKStoUCS.* \
hidapi \
joystick/controller_type.c \
joystick/controller_type.h \
joystick/hidapi/steam/controller_constants.h \
joystick/hidapi/steam/controller_structs.h \
joystick/SDL_gamepad_db.h \
libm \
render/*/*Shader*.h \
render/vitagxm/SDL_render_vita_gxm_shaders.h \
render/metal/SDL_shaders_metal_*.h \
stdlib/SDL_malloc.c \
stdlib/SDL_qsort.c \
stdlib/SDL_strtokr.c \
test/ \
video/directx/SDL_d3d12_xbox_cmacros.h \
video/directx/d3d12.h \
video/directx/d3d12sdklayers.h \
video/khronos \
video/x11/edid-parse.c \
video/x11/xsettings-client.* \
video/yuv2rgb
sed -i'' -e 's,/\* *\([^*]*\)\*/ *$,// \1,' -e 's, \+$,,' hidapi/SDL_hidapi.c
2024-08-22 10:30:45 -07:00
|
|
|
#endif // HAVE_VSNPRINTF
|
2015-06-21 17:33:46 +02:00
|
|
|
|
2023-12-04 21:21:51 -08:00
|
|
|
int SDL_vswprintf(SDL_OUT_Z_CAP(maxlen) wchar_t *text, size_t maxlen, const wchar_t *fmt, va_list ap)
|
2023-05-24 09:41:22 -07:00
|
|
|
{
|
|
|
|
|
char *text_utf8 = NULL, *fmt_utf8 = NULL;
|
2024-08-22 17:33:49 -07:00
|
|
|
int result;
|
2023-05-24 09:41:22 -07:00
|
|
|
|
|
|
|
|
if (fmt) {
|
|
|
|
|
fmt_utf8 = SDL_iconv_string("UTF-8", "WCHAR_T", (const char *)fmt, (SDL_wcslen(fmt) + 1) * sizeof(wchar_t));
|
|
|
|
|
if (!fmt_utf8) {
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!maxlen) {
|
Use C++ style comments consistently in SDL source code
Implemented using this script:
find . -type f -exec sed -i'' -e 's,/\* *\([^*]*\)\*/ *$,// \1,' -e 's, \+$,,' {} \;
git checkout \
core/linux/SDL_evdev_kbd_default_keymap.h \
events/imKStoUCS.* \
hidapi \
joystick/controller_type.c \
joystick/controller_type.h \
joystick/hidapi/steam/controller_constants.h \
joystick/hidapi/steam/controller_structs.h \
joystick/SDL_gamepad_db.h \
libm \
render/*/*Shader*.h \
render/vitagxm/SDL_render_vita_gxm_shaders.h \
render/metal/SDL_shaders_metal_*.h \
stdlib/SDL_malloc.c \
stdlib/SDL_qsort.c \
stdlib/SDL_strtokr.c \
test/ \
video/directx/SDL_d3d12_xbox_cmacros.h \
video/directx/d3d12.h \
video/directx/d3d12sdklayers.h \
video/khronos \
video/x11/edid-parse.c \
video/x11/xsettings-client.* \
video/yuv2rgb
sed -i'' -e 's,/\* *\([^*]*\)\*/ *$,// \1,' -e 's, \+$,,' hidapi/SDL_hidapi.c
2024-08-22 10:30:45 -07:00
|
|
|
// We still need to generate the text to find the final text length
|
2023-05-24 09:41:22 -07:00
|
|
|
maxlen = 1024;
|
|
|
|
|
}
|
|
|
|
|
text_utf8 = (char *)SDL_malloc(maxlen * 4);
|
|
|
|
|
if (!text_utf8) {
|
|
|
|
|
SDL_free(fmt_utf8);
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-22 17:33:49 -07:00
|
|
|
result = SDL_vsnprintf(text_utf8, maxlen * 4, fmt_utf8, ap);
|
2023-05-24 09:41:22 -07:00
|
|
|
|
2024-08-22 17:33:49 -07:00
|
|
|
if (result >= 0) {
|
2023-05-24 09:41:22 -07:00
|
|
|
wchar_t *text_wchar = (wchar_t *)SDL_iconv_string("WCHAR_T", "UTF-8", text_utf8, SDL_strlen(text_utf8) + 1);
|
|
|
|
|
if (text_wchar) {
|
|
|
|
|
if (text) {
|
|
|
|
|
SDL_wcslcpy(text, text_wchar, maxlen);
|
|
|
|
|
}
|
2024-08-22 17:33:49 -07:00
|
|
|
result = (int)SDL_wcslen(text_wchar);
|
2023-05-24 09:41:22 -07:00
|
|
|
SDL_free(text_wchar);
|
|
|
|
|
} else {
|
2024-08-22 17:33:49 -07:00
|
|
|
result = -1;
|
2023-05-24 09:41:22 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SDL_free(text_utf8);
|
|
|
|
|
SDL_free(fmt_utf8);
|
|
|
|
|
|
2024-08-22 17:33:49 -07:00
|
|
|
return result;
|
2023-05-24 09:41:22 -07:00
|
|
|
}
|
|
|
|
|
|
2022-11-30 12:51:59 -08:00
|
|
|
int SDL_asprintf(char **strp, SDL_PRINTF_FORMAT_STRING const char *fmt, ...)
|
2021-09-14 20:37:35 +01:00
|
|
|
{
|
|
|
|
|
va_list ap;
|
2024-08-22 17:33:49 -07:00
|
|
|
int result;
|
2021-09-14 20:37:35 +01:00
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
2024-08-22 17:33:49 -07:00
|
|
|
result = SDL_vasprintf(strp, fmt, ap);
|
2021-09-14 20:37:35 +01:00
|
|
|
va_end(ap);
|
|
|
|
|
|
2024-08-22 17:33:49 -07:00
|
|
|
return result;
|
2021-09-14 20:37:35 +01:00
|
|
|
}
|
|
|
|
|
|
2023-12-04 21:21:51 -08:00
|
|
|
int SDL_vasprintf(char **strp, SDL_PRINTF_FORMAT_STRING const char *fmt, va_list ap)
|
2021-09-14 20:37:35 +01:00
|
|
|
{
|
2024-08-22 17:33:49 -07:00
|
|
|
int result;
|
Use C++ style comments consistently in SDL source code
Implemented using this script:
find . -type f -exec sed -i'' -e 's,/\* *\([^*]*\)\*/ *$,// \1,' -e 's, \+$,,' {} \;
git checkout \
core/linux/SDL_evdev_kbd_default_keymap.h \
events/imKStoUCS.* \
hidapi \
joystick/controller_type.c \
joystick/controller_type.h \
joystick/hidapi/steam/controller_constants.h \
joystick/hidapi/steam/controller_structs.h \
joystick/SDL_gamepad_db.h \
libm \
render/*/*Shader*.h \
render/vitagxm/SDL_render_vita_gxm_shaders.h \
render/metal/SDL_shaders_metal_*.h \
stdlib/SDL_malloc.c \
stdlib/SDL_qsort.c \
stdlib/SDL_strtokr.c \
test/ \
video/directx/SDL_d3d12_xbox_cmacros.h \
video/directx/d3d12.h \
video/directx/d3d12sdklayers.h \
video/khronos \
video/x11/edid-parse.c \
video/x11/xsettings-client.* \
video/yuv2rgb
sed -i'' -e 's,/\* *\([^*]*\)\*/ *$,// \1,' -e 's, \+$,,' hidapi/SDL_hidapi.c
2024-08-22 10:30:45 -07:00
|
|
|
int size = 100; // Guess we need no more than 100 bytes
|
2021-09-14 20:37:35 +01:00
|
|
|
char *p, *np;
|
|
|
|
|
va_list aq;
|
|
|
|
|
|
|
|
|
|
*strp = NULL;
|
|
|
|
|
|
|
|
|
|
p = (char *)SDL_malloc(size);
|
2023-11-09 22:29:15 +01:00
|
|
|
if (!p) {
|
2021-09-14 20:37:35 +01:00
|
|
|
return -1;
|
2022-11-27 17:38:43 +01:00
|
|
|
}
|
2021-09-14 20:37:35 +01:00
|
|
|
|
|
|
|
|
while (1) {
|
Use C++ style comments consistently in SDL source code
Implemented using this script:
find . -type f -exec sed -i'' -e 's,/\* *\([^*]*\)\*/ *$,// \1,' -e 's, \+$,,' {} \;
git checkout \
core/linux/SDL_evdev_kbd_default_keymap.h \
events/imKStoUCS.* \
hidapi \
joystick/controller_type.c \
joystick/controller_type.h \
joystick/hidapi/steam/controller_constants.h \
joystick/hidapi/steam/controller_structs.h \
joystick/SDL_gamepad_db.h \
libm \
render/*/*Shader*.h \
render/vitagxm/SDL_render_vita_gxm_shaders.h \
render/metal/SDL_shaders_metal_*.h \
stdlib/SDL_malloc.c \
stdlib/SDL_qsort.c \
stdlib/SDL_strtokr.c \
test/ \
video/directx/SDL_d3d12_xbox_cmacros.h \
video/directx/d3d12.h \
video/directx/d3d12sdklayers.h \
video/khronos \
video/x11/edid-parse.c \
video/x11/xsettings-client.* \
video/yuv2rgb
sed -i'' -e 's,/\* *\([^*]*\)\*/ *$,// \1,' -e 's, \+$,,' hidapi/SDL_hidapi.c
2024-08-22 10:30:45 -07:00
|
|
|
// Try to print in the allocated space
|
2021-09-14 20:37:35 +01:00
|
|
|
va_copy(aq, ap);
|
2024-08-22 17:33:49 -07:00
|
|
|
result = SDL_vsnprintf(p, size, fmt, aq);
|
2021-09-14 20:37:35 +01:00
|
|
|
va_end(aq);
|
|
|
|
|
|
Use C++ style comments consistently in SDL source code
Implemented using this script:
find . -type f -exec sed -i'' -e 's,/\* *\([^*]*\)\*/ *$,// \1,' -e 's, \+$,,' {} \;
git checkout \
core/linux/SDL_evdev_kbd_default_keymap.h \
events/imKStoUCS.* \
hidapi \
joystick/controller_type.c \
joystick/controller_type.h \
joystick/hidapi/steam/controller_constants.h \
joystick/hidapi/steam/controller_structs.h \
joystick/SDL_gamepad_db.h \
libm \
render/*/*Shader*.h \
render/vitagxm/SDL_render_vita_gxm_shaders.h \
render/metal/SDL_shaders_metal_*.h \
stdlib/SDL_malloc.c \
stdlib/SDL_qsort.c \
stdlib/SDL_strtokr.c \
test/ \
video/directx/SDL_d3d12_xbox_cmacros.h \
video/directx/d3d12.h \
video/directx/d3d12sdklayers.h \
video/khronos \
video/x11/edid-parse.c \
video/x11/xsettings-client.* \
video/yuv2rgb
sed -i'' -e 's,/\* *\([^*]*\)\*/ *$,// \1,' -e 's, \+$,,' hidapi/SDL_hidapi.c
2024-08-22 10:30:45 -07:00
|
|
|
// Check error code
|
2024-08-22 17:33:49 -07:00
|
|
|
if (result < 0) {
|
2023-11-05 20:01:02 +03:00
|
|
|
SDL_free(p);
|
2024-08-22 17:33:49 -07:00
|
|
|
return result;
|
2022-11-27 17:38:43 +01:00
|
|
|
}
|
2021-09-14 20:37:35 +01:00
|
|
|
|
Use C++ style comments consistently in SDL source code
Implemented using this script:
find . -type f -exec sed -i'' -e 's,/\* *\([^*]*\)\*/ *$,// \1,' -e 's, \+$,,' {} \;
git checkout \
core/linux/SDL_evdev_kbd_default_keymap.h \
events/imKStoUCS.* \
hidapi \
joystick/controller_type.c \
joystick/controller_type.h \
joystick/hidapi/steam/controller_constants.h \
joystick/hidapi/steam/controller_structs.h \
joystick/SDL_gamepad_db.h \
libm \
render/*/*Shader*.h \
render/vitagxm/SDL_render_vita_gxm_shaders.h \
render/metal/SDL_shaders_metal_*.h \
stdlib/SDL_malloc.c \
stdlib/SDL_qsort.c \
stdlib/SDL_strtokr.c \
test/ \
video/directx/SDL_d3d12_xbox_cmacros.h \
video/directx/d3d12.h \
video/directx/d3d12sdklayers.h \
video/khronos \
video/x11/edid-parse.c \
video/x11/xsettings-client.* \
video/yuv2rgb
sed -i'' -e 's,/\* *\([^*]*\)\*/ *$,// \1,' -e 's, \+$,,' hidapi/SDL_hidapi.c
2024-08-22 10:30:45 -07:00
|
|
|
// If that worked, return the string
|
2024-08-22 17:33:49 -07:00
|
|
|
if (result < size) {
|
2021-09-14 20:37:35 +01:00
|
|
|
*strp = p;
|
2024-08-22 17:33:49 -07:00
|
|
|
return result;
|
2021-09-14 20:37:35 +01:00
|
|
|
}
|
|
|
|
|
|
Use C++ style comments consistently in SDL source code
Implemented using this script:
find . -type f -exec sed -i'' -e 's,/\* *\([^*]*\)\*/ *$,// \1,' -e 's, \+$,,' {} \;
git checkout \
core/linux/SDL_evdev_kbd_default_keymap.h \
events/imKStoUCS.* \
hidapi \
joystick/controller_type.c \
joystick/controller_type.h \
joystick/hidapi/steam/controller_constants.h \
joystick/hidapi/steam/controller_structs.h \
joystick/SDL_gamepad_db.h \
libm \
render/*/*Shader*.h \
render/vitagxm/SDL_render_vita_gxm_shaders.h \
render/metal/SDL_shaders_metal_*.h \
stdlib/SDL_malloc.c \
stdlib/SDL_qsort.c \
stdlib/SDL_strtokr.c \
test/ \
video/directx/SDL_d3d12_xbox_cmacros.h \
video/directx/d3d12.h \
video/directx/d3d12sdklayers.h \
video/khronos \
video/x11/edid-parse.c \
video/x11/xsettings-client.* \
video/yuv2rgb
sed -i'' -e 's,/\* *\([^*]*\)\*/ *$,// \1,' -e 's, \+$,,' hidapi/SDL_hidapi.c
2024-08-22 10:30:45 -07:00
|
|
|
// Else try again with more space
|
2024-08-22 17:33:49 -07:00
|
|
|
size = result + 1; // Precisely what is needed
|
2021-09-14 20:37:35 +01:00
|
|
|
|
|
|
|
|
np = (char *)SDL_realloc(p, size);
|
2023-11-09 22:29:15 +01:00
|
|
|
if (!np) {
|
2021-09-14 20:37:35 +01:00
|
|
|
SDL_free(p);
|
|
|
|
|
return -1;
|
|
|
|
|
} else {
|
|
|
|
|
p = np;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-09-02 14:13:54 +02:00
|
|
|
|
|
|
|
|
char * SDL_strpbrk(const char *str, const char *breakset)
|
|
|
|
|
{
|
|
|
|
|
#ifdef HAVE_STRPBRK
|
|
|
|
|
return strpbrk(str, breakset);
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
|
|
for (; *str; str++) {
|
|
|
|
|
const char *b;
|
|
|
|
|
|
|
|
|
|
for (b = breakset; *b; b++) {
|
|
|
|
|
if (*str == *b) {
|
|
|
|
|
return (char *) str;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return NULL;
|
|
|
|
|
#endif
|
|
|
|
|
}
|