2 * Copyright (C) 2009 Red Hat, Inc.
4 * This is part of HarfBuzz, a text shaping library.
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.
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
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.
24 * Red Hat Author(s): Behdad Esfahbod
27 #include "hb-private.h"
29 #include "hb-language.h"
31 static const char canon_map[256] = {
32 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
33 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
34 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, '-', 0, 0,
35 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 0, 0, 0, 0, 0, 0,
36 '-', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
37 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 0, 0, 0, 0, '-',
38 0, 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
39 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 0, 0, 0, 0, 0
43 lang_equal (const void *v1,
46 const unsigned char *p1 = v1;
47 const unsigned char *p2 = v2;
49 while (canon_map[*p1] && canon_map[*p1] == canon_map[*p2])
54 return (canon_map[*p1] == canon_map[*p2]);
59 lang_hash (const void *key)
61 const unsigned char *p = key;
65 h = (h << 5) - h + canon_map[*p];
75 hb_language_from_string (const char *str)
77 static unsigned int num_langs;
78 static unsigned int num_alloced;
79 static const char **langs;
83 /* TODO Use a hash table or something */
88 for (i = 0; i < num_langs; i++)
89 if (lang_equal (str, langs[i]))
92 if (HB_UNLIKELY (num_langs == num_alloced)) {
93 unsigned int new_alloced = 2 * (8 + num_alloced);
94 const char **new_langs = realloc (langs, new_alloced * sizeof (langs[0]));
97 num_alloced = new_alloced;
101 langs[i] = strdup (str);
102 for (p = (unsigned char *) langs[i]; *p; p++)
105 return (hb_language_t) langs[i];
109 hb_language_to_string (hb_language_t language)
111 return (const char *) language;