1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
11 int NS_FASTCALL
Compare(const mozilla::detail::nsTStringRepr
<T
>& aLhs
,
12 const mozilla::detail::nsTStringRepr
<T
>& aRhs
,
13 const nsTStringComparator
<T
> comp
) {
14 typedef typename nsTSubstring
<T
>::size_type size_type
;
15 typedef typename nsTSubstring
<T
>::const_iterator const_iterator
;
21 const_iterator leftIter
, rightIter
;
22 aLhs
.BeginReading(leftIter
);
23 aRhs
.BeginReading(rightIter
);
25 size_type lLength
= aLhs
.Length();
26 size_type rLength
= aRhs
.Length();
27 size_type lengthToCompare
= XPCOM_MIN(lLength
, rLength
);
30 if ((result
= comp(leftIter
.get(), rightIter
.get(), lengthToCompare
,
31 lengthToCompare
)) == 0) {
32 if (lLength
< rLength
) {
34 } else if (rLength
< lLength
) {
44 template int NS_FASTCALL Compare
<char>(
45 mozilla::detail::nsTStringRepr
<char> const&,
46 mozilla::detail::nsTStringRepr
<char> const&, nsTStringComparator
<char>);
48 template int NS_FASTCALL
49 Compare
<char16_t
>(mozilla::detail::nsTStringRepr
<char16_t
> const&,
50 mozilla::detail::nsTStringRepr
<char16_t
> const&,
51 nsTStringComparator
<char16_t
>);
54 int nsTDefaultStringComparator(const T
* aLhs
, const T
* aRhs
, size_t aLLength
,
56 return aLLength
== aRLength
? nsCharTraits
<T
>::compare(aLhs
, aRhs
, aLLength
)
57 : (aLLength
> aRLength
) ? 1
61 template int nsTDefaultStringComparator(const char*, const char*, size_t,
63 template int nsTDefaultStringComparator(const char16_t
*, const char16_t
*,
66 int nsCaseInsensitiveCStringComparator(const char* aLhs
, const char* aRhs
,
67 size_t aLhsLength
, size_t aRhsLength
) {
68 #if defined(LIBFUZZER) && defined(LINUX)
69 // Make sure libFuzzer can see this string compare by calling the POSIX
70 // native function which is intercepted. We also call this if the lengths
71 // don't match so libFuzzer can at least see a partial string, but we throw
72 // away the result afterwards again.
74 int32_t(strncasecmp(aLhs
, aRhs
, std::min(aLhsLength
, aRhsLength
)));
76 if (aLhsLength
!= aRhsLength
) {
77 return (aLhsLength
> aRhsLength
) ? 1 : -1;
80 if (aLhsLength
!= aRhsLength
) {
81 return (aLhsLength
> aRhsLength
) ? 1 : -1;
83 int32_t result
= int32_t(PL_strncasecmp(aLhs
, aRhs
, aLhsLength
));
85 // Egads. PL_strncasecmp is returning *very* negative numbers.
86 // Some folks expect -1,0,1, so let's temper its enthusiasm.