phonebookengines/VirtualPhonebook/inc/TFixedDigitString.h
changeset 0 e686773b3f54
equal deleted inserted replaced
-1:000000000000 0:e686773b3f54
       
     1 /*
       
     2 * Copyright (c) 2005-2007 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:  Fixed digit string handler class
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #ifndef VPBKENGUTILS_TFIXEDDIGITSTRING_H
       
    20 #define VPBKENGUTILS_TFIXEDDIGITSTRING_H
       
    21 
       
    22 // INCLUDES
       
    23 #include <e32std.h>
       
    24 
       
    25 namespace VPbkEngUtils {
       
    26 
       
    27 // FORWARD DECLARATIONS
       
    28 
       
    29 
       
    30 // CLASS DECLARATIONS
       
    31 
       
    32 /**
       
    33  * Fixed-length digit string.
       
    34  * Phone number matching helper class: encodes decimal digit characters to BCD 
       
    35  * (Binary Coded Decimal) format for efficient matching.
       
    36  */
       
    37 class TFixedDigitString
       
    38     {
       
    39     public:
       
    40         /**
       
    41          * Default constructor. Resets this digit string to empty.
       
    42          */
       
    43         TFixedDigitString();
       
    44 
       
    45         /**
       
    46          * Returns the length of this digit string.
       
    47          * @return the number of digits in this digit string,
       
    48          */
       
    49         TInt Length() const;
       
    50 
       
    51         /**
       
    52          * Returns true if this digit string is empty.
       
    53          */
       
    54         TBool IsEmpty() const;
       
    55 
       
    56         /**
       
    57          * Returns the maximum number of digits this object may contain.
       
    58          */
       
    59         static TInt MaxLength();
       
    60 
       
    61         /**
       
    62          * Returns non-modifiable decimal digit at position aIndex.
       
    63          *
       
    64          * @pre  aIndex >= 0 && aIndex < Length()
       
    65          * @post result >= 0 && result <= 9
       
    66          */
       
    67         TInt operator[](TInt aIndex) const;
       
    68 
       
    69         /**
       
    70          * Compares the digits in this string to another digit string.
       
    71          *
       
    72          * @param aOther    the other digit string to compare this string with.
       
    73          * @param aNumDigits    the number of digits to compare. Zero means
       
    74          *                      all digits are compared.
       
    75          * @pre Result is undefined if aNumDigits < 0 || aNumDigits > MaxLength().
       
    76          * @return true if aNumDigits digits match
       
    77          */
       
    78         TBool Compare(const TFixedDigitString& aOther, TInt aNumDigits) const;
       
    79 
       
    80         /**
       
    81          * Resets this digit string to empty.
       
    82          * @post Length() == 0
       
    83          */
       
    84         void Reset();
       
    85 
       
    86         /**
       
    87          * Encodes digit characters from text to this digit string.
       
    88          *
       
    89          * @param aDigitsText   text to encode decimal digit characters from.
       
    90          *                      Nondigit characters are skipped. The encoded 
       
    91          *                      and skipped characters are removed from the 
       
    92          *                      parameter. At maximum MaxLength() digits
       
    93          *                      are encoded.
       
    94          */
       
    95         void EncodeDigits(TPtrC& aDigitsText);
       
    96 
       
    97     private:  // Implementation
       
    98         enum 
       
    99             {
       
   100             KBitsPerDigit = 4,
       
   101             KMaxDigits = 8*sizeof(TUint) / KBitsPerDigit,
       
   102             KMaxBits = KMaxDigits * KBitsPerDigit,
       
   103             KNaNBCD = 0xf,
       
   104             KNaN = -1
       
   105             };
       
   106         inline TBool DoCompare(const TFixedDigitString& aOther, TInt aNumDigits) const;
       
   107 
       
   108     private:  // Data
       
   109         /// Own. Bcd digits to be compared
       
   110         TUint iBcdDigits;
       
   111     };
       
   112 
       
   113 
       
   114 // INLINE FUNCTIONS
       
   115 
       
   116 inline TInt TFixedDigitString::MaxLength()
       
   117     {
       
   118     return KMaxDigits;
       
   119     }
       
   120 
       
   121 /**
       
   122  * Inline implementation of Compare.
       
   123  */
       
   124 inline TBool TFixedDigitString::DoCompare
       
   125         (const TFixedDigitString& aOther, TInt aNumDigits) const
       
   126     {
       
   127     const TUint mask = ~TUint(0) >> ((KBitsPerDigit*(KMaxDigits - aNumDigits)) % (KMaxBits));
       
   128     if ((iBcdDigits ^ aOther.iBcdDigits) & mask)
       
   129         return EFalse;
       
   130     else
       
   131         return ETrue;
       
   132     }
       
   133 
       
   134 #ifdef NDEBUG
       
   135 // Inline version of TFixedDigitString::Compare for non-debug builds
       
   136 inline TBool TFixedDigitString::Compare
       
   137         (const TFixedDigitString& aOther, TInt aNumDigits) const
       
   138     {
       
   139     return DoCompare(aOther, aNumDigits);
       
   140     }
       
   141 #endif
       
   142 
       
   143 
       
   144 } // namespace VPbkEngUtils
       
   145 
       
   146 #endif  // VPBKENGUTILS_TFIXEDDIGITSTRING_H
       
   147