LCOV - code coverage report
Current view: top level - dom/xslt/xslt - txXSLTNumberCounters.cpp (source / functions) Hit Total Coverage
Test: output.info Lines: 0 90 0.0 %
Date: 2017-07-14 16:53:18 Functions: 0 14 0.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
       2             : /* This Source Code Form is subject to the terms of the Mozilla Public
       3             :  * License, v. 2.0. If a copy of the MPL was not distributed with this
       4             :  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
       5             : 
       6             : #include "txXSLTNumber.h"
       7             : #include "nsReadableUtils.h"
       8             : #include "txCore.h"
       9             : 
      10           0 : class txDecimalCounter : public txFormattedCounter {
      11             : public:
      12           0 :     txDecimalCounter() : mMinLength(1), mGroupSize(50)
      13             :     {
      14           0 :     }
      15             : 
      16             :     txDecimalCounter(int32_t aMinLength, int32_t aGroupSize,
      17             :                      const nsAString& mGroupSeparator);
      18             : 
      19             :     virtual void appendNumber(int32_t aNumber, nsAString& aDest);
      20             : 
      21             : private:
      22             :     int32_t mMinLength;
      23             :     int32_t mGroupSize;
      24             :     nsString mGroupSeparator;
      25             : };
      26             : 
      27           0 : class txAlphaCounter : public txFormattedCounter {
      28             : public:
      29           0 :     explicit txAlphaCounter(char16_t aOffset) : mOffset(aOffset)
      30             :     {
      31           0 :     }
      32             : 
      33             :     virtual void appendNumber(int32_t aNumber, nsAString& aDest);
      34             : 
      35             : private:
      36             :     char16_t mOffset;
      37             : };
      38             : 
      39           0 : class txRomanCounter : public txFormattedCounter {
      40             : public:
      41           0 :     explicit txRomanCounter(bool aUpper) : mTableOffset(aUpper ? 30 : 0)
      42             :     {
      43           0 :     }
      44             : 
      45             :     void appendNumber(int32_t aNumber, nsAString& aDest);
      46             : 
      47             : private:
      48             :     int32_t mTableOffset;
      49             : };
      50             : 
      51             : 
      52             : nsresult
      53           0 : txFormattedCounter::getCounterFor(const nsString& aToken,
      54             :                                   int32_t aGroupSize,
      55             :                                   const nsAString& aGroupSeparator,
      56             :                                   txFormattedCounter*& aCounter)
      57             : {
      58           0 :     int32_t length = aToken.Length();
      59           0 :     NS_ASSERTION(length, "getting counter for empty token");
      60           0 :     aCounter = 0;
      61             : 
      62           0 :     if (length == 1) {
      63           0 :         char16_t ch = aToken.CharAt(0);
      64           0 :         switch (ch) {
      65             : 
      66             :             case 'i':
      67             :             case 'I':
      68           0 :                 aCounter = new txRomanCounter(ch == 'I');
      69           0 :                 break;
      70             : 
      71             :             case 'a':
      72             :             case 'A':
      73           0 :                 aCounter = new txAlphaCounter(ch);
      74           0 :                 break;
      75             : 
      76             :             case '1':
      77             :             default:
      78             :                 // if we don't recognize the token then use "1"
      79           0 :                 aCounter = new txDecimalCounter(1, aGroupSize,
      80           0 :                                                 aGroupSeparator);
      81           0 :                 break;
      82             :         }
      83           0 :         MOZ_ASSERT(aCounter);
      84           0 :         return NS_OK;
      85             :     }
      86             : 
      87             :     // for now, the only multi-char token we support are decimals
      88             :     int32_t i;
      89           0 :     for (i = 0; i < length-1; ++i) {
      90           0 :         if (aToken.CharAt(i) != '0')
      91           0 :             break;
      92             :     }
      93           0 :     if (i == length-1 && aToken.CharAt(i) == '1') {
      94           0 :         aCounter = new txDecimalCounter(length, aGroupSize, aGroupSeparator);
      95             :     }
      96             :     else {
      97             :         // if we don't recognize the token then use '1'
      98           0 :         aCounter = new txDecimalCounter(1, aGroupSize, aGroupSeparator);
      99             :     }
     100           0 :     MOZ_ASSERT(aCounter);
     101           0 :     return NS_OK;
     102             : }
     103             : 
     104             : 
     105           0 : txDecimalCounter::txDecimalCounter(int32_t aMinLength, int32_t aGroupSize,
     106           0 :                                    const nsAString& aGroupSeparator)
     107             :     : mMinLength(aMinLength), mGroupSize(aGroupSize),
     108           0 :       mGroupSeparator(aGroupSeparator)
     109             : {
     110           0 :     if (mGroupSize <= 0) {
     111           0 :         mGroupSize = aMinLength + 10;
     112             :     }
     113           0 : }
     114             : 
     115           0 : void txDecimalCounter::appendNumber(int32_t aNumber, nsAString& aDest)
     116             : {
     117           0 :     const int32_t bufsize = 10; //must be able to fit an int32_t
     118             :     char16_t buf[bufsize];
     119           0 :     int32_t pos = bufsize;
     120           0 :     while (aNumber > 0) {
     121           0 :         int32_t ch = aNumber % 10;
     122           0 :         aNumber /= 10;
     123           0 :         buf[--pos] = ch + '0';
     124             :     }
     125             : 
     126             :     // in case we didn't get a long enough string
     127           0 :     int32_t end  = (bufsize > mMinLength) ? bufsize - mMinLength : 0;
     128           0 :     while (pos > end) {
     129           0 :         buf[--pos] = '0';
     130             :     }
     131             : 
     132             :     // in case we *still* didn't get a long enough string.
     133             :     // this should be very rare since it only happens if mMinLength is bigger
     134             :     // then the length of any int32_t.
     135             :     // pos will always be zero
     136           0 :     int32_t extraPos = mMinLength;
     137           0 :     while (extraPos > bufsize) {
     138           0 :         aDest.Append(char16_t('0'));
     139           0 :         --extraPos;
     140           0 :         if (extraPos % mGroupSize == 0) {
     141           0 :             aDest.Append(mGroupSeparator);
     142             :         }
     143             :     }
     144             : 
     145             :     // copy string to buffer
     146           0 :     if (mGroupSize >= bufsize - pos) {
     147             :         // no grouping will occur
     148           0 :         aDest.Append(buf + pos, (uint32_t)(bufsize - pos));
     149             :     }
     150             :     else {
     151             :         // append chars up to first grouping separator
     152           0 :         int32_t len = ((bufsize - pos - 1) % mGroupSize) + 1;
     153           0 :         aDest.Append(buf + pos, len);
     154           0 :         pos += len;
     155           0 :         while (bufsize - pos > 0) {
     156           0 :             aDest.Append(mGroupSeparator);
     157           0 :             aDest.Append(buf + pos, mGroupSize);
     158           0 :             pos += mGroupSize;
     159             :         }
     160           0 :         NS_ASSERTION(bufsize == pos, "error while grouping");
     161             :     }
     162           0 : }
     163             : 
     164             : 
     165           0 : void txAlphaCounter::appendNumber(int32_t aNumber, nsAString& aDest)
     166             : {
     167             :     char16_t buf[12];
     168           0 :     buf[11] = 0;
     169           0 :     int32_t pos = 11;
     170           0 :     while (aNumber > 0) {
     171           0 :         --aNumber;
     172           0 :         int32_t ch = aNumber % 26;
     173           0 :         aNumber /= 26;
     174           0 :         buf[--pos] = ch + mOffset;
     175             :     }
     176             : 
     177           0 :     aDest.Append(buf + pos, (uint32_t)(11 - pos));
     178           0 : }
     179             : 
     180             : 
     181             : const char* const kTxRomanNumbers[] =
     182             :     {"", "c", "cc", "ccc", "cd", "d", "dc", "dcc", "dccc", "cm",
     183             :      "", "x", "xx", "xxx", "xl", "l", "lx", "lxx", "lxxx", "xc",
     184             :      "", "i", "ii", "iii", "iv", "v", "vi", "vii", "viii", "ix",
     185             :      "", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM",
     186             :      "", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC",
     187             :      "", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"};
     188             : 
     189           0 : void txRomanCounter::appendNumber(int32_t aNumber, nsAString& aDest)
     190             : {
     191             :     // Numbers bigger then 3999 and negative numbers can't be done in roman
     192           0 :     if (uint32_t(aNumber) >= 4000) {
     193           0 :         txDecimalCounter().appendNumber(aNumber, aDest);
     194           0 :         return;
     195             :     }
     196             : 
     197           0 :     while (aNumber >= 1000) {
     198           0 :         aDest.Append(!mTableOffset ? char16_t('m') : char16_t('M'));
     199           0 :         aNumber -= 1000;
     200             :     }
     201             : 
     202             :     int32_t posValue;
     203             : 
     204             :     // Hundreds
     205           0 :     posValue = aNumber / 100;
     206           0 :     aNumber %= 100;
     207           0 :     AppendASCIItoUTF16(kTxRomanNumbers[posValue + mTableOffset], aDest);
     208             :     // Tens
     209           0 :     posValue = aNumber / 10;
     210           0 :     aNumber %= 10;
     211           0 :     AppendASCIItoUTF16(kTxRomanNumbers[10 + posValue + mTableOffset], aDest);
     212             :     // Ones
     213           0 :     AppendASCIItoUTF16(kTxRomanNumbers[20 + aNumber + mTableOffset], aDest);
     214             : }

Generated by: LCOV version 1.13