Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: ca8ae6c1046c7690d09c8905358f345d119cf1fc (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/*******************************************************************************
 * Copyright (c) 2019 IBM Corporation and others.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * This is an implementation of an early-draft specification developed under the Java
 * Community Process (JCP) and is made available for testing and evaluation purposes
 * only. The code is not compatible with any specification of the JCP.
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.jdt.core.internal.tools.unicode;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Collections;

public class CodePointsBuilder {

	public static Integer[] build(String[] codePointTable, Environment environment) {
		ArrayList<Integer> values = new ArrayList<>();
		for (String codePointTableEntry : codePointTable) {
			if (codePointTableEntry.length() != 0) {
				int indexOfDots = codePointTableEntry.indexOf(".."); //$NON-NLS-1$
				if (indexOfDots == -1) {
					// single value on the line
					try {
						values.add(Integer.parseInt(codePointTableEntry, 16));
					} catch (NumberFormatException e) {
						System.err.println("NumberFormatException processing : " + codePointTableEntry); //$NON-NLS-1$
						return null;
					}
				} else {
					// range of values
					try {
						int firstValue = Integer.parseInt(codePointTableEntry.substring(0, indexOfDots), 16);
						int secondValue = Integer.parseInt(codePointTableEntry.substring(indexOfDots + 2), 16);
						for (int i = firstValue; i <= secondValue; i++) {
							values.add(i);
						}
					} catch (NumberFormatException e) {
						System.err.println("NumberFormatException processing : " + codePointTableEntry); //$NON-NLS-1$
						return null;
					}
				}
			}
		}
		Collections.sort(values);
		printDistribution(values, 0x10000);
		return values.toArray(new Integer[values.size()]);
	}

	private static void printDistribution(ArrayList<Integer> array, int increment) {
		int bound = increment;
		int counter = 0;
		int totalCounter = 0;
		int length = array.size();
		int max = array.get(length - 1).intValue();
		int numberOfFiguresForRange = (int) (Math.log(max) / Math.log(10));
		if ((max % increment) == 0) {
			numberOfFiguresForRange = (int) (Math.log(max + 1) / Math.log(10));
		}
		int numberOfFiguresForCounter = (int) (Math.log(length) / Math.log(10));
		if ((length % increment) == 0) {
			numberOfFiguresForCounter = (int) (Math.log(length + 1) / Math.log(10));
		}
		for (int i = 0; i < length; i++) {
			if (array.get(i).intValue() < bound) {
				counter++;
			} else {
				i--;
				totalCounter += counter;
				printRange(counter, bound, increment, totalCounter, length, numberOfFiguresForRange,
						numberOfFiguresForCounter);
				counter = 0;
				bound += increment;
			}
		}
		totalCounter += counter;
		printRange(counter, bound, increment, totalCounter, length, numberOfFiguresForRange, numberOfFiguresForCounter);
	}

	private static void printRange(int counter, int bound, int increment, int totalCounter, int length,
			int numberOfFiguresForRange, int numberOfFiguresForCounters) {
		if (counter != 0) {
			StringBuffer buffer = new StringBuffer();
			int low = bound - increment;
			if (low != 0) {
				low++;
			}
			DecimalFormat format = new DecimalFormat("###.##"); //$NON-NLS-1$
			buffer.append(display(low, numberOfFiguresForRange, 16)).append(" - ") //$NON-NLS-1$
					.append(display(bound, numberOfFiguresForRange, 16)).append(" : ") //$NON-NLS-1$
					.append(display(counter, numberOfFiguresForCounters, 10)).append("\t") //$NON-NLS-1$
					.append((low & 0x1F0000) >> 16).append("\t\t") //$NON-NLS-1$
					.append(format.format(100.0 * ((double) totalCounter / length)));
			System.out.println(String.valueOf(buffer));
		}
	}

	private static String display(int value, int numberOfFiguresForRange, int radix) {
		int numberOfFigures = value == 0 ? 1 : (int) (Math.log(value) / Math.log(10));
		if ((value % 10) == 0) {
			numberOfFigures = (int) (Math.log(value + 1) / Math.log(10));
		}
		StringBuffer buffer = new StringBuffer();
		switch (radix) {
		case 10:
			while (numberOfFigures < numberOfFiguresForRange) {
				buffer.append(" "); //$NON-NLS-1$
				numberOfFigures++;
			}
			buffer.append(value);
			break;
		case 16:
			buffer.append("0x" + Integer.toHexString(value)); //$NON-NLS-1$
		}
		return String.valueOf(buffer);
	}
}

Back to the top