• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* GENERATED SOURCE. DO NOT MODIFY. */
2 // © 2016 and later: Unicode, Inc. and others.
3 // License & terms of use: http://www.unicode.org/copyright.html#License
4 /*
5  *******************************************************************************
6  * Copyright (C) 2004-2009, International Business Machines Corporation and    *
7  * others. All Rights Reserved.                                                *
8  *******************************************************************************
9  */
10 
11 package ohos.global.icu.text;
12 
13 /**
14  * A post-processor for Chinese text.
15  */
16 final class RBNFChinesePostProcessor implements RBNFPostProcessor {
17     //private NFRuleSet lastRuleSet;
18     private boolean longForm;
19     private int format;
20 
21     private static final String[] rulesetNames = {
22         "%traditional", "%simplified", "%accounting", "%time"
23     };
24 
25     /**
26      * Initialization routine for this instance, called once
27      * immediately after first construction and never again.
28      */
init(RuleBasedNumberFormat formatter, String rules)29     public void init(RuleBasedNumberFormat formatter, String rules) {
30     }
31 
32     /**
33      * Work routine.  Post process the output, which was generated by the
34      * ruleset with the given name.
35      */
process(StringBuilder buf, NFRuleSet ruleSet)36     public void process(StringBuilder buf, NFRuleSet ruleSet) {
37         // markers depend on what rule set we are using
38 
39         // Commented by johnvu on the if statement since lastRuleSet is never initialized
40         //if (ruleSet != lastRuleSet) {
41             String name = ruleSet.getName();
42             for (int i = 0; i < rulesetNames.length; ++i) {
43                 if (rulesetNames[i].equals(name)) {
44                     format = i;
45                     longForm = i == 1 || i == 3;
46                     break;
47                 }
48             }
49         //}
50 
51         if (longForm) {
52             for (int i = buf.indexOf("*"); i != -1; i = buf.indexOf("*", i)) {
53                 buf.delete(i, i+1);
54             }
55             return;
56         }
57 
58         final String DIAN = "\u9ede"; // decimal point
59 
60         final String[][] markers = {
61             { "\u842c", "\u5104", "\u5146", "\u3007" }, // marker chars, last char is the 'zero'
62             { "\u4e07", "\u4ebf", "\u5146", "\u3007" },
63             { "\u842c", "\u5104", "\u5146", "\u96f6" }
64             // need markers for time?
65         };
66 
67         // remove unwanted lings
68         // a '0' (ling) with * might be removed
69         // mark off 10,000 'chunks', markers are Z, Y, W (zhao, yii, and wan)
70         // already, we avoid two lings in the same chunk -- ling without * wins
71         // now, just need  to avoid optional lings in adjacent chunks
72         // process right to left
73 
74         // decision matrix:
75         // state, situation
76         //     state         none       opt.          req.
77         //     -----         ----       ----          ----
78         // none to right     none       opt.          req.
79         // opt. to right     none   clear, none  clear right, req.
80         // req. to right     none   clear, none       req.
81 
82         // mark chunks with '|' for convenience
83         {
84             String[] m = markers[format];
85             for (int i = 0; i < m.length-1; ++i) {
86                 int n = buf.indexOf(m[i]);
87                 if (n != -1) {
88                     buf.insert(n+m[i].length(), '|');
89                 }
90             }
91         }
92 
93         int x = buf.indexOf(DIAN);
94         if (x == -1) {
95             x = buf.length();
96         }
97         int s = 0; // 0 = none to right, 1 = opt. to right, 2 = req. to right
98         int n = -1; // previous optional ling
99         String ling = markers[format][3];
100         while (x >= 0) {
101             int m = buf.lastIndexOf("|", x);
102             int nn = buf.lastIndexOf(ling, x);
103             int ns = 0;
104             if (nn > m) {
105                 ns = (nn > 0 && buf.charAt(nn-1) != '*') ? 2 : 1;
106             }
107             x = m - 1;
108 
109             // actually much simpler, but leave this verbose for now so it's easier to follow
110             switch (s*3+ns) {
111             case 0: /* none, none */
112                 s = ns; // redundant
113                 n = -1;
114                 break;
115             case 1: /* none, opt. */
116                 s = ns;
117                 n = nn; // remember optional ling to right
118                 break;
119             case 2: /* none, req. */
120                 s = ns;
121                 n = -1;
122                 break;
123             case 3: /* opt., none */
124                 s = ns;
125                 n = -1;
126                 break;
127             case 4: /* opt., opt. */
128                 buf.delete(nn-1, nn+ling.length()); // delete current optional ling
129                 s = 0;
130                 n = -1;
131                 break;
132             case 5: /* opt., req. */
133                 buf.delete(n-1, n+ling.length()); // delete previous optional ling
134                 s = ns;
135                 n = -1;
136                 break;
137             case 6: /* req., none */
138                 s = ns;
139                 n = -1;
140                 break;
141             case 7: /* req., opt. */
142                 buf.delete(nn-1, nn+ling.length()); // delete current optional ling
143                 s = 0;
144                 n = -1;
145                 break;
146             case 8: /* req., req. */
147                 s = ns;
148                 n = -1;
149                 break;
150             default:
151                 throw new IllegalStateException();
152             }
153         }
154 
155         for (int i = buf.length(); --i >= 0;) {
156             char c = buf.charAt(i);
157             if (c == '*' || c == '|') {
158                 buf.delete(i, i+1);
159             }
160         }
161     }
162 }
163