summaryrefslogtreecommitdiffstats
path: root/dom/xslt/xslt/txXPathResultComparator.h
blob: 273bbf4f1abbd6f818488e961fa64883afaf51f2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#ifndef TRANSFRMX_XPATHRESULTCOMPARATOR_H
#define TRANSFRMX_XPATHRESULTCOMPARATOR_H

#include "mozilla/Attributes.h"
#include "mozilla/Maybe.h"
#include "mozilla/intl/Collator.h"
#include "mozilla/UniquePtr.h"
#include "txCore.h"
#include "nsCOMPtr.h"
#include "nsString.h"

class Expr;
class txIEvalContext;

/*
 * Result comparators
 */
class txXPathResultComparator {
 public:
  virtual ~txXPathResultComparator() = default;

  /*
   * Compares two XPath results. Returns -1 if val1 < val2,
   * 1 if val1 > val2 and 0 if val1 == val2.
   */
  virtual int compareValues(txObject* val1, txObject* val2) = 0;

  /*
   * Create a sortable value.
   */
  virtual nsresult createSortableValue(Expr* aExpr, txIEvalContext* aContext,
                                       txObject*& aResult) = 0;
};

/*
 * Compare results as stings (data-type="text")
 */
class txResultStringComparator : public txXPathResultComparator {
 public:
  txResultStringComparator(bool aAscending, bool aUpperFirst,
                           const nsString& aLanguage);

  int compareValues(txObject* aVal1, txObject* aVal2) override;
  nsresult createSortableValue(Expr* aExpr, txIEvalContext* aContext,
                               txObject*& aResult) override;

 private:
  mozilla::UniquePtr<const mozilla::intl::Collator> mCollator;
  nsresult init(const nsString& aLanguage);
  int mSorting;

  class StringValue : public txObject {
   public:
    StringValue();
    ~StringValue();

    nsresult initCaseKey(const mozilla::intl::Collator& aCollator);

    nsTArray<uint8_t> mKey;
    // Either mCaseKeyString is non-null, or we have a usable key in mCaseKey
    // already.
    mozilla::UniquePtr<nsString> mCaseKeyString;
    nsTArray<uint8_t> mCaseKey;
  };
};

/*
 * Compare results as numbers (data-type="number")
 */
class txResultNumberComparator : public txXPathResultComparator {
 public:
  explicit txResultNumberComparator(bool aAscending);

  int compareValues(txObject* aVal1, txObject* aVal2) override;
  nsresult createSortableValue(Expr* aExpr, txIEvalContext* aContext,
                               txObject*& aResult) override;

 private:
  int mAscending;

  class NumberValue : public txObject {
   public:
    double mVal;
  };
};

#endif