| 1 |
|
| 2 |
/* |
| 3 |
* Copyright (c) 2018 Christian Cornelssen |
| 4 |
* |
| 5 |
* Permission to use, copy, modify, and distribute this software for any |
| 6 |
* purpose with or without fee is hereby granted, provided that the above |
| 7 |
* copyright notice and this permission notice appear in all copies. |
| 8 |
* |
| 9 |
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
| 10 |
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 11 |
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
| 12 |
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 13 |
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 14 |
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
| 15 |
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 16 |
*/ |
| 17 |
|
| 18 |
// MP support header |
| 19 |
#include "MacportsLegacySupport.h" |
| 20 |
#if __MP_LEGACY_SUPPORT_WCSCASECMP__ |
| 21 |
|
| 22 |
#include <wchar.h> |
| 23 |
#include <wctype.h> |
| 24 |
#include <xlocale.h> |
| 25 |
|
| 26 |
#include <limits.h> |
| 27 |
#include <stdint.h> |
| 28 |
#if WCHAR_MAX > INT_MAX |
| 29 |
/* Caution: multiple argument evaluations */ |
| 30 |
#define _MP_WCDIFFSIGN(a, b) (((b) < (a)) - ((a) < (b))) |
| 31 |
#else |
| 32 |
#define _MP_WCDIFFSIGN(a, b) ((int)(a) - (int)(b)) |
| 33 |
#endif |
| 34 |
|
| 35 |
int wcsncasecmp_l(const wchar_t *l, const wchar_t *r, size_t n, locale_t locale) |
| 36 |
{ |
| 37 |
wint_t lc, rc; |
| 38 |
int d = 0; |
| 39 |
while (!d && n) { |
| 40 |
lc = *l++; rc = *r++; --n; |
| 41 |
if (!(lc && rc)) n = 0; |
| 42 |
if (lc == rc) continue; |
| 43 |
lc = towlower_l(lc, locale); |
| 44 |
rc = towlower_l(rc, locale); |
| 45 |
d = _MP_WCDIFFSIGN(lc, rc); |
| 46 |
} |
| 47 |
return d; |
| 48 |
} |
| 49 |
|
| 50 |
int wcsncasecmp(const wchar_t *l, const wchar_t *r, size_t n) |
| 51 |
{ |
| 52 |
return wcsncasecmp_l(l, r, n, 0); |
| 53 |
} |
| 54 |
|
| 55 |
int wcscasecmp_l(const wchar_t *l, const wchar_t *r, locale_t locale) |
| 56 |
{ |
| 57 |
return wcsncasecmp_l(l, r, -1, locale); |
| 58 |
} |
| 59 |
|
| 60 |
int wcscasecmp(const wchar_t *l, const wchar_t *r) |
| 61 |
{ |
| 62 |
return wcsncasecmp_l(l, r, -1, 0); |
| 63 |
} |
| 64 |
|
| 65 |
#endif /* __MP_LEGACY_SUPPORT_WCSCASECMP__ */ |