1 19 20 package org.netbeans.modules.lexer.gen.util; 21 22 29 30 public class UnicodeRanges { 31 32 public static final int IS_JAVA_IDENTIFIER_START = 1; 33 34 public static final int IS_JAVA_IDENTIFIER_PART = 2; 35 36 public static char[] findRanges(int testedMethod) { 37 char[] ranges = new char[16]; int rangesCount = 0; 39 int rangeStart = -1; 40 for (int i = 0; i < 65536; i++) { 41 boolean valid = false; 42 switch (testedMethod) { 43 case IS_JAVA_IDENTIFIER_START: 44 valid = Character.isJavaIdentifierStart((char)i); 45 break; 46 47 case IS_JAVA_IDENTIFIER_PART: 48 valid = Character.isJavaIdentifierPart((char)i); 49 break; 50 51 } 52 53 if (i == 65535 && valid) { 55 if (rangeStart < 0) { 56 rangeStart = i; 57 } 58 i++; 59 valid = false; 60 } 61 62 if (valid) { 63 if (rangeStart < 0) { 64 rangeStart = i; 65 } 66 67 } else { if (rangeStart >= 0) { 69 if (ranges.length - rangesCount < 2) { 71 char[] tmp = new char[ranges.length * 2]; 72 System.arraycopy(ranges, 0, tmp, 0, rangesCount); 73 ranges = tmp; 74 } 75 ranges[rangesCount++] = (char)rangeStart; 76 ranges[rangesCount++] = (char)(i - 1); 77 78 rangeStart = -1; 79 } 80 } 81 } 82 83 if (rangesCount < ranges.length) { 84 char[] tmp = new char[rangesCount]; 85 System.arraycopy(ranges, 0, tmp, 0, rangesCount); 86 ranges = tmp; 87 } 88 return ranges; 89 } 90 91 public static void appendUnicodeChar(StringBuffer sb, char ch, char quoteChar) { 92 String ret = Integer.toHexString(ch); 93 while (ret.length() < 4) { 94 ret = "0" + ret; 95 } 96 sb.append(quoteChar); 97 sb.append("\\u"); 98 sb.append(ret); 99 sb.append(quoteChar); 100 } 101 102 public static void indent(StringBuffer sb, int indent) { 103 while (indent-- > 0) { 104 sb.append(' '); 105 } 106 } 107 108 protected static String usage() { 109 return "Prints ranges of characters belonging to selected category\n" 110 + "arg0=Tested method:\n" 111 + " 1 - Character.isJavaIdentifierStart()\n" 112 + " 2 - Character.isJavaIdentifierPart()\n" 113 + "arg1=Indentation e.g. 8\n"; 114 } 115 116 } 117 | Popular Tags |