mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-01-31 19:43:32 +00:00
ba42248575
The C standard states: > Unless explicitly stated otherwise, the functions described in this > subclause order two wide characters the same way as two integers of > the underlying integer type designated by wchar_t. > > [...] > > The wcscmp function returns an integer greater than, equal to, or > less than zero, accordingly as the wide string pointed to by s1 is > greater than, equal to, or less than the wide string pointed to by > s2. > > [...] > > The wcsncmp function returns an integer greater than, equal to, or > less than zero, accordingly as the possibly null-terminated array > pointed to by s1 is greater than, equal to, or less than the > possibly null-terminated array pointed to by s2. - C Standard, 7.31.4.4. Wide string comparison functions Cosmopolitan fails to obey this in cases where the difference between two wide characters is larger than WCHAR_MAX. This means that, for example, the following program: #include <stdio.h> #include <wchar.h> #include <limits.h> int main() { wchar_t str1[] = { WCHAR_MIN, L'\0' }; wchar_t str2[] = { WCHAR_MAX, L'\0' }; printf("%d\n", wcscmp(str1, str2)); printf("%d\n", wcsncmp(str1, str2, 2)); } will print `1` twice, instead of the negative numbers mandated by the standard (as WCHAR_MIN is less than WCHAR_MAX) This patch fixes this, along with the associated Github issue, https://github.com/jart/cosmopolitan/issues/783
34 lines
2.3 KiB
C
34 lines
2.3 KiB
C
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
|
|
│vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│
|
|
╞══════════════════════════════════════════════════════════════════════════════╡
|
|
│ Copyright 2020 Justine Alexandra Roberts Tunney │
|
|
│ │
|
|
│ Permission to use, copy, modify, and/or distribute this software for │
|
|
│ any purpose with or without fee is hereby granted, provided that the │
|
|
│ above copyright notice and this permission notice appear in all copies. │
|
|
│ │
|
|
│ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │
|
|
│ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │
|
|
│ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │
|
|
│ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │
|
|
│ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │
|
|
│ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │
|
|
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
|
|
│ PERFORMANCE OF THIS SOFTWARE. │
|
|
╚─────────────────────────────────────────────────────────────────────────────*/
|
|
#include "libc/str/str.h"
|
|
|
|
/**
|
|
* Compares NUL-terminated wide strings w/ limit.
|
|
*
|
|
* @param a is first non-null NUL-terminated string pointer
|
|
* @param b is second non-null NUL-terminated string pointer
|
|
* @return is <0, 0, or >0 based on uint8_t comparison
|
|
* @asyncsignalsafe
|
|
*/
|
|
int wcsncmp(const wchar_t *a, const wchar_t *b, size_t n) {
|
|
size_t i = 0;
|
|
if (!n-- || a == b) return 0;
|
|
while (i < n && a[i] == b[i] && b[i]) ++i;
|
|
return (a[i] > b[i]) - (a[i] < b[i]);
|
|
}
|