1 /* src/vm/utf.c - utf functions
3 Copyright (C) 1996-2005 R. Grafl, A. Krall, C. Kruegel, C. Oates,
4 R. Obermaisser, M. Platter, M. Probst, S. Ring, E. Steiner,
5 C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich, J. Wenninger,
6 Institut f. Computersprachen - TU Wien
8 This file is part of CACAO.
10 This program is free software; you can redistribute it and/or
11 modify it under the terms of the GNU General Public License as
12 published by the Free Software Foundation; either version 2, or (at
13 your option) any later version.
15 This program is distributed in the hope that it will be useful, but
16 WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
25 Contact: cacao@complang.tuwien.ac.at
27 Authors: Reinhard Grafl
33 $Id: utf8.c 3541 2005-11-03 20:33:51Z twisti $
40 #include "mm/memory.h"
41 #include "vm/exceptions.h"
42 #include "vm/options.h"
43 #include "vm/statistics.h"
44 #include "vm/stringlocal.h"
45 #include "vm/tables.h"
49 hashtable utf_hash; /* hashtable for utf8-symbols */
52 /* utf-symbols for pointer comparison of frequently used strings **************/
54 utf *utf_java_lang_Object; /* java/lang/Object */
56 utf *utf_java_lang_Class;
57 utf *utf_java_lang_ClassLoader;
58 utf *utf_java_lang_Cloneable;
59 utf *utf_java_lang_SecurityManager;
60 utf *utf_java_lang_String;
61 utf *utf_java_lang_System;
62 utf *utf_java_lang_ThreadGroup;
63 utf *utf_java_io_Serializable;
65 utf *utf_java_lang_Throwable;
66 utf *utf_java_lang_VMThrowable;
67 utf *utf_java_lang_Error;
68 utf *utf_java_lang_NoClassDefFoundError;
69 utf *utf_java_lang_OutOfMemoryError;
71 utf *utf_java_lang_Exception;
72 utf *utf_java_lang_ClassNotFoundException;
73 utf *utf_java_lang_IllegalArgumentException;
74 utf *utf_java_lang_IllegalMonitorStateException;
76 utf *utf_java_lang_NullPointerException;
78 utf* utf_java_lang_Void;
79 utf* utf_java_lang_Boolean;
80 utf* utf_java_lang_Byte;
81 utf* utf_java_lang_Character;
82 utf* utf_java_lang_Short;
83 utf* utf_java_lang_Integer;
84 utf* utf_java_lang_Long;
85 utf* utf_java_lang_Float;
86 utf* utf_java_lang_Double;
88 utf *utf_java_lang_StackTraceElement;
89 utf *utf_java_lang_reflect_Constructor;
90 utf *utf_java_lang_reflect_Field;
91 utf *utf_java_lang_reflect_Method;
92 utf *utf_java_util_Vector;
94 utf *utf_InnerClasses; /* InnerClasses */
95 utf *utf_ConstantValue; /* ConstantValue */
96 utf *utf_Code; /* Code */
97 utf *utf_Exceptions; /* Exceptions */
98 utf *utf_LineNumberTable; /* LineNumberTable */
99 utf *utf_SourceFile; /* SourceFile */
101 utf *utf_init; /* <init> */
102 utf *utf_clinit; /* <clinit> */
103 utf *utf_clone; /* clone */
104 utf *utf_finalize; /* finalize */
105 utf *utf_run; /* run */
107 utf *utf_add; /* add */
108 utf *utf_remove; /* remove */
109 utf *utf_put; /* put */
110 utf *utf_get; /* get */
111 utf *utf_value; /* value */
113 utf *utf_fillInStackTrace;
114 utf *utf_getSystemClassLoader;
116 utf *utf_printStackTrace;
127 utf *utf_void__void; /* ()V */
128 utf *utf_boolean__void; /* (Z)V */
129 utf *utf_byte__void; /* (B)V */
130 utf *utf_char__void; /* (C)V */
131 utf *utf_short__void; /* (S)V */
132 utf *utf_int__void; /* (I)V */
133 utf *utf_long__void; /* (J)V */
134 utf *utf_float__void; /* (F)V */
135 utf *utf_double__void; /* (D)V */
137 utf *utf_void__java_lang_ClassLoader; /* ()Ljava/lang/ClassLoader; */
138 utf *utf_void__java_lang_Object; /* ()Ljava/lang/Object; */
139 utf *utf_void__java_lang_Throwable; /* ()Ljava/lang/Throwable; */
140 utf *utf_java_lang_Object__java_lang_Object;
141 utf *utf_java_lang_String__void; /* (Ljava/lang/String;)V */
142 utf *utf_java_lang_String__java_lang_Class;
143 utf *utf_java_lang_Throwable__void; /* (Ljava/lang/Throwable;)V */
145 utf *utf_not_named_yet; /* special name for unnamed classes */
147 utf *array_packagename;
150 /* utf_init ********************************************************************
152 Initializes the utf8 subsystem.
154 *******************************************************************************/
158 /* create utf-symbols for pointer comparison of frequently used strings */
160 utf_java_lang_Object = utf_new_char("java/lang/Object");
162 utf_java_lang_Class = utf_new_char("java/lang/Class");
163 utf_java_lang_ClassLoader = utf_new_char("java/lang/ClassLoader");
164 utf_java_lang_Cloneable = utf_new_char("java/lang/Cloneable");
165 utf_java_lang_SecurityManager = utf_new_char("java/lang/SecurityManager");
166 utf_java_lang_String = utf_new_char("java/lang/String");
167 utf_java_lang_System = utf_new_char("java/lang/System");
168 utf_java_lang_ThreadGroup = utf_new_char("java/lang/ThreadGroup");
169 utf_java_io_Serializable = utf_new_char("java/io/Serializable");
171 utf_java_lang_Throwable = utf_new_char(string_java_lang_Throwable);
172 utf_java_lang_VMThrowable = utf_new_char(string_java_lang_VMThrowable);
173 utf_java_lang_Error = utf_new_char(string_java_lang_Error);
175 utf_java_lang_NoClassDefFoundError =
176 utf_new_char(string_java_lang_NoClassDefFoundError);
178 utf_java_lang_OutOfMemoryError =
179 utf_new_char(string_java_lang_OutOfMemoryError);
181 utf_java_lang_Exception = utf_new_char(string_java_lang_Exception);
183 utf_java_lang_ClassNotFoundException =
184 utf_new_char(string_java_lang_ClassNotFoundException);
186 utf_java_lang_IllegalArgumentException =
187 utf_new_char(string_java_lang_IllegalArgumentException);
189 utf_java_lang_IllegalMonitorStateException =
190 utf_new_char(string_java_lang_IllegalMonitorStateException);
192 utf_java_lang_NullPointerException =
193 utf_new_char(string_java_lang_NullPointerException);
195 utf_java_lang_Void = utf_new_char("java/lang/Void");
196 utf_java_lang_Boolean = utf_new_char("java/lang/Boolean");
197 utf_java_lang_Byte = utf_new_char("java/lang/Byte");
198 utf_java_lang_Character = utf_new_char("java/lang/Character");
199 utf_java_lang_Short = utf_new_char("java/lang/Short");
200 utf_java_lang_Integer = utf_new_char("java/lang/Integer");
201 utf_java_lang_Long = utf_new_char("java/lang/Long");
202 utf_java_lang_Float = utf_new_char("java/lang/Float");
203 utf_java_lang_Double = utf_new_char("java/lang/Double");
205 utf_java_lang_StackTraceElement =
206 utf_new_char("java/lang/StackTraceElement");
208 utf_java_lang_reflect_Constructor =
209 utf_new_char("java/lang/reflect/Constructor");
211 utf_java_lang_reflect_Field = utf_new_char("java/lang/reflect/Field");
212 utf_java_lang_reflect_Method = utf_new_char("java/lang/reflect/Method");
213 utf_java_util_Vector = utf_new_char("java/util/Vector");
215 utf_InnerClasses = utf_new_char("InnerClasses");
216 utf_ConstantValue = utf_new_char("ConstantValue");
217 utf_Code = utf_new_char("Code");
218 utf_Exceptions = utf_new_char("Exceptions");
219 utf_LineNumberTable = utf_new_char("LineNumberTable");
220 utf_SourceFile = utf_new_char("SourceFile");
222 utf_init = utf_new_char("<init>");
223 utf_clinit = utf_new_char("<clinit>");
224 utf_clone = utf_new_char("clone");
225 utf_finalize = utf_new_char("finalize");
226 utf_run = utf_new_char("run");
228 utf_add = utf_new_char("add");
229 utf_remove = utf_new_char("remove");
230 utf_put = utf_new_char("put");
231 utf_get = utf_new_char("get");
232 utf_value = utf_new_char("value");
234 utf_printStackTrace = utf_new_char("printStackTrace");
235 utf_fillInStackTrace = utf_new_char("fillInStackTrace");
236 utf_loadClass = utf_new_char("loadClass");
237 utf_getSystemClassLoader = utf_new_char("getSystemClassLoader");
239 utf_Z = utf_new_char("Z");
240 utf_B = utf_new_char("B");
241 utf_C = utf_new_char("C");
242 utf_S = utf_new_char("S");
243 utf_I = utf_new_char("I");
244 utf_J = utf_new_char("J");
245 utf_F = utf_new_char("F");
246 utf_D = utf_new_char("D");
248 utf_void__void = utf_new_char("()V");
249 utf_boolean__void = utf_new_char("(Z)V");
250 utf_byte__void = utf_new_char("(B)V");
251 utf_char__void = utf_new_char("(C)V");
252 utf_short__void = utf_new_char("(S)V");
253 utf_int__void = utf_new_char("(I)V");
254 utf_long__void = utf_new_char("(J)V");
255 utf_float__void = utf_new_char("(F)V");
256 utf_double__void = utf_new_char("(D)V");
257 utf_void__java_lang_Object = utf_new_char("()Ljava/lang/Object;");
258 utf_void__java_lang_Throwable = utf_new_char("()Ljava/lang/Throwable;");
260 utf_void__java_lang_ClassLoader =
261 utf_new_char("()Ljava/lang/ClassLoader;");
263 utf_java_lang_Object__java_lang_Object =
264 utf_new_char("(Ljava/lang/Object;)Ljava/lang/Object;");
266 utf_java_lang_String__void = utf_new_char("(Ljava/lang/String;)V");
268 utf_java_lang_String__java_lang_Class =
269 utf_new_char("(Ljava/lang/String;)Ljava/lang/Class;");
271 utf_java_lang_Throwable__void = utf_new_char("(Ljava/lang/Throwable;)V");
273 utf_not_named_yet = utf_new_char("\t<not_named_yet>");
275 array_packagename = utf_new_char("\t<the array package>");
279 /* utf_hashkey *****************************************************************
281 The hashkey is computed from the utf-text by using up to 8
282 characters. For utf-symbols longer than 15 characters 3 characters
283 are taken from the beginning and the end, 2 characters are taken
286 *******************************************************************************/
288 #define nbs(val) ((u4) *(++text) << val) /* get next byte, left shift by val */
289 #define fbs(val) ((u4) *( text) << val) /* get first byte, left shift by val */
291 u4 utf_hashkey(const char *text, u4 length)
293 const char *start_pos = text; /* pointer to utf text */
297 case 0: /* empty string */
300 case 1: return fbs(0);
301 case 2: return fbs(0) ^ nbs(3);
302 case 3: return fbs(0) ^ nbs(3) ^ nbs(5);
303 case 4: return fbs(0) ^ nbs(2) ^ nbs(4) ^ nbs(6);
304 case 5: return fbs(0) ^ nbs(2) ^ nbs(3) ^ nbs(4) ^ nbs(6);
305 case 6: return fbs(0) ^ nbs(1) ^ nbs(2) ^ nbs(3) ^ nbs(5) ^ nbs(6);
306 case 7: return fbs(0) ^ nbs(1) ^ nbs(2) ^ nbs(3) ^ nbs(4) ^ nbs(5) ^ nbs(6);
307 case 8: return fbs(0) ^ nbs(1) ^ nbs(2) ^ nbs(3) ^ nbs(4) ^ nbs(5) ^ nbs(6) ^ nbs(7);
314 return a ^ nbs(4) ^ nbs(5) ^ nbs(6) ^ nbs(7) ^ nbs(8);
323 return a ^ nbs(6) ^ nbs(7) ^ nbs(8) ^ nbs(9);
332 return a ^ nbs(6) ^ nbs(7) ^ nbs(8) ^ nbs(9) ^ nbs(10);
344 return a ^ nbs(9) ^ nbs(10);
356 return a ^ nbs(9) ^ nbs(10);
367 return a ^ nbs(9) ^ nbs(10) ^ nbs(11);
378 return a ^ nbs(9) ^ nbs(10) ^ nbs(11);
380 default: /* 3 characters from beginning */
386 /* 2 characters from middle */
387 text = start_pos + (length / 2);
392 /* 3 characters from end */
393 text = start_pos + length - 4;
398 return a ^ nbs(10) ^ nbs(11);
403 /* utf_hashkey *****************************************************************
405 Compute the hashkey of a unicode string.
407 *******************************************************************************/
409 u4 unicode_hashkey(u2 *text, u2 len)
411 return utf_hashkey((char *) text, len);
415 /* utf_new *********************************************************************
417 Creates a new utf-symbol, the text of the symbol is passed as a
418 u1-array. The function searches the utf-hashtable for a utf-symbol
419 with this text. On success the element returned, otherwise a new
420 hashtable element is created.
422 If the number of entries in the hashtable exceeds twice the size of
423 the hashtable slots a reorganization of the hashtable is done and
424 the utf symbols are copied to a new hashtable with doubled size.
426 *******************************************************************************/
428 utf *utf_new_intern(const char *text, u2 length);
430 utf *utf_new(const char *text, u2 length)
434 #if defined(USE_THREADS) && defined(NATIVE_THREADS)
438 r = utf_new_intern(text, length);
440 #if defined(USE_THREADS) && defined(NATIVE_THREADS)
448 utf *utf_new_intern(const char *text, u2 length)
450 u4 key; /* hashkey computed from utf-text */
451 u4 slot; /* slot in hashtable */
452 utf *u; /* hashtable element */
460 key = utf_hashkey(text, length);
461 slot = key & (utf_hash.size - 1);
462 u = utf_hash.ptr[slot];
464 /* search external hash chain for utf-symbol */
466 if (u->blength == length) {
468 /* compare text of hashtable elements */
469 for (i = 0; i < length; i++)
470 if (text[i] != u->text[i]) goto nomatch;
474 count_utf_new_found++;
477 /* symbol found in hashtable */
481 u = u->hashlink; /* next element in external chain */
486 count_utf_len += sizeof(utf) + length + 1;
489 /* location in hashtable found, create new utf element */
491 u->blength = length; /* length in bytes of utfstring */
492 u->hashlink = utf_hash.ptr[slot]; /* link in external hashchain */
493 u->text = mem_alloc(length + 1);/* allocate memory for utf-text */
494 memcpy(u->text, text, length); /* copy utf-text */
495 u->text[length] = '\0';
496 utf_hash.ptr[slot] = u; /* insert symbol into table */
498 utf_hash.entries++; /* update number of entries */
500 if (utf_hash.entries > (utf_hash.size * 2)) {
502 /* reorganization of hashtable, average length of
503 the external chains is approx. 2 */
507 hashtable newhash; /* the new hashtable */
509 /* create new hashtable, double the size */
510 init_hashtable(&newhash, utf_hash.size * 2);
511 newhash.entries = utf_hash.entries;
515 count_utf_len += sizeof(utf*) * utf_hash.size;
518 /* transfer elements to new hashtable */
519 for (i = 0; i < utf_hash.size; i++) {
520 u = (utf *) utf_hash.ptr[i];
522 utf *nextu = u->hashlink;
523 u4 slot = utf_hashkey(u->text, u->blength) & (newhash.size - 1);
525 u->hashlink = (utf *) newhash.ptr[slot];
526 newhash.ptr[slot] = u;
528 /* follow link in external hash chain */
533 /* dispose old table */
534 MFREE(utf_hash.ptr, void*, utf_hash.size);
542 /* utf_new_u2 ******************************************************************
544 Make utf symbol from u2 array, if isclassname is true '.' is
547 *******************************************************************************/
549 utf *utf_new_u2(u2 *unicode_pos, u4 unicode_length, bool isclassname)
551 char *buffer; /* memory buffer for unicode characters */
552 char *pos; /* pointer to current position in buffer */
553 u4 left; /* unicode characters left */
554 u4 buflength; /* utf length in bytes of the u2 array */
555 utf *result; /* resulting utf-string */
558 /* determine utf length in bytes and allocate memory */
560 buflength = u2_utflength(unicode_pos, unicode_length);
561 buffer = MNEW(char, buflength);
566 for (i = 0; i++ < unicode_length; unicode_pos++) {
567 /* next unicode character */
570 if ((c != 0) && (c < 0x80)) {
573 if ((int) left < 0) break;
574 /* convert classname */
575 if (isclassname && c == '.')
580 } else if (c < 0x800) {
582 unsigned char high = c >> 6;
583 unsigned char low = c & 0x3F;
585 if ((int) left < 0) break;
586 *pos++ = high | 0xC0;
592 char mid = (c >> 6) & 0x3F;
595 if ((int) left < 0) break;
596 *pos++ = high | 0xE0;
602 /* insert utf-string into symbol-table */
603 result = utf_new(buffer,buflength);
605 MFREE(buffer, char, buflength);
611 /* utf_new_char ****************************************************************
613 Creates a new utf symbol, the text for this symbol is passed as a
614 c-string ( = char* ).
616 *******************************************************************************/
618 utf *utf_new_char(const char *text)
620 return utf_new(text, strlen(text));
624 /* utf_new_char_classname ******************************************************
626 Creates a new utf symbol, the text for this symbol is passed as a
627 c-string ( = char* ) "." characters are going to be replaced by
628 "/". Since the above function is used often, this is a separte
629 function, instead of an if.
631 *******************************************************************************/
633 utf *utf_new_char_classname(const char *text)
635 if (strchr(text, '.')) {
636 char *txt = strdup(text);
637 char *end = txt + strlen(txt);
641 for (c = txt; c < end; c++)
642 if (*c == '.') *c = '/';
644 tmpRes = utf_new(txt, strlen(txt));
650 return utf_new(text, strlen(text));
654 /* utf_nextu2 ******************************************************************
656 Read the next unicode character from the utf string and increment
657 the utf-string pointer accordingly.
659 *******************************************************************************/
661 u2 utf_nextu2(char **utf_ptr)
663 /* uncompressed unicode character */
665 /* current position in utf text */
666 unsigned char *utf = (unsigned char *) (*utf_ptr);
667 /* bytes representing the unicode character */
668 unsigned char ch1, ch2, ch3;
669 /* number of bytes used to represent the unicode character */
672 switch ((ch1 = utf[0]) >> 4) {
673 default: /* 1 byte */
677 case 0xD: /* 2 bytes */
678 if (((ch2 = utf[1]) & 0xC0) == 0x80) {
679 unsigned char high = ch1 & 0x1F;
680 unsigned char low = ch2 & 0x3F;
681 unicode_char = (high << 6) + low;
686 case 0xE: /* 2 or 3 bytes */
687 if (((ch2 = utf[1]) & 0xC0) == 0x80) {
688 if (((ch3 = utf[2]) & 0xC0) == 0x80) {
689 unsigned char low = ch3 & 0x3f;
690 unsigned char mid = ch2 & 0x3f;
691 unsigned char high = ch1 & 0x0f;
692 unicode_char = (((high << 6) + mid) << 6) + low;
700 /* update position in utf-text */
701 *utf_ptr = (char *) (utf + len);
707 /* utf_strlen ******************************************************************
709 Determine number of unicode characters in the utf string.
711 *******************************************************************************/
713 u4 utf_strlen(utf *u)
715 char *endpos; /* points behind utf string */
716 char *utf_ptr; /* current position in utf text */
717 u4 len = 0; /* number of unicode characters */
720 *exceptionptr = new_nullpointerexception();
727 while (utf_ptr < endpos) {
729 /* next unicode character */
730 utf_nextu2(&utf_ptr);
733 if (utf_ptr != endpos)
734 /* string ended abruptly */
735 throw_cacao_exception_exit(string_java_lang_InternalError,
736 "Illegal utf8 string");
742 /* u2_utflength ****************************************************************
744 Returns the utf length in bytes of a u2 array.
746 *******************************************************************************/
748 u4 u2_utflength(u2 *text, u4 u2_length)
750 u4 result_len = 0; /* utf length in bytes */
751 u2 ch; /* current unicode character */
754 for (len = 0; len < u2_length; len++) {
755 /* next unicode character */
758 /* determine bytes required to store unicode character as utf */
759 if (ch && (ch < 0x80))
771 /* utf_display *****************************************************************
773 Write utf symbol to stdout (for debugging purposes).
775 *******************************************************************************/
777 void utf_display(utf *u)
779 char *endpos; /* points behind utf string */
780 char *utf_ptr; /* current position in utf text */
791 while (utf_ptr < endpos) {
792 /* read next unicode character */
793 u2 c = utf_nextu2(&utf_ptr);
794 if (c >= 32 && c <= 127) printf("%c", c);
802 /* utf_display_classname *******************************************************
804 Write utf symbol to stdout with `/' converted to `.' (for debugging
807 *******************************************************************************/
809 void utf_display_classname(utf *u)
811 char *endpos; /* points behind utf string */
812 char *utf_ptr; /* current position in utf text */
823 while (utf_ptr < endpos) {
824 /* read next unicode character */
825 u2 c = utf_nextu2(&utf_ptr);
826 if (c == '/') c = '.';
827 if (c >= 32 && c <= 127) printf("%c", c);
835 /* utf_sprint ******************************************************************
837 Write utf symbol into c-string (for debugging purposes).
839 *******************************************************************************/
841 void utf_sprint(char *buffer, utf *u)
843 char *endpos; /* points behind utf string */
844 char *utf_ptr; /* current position in utf text */
845 u2 pos = 0; /* position in c-string */
848 strcpy(buffer, "NULL");
855 while (utf_ptr < endpos)
856 /* copy next unicode character */
857 buffer[pos++] = utf_nextu2(&utf_ptr);
859 /* terminate string */
864 /* utf_sprint_classname ********************************************************
866 Write utf symbol into c-string with `/' converted to `.' (for debugging
869 *******************************************************************************/
871 void utf_sprint_classname(char *buffer, utf *u)
873 char *endpos; /* points behind utf string */
874 char *utf_ptr; /* current position in utf text */
875 u2 pos = 0; /* position in c-string */
878 strcpy(buffer, "NULL");
885 while (utf_ptr < endpos) {
886 /* copy next unicode character */
887 u2 c = utf_nextu2(&utf_ptr);
888 if (c == '/') c = '.';
892 /* terminate string */
897 /* utf_strcat ******************************************************************
899 Like libc strcat, but uses an utf8 string.
901 *******************************************************************************/
903 void utf_strcat(char *buffer, utf *u)
905 utf_sprint(buffer + strlen(buffer), u);
909 /* utf_strcat_classname ********************************************************
911 Like libc strcat, but uses an utf8 string.
913 *******************************************************************************/
915 void utf_strcat_classname(char *buffer, utf *u)
917 utf_sprint_classname(buffer + strlen(buffer), u);
921 /* utf_fprint ******************************************************************
923 Write utf symbol into file.
925 *******************************************************************************/
927 void utf_fprint(FILE *file, utf *u)
929 char *endpos; /* points behind utf string */
930 char *utf_ptr; /* current position in utf text */
938 while (utf_ptr < endpos) {
939 /* read next unicode character */
940 u2 c = utf_nextu2(&utf_ptr);
942 if (c >= 32 && c <= 127) fprintf(file, "%c", c);
943 else fprintf(file, "?");
948 /* utf_fprint_classname ********************************************************
950 Write utf symbol into file with `/' converted to `.'.
952 *******************************************************************************/
954 void utf_fprint_classname(FILE *file, utf *u)
956 char *endpos; /* points behind utf string */
957 char *utf_ptr; /* current position in utf text */
965 while (utf_ptr < endpos) {
966 /* read next unicode character */
967 u2 c = utf_nextu2(&utf_ptr);
968 if (c == '/') c = '.';
970 if (c >= 32 && c <= 127) fprintf(file, "%c", c);
971 else fprintf(file, "?");
976 /* is_valid_utf ****************************************************************
978 Return true if the given string is a valid UTF-8 string.
980 utf_ptr...points to first character
981 end_pos...points after last character
983 *******************************************************************************/
985 /* static unsigned long min_codepoint[6] = {0,1L<<7,1L<<11,1L<<16,1L<<21,1L<<26}; */
987 bool is_valid_utf(char *utf_ptr, char *end_pos)
994 if (end_pos < utf_ptr) return false;
995 bytes = end_pos - utf_ptr;
999 if (!c) return false; /* 0x00 is not allowed */
1000 if ((c & 0x80) == 0) continue; /* ASCII */
1002 if ((c & 0xe0) == 0xc0) len = 1; /* 110x xxxx */
1003 else if ((c & 0xf0) == 0xe0) len = 2; /* 1110 xxxx */
1004 else if ((c & 0xf8) == 0xf0) len = 3; /* 1111 0xxx */
1005 else if ((c & 0xfc) == 0xf8) len = 4; /* 1111 10xx */
1006 else if ((c & 0xfe) == 0xfc) len = 5; /* 1111 110x */
1007 else return false; /* invalid leading byte */
1009 if (len > 2) return false; /* Java limitation */
1011 v = (unsigned long)c & (0x3f >> len);
1013 if ((bytes -= len) < 0) return false; /* missing bytes */
1015 for (i = len; i--; ) {
1017 if ((c & 0xc0) != 0x80) /* 10xx xxxx */
1019 v = (v << 6) | (c & 0x3f);
1023 if (len != 1) return false; /* Java special */
1026 /* Sun Java seems to allow overlong UTF-8 encodings */
1028 /* if (v < min_codepoint[len]) */
1029 /* XXX throw exception? */
1032 /* surrogates in UTF-8 seem to be allowed in Java classfiles */
1033 /* if (v >= 0xd800 && v <= 0xdfff) return false; */ /* surrogates */
1035 /* even these seem to be allowed */
1036 /* if (v == 0xfffe || v == 0xffff) return false; */ /* invalid codepoints */
1043 /* is_valid_name ***************************************************************
1045 Return true if the given string may be used as a class/field/method
1046 name. (Currently this only disallows empty strings and control
1049 NOTE: The string is assumed to have passed is_valid_utf!
1051 utf_ptr...points to first character
1052 end_pos...points after last character
1054 *******************************************************************************/
1056 bool is_valid_name(char *utf_ptr, char *end_pos)
1058 if (end_pos <= utf_ptr) return false; /* disallow empty names */
1060 while (utf_ptr < end_pos) {
1061 unsigned char c = *utf_ptr++;
1063 if (c < 0x20) return false; /* disallow control characters */
1064 if (c == 0xc0 && (unsigned char) *utf_ptr == 0x80) /* disallow zero */
1071 bool is_valid_name_utf(utf *u)
1073 return is_valid_name(u->text, UTF_END(u));
1077 /* utf_show ********************************************************************
1079 Writes the utf symbols in the utfhash to stdout and displays the
1080 number of external hash chains grouped according to the chainlength
1081 (for debugging purposes).
1083 *******************************************************************************/
1088 #define CHAIN_LIMIT 20 /* limit for seperated enumeration */
1090 u4 chain_count[CHAIN_LIMIT]; /* numbers of chains */
1091 u4 max_chainlength = 0; /* maximum length of the chains */
1092 u4 sum_chainlength = 0; /* sum of the chainlengths */
1093 u4 beyond_limit = 0; /* number of utf-symbols in chains with length>=CHAIN_LIMIT-1 */
1096 printf ("UTF-HASH:\n");
1098 /* show element of utf-hashtable */
1099 for (i=0; i<utf_hash.size; i++) {
1100 utf *u = utf_hash.ptr[i];
1102 printf ("SLOT %d: ", (int) i);
1114 printf ("UTF-HASH: %d slots for %d entries\n",
1115 (int) utf_hash.size, (int) utf_hash.entries );
1118 if (utf_hash.entries == 0)
1121 printf("chains:\n chainlength number of chains %% of utfstrings\n");
1123 for (i=0;i<CHAIN_LIMIT;i++)
1126 /* count numbers of hashchains according to their length */
1127 for (i=0; i<utf_hash.size; i++) {
1129 utf *u = (utf*) utf_hash.ptr[i];
1130 u4 chain_length = 0;
1132 /* determine chainlength */
1138 /* update sum of all chainlengths */
1139 sum_chainlength+=chain_length;
1141 /* determine the maximum length of the chains */
1142 if (chain_length>max_chainlength)
1143 max_chainlength = chain_length;
1145 /* update number of utf-symbols in chains with length>=CHAIN_LIMIT-1 */
1146 if (chain_length>=CHAIN_LIMIT) {
1147 beyond_limit+=chain_length;
1148 chain_length=CHAIN_LIMIT-1;
1151 /* update number of hashchains of current length */
1152 chain_count[chain_length]++;
1155 /* display results */
1156 for (i=1;i<CHAIN_LIMIT-1;i++)
1157 printf(" %2d %17d %18.2f%%\n",i,chain_count[i],(((float) chain_count[i]*i*100)/utf_hash.entries));
1159 printf(" >=%2d %17d %18.2f%%\n",CHAIN_LIMIT-1,chain_count[CHAIN_LIMIT-1],((float) beyond_limit*100)/utf_hash.entries);
1162 printf("max. chainlength:%5d\n",max_chainlength);
1164 /* avg. chainlength = sum of chainlengths / number of chains */
1165 printf("avg. chainlength:%5.2f\n",(float) sum_chainlength / (utf_hash.size-chain_count[0]));
1170 * These are local overrides for various environment variables in Emacs.
1171 * Please do not remove this and leave it at the end of the file, where
1172 * Emacs will automagically detect them.
1173 * ---------------------------------------------------------------------
1176 * indent-tabs-mode: t