Debian Codegraph @

net/sf/saxon/expr/number/IrregularGroupFormatter.java

raw symbols SCIP
1 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2 // Copyright (c) 2018 Saxonica Limited.
3 // This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
4 // If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
5 // This Source Code Form is "Incompatible With Secondary Licenses", as defined by the Mozilla Public License, v. 2.0.
6 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
7
8 package net.sf.saxon.expr.number;
9
10 import net.sf.saxon.regex.UnicodeString;
11 import net.sf.saxon.tree.util.FastStringBuffer;
12 import net.sf.saxon.z.IntSet;
13
14 import java.util.List;
15
16 /**
17 * Handles grouping separators when formatting a number in cases where the grouping separators are
18 * not at regular intervals
19 */
20
21 public class IrregularGroupFormatter extends NumericGroupFormatter {
22
23 /*@Nullable*/ private IntSet groupingPositions = null;
24 private List<Integer> separators = null;
25
26 /**
27 * Create a formatter for numbers where the grouping separators occur at irregular positions
28 *
29 * @param groupingPositions the positions where the separators are to be inserted
30 * @param sep array holding the separators to be inserted, as Unicode codepoints, in order starting
31 * with the right-most
32 * @param adjustedPicture
33 */
34
35 public IrregularGroupFormatter(IntSet groupingPositions, List<Integer> sep, UnicodeString adjustedPicture) {
36 this.groupingPositions = groupingPositions;
37 separators = sep;
38 this.adjustedPicture = adjustedPicture;
39 }
40
41 @Override
42 public String format(FastStringBuffer value) {
43 UnicodeString in = UnicodeString.makeUnicodeString(value);
44 int l, m = 0;
45 for (l = 0; l < in.uLength(); l++) {
46 if (groupingPositions.contains(l)) {
47 m++;
48 }
49 }
50 int[] out = new int[in.uLength() + m];
51 int j = 0;
52 int k = out.length - 1;
53 for (int i = in.uLength() - 1; i >= 0; i--) {
54 out[k--] = in.uCharAt(i);
55 if ((i > 0) && groupingPositions.contains(in.uLength() - i)) {
56 out[k--] = separators.get(j++);
57 }
58 }
59 return UnicodeString.makeUnicodeString(out).toString();
60 }
61
62 /**
63 * Get the grouping separator to be used. If more than one is used, return the last.
64 * If no grouping separators are used, return null
65 *
66 * @return the grouping separator
67 */
68 @Override
69 public String getSeparator() {
70 if (separators.size() == 0) {
71 return null;
72 } else {
73 int sep = separators.get(separators.size() - 1);
74 FastStringBuffer fsb = new FastStringBuffer(FastStringBuffer.C16);
75 fsb.appendWideChar(sep);
76 return fsb.toString();
77 }
78 }
79 }
80