1 | var zero = '0'.charCodeAt(0);
|
2 | var plus = '+'.charCodeAt(0);
|
3 | var minus = '-'.charCodeAt(0);
|
4 |
|
5 | function isWhitespace(code) {
|
6 | return code <= 32;
|
7 | }
|
8 |
|
9 | function isDigit(code) {
|
10 | return 48 <= code && code <= 57;
|
11 | }
|
12 |
|
13 | function isSign(code) {
|
14 | return code === minus || code === plus;
|
15 | }
|
16 |
|
17 | module.exports = function (opts, a, b) {
|
18 | var checkSign = opts.sign;
|
19 | var ia = 0;
|
20 | var ib = 0;
|
21 | var ma = a.length;
|
22 | var mb = b.length;
|
23 | var ca, cb;
|
24 | var za, zb;
|
25 | var na, nb;
|
26 | var sa, sb;
|
27 | var ta, tb;
|
28 | var bias;
|
29 |
|
30 | while (ia < ma && ib < mb) {
|
31 | ca = a.charCodeAt(ia);
|
32 | cb = b.charCodeAt(ib);
|
33 | za = zb = 0;
|
34 | na = nb = 0;
|
35 | sa = sb = true;
|
36 | bias = 0;
|
37 |
|
38 |
|
39 | while (isWhitespace(ca)) {
|
40 | ia += 1;
|
41 | ca = a.charCodeAt(ia);
|
42 | }
|
43 | while (isWhitespace(cb)) {
|
44 | ib += 1;
|
45 | cb = b.charCodeAt(ib);
|
46 | }
|
47 |
|
48 |
|
49 | if (checkSign) {
|
50 | ta = a.charCodeAt(ia + 1);
|
51 | if (isSign(ca) && isDigit(ta)) {
|
52 | if (ca === minus) {
|
53 | sa = false;
|
54 | }
|
55 | ia += 1;
|
56 | ca = ta;
|
57 | }
|
58 | tb = b.charCodeAt(ib + 1);
|
59 | if (isSign(cb) && isDigit(tb)) {
|
60 | if (cb === minus) {
|
61 | sb = false;
|
62 | }
|
63 | ib += 1;
|
64 | cb = tb;
|
65 | }
|
66 | }
|
67 |
|
68 |
|
69 | if (isDigit(ca) && !isDigit(cb)) {
|
70 | return -1;
|
71 | }
|
72 | if (!isDigit(ca) && isDigit(cb)) {
|
73 | return 1;
|
74 | }
|
75 |
|
76 |
|
77 | if (!sa && sb) {
|
78 | return -1;
|
79 | }
|
80 | if (sa && !sb) {
|
81 | return 1;
|
82 | }
|
83 |
|
84 |
|
85 | while (ca === zero) {
|
86 | za += 1;
|
87 | ia += 1;
|
88 | ca = a.charCodeAt(ia);
|
89 | }
|
90 | while (cb === zero) {
|
91 | zb += 1;
|
92 | ib += 1;
|
93 | cb = b.charCodeAt(ib);
|
94 | }
|
95 |
|
96 |
|
97 | while (isDigit(ca) || isDigit(cb)) {
|
98 | if (isDigit(ca) && isDigit(cb) && bias === 0) {
|
99 | if (sa) {
|
100 | if (ca < cb) {
|
101 | bias = -1;
|
102 | } else if (ca > cb) {
|
103 | bias = 1;
|
104 | }
|
105 | } else {
|
106 | if (ca > cb) {
|
107 | bias = -1;
|
108 | } else if (ca < cb) {
|
109 | bias = 1;
|
110 | }
|
111 | }
|
112 | }
|
113 | if (isDigit(ca)) {
|
114 | ia += 1;
|
115 | na += 1;
|
116 | ca = a.charCodeAt(ia);
|
117 | }
|
118 | if (isDigit(cb)) {
|
119 | ib += 1;
|
120 | nb += 1;
|
121 | cb = b.charCodeAt(ib);
|
122 | }
|
123 | }
|
124 |
|
125 |
|
126 | if (sa) {
|
127 | if (na < nb) {
|
128 | return -1;
|
129 | }
|
130 | if (na > nb) {
|
131 | return 1;
|
132 | }
|
133 | } else {
|
134 | if (na > nb) {
|
135 | return -1;
|
136 | }
|
137 | if (na < nb) {
|
138 | return 1;
|
139 | }
|
140 | }
|
141 |
|
142 |
|
143 | if (bias) {
|
144 | return bias;
|
145 | }
|
146 |
|
147 |
|
148 | if (sa) {
|
149 | if (za > zb) {
|
150 | return -1;
|
151 | }
|
152 | if (za < zb) {
|
153 | return 1;
|
154 | }
|
155 | } else {
|
156 | if (za < zb) {
|
157 | return -1;
|
158 | }
|
159 | if (za > zb) {
|
160 | return 1;
|
161 | }
|
162 | }
|
163 |
|
164 |
|
165 | if (ca < cb) {
|
166 | return -1;
|
167 | }
|
168 | if (ca > cb) {
|
169 | return 1;
|
170 | }
|
171 |
|
172 | ia += 1;
|
173 | ib += 1;
|
174 | }
|
175 |
|
176 |
|
177 | if (ma < mb) {
|
178 | return -1;
|
179 | }
|
180 | if (ma > mb) {
|
181 | return 1;
|
182 | }
|
183 | };
|