fpurge: Improve configure test.
[gnulib.git] / lib / unictype / blocks.c
blob987fe67b285c50bd3a953a807339f980547b4bce
1 /* Blocks of Unicode characters.
2 Copyright (C) 2007, 2009-2024 Free Software Foundation, Inc.
3 Written by Bruno Haible <bruno@clisp.org>, 2007.
5 This file is free software.
6 It is dual-licensed under "the GNU LGPLv3+ or the GNU GPLv2+".
7 You can redistribute it and/or modify it under either
8 - the terms of the GNU Lesser General Public License as published
9 by the Free Software Foundation, either version 3, or (at your
10 option) any later version, or
11 - the terms of the GNU General Public License as published by the
12 Free Software Foundation; either version 2, or (at your option)
13 any later version, or
14 - the same dual license "the GNU LGPLv3+ or the GNU GPLv2+".
16 This file is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 Lesser General Public License and the GNU General Public License
20 for more details.
22 You should have received a copy of the GNU Lesser General Public
23 License and of the GNU General Public License along with this
24 program. If not, see <https://www.gnu.org/licenses/>. */
26 #include <config.h>
28 /* Specification. */
29 #include "unictype.h"
31 #include "blocks.h"
33 const uc_block_t *
34 uc_block (ucs4_t uc)
36 unsigned int first_index;
37 unsigned int last_index;
39 if (uc < blocks_level1_threshold)
41 unsigned int index1 = uc >> blocks_level1_shift;
42 first_index = blocks_level1[2 * index1];
43 last_index = blocks_level1[2 * index1 + 1];
45 else
47 first_index = blocks_upper_first_index;
48 last_index = blocks_upper_last_index;
50 /* We know that the relevant blocks are blocks[i] with
51 first_index <= i < last_index. Now perform a binary search. */
52 while (first_index < last_index)
54 unsigned int mid_index = (first_index + last_index) / 2;
55 if (blocks[mid_index].end < uc)
56 first_index = mid_index + 1;
57 else if (uc < blocks[mid_index].start)
58 last_index = mid_index;
59 else
60 return &blocks[mid_index];
62 return NULL;
65 void
66 uc_all_blocks (const uc_block_t **blocksp, size_t *countp)
68 *blocksp = blocks;
69 *countp = sizeof (blocks) / sizeof (blocks[0]);