no bug - Bumping Firefox l10n changesets r=release a=l10n-bump DONTBUILD CLOSED TREE
[gecko.git] / xpcom / string / nsTStringComparator.cpp
blob801a3623b940340a454b7ddf305cbf37afe521b7
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/. */
7 #include "nsString.h"
8 #include "plstr.h"
10 template <typename T>
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;
17 if (&aLhs == &aRhs) {
18 return 0;
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);
29 int result;
30 if ((result = comp(leftIter.get(), rightIter.get(), lengthToCompare,
31 lengthToCompare)) == 0) {
32 if (lLength < rLength) {
33 result = -1;
34 } else if (rLength < lLength) {
35 result = 1;
36 } else {
37 result = 0;
41 return result;
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>);
53 template <typename T>
54 int nsTDefaultStringComparator(const T* aLhs, const T* aRhs, size_t aLLength,
55 size_t aRLength) {
56 return aLLength == aRLength ? nsCharTraits<T>::compare(aLhs, aRhs, aLLength)
57 : (aLLength > aRLength) ? 1
58 : -1;
61 template int nsTDefaultStringComparator(const char*, const char*, size_t,
62 size_t);
63 template int nsTDefaultStringComparator(const char16_t*, const char16_t*,
64 size_t, size_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.
73 int32_t result =
74 int32_t(strncasecmp(aLhs, aRhs, std::min(aLhsLength, aRhsLength)));
76 if (aLhsLength != aRhsLength) {
77 return (aLhsLength > aRhsLength) ? 1 : -1;
79 #else
80 if (aLhsLength != aRhsLength) {
81 return (aLhsLength > aRhsLength) ? 1 : -1;
83 int32_t result = int32_t(PL_strncasecmp(aLhs, aRhs, aLhsLength));
84 #endif
85 // Egads. PL_strncasecmp is returning *very* negative numbers.
86 // Some folks expect -1,0,1, so let's temper its enthusiasm.
87 if (result < 0) {
88 result = -1;
90 return result;