Coverage Report

Created: 2026-12-01 07:14

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/harfbuzz/src/hb-set.cc
Line
Count
Source
1
/*
2
 * Copyright © 2012  Google, Inc.
3
 *
4
 *  This is part of HarfBuzz, a text shaping library.
5
 *
6
 * Permission is hereby granted, without written agreement and without
7
 * license or royalty fees, to use, copy, modify, and distribute this
8
 * software and its documentation for any purpose, provided that the
9
 * above copyright notice and the following two paragraphs appear in
10
 * all copies of this software.
11
 *
12
 * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
13
 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
14
 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
15
 * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
16
 * DAMAGE.
17
 *
18
 * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
19
 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
20
 * FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
21
 * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
22
 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
23
 *
24
 * Google Author(s): Behdad Esfahbod
25
 */
26
27
#include "hb-set.hh"
28
29
30
/**
31
 * SECTION:hb-set
32
 * @title: hb-set
33
 * @short_description: Objects representing a set of integers
34
 * @include: hb.h
35
 *
36
 * Set objects represent a mathematical set of integer values.  They are
37
 * used in non-shaping APIs to query certain sets of characters or glyphs,
38
 * or other integer values.
39
 **/
40
41
42
/**
43
 * hb_set_create:
44
 *
45
 * Creates a new, initially empty set.
46
 *
47
 * Return value: (transfer full): The new #hb_set_t, or hb_set_get_empty()
48
 * on allocation failure.
49
 *
50
 * Since: 0.9.2
51
 **/
52
hb_set_t *
53
hb_set_create ()
54
0
{
55
0
  hb_set_t *set;
56
57
0
  if (!(set = hb_object_create<hb_set_t> ()))
58
0
    return hb_set_get_empty ();
59
60
0
  return set;
61
0
}
62
63
/**
64
 * hb_set_get_empty:
65
 *
66
 * Fetches the singleton empty #hb_set_t.
67
 *
68
 * Return value: (transfer full): The empty #hb_set_t
69
 *
70
 * Since: 0.9.2
71
 **/
72
hb_set_t *
73
hb_set_get_empty ()
74
0
{
75
0
  return const_cast<hb_set_t *> (&Null (hb_set_t));
76
0
}
77
78
/**
79
 * hb_set_reference: (skip)
80
 * @set: A set
81
 *
82
 * Increases the reference count on a set.
83
 *
84
 * Return value: (transfer full): The set
85
 *
86
 * Since: 0.9.2
87
 **/
88
hb_set_t *
89
hb_set_reference (hb_set_t *set)
90
0
{
91
0
  return hb_object_reference (set);
92
0
}
93
94
/**
95
 * hb_set_destroy: (skip)
96
 * @set: A set
97
 *
98
 * Decreases the reference count on a set. When
99
 * the reference count reaches zero, the set is
100
 * destroyed, freeing all memory.
101
 *
102
 * Since: 0.9.2
103
 **/
104
void
105
hb_set_destroy (hb_set_t *set)
106
0
{
107
0
  if (!hb_object_destroy (set)) return;
108
109
0
  hb_free (set);
110
0
}
111
112
/**
113
 * hb_set_set_user_data: (skip)
114
 * @set: A set
115
 * @key: The user-data key to set
116
 * @data: A pointer to the user data to set
117
 * @destroy: (nullable): A callback to call when @data is not needed anymore
118
 * @replace: Whether to replace an existing data with the same key
119
 *
120
 * Attaches a user-data key/data pair to the specified set.
121
 *
122
 * Return value: `true` if success, `false` otherwise
123
 *
124
 * Since: 0.9.2
125
 **/
126
hb_bool_t
127
hb_set_set_user_data (hb_set_t           *set,
128
          hb_user_data_key_t *key,
129
          void *              data,
130
          hb_destroy_func_t   destroy,
131
          hb_bool_t           replace)
132
0
{
133
0
  return hb_object_set_user_data (set, key, data, destroy, replace);
134
0
}
135
136
/**
137
 * hb_set_get_user_data: (skip)
138
 * @set: A set
139
 * @key: The user-data key to query
140
 *
141
 * Fetches the user data associated with the specified key,
142
 * attached to the specified set.
143
 *
144
 * Return value: (transfer none): A pointer to the user data
145
 *
146
 * Since: 0.9.2
147
 **/
148
void *
149
hb_set_get_user_data (const hb_set_t     *set,
150
          hb_user_data_key_t *key)
151
0
{
152
0
  return hb_object_get_user_data (set, key);
153
0
}
154
155
156
/**
157
 * hb_set_allocation_successful:
158
 * @set: A set
159
 *
160
 * Tests whether memory allocation for a set was successful.
161
 *
162
 * Return value: `true` if allocation succeeded, `false` otherwise
163
 *
164
 * Since: 0.9.2
165
 **/
166
hb_bool_t
167
hb_set_allocation_successful (const hb_set_t  *set)
168
0
{
169
0
  return !set->in_error ();
170
0
}
171
172
/**
173
 * hb_set_copy:
174
 * @set: A set
175
 *
176
 * Allocate a copy of @set.
177
 * Use hb_set_allocation_successful() on the result to check whether its
178
 * contents were copied successfully.
179
 *
180
 * Return value: (transfer full): A copy of @set, or hb_set_get_empty()
181
 * if the set object cannot be allocated.
182
 *
183
 * Since: 2.8.2
184
 **/
185
hb_set_t *
186
hb_set_copy (const hb_set_t *set)
187
0
{
188
0
  hb_set_t *copy = hb_set_create ();
189
0
  if (unlikely (copy->in_error ()))
190
0
    return hb_set_get_empty ();
191
192
0
  copy->set (*set);
193
0
  return copy;
194
0
}
195
196
/**
197
 * hb_set_clear:
198
 * @set: A set
199
 *
200
 * Clears out the contents of a set.
201
 *
202
 * Since: 0.9.2
203
 **/
204
void
205
hb_set_clear (hb_set_t *set)
206
0
{
207
  /* Immutable-safe. */
208
0
  set->clear ();
209
0
}
210
211
/**
212
 * hb_set_is_empty:
213
 * @set: a set.
214
 *
215
 * Tests whether a set is empty (contains no elements).
216
 *
217
 * Return value: `true` if @set is empty
218
 *
219
 * Since: 0.9.7
220
 **/
221
hb_bool_t
222
hb_set_is_empty (const hb_set_t *set)
223
0
{
224
0
  return set->is_empty ();
225
0
}
226
227
/**
228
 * hb_set_has:
229
 * @set: A set
230
 * @codepoint: The element to query
231
 *
232
 * Tests whether @codepoint belongs to @set.
233
 *
234
 * Return value: `true` if @codepoint is in @set, `false` otherwise
235
 *
236
 * Since: 0.9.2
237
 **/
238
hb_bool_t
239
hb_set_has (const hb_set_t *set,
240
      hb_codepoint_t  codepoint)
241
0
{
242
0
  return set->has (codepoint);
243
0
}
244
245
/**
246
 * hb_set_add:
247
 * @set: A set
248
 * @codepoint: The element to add to @set
249
 *
250
 * Adds @codepoint to @set.
251
 *
252
 * Since: 0.9.2
253
 **/
254
void
255
hb_set_add (hb_set_t       *set,
256
      hb_codepoint_t  codepoint)
257
0
{
258
  /* Immutable-safe. */
259
0
  set->add (codepoint);
260
0
}
261
262
/**
263
 * hb_set_add_sorted_array:
264
 * @set: A set
265
 * @sorted_codepoints: (array length=num_codepoints): Array of codepoints to add
266
 * @num_codepoints: Length of @sorted_codepoints
267
 *
268
 * Adds @num_codepoints codepoints to a set at once.
269
 * The codepoints array must be in increasing order,
270
 * with size at least @num_codepoints.
271
 *
272
 * Since: 4.1.0
273
 */
274
HB_EXTERN void
275
hb_set_add_sorted_array (hb_set_t             *set,
276
             const hb_codepoint_t *sorted_codepoints,
277
             unsigned int          num_codepoints)
278
0
{
279
  /* Immutable-safe. */
280
0
  set->add_sorted_array (sorted_codepoints,
281
0
             num_codepoints,
282
0
             sizeof(hb_codepoint_t));
283
0
}
284
285
/**
286
 * hb_set_add_range:
287
 * @set: A set
288
 * @first: The first element to add to @set
289
 * @last: The final element to add to @set
290
 *
291
 * Adds all of the elements from @first to @last
292
 * (inclusive) to @set.
293
 *
294
 * Since: 0.9.7
295
 **/
296
void
297
hb_set_add_range (hb_set_t       *set,
298
      hb_codepoint_t  first,
299
      hb_codepoint_t  last)
300
0
{
301
  /* Immutable-safe. */
302
0
  set->add_range (first, last);
303
0
}
304
305
/**
306
 * hb_set_del:
307
 * @set: A set
308
 * @codepoint: Removes @codepoint from @set
309
 *
310
 * Removes @codepoint from @set.
311
 *
312
 * Since: 0.9.2
313
 **/
314
void
315
hb_set_del (hb_set_t       *set,
316
      hb_codepoint_t  codepoint)
317
0
{
318
  /* Immutable-safe. */
319
0
  set->del (codepoint);
320
0
}
321
322
/**
323
 * hb_set_del_range:
324
 * @set: A set
325
 * @first: The first element to remove from @set
326
 * @last: The final element to remove from @set
327
 *
328
 * Removes all of the elements from @first to @last
329
 * (inclusive) from @set.
330
 *
331
 * If @last is #HB_SET_VALUE_INVALID, then all values
332
 * greater than or equal to @first are removed.
333
 *
334
 * Since: 0.9.7
335
 **/
336
void
337
hb_set_del_range (hb_set_t       *set,
338
      hb_codepoint_t  first,
339
      hb_codepoint_t  last)
340
0
{
341
  /* Immutable-safe. */
342
0
  set->del_range (first, last);
343
0
}
344
345
/**
346
 * hb_set_is_equal:
347
 * @set: A set
348
 * @other: Another set
349
 *
350
 * Tests whether @set and @other are equal (contain the same
351
 * elements).
352
 *
353
 * Return value: `true` if the two sets are equal, `false` otherwise.
354
 *
355
 * Since: 0.9.7
356
 **/
357
hb_bool_t
358
hb_set_is_equal (const hb_set_t *set,
359
     const hb_set_t *other)
360
0
{
361
0
  return set->is_equal (*other);
362
0
}
363
364
/**
365
 * hb_set_intersects:
366
 * @set: A set
367
 * @other: Another set
368
 *
369
 * Tests whether @set and @other have any elements in common.
370
 *
371
 * Return value: `true` if the two sets intersect, `false` otherwise.
372
 *
373
 * Since: 14.4.0
374
 **/
375
hb_bool_t
376
hb_set_intersects (const hb_set_t *set,
377
       const hb_set_t *other)
378
0
{
379
0
  return set->intersects (*other);
380
0
}
381
382
/**
383
 * hb_set_hash:
384
 * @set: A set
385
 *
386
 * Creates a hash representing @set.
387
 *
388
 * Return value:
389
 * A hash of @set.
390
 *
391
 * Since: 4.4.0
392
 **/
393
HB_EXTERN unsigned int
394
hb_set_hash (const hb_set_t *set)
395
0
{
396
0
  return set->hash ();
397
0
}
398
399
/**
400
 * hb_set_is_subset:
401
 * @set: A set
402
 * @larger_set: Another set
403
 *
404
 * Tests whether @set is a subset of @larger_set.
405
 *
406
 * Return value: `true` if the @set is a subset of (or equal to) @larger_set, `false` otherwise.
407
 *
408
 * Since: 1.8.1
409
 **/
410
hb_bool_t
411
hb_set_is_subset (const hb_set_t *set,
412
      const hb_set_t *larger_set)
413
0
{
414
0
  return set->is_subset (*larger_set);
415
0
}
416
417
/**
418
 * hb_set_set:
419
 * @set: A set
420
 * @other: Another set
421
 *
422
 * Makes the contents of @set equal to the contents of @other.
423
 *
424
 * Since: 0.9.2
425
 **/
426
void
427
hb_set_set (hb_set_t       *set,
428
      const hb_set_t *other)
429
0
{
430
  /* Immutable-safe. */
431
0
  set->set (*other);
432
0
}
433
434
/**
435
 * hb_set_union:
436
 * @set: A set
437
 * @other: Another set
438
 *
439
 * Makes @set the union of @set and @other.
440
 *
441
 * Since: 0.9.2
442
 **/
443
void
444
hb_set_union (hb_set_t       *set,
445
        const hb_set_t *other)
446
0
{
447
  /* Immutable-safe. */
448
0
  set->union_ (*other);
449
0
}
450
451
/**
452
 * hb_set_intersect:
453
 * @set: A set
454
 * @other: Another set
455
 *
456
 * Makes @set the intersection of @set and @other.
457
 *
458
 * Since: 0.9.2
459
 **/
460
void
461
hb_set_intersect (hb_set_t       *set,
462
      const hb_set_t *other)
463
0
{
464
  /* Immutable-safe. */
465
0
  set->intersect (*other);
466
0
}
467
468
/**
469
 * hb_set_subtract:
470
 * @set: A set
471
 * @other: Another set
472
 *
473
 * Subtracts the contents of @other from @set.
474
 *
475
 * Since: 0.9.2
476
 **/
477
void
478
hb_set_subtract (hb_set_t       *set,
479
     const hb_set_t *other)
480
0
{
481
  /* Immutable-safe. */
482
0
  set->subtract (*other);
483
0
}
484
485
/**
486
 * hb_set_symmetric_difference:
487
 * @set: A set
488
 * @other: Another set
489
 *
490
 * Makes @set the symmetric difference of @set
491
 * and @other.
492
 *
493
 * Since: 0.9.2
494
 **/
495
void
496
hb_set_symmetric_difference (hb_set_t       *set,
497
           const hb_set_t *other)
498
0
{
499
  /* Immutable-safe. */
500
0
  set->symmetric_difference (*other);
501
0
}
502
503
/**
504
 * hb_set_invert:
505
 * @set: A set
506
 *
507
 * Inverts the contents of @set.
508
 *
509
 * This function was deprecated in 1.6.1 and became a no-op. It was
510
 * restored in 2.9.1.
511
 *
512
 * Since: 0.9.10
513
 **/
514
void
515
hb_set_invert (hb_set_t *set)
516
0
{
517
  /* Immutable-safe. */
518
0
  set->invert ();
519
0
}
520
521
/**
522
 * hb_set_is_inverted:
523
 * @set: A set
524
 *
525
 * Returns whether the set is inverted.
526
 *
527
 * Return value: `true` if the set is inverted, `false` otherwise
528
 *
529
 * Since: 7.0.0
530
 **/
531
hb_bool_t
532
hb_set_is_inverted (const hb_set_t *set)
533
0
{
534
0
  return set->is_inverted ();
535
0
}
536
537
/**
538
 * hb_set_get_population:
539
 * @set: A set
540
 *
541
 * Returns the number of elements in the set.
542
 *
543
 * Return value: The population of @set
544
 *
545
 * Since: 0.9.7
546
 **/
547
unsigned int
548
hb_set_get_population (const hb_set_t *set)
549
0
{
550
0
  return set->get_population ();
551
0
}
552
553
/**
554
 * hb_set_get_min:
555
 * @set: A set
556
 *
557
 * Finds the smallest element in the set.
558
 *
559
 * Return value: minimum of @set, or #HB_SET_VALUE_INVALID if @set is empty.
560
 *
561
 * Since: 0.9.7
562
 **/
563
hb_codepoint_t
564
hb_set_get_min (const hb_set_t *set)
565
0
{
566
0
  return set->get_min ();
567
0
}
568
569
/**
570
 * hb_set_get_max:
571
 * @set: A set
572
 *
573
 * Finds the largest element in the set.
574
 *
575
 * Return value: maximum of @set, or #HB_SET_VALUE_INVALID if @set is empty.
576
 *
577
 * Since: 0.9.7
578
 **/
579
hb_codepoint_t
580
hb_set_get_max (const hb_set_t *set)
581
0
{
582
0
  return set->get_max ();
583
0
}
584
585
/**
586
 * hb_set_next:
587
 * @set: A set
588
 * @codepoint: (inout): Input = Code point to query
589
 *             Output = Code point retrieved
590
 *
591
 * Fetches the next element in @set that is greater than current value of @codepoint.
592
 *
593
 * Set @codepoint to #HB_SET_VALUE_INVALID to get started.
594
 *
595
 * Return value: `true` if there was a next value, `false` otherwise
596
 *
597
 * Since: 0.9.2
598
 **/
599
hb_bool_t
600
hb_set_next (const hb_set_t *set,
601
       hb_codepoint_t *codepoint)
602
0
{
603
0
  return set->next (codepoint);
604
0
}
605
606
/**
607
 * hb_set_previous:
608
 * @set: A set
609
 * @codepoint: (inout): Input = Code point to query
610
 *             Output = Code point retrieved
611
 *
612
 * Fetches the previous element in @set that is lower than current value of @codepoint.
613
 *
614
 * Set @codepoint to #HB_SET_VALUE_INVALID to get started.
615
 *
616
 * Return value: `true` if there was a previous value, `false` otherwise
617
 *
618
 * Since: 1.8.0
619
 **/
620
hb_bool_t
621
hb_set_previous (const hb_set_t *set,
622
     hb_codepoint_t *codepoint)
623
0
{
624
0
  return set->previous (codepoint);
625
0
}
626
627
/**
628
 * hb_set_next_range:
629
 * @set: A set
630
 * @first: (out): The first code point in the range
631
 * @last: (inout): Input = The current last code point in the range
632
 *         Output = The last code point in the range
633
 *
634
 * Fetches the next consecutive range of elements in @set that
635
 * are greater than current value of @last.
636
 *
637
 * Set @last to #HB_SET_VALUE_INVALID to get started.
638
 *
639
 * Return value: `true` if there was a next range, `false` otherwise
640
 *
641
 * Since: 0.9.7
642
 **/
643
hb_bool_t
644
hb_set_next_range (const hb_set_t *set,
645
       hb_codepoint_t *first,
646
       hb_codepoint_t *last)
647
0
{
648
0
  return set->next_range (first, last);
649
0
}
650
651
/**
652
 * hb_set_previous_range:
653
 * @set: A set
654
 * @first: (inout): Input = The current first code point in the range
655
 *         Output = The first code point in the range
656
 * @last: (out): The last code point in the range
657
 *
658
 * Fetches the previous consecutive range of elements in @set that
659
 * are greater than current value of @last.
660
 *
661
 * Set @first to #HB_SET_VALUE_INVALID to get started.
662
 *
663
 * Return value: `true` if there was a previous range, `false` otherwise
664
 *
665
 * Since: 1.8.0
666
 **/
667
hb_bool_t
668
hb_set_previous_range (const hb_set_t *set,
669
           hb_codepoint_t *first,
670
           hb_codepoint_t *last)
671
0
{
672
0
  return set->previous_range (first, last);
673
0
}
674
675
/**
676
 * hb_set_next_many:
677
 * @set: A set
678
 * @codepoint: Outputting codepoints starting after this one.
679
 *             Use #HB_SET_VALUE_INVALID to get started.
680
 * @out: (array length=size): An array of codepoints to write to.
681
 * @size: The maximum number of codepoints to write out.
682
 *
683
 * Finds the next element in @set that is greater than @codepoint. Writes out
684
 * codepoints to @out, until either the set runs out of elements, or @size
685
 * codepoints are written, whichever comes first.
686
 *
687
 * Return value: the number of values written.
688
 *
689
 * Since: 4.2.0
690
 **/
691
unsigned int
692
hb_set_next_many (const hb_set_t *set,
693
      hb_codepoint_t  codepoint,
694
      hb_codepoint_t *out,
695
      unsigned int    size)
696
0
{
697
0
  return set->next_many (codepoint, out, size);
698
0
}