Always allocate new ligature id
[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 HB_BEGIN_DECLS
32
33
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
43 };
44
45 static hb_bool_t
46 lang_equal (const void *v1,
47             const void *v2)
48 {
49   const unsigned char *p1 = v1;
50   const unsigned char *p2 = v2;
51
52   while (canon_map[*p1] && canon_map[*p1] == canon_map[*p2])
53     {
54       p1++, p2++;
55     }
56
57   return (canon_map[*p1] == canon_map[*p2]);
58 }
59
60 #if 0
61 static unsigned int
62 lang_hash (const void *key)
63 {
64   const unsigned char *p = key;
65   unsigned int h = 0;
66   while (canon_map[*p])
67     {
68       h = (h << 5) - h + canon_map[*p];
69       p++;
70     }
71
72   return h;
73 }
74 #endif
75
76
77 hb_language_t
78 hb_language_from_string (const char *str)
79 {
80   static unsigned int num_langs;
81   static unsigned int num_alloced;
82   static const char **langs;
83   unsigned int i;
84   unsigned char *p;
85
86   /* TODO Use a hash table or something */
87
88   if (!str)
89     return NULL;
90
91   for (i = 0; i < num_langs; i++)
92     if (lang_equal (str, langs[i]))
93       return langs[i];
94
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]));
98     if (!new_langs)
99       return NULL;
100     num_alloced = new_alloced;
101     langs = new_langs;
102   }
103
104   langs[i] = strdup (str);
105   for (p = (unsigned char *) langs[i]; *p; p++)
106     *p = canon_map[*p];
107
108   num_langs++;
109
110   return (hb_language_t) langs[i];
111 }
112
113 const char *
114 hb_language_to_string (hb_language_t language)
115 {
116   return (const char *) language;
117 }
118
119
120 HB_END_DECLS