Fix LDC, LDC_W, and INSTANCEOF opcodes, more debugging
[jamvm-avr32-jem.git] / src / utf8.c
blob2a2cb28669c8b90699eea3ce2a759fe83e6b8d31
1 /*
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.
22 #include <string.h>
23 #include <stdlib.h>
24 #include "jam.h"
25 #include "hash.h"
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) \
38 { \
39 int x = *ptr++; \
40 if(x & 0x80) { \
41 int y = *ptr++; \
42 if(x & 0x20) { \
43 int z = *ptr++; \
44 c = ((x&0xf)<<12)+((y&0x3f)<<6)+(z&0x3f); \
45 } else \
46 c = ((x&0x1f)<<6)+(y&0x3f); \
47 } else \
48 c = x; \
51 int utf8Len(char *utf8) {
52 int count;
54 for(count = 0; *utf8; count++) {
55 int x = *utf8;
56 utf8 += (x & 0x80) ? ((x & 0x20) ? 3 : 2) : 1;
59 return count;
62 void convertUtf8(char *utf8, unsigned short *buff) {
63 while(*utf8)
64 GET_UTF8_CHAR(utf8, *buff++);
67 int utf8Hash(char *utf8) {
68 int hash = 0;
70 while(*utf8) {
71 unsigned short c;
72 GET_UTF8_CHAR(utf8, c);
73 hash = hash * 37 + c;
76 return hash;
79 int utf8Comp(char *ptr, char *ptr2) {
80 while(*ptr && *ptr2) {
81 unsigned short c, c2;
83 GET_UTF8_CHAR(ptr, c);
84 GET_UTF8_CHAR(ptr2, c2);
85 if(c != c2)
86 return FALSE;
88 if(*ptr || *ptr2)
89 return FALSE;
91 return TRUE;
94 char *findUtf8String(char *string) {
95 char *interned;
97 /* Add if absent, no scavenge, locked */
98 findHashEntry(hash_table, string, interned, TRUE, FALSE, TRUE);
100 return interned;
103 char *slash2dots(char *utf8) {
104 int len = strlen(utf8);
105 char *conv = (char*)sysMalloc(len+1);
106 int i;
108 for(i = 0; i <= len; i++)
109 if(utf8[i] == '/')
110 conv[i] = '.';
111 else
112 conv[i] = utf8[i];
114 return conv;
117 char *slash2dots2buff(char *utf8, char *buff, int buff_len) {
118 char *pntr = buff;
120 while(*utf8 != '\0' && --buff_len) {
121 char c = *utf8++;
122 *pntr++ = c == '/' ? '.' : c;
125 *pntr = '\0';
126 return buff;
129 void initialiseUtf8() {
130 /* Init hash table, and create lock */
131 initHashTable(hash_table, HASHTABSZE, TRUE);
134 #ifndef NO_JNI
135 /* Functions used by JNI */
137 int utf8CharLen(unsigned short *unicode, int len) {
138 int count = 0;
140 for(; len > 0; len--) {
141 unsigned short c = *unicode++;
142 count += c > 0x7f ? (c > 0x7ff ? 3 : 2) : 1;
145 return count;
148 char *unicode2Utf8(unsigned short *unicode, int len, char *utf8) {
149 char *ptr = utf8;
151 for(; len > 0; len--) {
152 unsigned short c = *unicode++;
153 if((c == 0) || (c > 0x7f)) {
154 if(c > 0x7ff) {
155 *ptr++ = (c >> 12) | 0xe0;
156 *ptr++ = ((c >> 6) & 0x3f) | 0x80;
157 } else
158 *ptr++ = (c >> 6) | 0xc0;
159 *ptr++ = (c&0x3f) | 0x80;
160 } else
161 *ptr++ = c;
164 *ptr = '\0';
165 return utf8;
167 #endif