Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/make/src/classes/build/tools/charsetmapping/SBCS.java
32287 views
/*1* Copyright (c) 2008, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package build.tools.charsetmapping;2627import java.io.*;28import java.util.Arrays;29import java.util.ArrayList;30import java.util.Scanner;31import java.util.Formatter;32import java.util.regex.*;33import java.nio.charset.*;34import static build.tools.charsetmapping.Utils.*;3536public class SBCS {3738public static void genClass(String args[]) throws Exception {3940Scanner s = new Scanner(new File(args[0], args[2]));41while (s.hasNextLine()) {42String line = s.nextLine();43if (line.startsWith("#") || line.length() == 0)44continue;45String[] fields = line.split("\\s+");46if (fields.length < 5) {47System.err.println("Misconfiged sbcs line <" + line + ">?");48continue;49}50String clzName = fields[0];51String csName = fields[1];52String hisName = fields[2];53boolean isASCII = Boolean.valueOf(fields[3]);54String pkgName = fields[4];55System.out.printf("%s,%s,%s,%b,%s%n", clzName, csName, hisName, isASCII, pkgName);5657genClass0(args[0], args[1], "SingleByte-X.java.template",58clzName, csName, hisName, pkgName, isASCII);59}60}6162private static void toString(char[] sb, int off, int end,63Formatter out, String closure,64boolean comment) {65while (off < end) {66out.format(" \"");67for (int j = 0; j < 8; j++) {68if (off == end)69break;70char c = sb[off++];71switch (c) {72case '\b':73out.format("\\b"); break;74case '\t':75out.format("\\t"); break;76case '\n':77out.format("\\n"); break;78case '\f':79out.format("\\f"); break;80case '\r':81out.format("\\r"); break;82case '\"':83out.format("\\\""); break;84case '\'':85out.format("\\'"); break;86case '\\':87out.format("\\\\"); break;88default:89out.format("\\u%04X", c & 0xffff);90}91}92if (comment) {93if (off == end)94out.format("\" %s // 0x%02x - 0x%02x%n",95closure, off-8, off-1);96else97out.format("\" + // 0x%02x - 0x%02x%n",98off-8, off-1);99} else {100if (off == end)101out.format("\"%s%n", closure);102else103out.format("\" +%n");104}105}106}107108static Pattern sbmap = Pattern.compile("0x(\\p{XDigit}++)\\s++(?:U\\+|0x)?(\\p{XDigit}++)(?:\\s++#.*)?");109110private static void genClass0(String srcDir, String dstDir,111String template,112String clzName,113String csName,114String hisName,115String pkgName,116boolean isASCII)117throws Exception118{119StringBuilder b2cSB = new StringBuilder();120StringBuilder b2cNRSB = new StringBuilder();121StringBuilder c2bNRSB = new StringBuilder();122123char[] sb = new char[0x100];124char[] c2bIndex = new char[0x100];125int c2bOff = 0;126Arrays.fill(sb, UNMAPPABLE_DECODING);127Arrays.fill(c2bIndex, UNMAPPABLE_DECODING);128129// (1)read in .map to parse all b->c entries130FileInputStream in = new FileInputStream(131new File(srcDir, clzName + ".map"));132Parser p = new Parser(in, sbmap);133Entry e = null;134135while ((e = p.next()) != null) {136sb[e.bs] = (char)e.cp;137if (c2bIndex[e.cp>>8] == UNMAPPABLE_DECODING) {138c2bOff += 0x100;139c2bIndex[e.cp>>8] = 1;140}141}142143Formatter fm = new Formatter(b2cSB);144fm.format("%n");145146// vm -server shows cc[byte + 128] access is much faster than147// cc[byte&0xff] so we output the upper segment first148toString(sb, 0x80, 0x100, fm, "+", true);149toString(sb, 0x00, 0x80, fm, ";", true);150fm.close();151152// (2)now the .nr file which includes "b->c" non-roundtrip entries153File f = new File(srcDir, clzName + ".nr");154if (f.exists()) {155in = new FileInputStream(f);156fm = new Formatter(b2cNRSB);157p = new Parser(in, sbmap);158e = null;159160fm.format("// remove non-roundtrip entries%n");161fm.format(" b2cMap = b2cTable.toCharArray();%n");162while ((e = p.next()) != null) {163fm.format(" b2cMap[%d] = UNMAPPABLE_DECODING;%n",164(e.bs>=0x80)?(e.bs-0x80):(e.bs+0x80));165}166fm.close();167}168169// (3)finally the .c2b file which includes c->b non-roundtrip entries170f = new File(srcDir, clzName + ".c2b");171if (f.exists()) {172in = new FileInputStream(f);173fm = new Formatter(c2bNRSB);174p = new Parser(in, sbmap);175e = null;176ArrayList<Entry> es = new ArrayList<Entry>();177while ((e = p.next()) != null) {178if (c2bIndex[e.cp>>8] == UNMAPPABLE_DECODING) {179c2bOff += 0x100;180c2bIndex[e.cp>>8] = 1;181}182es.add(e);183}184fm.format("// non-roundtrip c2b only entries%n");185if (es.size() < 100) {186fm.format(" c2bNR = new char[%d];%n", es.size() * 2);187int i = 0;188for (Entry entry: es) {189fm.format(" c2bNR[%d] = 0x%x; c2bNR[%d] = 0x%x;%n",190i++, entry.bs, i++, entry.cp);191}192} else {193char[] cc = new char[es.size() * 2];194int i = 0;195for (Entry entry: es) {196cc[i++] = (char)entry.bs;197cc[i++] = (char)entry.cp;198}199fm.format(" c2bNR = (%n");200toString(cc, 0, i, fm, ").toCharArray();", false);201}202fm.close();203}204205// (4)it's time to generate the source file206String b2c = b2cSB.toString();207String b2cNR = b2cNRSB.toString();208String c2bNR = c2bNRSB.toString();209210Scanner s = new Scanner(new File(srcDir, template));211PrintStream out = new PrintStream(new FileOutputStream(212new File(dstDir, clzName + ".java")));213214while (s.hasNextLine()) {215String line = s.nextLine();216int i = line.indexOf("$");217if (i == -1) {218out.println(line);219continue;220}221if (line.indexOf("$PACKAGE$", i) != -1) {222line = line.replace("$PACKAGE$", pkgName);223}224if (line.indexOf("$NAME_CLZ$", i) != -1) {225line = line.replace("$NAME_CLZ$", clzName);226}227if (line.indexOf("$NAME_CS$", i) != -1) {228line = line.replace("$NAME_CS$", csName);229}230if (line.indexOf("$NAME_ALIASES$", i) != -1) {231if ("sun.nio.cs".equals(pkgName))232line = line.replace("$NAME_ALIASES$",233"StandardCharsets.aliases_" + clzName);234else235line = line.replace("$NAME_ALIASES$",236"ExtendedCharsets.aliasesFor(\"" + csName + "\")");237}238if (line.indexOf("$NAME_HIS$", i) != -1) {239line = line.replace("$NAME_HIS$", hisName);240}241if (line.indexOf("$CONTAINS$", i) != -1) {242if (isASCII)243line = " return ((cs.name().equals(\"US-ASCII\")) || (cs instanceof " + clzName + "));";244else245line = " return (cs instanceof " + clzName + ");";246}247if (line.indexOf("$B2CTABLE$") != -1) {248line = line.replace("$B2CTABLE$", b2c);249}250if (line.indexOf("$C2BLENGTH$") != -1) {251line = line.replace("$C2BLENGTH$", "0x" + Integer.toString(c2bOff, 16));252}253if (line.indexOf("$NONROUNDTRIP_B2C$") != -1) {254if (b2cNR.length() == 0)255continue;256line = line.replace("$NONROUNDTRIP_B2C$", b2cNR);257}258259if (line.indexOf("$NONROUNDTRIP_C2B$") != -1) {260if (c2bNR.length() == 0)261continue;262line = line.replace("$NONROUNDTRIP_C2B$", c2bNR);263}264out.println(line);265}266out.close();267}268}269270271