2 * Copyright (C) 2003, 2004, 2005, 2006, 2007
3 * Robert Lougher <rob@lougher.org.uk>.
5 * This file is part of JamVM.
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2,
10 * or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
27 #define HASHTABSZE 1<<10
28 #define HASH(ptr) utf8Hash(ptr)
29 #define COMPARE(ptr1, ptr2, hash1, hash2) (ptr1 == ptr2) || \
30 ((hash1 == hash2) && utf8Comp(ptr1, ptr2))
31 #define PREPARE(ptr) ptr
32 #define SCAVENGE(ptr) FALSE
33 #define FOUND(ptr) ptr
35 static HashTable hash_table
;
37 #define GET_UTF8_CHAR(ptr, c) \
44 c = ((x&0xf)<<12)+((y&0x3f)<<6)+(z&0x3f); \
46 c = ((x&0x1f)<<6)+(y&0x3f); \
51 int utf8Len(char *utf8
) {
54 for(count
= 0; *utf8
; count
++) {
56 utf8
+= (x
& 0x80) ? ((x
& 0x20) ? 3 : 2) : 1;
62 void convertUtf8(char *utf8
, unsigned short *buff
) {
64 GET_UTF8_CHAR(utf8
, *buff
++);
67 int utf8Hash(char *utf8
) {
72 GET_UTF8_CHAR(utf8
, c
);
79 int utf8Comp(char *ptr
, char *ptr2
) {
80 while(*ptr
&& *ptr2
) {
83 GET_UTF8_CHAR(ptr
, c
);
84 GET_UTF8_CHAR(ptr2
, c2
);
94 char *findUtf8String(char *string
) {
97 /* Add if absent, no scavenge, locked */
98 findHashEntry(hash_table
, string
, interned
, TRUE
, FALSE
, TRUE
);
103 char *slash2dots(char *utf8
) {
104 int len
= strlen(utf8
);
105 char *conv
= (char*)sysMalloc(len
+1);
108 for(i
= 0; i
<= len
; i
++)
117 char *slash2dots2buff(char *utf8
, char *buff
, int buff_len
) {
120 while(*utf8
!= '\0' && --buff_len
) {
122 *pntr
++ = c
== '/' ? '.' : c
;
129 void initialiseUtf8() {
130 /* Init hash table, and create lock */
131 initHashTable(hash_table
, HASHTABSZE
, TRUE
);
135 /* Functions used by JNI */
137 int utf8CharLen(unsigned short *unicode
, int len
) {
140 for(; len
> 0; len
--) {
141 unsigned short c
= *unicode
++;
142 count
+= c
> 0x7f ? (c
> 0x7ff ? 3 : 2) : 1;
148 char *unicode2Utf8(unsigned short *unicode
, int len
, char *utf8
) {
151 for(; len
> 0; len
--) {
152 unsigned short c
= *unicode
++;
153 if((c
== 0) || (c
> 0x7f)) {
155 *ptr
++ = (c
>> 12) | 0xe0;
156 *ptr
++ = ((c
>> 6) & 0x3f) | 0x80;
158 *ptr
++ = (c
>> 6) | 0xc0;
159 *ptr
++ = (c
&0x3f) | 0x80;