nsTStringComparator.cpp 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. int NS_FASTCALL
  6. Compare(const nsTSubstring_CharT::base_string_type& aLhs,
  7. const nsTSubstring_CharT::base_string_type& aRhs,
  8. const nsTStringComparator_CharT& comp)
  9. {
  10. typedef nsTSubstring_CharT::size_type size_type;
  11. if (&aLhs == &aRhs) {
  12. return 0;
  13. }
  14. nsTSubstring_CharT::const_iterator leftIter, rightIter;
  15. aLhs.BeginReading(leftIter);
  16. aRhs.BeginReading(rightIter);
  17. size_type lLength = aLhs.Length();
  18. size_type rLength = aRhs.Length();
  19. size_type lengthToCompare = XPCOM_MIN(lLength, rLength);
  20. int result;
  21. if ((result = comp(leftIter.get(), rightIter.get(),
  22. lengthToCompare, lengthToCompare)) == 0) {
  23. if (lLength < rLength) {
  24. result = -1;
  25. } else if (rLength < lLength) {
  26. result = 1;
  27. } else {
  28. result = 0;
  29. }
  30. }
  31. return result;
  32. }
  33. int
  34. nsTDefaultStringComparator_CharT::operator()(const char_type* aLhs,
  35. const char_type* aRhs,
  36. uint32_t aLLength,
  37. uint32_t aRLength) const
  38. {
  39. return
  40. aLLength == aRLength ? nsCharTraits<CharT>::compare(aLhs, aRhs, aLLength) :
  41. (aLLength > aRLength) ? 1 : -1;
  42. }