Coverage Report

Created: 2018-09-25 14:53

/work/obj-fuzz/dist/include/nsCSSPropertyIDSet.h
Line
Count
Source (jump to first uncovered line)
1
/* This Source Code Form is subject to the terms of the Mozilla Public
2
 * License, v. 2.0. If a copy of the MPL was not distributed with this
3
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4
5
/* bit vectors for sets of CSS properties */
6
7
#ifndef nsCSSPropertyIDSet_h__
8
#define nsCSSPropertyIDSet_h__
9
10
#include "mozilla/ArrayUtils.h"
11
12
#include "nsCSSPropertyID.h"
13
#include <limits.h> // for CHAR_BIT
14
15
/**
16
 * nsCSSPropertyIDSet maintains a set of non-shorthand CSS properties.  In
17
 * other words, for each longhand CSS property we support, it has a bit
18
 * for whether that property is in the set.
19
 */
20
class nsCSSPropertyIDSet {
21
public:
22
    nsCSSPropertyIDSet() { Empty(); }
23
    // auto-generated copy-constructor OK
24
25
    void AssertInSetRange(nsCSSPropertyID aProperty) const {
26
        NS_ASSERTION(0 <= aProperty &&
27
                     aProperty < eCSSProperty_COUNT_no_shorthands,
28
                     "out of bounds");
29
    }
30
31
    // Conversion of aProperty to |size_t| after AssertInSetRange
32
    // lets the compiler generate significantly tighter code.
33
34
    void AddProperty(nsCSSPropertyID aProperty) {
35
        AssertInSetRange(aProperty);
36
        size_t p = aProperty;
37
        mProperties[p / kBitsInChunk] |=
38
          property_set_type(1) << (p % kBitsInChunk);
39
    }
40
41
0
    void RemoveProperty(nsCSSPropertyID aProperty) {
42
0
        AssertInSetRange(aProperty);
43
0
        size_t p = aProperty;
44
0
        mProperties[p / kBitsInChunk] &=
45
0
            ~(property_set_type(1) << (p % kBitsInChunk));
46
0
    }
47
48
    bool HasProperty(nsCSSPropertyID aProperty) const {
49
        AssertInSetRange(aProperty);
50
        size_t p = aProperty;
51
        return (mProperties[p / kBitsInChunk] &
52
                (property_set_type(1) << (p % kBitsInChunk))) != 0;
53
    }
54
55
    void Empty() {
56
        memset(mProperties, 0, sizeof(mProperties));
57
    }
58
59
0
    void AssertIsEmpty(const char* aText) const {
60
0
        for (size_t i = 0; i < mozilla::ArrayLength(mProperties); ++i) {
61
0
            NS_ASSERTION(mProperties[i] == 0, aText);
62
0
        }
63
0
    }
64
65
    bool Equals(const nsCSSPropertyIDSet& aOther) const {
66
      return mozilla::ArrayEqual(mProperties, aOther.mProperties);
67
    }
68
69
    bool IsEmpty() const {
70
      for (size_t i = 0; i < mozilla::ArrayLength(mProperties); ++i) {
71
          if (mProperties[i] != 0) {
72
            return false;
73
          }
74
      }
75
      return true;
76
    }
77
78
    // Return a new nsCSSPropertyIDSet which is the inverse of this set.
79
    nsCSSPropertyIDSet Inverse() const {
80
      nsCSSPropertyIDSet result;
81
      for (size_t i = 0; i < mozilla::ArrayLength(mProperties); ++i) {
82
        result.mProperties[i] = ~mProperties[i];
83
      }
84
      return result;
85
    }
86
87
    // Returns a new nsCSSPropertyIDSet with all properties that are both in
88
    // this set and |aOther|.
89
    nsCSSPropertyIDSet Intersect(const nsCSSPropertyIDSet& aOther) const {
90
      nsCSSPropertyIDSet result;
91
      for (size_t i = 0; i < mozilla::ArrayLength(mProperties); ++i) {
92
        result.mProperties[i] = mProperties[i] & aOther.mProperties[i];
93
      }
94
      return result;
95
    }
96
97
    // Return a new nsCSSPropertyIDSet with all properties that are in either
98
    // this set or |aOther| but not both.
99
    nsCSSPropertyIDSet Xor(const nsCSSPropertyIDSet& aOther) const {
100
      nsCSSPropertyIDSet result;
101
      for (size_t i = 0; i < mozilla::ArrayLength(mProperties); ++i) {
102
        result.mProperties[i] = mProperties[i] ^ aOther.mProperties[i];
103
      }
104
      return result;
105
    }
106
107
private:
108
    typedef unsigned long property_set_type;
109
public:
110
    // number of bits in |property_set_type|.
111
    static const size_t kBitsInChunk = sizeof(property_set_type)*CHAR_BIT;
112
    // number of |property_set_type|s in the set
113
    static const size_t kChunkCount =
114
        (eCSSProperty_COUNT_no_shorthands + kBitsInChunk - 1) / kBitsInChunk;
115
116
    /*
117
     * For fast enumeration of all the bits that are set, callers can
118
     * check each chunk against zero (since in normal cases few bits are
119
     * likely to be set).
120
     */
121
0
    bool HasPropertyInChunk(size_t aChunk) const {
122
0
        return mProperties[aChunk] != 0;
123
0
    }
124
0
    bool HasPropertyAt(size_t aChunk, size_t aBit) const {
125
0
        return (mProperties[aChunk] & (property_set_type(1) << aBit)) != 0;
126
0
    }
127
0
    static nsCSSPropertyID CSSPropertyAt(size_t aChunk, size_t aBit) {
128
0
        return nsCSSPropertyID(aChunk * kBitsInChunk + aBit);
129
0
    }
130
131
private:
132
    property_set_type mProperties[kChunkCount];
133
};
134
135
#endif /* !defined(nsCSSPropertyIDSet_h__) */