xref: /freebsd/contrib/expat/lib/xcs.c (revision 22c3edb31297a79ec3c7bf8f906be2df11b472d2)
1*22c3edb3SPhilip Paeps /*
2*22c3edb3SPhilip Paeps                             __  __            _
3*22c3edb3SPhilip Paeps                          ___\ \/ /_ __   __ _| |_
4*22c3edb3SPhilip Paeps                         / _ \\  /| '_ \ / _` | __|
5*22c3edb3SPhilip Paeps                        |  __//  \| |_) | (_| | |_
6*22c3edb3SPhilip Paeps                         \___/_/\_\ .__/ \__,_|\__|
7*22c3edb3SPhilip Paeps                                  |_| XML parser
8*22c3edb3SPhilip Paeps 
9*22c3edb3SPhilip Paeps    Copyright (c) 2022-2026 Sebastian Pipping <sebastian@pipping.org>
10*22c3edb3SPhilip Paeps    Licensed under the MIT license:
11*22c3edb3SPhilip Paeps 
12*22c3edb3SPhilip Paeps    Permission is  hereby granted,  free of charge,  to any  person obtaining
13*22c3edb3SPhilip Paeps    a  copy  of  this  software   and  associated  documentation  files  (the
14*22c3edb3SPhilip Paeps    "Software"),  to  deal in  the  Software  without restriction,  including
15*22c3edb3SPhilip Paeps    without  limitation the  rights  to use,  copy,  modify, merge,  publish,
16*22c3edb3SPhilip Paeps    distribute, sublicense, and/or sell copies of the Software, and to permit
17*22c3edb3SPhilip Paeps    persons  to whom  the Software  is  furnished to  do so,  subject to  the
18*22c3edb3SPhilip Paeps    following conditions:
19*22c3edb3SPhilip Paeps 
20*22c3edb3SPhilip Paeps    The above copyright  notice and this permission notice  shall be included
21*22c3edb3SPhilip Paeps    in all copies or substantial portions of the Software.
22*22c3edb3SPhilip Paeps 
23*22c3edb3SPhilip Paeps    THE  SOFTWARE  IS  PROVIDED  "AS  IS",  WITHOUT  WARRANTY  OF  ANY  KIND,
24*22c3edb3SPhilip Paeps    EXPRESS  OR IMPLIED,  INCLUDING  BUT  NOT LIMITED  TO  THE WARRANTIES  OF
25*22c3edb3SPhilip Paeps    MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
26*22c3edb3SPhilip Paeps    NO EVENT SHALL THE AUTHORS OR  COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
27*22c3edb3SPhilip Paeps    DAMAGES OR  OTHER LIABILITY, WHETHER  IN AN  ACTION OF CONTRACT,  TORT OR
28*22c3edb3SPhilip Paeps    OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
29*22c3edb3SPhilip Paeps    USE OR OTHER DEALINGS IN THE SOFTWARE.
30*22c3edb3SPhilip Paeps 
31*22c3edb3SPhilip Paeps    SPDX-License-Identifier: MIT
32*22c3edb3SPhilip Paeps */
33*22c3edb3SPhilip Paeps 
34*22c3edb3SPhilip Paeps #include "xcs.h"
35*22c3edb3SPhilip Paeps 
36*22c3edb3SPhilip Paeps #if defined(XML_UNICODE)
37*22c3edb3SPhilip Paeps #  if defined(XML_UNICODE_WCHAR_T)
38*22c3edb3SPhilip Paeps #    include <wchar.h> // for wcscmp, wcslen, wcsncmp
39*22c3edb3SPhilip Paeps #  endif
40*22c3edb3SPhilip Paeps #else
41*22c3edb3SPhilip Paeps #  include <string.h> // for strcmp, strlen, strncmp
42*22c3edb3SPhilip Paeps #endif
43*22c3edb3SPhilip Paeps 
44*22c3edb3SPhilip Paeps size_t
45*22c3edb3SPhilip Paeps xcslen(const XML_Char *s) {
46*22c3edb3SPhilip Paeps #ifdef XML_UNICODE
47*22c3edb3SPhilip Paeps #  ifdef XML_UNICODE_WCHAR_T
48*22c3edb3SPhilip Paeps   return wcslen(s);
49*22c3edb3SPhilip Paeps #  else
50*22c3edb3SPhilip Paeps   // XML_Char is unsigned short
51*22c3edb3SPhilip Paeps   size_t len = 0;
52*22c3edb3SPhilip Paeps   while (s[len]) {
53*22c3edb3SPhilip Paeps     len++;
54*22c3edb3SPhilip Paeps   }
55*22c3edb3SPhilip Paeps   return len;
56*22c3edb3SPhilip Paeps #  endif
57*22c3edb3SPhilip Paeps #else
58*22c3edb3SPhilip Paeps   return strlen(s);
59*22c3edb3SPhilip Paeps #endif
60*22c3edb3SPhilip Paeps }
61*22c3edb3SPhilip Paeps 
62*22c3edb3SPhilip Paeps int
63*22c3edb3SPhilip Paeps xcscmp(const XML_Char *a, const XML_Char *b) {
64*22c3edb3SPhilip Paeps #if defined(XML_UNICODE)
65*22c3edb3SPhilip Paeps #  if defined(XML_UNICODE_WCHAR_T)
66*22c3edb3SPhilip Paeps   return wcscmp(a, b);
67*22c3edb3SPhilip Paeps #  else
68*22c3edb3SPhilip Paeps   for (; a[0] && b[0] && a[0] == b[0]; a++, b++)
69*22c3edb3SPhilip Paeps     ;
70*22c3edb3SPhilip Paeps   return a[0] - b[0];
71*22c3edb3SPhilip Paeps #  endif
72*22c3edb3SPhilip Paeps #else
73*22c3edb3SPhilip Paeps   return strcmp(a, b);
74*22c3edb3SPhilip Paeps #endif
75*22c3edb3SPhilip Paeps }
76*22c3edb3SPhilip Paeps 
77*22c3edb3SPhilip Paeps int
78*22c3edb3SPhilip Paeps xcsncmp(const XML_Char *a, const XML_Char *b, size_t len) {
79*22c3edb3SPhilip Paeps #if defined(XML_UNICODE)
80*22c3edb3SPhilip Paeps #  if defined(XML_UNICODE_WCHAR_T)
81*22c3edb3SPhilip Paeps   return wcsncmp(a, b, len);
82*22c3edb3SPhilip Paeps #  else
83*22c3edb3SPhilip Paeps   for (; len > 0 && a[0] && b[0] && a[0] == b[0]; len--, a++, b++) {
84*22c3edb3SPhilip Paeps   }
85*22c3edb3SPhilip Paeps   return (len == 0) ? 0 : (a[0] - b[0]);
86*22c3edb3SPhilip Paeps #  endif
87*22c3edb3SPhilip Paeps #else
88*22c3edb3SPhilip Paeps   return strncmp(a, b, len);
89*22c3edb3SPhilip Paeps #endif
90*22c3edb3SPhilip Paeps }
91