[set] Make all operators null-safe again

Changed my mind.

Also for hb_map_clear().

Part of https://github.com/harfbuzz/harfbuzz/pull/3162
This commit is contained in:
Behdad Esfahbod 2021-08-24 10:31:49 -06:00
parent 65c622c689
commit d3e09bf465
4 changed files with 39 additions and 32 deletions

View File

@ -46,9 +46,22 @@ struct hb_bit_set_invertible_t
bool in_error () const { return s.in_error (); } bool in_error () const { return s.in_error (); }
explicit operator bool () const { return !is_empty (); } explicit operator bool () const { return !is_empty (); }
void reset () { s.reset (); inverted = false; } void reset ()
void clear () { s.clear (); inverted = false; } {
void invert () { inverted = !inverted; } s.reset ();
inverted = false;
}
void clear ()
{
s.clear ();
if (likely (s.successful))
inverted = false;
}
void invert ()
{
if (likely (s.successful))
inverted = !inverted;
}
bool is_empty () const bool is_empty () const
{ {
@ -116,7 +129,12 @@ struct hb_bit_set_invertible_t
return next (&c) && c <= last; return next (&c) && c <= last;
} }
void set (const hb_bit_set_invertible_t &other) { s.set (other.s); inverted = other.inverted; } void set (const hb_bit_set_invertible_t &other)
{
s.set (other.s);
if (likely (s.successful))
inverted = other.inverted;
}
bool is_equal (const hb_bit_set_invertible_t &other) const bool is_equal (const hb_bit_set_invertible_t &other) const
{ {
@ -161,7 +179,8 @@ struct hb_bit_set_invertible_t
else else
process (hb_bitwise_lt, other); process (hb_bitwise_lt, other);
} }
inverted = inverted || other.inverted; if (likely (s.successful))
inverted = inverted || other.inverted;
} }
void intersect (const hb_bit_set_invertible_t &other) void intersect (const hb_bit_set_invertible_t &other)
{ {
@ -179,7 +198,8 @@ struct hb_bit_set_invertible_t
else else
process (hb_bitwise_gt, other); process (hb_bitwise_gt, other);
} }
inverted = inverted && other.inverted; if (likely (s.successful))
inverted = inverted && other.inverted;
} }
void subtract (const hb_bit_set_invertible_t &other) void subtract (const hb_bit_set_invertible_t &other)
{ {
@ -197,12 +217,14 @@ struct hb_bit_set_invertible_t
else else
process (hb_bitwise_and, other); process (hb_bitwise_and, other);
} }
inverted = inverted && !other.inverted; if (likely (s.successful))
inverted = inverted && !other.inverted;
} }
void symmetric_difference (const hb_bit_set_invertible_t &other) void symmetric_difference (const hb_bit_set_invertible_t &other)
{ {
process (hb_bitwise_xor, other); process (hb_bitwise_xor, other);
inverted = inverted ^ other.inverted; if (likely (s.successful))
inverted = inverted ^ other.inverted;
} }
bool next (hb_codepoint_t *codepoint) const bool next (hb_codepoint_t *codepoint) const

View File

@ -255,9 +255,6 @@ hb_map_has (const hb_map_t *map,
void void
hb_map_clear (hb_map_t *map) hb_map_clear (hb_map_t *map)
{ {
if (unlikely (hb_object_is_immutable (map)))
return;
return map->clear (); return map->clear ();
} }

View File

@ -169,6 +169,8 @@ struct hb_hashmap_t
void clear () void clear ()
{ {
if (unlikely (!successful)) return;
if (items) if (items)
for (auto &_ : hb_iter (items, mask + 1)) for (auto &_ : hb_iter (items, mask + 1))
_.clear (); _.clear ();

View File

@ -201,9 +201,7 @@ hb_set_copy (const hb_set_t *set)
void void
hb_set_clear (hb_set_t *set) hb_set_clear (hb_set_t *set)
{ {
if (unlikely (hb_object_is_immutable (set))) /* Immutible-safe. */
return;
set->clear (); set->clear ();
} }
@ -368,9 +366,7 @@ void
hb_set_set (hb_set_t *set, hb_set_set (hb_set_t *set,
const hb_set_t *other) const hb_set_t *other)
{ {
if (unlikely (hb_object_is_immutable (set))) /* Immutible-safe. */
return;
set->set (*other); set->set (*other);
} }
@ -387,9 +383,7 @@ void
hb_set_union (hb_set_t *set, hb_set_union (hb_set_t *set,
const hb_set_t *other) const hb_set_t *other)
{ {
if (unlikely (hb_object_is_immutable (set))) /* Immutible-safe. */
return;
set->union_ (*other); set->union_ (*other);
} }
@ -406,9 +400,7 @@ void
hb_set_intersect (hb_set_t *set, hb_set_intersect (hb_set_t *set,
const hb_set_t *other) const hb_set_t *other)
{ {
if (unlikely (hb_object_is_immutable (set))) /* Immutible-safe. */
return;
set->intersect (*other); set->intersect (*other);
} }
@ -425,9 +417,7 @@ void
hb_set_subtract (hb_set_t *set, hb_set_subtract (hb_set_t *set,
const hb_set_t *other) const hb_set_t *other)
{ {
if (unlikely (hb_object_is_immutable (set))) /* Immutible-safe. */
return;
set->subtract (*other); set->subtract (*other);
} }
@ -445,9 +435,7 @@ void
hb_set_symmetric_difference (hb_set_t *set, hb_set_symmetric_difference (hb_set_t *set,
const hb_set_t *other) const hb_set_t *other)
{ {
if (unlikely (hb_object_is_immutable (set))) /* Immutible-safe. */
return;
set->symmetric_difference (*other); set->symmetric_difference (*other);
} }
@ -462,9 +450,7 @@ hb_set_symmetric_difference (hb_set_t *set,
void void
hb_set_invert (hb_set_t *set) hb_set_invert (hb_set_t *set)
{ {
if (unlikely (hb_object_is_immutable (set))) /* Immutible-safe. */
return;
set->invert (); set->invert ();
} }