Further cleanup of sizeof
[framework/uifw/harfbuzz.git] / src / hb-language.c
1 /*
2  * Copyright (C) 2009  Red Hat, 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  * Red Hat Author(s): Behdad Esfahbod
25  */
26
27 #include "hb-private.h"
28
29 #include "hb-language.h"
30
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
40 };
41
42 static hb_bool_t
43 lang_equal (const void *v1,
44             const void *v2)
45 {
46   const unsigned char *p1 = v1;
47   const unsigned char *p2 = v2;
48
49   while (canon_map[*p1] && canon_map[*p1] == canon_map[*p2])
50     {
51       p1++, p2++;
52     }
53
54   return (canon_map[*p1] == canon_map[*p2]);
55 }
56
57 #if 0
58 static unsigned int
59 lang_hash (const void *key)
60 {
61   const unsigned char *p = key;
62   unsigned int h = 0;
63   while (canon_map[*p])
64     {
65       h = (h << 5) - h + canon_map[*p];
66       p++;
67     }
68
69   return h;
70 }
71 #endif
72
73
74 hb_language_t
75 hb_language_from_string (const char *str)
76 {
77   static unsigned int num_langs;
78   static unsigned int num_alloced;
79   static const char **langs;
80   unsigned int i;
81   unsigned char *p;
82
83   /* TODO Use a hash table or something */
84
85   if (!str)
86     return NULL;
87
88   for (i = 0; i < num_langs; i++)
89     if (lang_equal (str, langs[i]))
90       return langs[i];
91
92   if (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]));
95     if (!new_langs)
96       return NULL;
97     num_alloced = new_alloced;
98     langs = new_langs;
99   }
100
101   langs[i] = strdup (str);
102   for (p = (unsigned char *) langs[i]; *p; p++)
103     *p = canon_map[*p];
104
105   num_langs++;
106
107   return (hb_language_t) langs[i];
108 }
109
110 const char *
111 hb_language_to_string (hb_language_t language)
112 {
113   return (const char *) language;
114 }
115