Back to index

lightning-sunbird  0.9+nobinonly
Public Member Functions | Static Public Member Functions | Public Attributes | Private Attributes
txDecimalCounter Class Reference
Inheritance diagram for txDecimalCounter:
Inheritance graph
[legend]
Collaboration diagram for txDecimalCounter:
Collaboration graph
[legend]

List of all members.

Public Member Functions

 txDecimalCounter ()
 txDecimalCounter (PRInt32 aMinLength, PRInt32 aGroupSize, const nsAString &mGroupSeparator)
virtual void appendNumber (PRInt32 aNumber, nsAString &aDest)

Static Public Member Functions

static nsresult getCounterFor (const nsAFlatString &aToken, int aGroupSize, const nsAString &aGroupSeparator, txFormattedCounter *&aCounter)

Public Attributes

nsString mSeparator

Private Attributes

PRInt32 mMinLength
PRInt32 mGroupSize
nsString mGroupSeparator

Detailed Description

Definition at line 43 of file txXSLTNumberCounters.cpp.


Constructor & Destructor Documentation

Definition at line 45 of file txXSLTNumberCounters.cpp.

                       : mMinLength(1), mGroupSize(50)
    {
    }
txDecimalCounter::txDecimalCounter ( PRInt32  aMinLength,
PRInt32  aGroupSize,
const nsAString &  mGroupSeparator 
)

Definition at line 137 of file txXSLTNumberCounters.cpp.

    : mMinLength(aMinLength), mGroupSize(aGroupSize),
      mGroupSeparator(aGroupSeparator)
{
    if (mGroupSize <= 0) {
        mGroupSize = aMinLength + 10;
    }
}

Member Function Documentation

void txDecimalCounter::appendNumber ( PRInt32  aNumber,
nsAString &  aDest 
) [virtual]

Implements txFormattedCounter.

Definition at line 147 of file txXSLTNumberCounters.cpp.

{
    const PRInt32 bufsize = 10; //must be able to fit an PRInt32
    PRUnichar buf[bufsize];
    PRInt32 pos = bufsize;
    while (aNumber > 0) {
        PRInt32 ch = aNumber % 10;
        aNumber /= 10;
        buf[--pos] = ch + '0';
    }

    // in case we didn't get a long enough string
    PRInt32 end  = (bufsize > mMinLength) ? bufsize - mMinLength : 0;
    while (pos > end) {
        buf[--pos] = '0';
    }
    
    // in case we *still* didn't get a long enough string.
    // this should be very rare since it only happens if mMinLength is bigger
    // then the length of any PRInt32.
    // pos will always be zero 
    PRInt32 extraPos = mMinLength;
    while (extraPos > bufsize) {
        aDest.Append(PRUnichar('0'));
        --extraPos;
        if (extraPos % mGroupSize == 0) {
            aDest.Append(mGroupSeparator);
        }
    }

    // copy string to buffer
    if (mGroupSize >= bufsize - pos) {
        // no grouping will occur
        aDest.Append(buf + pos, (PRUint32)(bufsize - pos));
    }
    else {
        // append chars up to first grouping separator
        PRInt32 len = ((bufsize - pos - 1) % mGroupSize) + 1;
        aDest.Append(buf + pos, len);
        pos += len;
        while (bufsize - pos > 0) {
            aDest.Append(mGroupSeparator);
            aDest.Append(buf + pos, mGroupSize);
            pos += mGroupSize;
        }
        NS_ASSERTION(bufsize == pos, "error while grouping");
    }
}

Here is the caller graph for this function:

nsresult txFormattedCounter::getCounterFor ( const nsAFlatString aToken,
int  aGroupSize,
const nsAString &  aGroupSeparator,
txFormattedCounter *&  aCounter 
) [static, inherited]

Definition at line 86 of file txXSLTNumberCounters.cpp.

{
    PRInt32 length = aToken.Length();
    NS_ASSERTION(length, "getting counter for empty token");
    aCounter = 0;
    
    if (length == 1) {
        PRUnichar ch = aToken.CharAt(0);
        switch (ch) {

            case 'i':
            case 'I':
                aCounter = new txRomanCounter(ch == 'I');
                break;
            
            case 'a':
            case 'A':
                aCounter = new txAlphaCounter(ch);
                break;
            
            case '1':
            default:
                // if we don't recognize the token then use "1"
                aCounter = new txDecimalCounter(1, aGroupSize,
                                                aGroupSeparator);
                break;
        }
        return aCounter ? NS_OK : NS_ERROR_OUT_OF_MEMORY;
    }
    
    // for now, the only multi-char token we support are decimals
    PRInt32 i;
    for (i = 0; i < length-1; ++i) {
        if (aToken.CharAt(i) != '0')
            break;
    }
    if (i == length-1 && aToken.CharAt(i) == '1') {
        aCounter = new txDecimalCounter(length, aGroupSize, aGroupSeparator);
    }
    else {
        // if we don't recognize the token then use '1'
        aCounter = new txDecimalCounter(1, aGroupSize, aGroupSeparator);
    }

    return aCounter ? NS_OK : NS_ERROR_OUT_OF_MEMORY;
}

Here is the caller graph for this function:


Member Data Documentation

Definition at line 57 of file txXSLTNumberCounters.cpp.

Definition at line 56 of file txXSLTNumberCounters.cpp.

Definition at line 55 of file txXSLTNumberCounters.cpp.

Definition at line 103 of file txXSLTNumber.h.


The documentation for this class was generated from the following file: