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"
34 static const char canon_map[256] = {
35 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
36 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
37 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, '-', 0, 0,
38 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 0, 0, 0, 0, 0, 0,
39 '-', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
40 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 0, 0, 0, 0, '-',
41 0, 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
42 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 0, 0, 0, 0, 0
46 lang_equal (const void *v1,
49 const unsigned char *p1 = v1;
50 const unsigned char *p2 = v2;
52 while (canon_map[*p1] && canon_map[*p1] == canon_map[*p2])
57 return (canon_map[*p1] == canon_map[*p2]);
62 lang_hash (const void *key)
64 const unsigned char *p = key;
68 h = (h << 5) - h + canon_map[*p];
78 hb_language_from_string (const char *str)
80 static unsigned int num_langs;
81 static unsigned int num_alloced;
82 static const char **langs;
86 /* TODO Use a hash table or something */
91 for (i = 0; i < num_langs; i++)
92 if (lang_equal (str, langs[i]))
95 if (unlikely (num_langs == num_alloced)) {
96 unsigned int new_alloced = 2 * (8 + num_alloced);
97 const char **new_langs = realloc (langs, new_alloced * sizeof (langs[0]));
100 num_alloced = new_alloced;
104 langs[i] = strdup (str);
105 for (p = (unsigned char *) langs[i]; *p; p++)
110 return (hb_language_t) langs[i];
114 hb_language_to_string (hb_language_t language)
116 return (const char *) language;