cpp

Coverage Report

Created: 2023-09-13 01:07

/home/andy/git/oilshell/oil/mycpp/comparators.h
Line
Count
Source
1
#ifndef MYCPP_COMPARATORS_H
2
#define MYCPP_COMPARATORS_H
3
4
#include <string.h>  // memcmp
5
6
#include <algorithm>  // std::min()
7
8
#include "mycpp/gc_str.h"  // len()
9
10
template <typename L, typename R>
11
class Tuple2;
12
13
bool str_equals(Str* left, Str* right);
14
bool maybe_str_equals(Str* left, Str* right);
15
16
bool are_equal(Str* left, Str* right);
17
18
bool are_equal(Str* left, Str* right);
19
bool are_equal(int left, int right);
20
bool are_equal(Tuple2<Str*, int>* t1, Tuple2<Str*, int>* t2);
21
bool are_equal(Tuple2<int, int>* t1, Tuple2<int, int>* t2);
22
23
bool keys_equal(int left, int right);
24
bool keys_equal(Str* left, Str* right);
25
bool keys_equal(Tuple2<int, int>* t1, Tuple2<int, int>* t2);
26
bool keys_equal(Tuple2<Str*, int>* t1, Tuple2<Str*, int>* t2);
27
28
namespace id_kind_asdl {
29
enum class Kind;
30
};
31
32
bool are_equal(id_kind_asdl::Kind left, id_kind_asdl::Kind right);
33
34
36
inline int int_cmp(int a, int b) {
35
36
  if (a == b) {
36
8
    return 0;
37
8
  }
38
28
  return a < b ? -1 : 1;
39
36
}
40
41
// mylib::str_cmp is in this common header to avoid gc_list.h -> gc_mylib.h
42
// dependency
43
//
44
// It's also used for _cmp(Str*) in gc_list.
45
namespace mylib {
46
47
// Used by [[ a > b ]] and so forth
48
58
inline int str_cmp(Str* a, Str* b) {
49
58
  int len_a = len(a);
50
58
  int len_b = len(b);
51
52
58
  int min = std::min(len_a, len_b);
53
58
  if (min == 0) {
54
16
    return int_cmp(len_a, len_b);
55
16
  }
56
42
  int comp = memcmp(a->data_, b->data_, min);
57
42
  if (comp == 0) {
58
8
    return int_cmp(len_a, len_b);  // tiebreaker
59
8
  }
60
34
  return comp;
61
42
}
62
63
}  // namespace mylib
64
65
#endif  // MYCPP_COMPARATORS_H