Skip to content
Permalink
Browse files Browse the repository at this point in the history
[set] Make all operators null-safe again
Changed my mind.

Also for hb_map_clear().

Part of #3162
  • Loading branch information
behdad committed Aug 24, 2021
1 parent 65c622c commit d3e09bf
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 32 deletions.
38 changes: 30 additions & 8 deletions src/hb-bit-set-invertible.hh
Expand Up @@ -46,9 +46,22 @@ struct hb_bit_set_invertible_t
bool in_error () const { return s.in_error (); }
explicit operator bool () const { return !is_empty (); }

void reset () { s.reset (); inverted = false; }
void clear () { s.clear (); inverted = false; }
void invert () { inverted = !inverted; }
void reset ()
{
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
{
Expand Down Expand Up @@ -116,7 +129,12 @@ struct hb_bit_set_invertible_t
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
{
Expand Down Expand Up @@ -161,7 +179,8 @@ struct hb_bit_set_invertible_t
else
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)
{
Expand All @@ -179,7 +198,8 @@ struct hb_bit_set_invertible_t
else
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)
{
Expand All @@ -197,12 +217,14 @@ struct hb_bit_set_invertible_t
else
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)
{
process (hb_bitwise_xor, other);
inverted = inverted ^ other.inverted;
if (likely (s.successful))
inverted = inverted ^ other.inverted;
}

bool next (hb_codepoint_t *codepoint) const
Expand Down
3 changes: 0 additions & 3 deletions src/hb-map.cc
Expand Up @@ -255,9 +255,6 @@ hb_map_has (const hb_map_t *map,
void
hb_map_clear (hb_map_t *map)
{
if (unlikely (hb_object_is_immutable (map)))
return;

return map->clear ();
}

Expand Down
2 changes: 2 additions & 0 deletions src/hb-map.hh
Expand Up @@ -169,6 +169,8 @@ struct hb_hashmap_t

void clear ()
{
if (unlikely (!successful)) return;

if (items)
for (auto &_ : hb_iter (items, mask + 1))
_.clear ();
Expand Down
28 changes: 7 additions & 21 deletions src/hb-set.cc
Expand Up @@ -201,9 +201,7 @@ hb_set_copy (const hb_set_t *set)
void
hb_set_clear (hb_set_t *set)
{
if (unlikely (hb_object_is_immutable (set)))
return;

/* Immutible-safe. */
set->clear ();
}

Expand Down Expand Up @@ -368,9 +366,7 @@ void
hb_set_set (hb_set_t *set,
const hb_set_t *other)
{
if (unlikely (hb_object_is_immutable (set)))
return;

/* Immutible-safe. */
set->set (*other);
}

Expand All @@ -387,9 +383,7 @@ void
hb_set_union (hb_set_t *set,
const hb_set_t *other)
{
if (unlikely (hb_object_is_immutable (set)))
return;

/* Immutible-safe. */
set->union_ (*other);
}

Expand All @@ -406,9 +400,7 @@ void
hb_set_intersect (hb_set_t *set,
const hb_set_t *other)
{
if (unlikely (hb_object_is_immutable (set)))
return;

/* Immutible-safe. */
set->intersect (*other);
}

Expand All @@ -425,9 +417,7 @@ void
hb_set_subtract (hb_set_t *set,
const hb_set_t *other)
{
if (unlikely (hb_object_is_immutable (set)))
return;

/* Immutible-safe. */
set->subtract (*other);
}

Expand All @@ -445,9 +435,7 @@ void
hb_set_symmetric_difference (hb_set_t *set,
const hb_set_t *other)
{
if (unlikely (hb_object_is_immutable (set)))
return;

/* Immutible-safe. */
set->symmetric_difference (*other);
}

Expand All @@ -462,9 +450,7 @@ hb_set_symmetric_difference (hb_set_t *set,
void
hb_set_invert (hb_set_t *set)
{
if (unlikely (hb_object_is_immutable (set)))
return;

/* Immutible-safe. */
set->invert ();
}

Expand Down

0 comments on commit d3e09bf

Please sign in to comment.