summaryrefslogtreecommitdiffstats
path: root/dom/xslt/xslt/txFormatNumberFunctionCall.cpp
blob: e851db2d7ecff59d0a3e3a541028831f1ee08e44 (plain)
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "mozilla/FloatingPoint.h"

#include "txXSLTFunctions.h"
#include "nsGkAtoms.h"
#include "txIXPathContext.h"
#include "txStylesheet.h"
#include <math.h>
#include "txNamespaceMap.h"

#include "prdtoa.h"

#define INVALID_PARAM_VALUE u"invalid parameter value for function"_ns

const char16_t txFormatNumberFunctionCall::FORMAT_QUOTE = '\'';

/*
 * FormatNumberFunctionCall
 * A representation of the XSLT additional function: format-number()
 */

/*
 * Creates a new format-number function call
 */
txFormatNumberFunctionCall::txFormatNumberFunctionCall(
    txStylesheet* aStylesheet, txNamespaceMap* aMappings)
    : mStylesheet(aStylesheet), mMappings(aMappings) {}

void txFormatNumberFunctionCall::ReportInvalidArg(txIEvalContext* aContext) {
  nsAutoString err(INVALID_PARAM_VALUE);
#ifdef TX_TO_STRING
  err.AppendLiteral(": ");
  toString(err);
#endif
  aContext->receiveError(err, NS_ERROR_XPATH_INVALID_ARG);
}

/*
 * Evaluates this Expr based on the given context node and processor state
 * @param context the context node for evaluation of this Expr
 * @param cs the ContextState containing the stack information needed
 * for evaluation
 * @return the result of the evaluation
 */
nsresult txFormatNumberFunctionCall::evaluate(txIEvalContext* aContext,
                                              txAExprResult** aResult) {
  *aResult = nullptr;
  if (!requireParams(2, 3, aContext)) return NS_ERROR_XPATH_BAD_ARGUMENT_COUNT;

  // Get number and format
  double value;
  txExpandedName formatName;

  nsresult rv = evaluateToNumber(mParams[0], aContext, &value);
  NS_ENSURE_SUCCESS(rv, rv);

  nsAutoString formatStr;
  rv = mParams[1]->evaluateToString(aContext, formatStr);
  NS_ENSURE_SUCCESS(rv, rv);

  if (mParams.Length() == 3) {
    nsAutoString formatQName;
    rv = mParams[2]->evaluateToString(aContext, formatQName);
    NS_ENSURE_SUCCESS(rv, rv);

    rv = formatName.init(formatQName, mMappings, false);
    NS_ENSURE_SUCCESS(rv, rv);
  }

  txDecimalFormat* format = mStylesheet->getDecimalFormat(formatName);
  if (!format) {
    nsAutoString err(u"unknown decimal format"_ns);
#ifdef TX_TO_STRING
    err.AppendLiteral(" for: ");
    toString(err);
#endif
    aContext->receiveError(err, NS_ERROR_XPATH_INVALID_ARG);
    return NS_ERROR_XPATH_INVALID_ARG;
  }

  // Special cases
  if (std::isnan(value)) {
    return aContext->recycler()->getStringResult(format->mNaN, aResult);
  }

  if (value == mozilla::PositiveInfinity<double>()) {
    return aContext->recycler()->getStringResult(format->mInfinity, aResult);
  }

  if (value == mozilla::NegativeInfinity<double>()) {
    nsAutoString res;
    res.Append(format->mMinusSign);
    res.Append(format->mInfinity);
    return aContext->recycler()->getStringResult(res, aResult);
  }

  // Value is a normal finite number
  nsAutoString prefix;
  nsAutoString suffix;
  int minIntegerSize = 0;
  int minFractionSize = 0;
  int maxFractionSize = 0;
  int multiplier = 1;
  int groupSize = -1;

  uint32_t pos = 0;
  uint32_t formatLen = formatStr.Length();
  bool inQuote;

  // Get right subexpression
  inQuote = false;
  if (mozilla::IsNegative(value)) {
    while (pos < formatLen &&
           (inQuote || formatStr.CharAt(pos) != format->mPatternSeparator)) {
      if (formatStr.CharAt(pos) == FORMAT_QUOTE) inQuote = !inQuote;
      pos++;
    }

    if (pos == formatLen) {
      pos = 0;
      prefix.Append(format->mMinusSign);
    } else
      pos++;
  }

  // Parse the format string
  FormatParseState pState = Prefix;
  inQuote = false;

  char16_t c = 0;
  while (pos < formatLen && pState != Finished) {
    c = formatStr.CharAt(pos++);

    switch (pState) {
      case Prefix:
      case Suffix:
        if (!inQuote) {
          if (c == format->mPercent) {
            if (multiplier == 1)
              multiplier = 100;
            else {
              ReportInvalidArg(aContext);
              return NS_ERROR_XPATH_INVALID_ARG;
            }
          } else if (c == format->mPerMille) {
            if (multiplier == 1)
              multiplier = 1000;
            else {
              ReportInvalidArg(aContext);
              return NS_ERROR_XPATH_INVALID_ARG;
            }
          } else if (c == format->mDecimalSeparator ||
                     c == format->mGroupingSeparator ||
                     c == format->mZeroDigit || c == format->mDigit ||
                     c == format->mPatternSeparator) {
            pState = pState == Prefix ? IntDigit : Finished;
            pos--;
            break;
          }
        }

        if (c == FORMAT_QUOTE)
          inQuote = !inQuote;
        else if (pState == Prefix)
          prefix.Append(c);
        else
          suffix.Append(c);
        break;

      case IntDigit:
        if (c == format->mGroupingSeparator)
          groupSize = 0;
        else if (c == format->mDigit) {
          if (groupSize >= 0) groupSize++;
        } else {
          pState = IntZero;
          pos--;
        }
        break;

      case IntZero:
        if (c == format->mGroupingSeparator)
          groupSize = 0;
        else if (c == format->mZeroDigit) {
          if (groupSize >= 0) groupSize++;
          minIntegerSize++;
        } else if (c == format->mDecimalSeparator) {
          pState = FracZero;
        } else {
          pState = Suffix;
          pos--;
        }
        break;

      case FracZero:
        if (c == format->mZeroDigit) {
          maxFractionSize++;
          minFractionSize++;
        } else {
          pState = FracDigit;
          pos--;
        }
        break;

      case FracDigit:
        if (c == format->mDigit)
          maxFractionSize++;
        else {
          pState = Suffix;
          pos--;
        }
        break;

      case Finished:
        break;
    }
  }

  // Did we manage to parse the entire formatstring and was it valid
  if ((c != format->mPatternSeparator && pos < formatLen) || inQuote ||
      groupSize == 0) {
    ReportInvalidArg(aContext);
    return NS_ERROR_XPATH_INVALID_ARG;
  }

  /*
   * FINALLY we're done with the parsing
   * now build the result string
   */

  value = fabs(value) * multiplier;

  // Make sure the multiplier didn't push value to infinity.
  if (value == mozilla::PositiveInfinity<double>()) {
    return aContext->recycler()->getStringResult(format->mInfinity, aResult);
  }

  // Make sure the multiplier didn't push value to infinity.
  if (value == mozilla::PositiveInfinity<double>()) {
    return aContext->recycler()->getStringResult(format->mInfinity, aResult);
  }

  // Prefix
  nsAutoString res(prefix);

  int bufsize;
  if (value > 1)
    bufsize = (int)log10(value) + 30;
  else
    bufsize = 1 + 30;

  auto buf = mozilla::MakeUnique<char[]>(bufsize);
  int bufIntDigits, sign;
  char* endp;
  PR_dtoa(value, 0, 0, &bufIntDigits, &sign, &endp, buf.get(), bufsize - 1);

  int buflen = endp - buf.get();
  int intDigits;
  intDigits = bufIntDigits > minIntegerSize ? bufIntDigits : minIntegerSize;

  if (groupSize < 0) groupSize = intDigits + 10;  // to simplify grouping

  // XXX We shouldn't use SetLength.
  res.SetLength(res.Length() + intDigits +     // integer digits
                1 +                            // decimal separator
                maxFractionSize +              // fractions
                (intDigits - 1) / groupSize);  // group separators

  int32_t i = bufIntDigits + maxFractionSize - 1;
  bool carry = (0 <= i + 1) && (i + 1 < buflen) && (buf[i + 1] >= '5');
  bool hasFraction = false;

  // The number of characters in res that we haven't filled in.
  mozilla::CheckedUint32 resRemain = mozilla::CheckedUint32(res.Length());

#define CHECKED_SET_CHAR(c)                                           \
  --resRemain;                                                        \
  if (!resRemain.isValid() || !res.SetCharAt(c, resRemain.value())) { \
    ReportInvalidArg(aContext);                                       \
    return NS_ERROR_XPATH_INVALID_ARG;                                \
  }

#define CHECKED_TRUNCATE()             \
  --resRemain;                         \
  if (!resRemain.isValid()) {          \
    ReportInvalidArg(aContext);        \
    return NS_ERROR_XPATH_INVALID_ARG; \
  }                                    \
  res.Truncate(resRemain.value());

  // Fractions
  for (; i >= bufIntDigits; --i) {
    int digit;
    if (i >= buflen || i < 0) {
      digit = 0;
    } else {
      digit = buf[i] - '0';
    }

    if (carry) {
      digit = (digit + 1) % 10;
      carry = digit == 0;
    }

    if (hasFraction || digit != 0 || i < bufIntDigits + minFractionSize) {
      hasFraction = true;
      CHECKED_SET_CHAR((char16_t)(digit + format->mZeroDigit));
    } else {
      CHECKED_TRUNCATE();
    }
  }

  // Decimal separator
  if (hasFraction) {
    CHECKED_SET_CHAR(format->mDecimalSeparator);
  } else {
    CHECKED_TRUNCATE();
  }

  // Integer digits
  for (i = 0; i < intDigits; ++i) {
    int digit;
    if (bufIntDigits - i - 1 >= buflen || bufIntDigits - i - 1 < 0) {
      digit = 0;
    } else {
      digit = buf[bufIntDigits - i - 1] - '0';
    }

    if (carry) {
      digit = (digit + 1) % 10;
      carry = digit == 0;
    }

    if (i != 0 && i % groupSize == 0) {
      CHECKED_SET_CHAR(format->mGroupingSeparator);
    }

    CHECKED_SET_CHAR((char16_t)(digit + format->mZeroDigit));
  }

#undef CHECKED_SET_CHAR
#undef CHECKED_TRUNCATE

  if (carry) {
    if (i % groupSize == 0) {
      res.Insert(format->mGroupingSeparator, resRemain.value());
    }
    res.Insert((char16_t)(1 + format->mZeroDigit), resRemain.value());
  }

  if (!hasFraction && !intDigits && !carry) {
    // If we havn't added any characters we add a '0'
    // This can only happen for formats like '##.##'
    res.Append(format->mZeroDigit);
  }

  // Build suffix
  res.Append(suffix);

  return aContext->recycler()->getStringResult(res, aResult);
}  //-- evaluate

Expr::ResultType txFormatNumberFunctionCall::getReturnType() {
  return STRING_RESULT;
}

bool txFormatNumberFunctionCall::isSensitiveTo(ContextSensitivity aContext) {
  return argsSensitiveTo(aContext);
}

#ifdef TX_TO_STRING
void txFormatNumberFunctionCall::appendName(nsAString& aDest) {
  aDest.Append(nsGkAtoms::formatNumber->GetUTF16String());
}
#endif

/*
 * txDecimalFormat
 * A representation of the XSLT element <xsl:decimal-format>
 */

txDecimalFormat::txDecimalFormat()
    : mInfinity(u"Infinity"_ns), mNaN(u"NaN"_ns) {
  mDecimalSeparator = '.';
  mGroupingSeparator = ',';
  mMinusSign = '-';
  mPercent = '%';
  mPerMille = 0x2030;
  mZeroDigit = '0';
  mDigit = '#';
  mPatternSeparator = ';';
}

bool txDecimalFormat::isEqual(txDecimalFormat* other) {
  return mDecimalSeparator == other->mDecimalSeparator &&
         mGroupingSeparator == other->mGroupingSeparator &&
         mInfinity.Equals(other->mInfinity) &&
         mMinusSign == other->mMinusSign && mNaN.Equals(other->mNaN) &&
         mPercent == other->mPercent && mPerMille == other->mPerMille &&
         mZeroDigit == other->mZeroDigit && mDigit == other->mDigit &&
         mPatternSeparator == other->mPatternSeparator;
}