[2674] | 1 | /* |
---|
| 2 | * Licensed to the Apache Software Foundation (ASF) under one or more |
---|
| 3 | * contributor license agreements. See the NOTICE file distributed with |
---|
| 4 | * this work for additional information regarding copyright ownership. |
---|
| 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 |
---|
| 6 | * (the "License"); you may not use this file except in compliance with |
---|
| 7 | * the License. You may obtain a copy of the License at |
---|
| 8 | * |
---|
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
---|
| 10 | * |
---|
| 11 | * Unless required by applicable law or agreed to in writing, software |
---|
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
---|
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
---|
| 14 | * See the License for the specific language governing permissions and |
---|
| 15 | * limitations under the License. |
---|
| 16 | */ |
---|
| 17 | |
---|
| 18 | /* |
---|
| 19 | * $Id: XMLBigInteger.hpp 568078 2007-08-21 11:43:25Z amassari $ |
---|
| 20 | */ |
---|
| 21 | |
---|
| 22 | #ifndef XML_BIGINTEGER_HPP |
---|
| 23 | #define XML_BIGINTEGER_HPP |
---|
| 24 | |
---|
| 25 | #include <xercesc/util/XMemory.hpp> |
---|
| 26 | #include <xercesc/util/XMLString.hpp> |
---|
| 27 | |
---|
| 28 | XERCES_CPP_NAMESPACE_BEGIN |
---|
| 29 | |
---|
| 30 | class XMLUTIL_EXPORT XMLBigInteger : public XMemory |
---|
| 31 | { |
---|
| 32 | public: |
---|
| 33 | |
---|
| 34 | /** |
---|
| 35 | * Constructs a newly allocated <code>XMLBigInteger</code> object that |
---|
| 36 | * represents the value represented by the string. The string is |
---|
| 37 | * converted to an int value as if by the <code>valueOf</code> method. |
---|
| 38 | * |
---|
| 39 | * @param strValue the <code>String</code> to be converted to an |
---|
| 40 | * <code>XMLBigInteger</code>. |
---|
| 41 | * @param manager Pointer to the memory manager to be used to |
---|
| 42 | * allocate objects. |
---|
| 43 | * @exception NumberFormatException if the <code>String</code> does not |
---|
| 44 | * contain a parsable XMLBigInteger. |
---|
| 45 | */ |
---|
| 46 | |
---|
| 47 | XMLBigInteger |
---|
| 48 | ( |
---|
| 49 | const XMLCh* const strValue |
---|
| 50 | , MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager |
---|
| 51 | ); |
---|
| 52 | ~XMLBigInteger(); |
---|
| 53 | |
---|
| 54 | XMLBigInteger(const XMLBigInteger& toCopy); |
---|
| 55 | |
---|
| 56 | static XMLCh* getCanonicalRepresentation |
---|
| 57 | ( |
---|
| 58 | const XMLCh* const rawData |
---|
| 59 | , MemoryManager* const memMgr = XMLPlatformUtils::fgMemoryManager |
---|
| 60 | , bool isNonPositiveInteger = false |
---|
| 61 | ); |
---|
| 62 | |
---|
| 63 | static void parseBigInteger(const XMLCh* const toConvert |
---|
| 64 | , XMLCh* const retBuffer |
---|
| 65 | , int& signValue |
---|
| 66 | , MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager); |
---|
| 67 | |
---|
| 68 | static int compareValues(const XMLBigInteger* const lValue |
---|
| 69 | ,const XMLBigInteger* const rValue |
---|
| 70 | , MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager); |
---|
| 71 | |
---|
| 72 | |
---|
| 73 | static int compareValues(const XMLCh* const lString |
---|
| 74 | , const int& lSign |
---|
| 75 | , const XMLCh* const rString |
---|
| 76 | , const int& rSign |
---|
| 77 | , MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager); |
---|
| 78 | |
---|
| 79 | void multiply(const unsigned int byteToShift); |
---|
| 80 | |
---|
| 81 | void divide(const unsigned int byteToShift); |
---|
| 82 | |
---|
| 83 | int getTotalDigit() const; |
---|
| 84 | |
---|
| 85 | /** |
---|
| 86 | * |
---|
| 87 | * Deprecated: please use getRawData |
---|
| 88 | * |
---|
| 89 | * Return a copy of the fMagnitue. |
---|
| 90 | * A leading sign is ALWAYS in place and the caller of this method |
---|
| 91 | * is responsible for the de-allocation of the memory. |
---|
| 92 | */ |
---|
| 93 | inline XMLCh* toString() const; |
---|
| 94 | |
---|
| 95 | /** |
---|
| 96 | * Return a copy of the fMagnitue. |
---|
| 97 | * This is similar to toString, except the internal buffer is returned directly |
---|
| 98 | * Caller is not required to delet the returned memory. |
---|
| 99 | */ |
---|
| 100 | inline XMLCh* getRawData() const; |
---|
| 101 | |
---|
| 102 | /** |
---|
| 103 | * Compares this object to the specified object. |
---|
| 104 | * The result is <code>true</code> if and only if the argument is not |
---|
| 105 | * <code>null</code> and is an <code>XMLBigInteger</code> object that contains |
---|
| 106 | * the same <code>int</code> value as this object. |
---|
| 107 | * |
---|
| 108 | * @param toCompare the object to compare with. |
---|
| 109 | * @return <code>true</code> if the objects are the same; |
---|
| 110 | * <code>false</code> otherwise. |
---|
| 111 | */ |
---|
| 112 | bool operator==(const XMLBigInteger& toCompare) const; |
---|
| 113 | |
---|
| 114 | /** |
---|
| 115 | * Returns the signum function of this number (i.e., -1, 0 or 1 as |
---|
| 116 | * the value of this number is negative, zero or positive). |
---|
| 117 | */ |
---|
| 118 | int getSign() const; |
---|
| 119 | |
---|
| 120 | int intValue() const; |
---|
| 121 | |
---|
| 122 | private: |
---|
| 123 | // ----------------------------------------------------------------------- |
---|
| 124 | // Unimplemented constructors and operators |
---|
| 125 | // ----------------------------------------------------------------------- |
---|
| 126 | XMLBigInteger& operator=(const XMLBigInteger&); |
---|
| 127 | |
---|
| 128 | |
---|
| 129 | void setSign(int); |
---|
| 130 | |
---|
| 131 | /* |
---|
| 132 | * The number is internally stored in "minimal" sign-fMagnitude format |
---|
| 133 | * (i.e., no BigIntegers have a leading zero byte in their magnitudes). |
---|
| 134 | * Zero is represented with a signum of 0 (and a zero-length fMagnitude). |
---|
| 135 | * Thus, there is exactly one representation for each value. |
---|
| 136 | */ |
---|
| 137 | // ----------------------------------------------------------------------- |
---|
| 138 | // Private data members |
---|
| 139 | // |
---|
| 140 | // fSign |
---|
| 141 | // to represent the sign of the number. |
---|
| 142 | // |
---|
| 143 | // fMagnitude |
---|
| 144 | // the buffer holding the number. |
---|
| 145 | // |
---|
| 146 | // fRawData |
---|
| 147 | // to preserve the original string used to construct this object, |
---|
| 148 | // needed for pattern matching. |
---|
| 149 | // |
---|
| 150 | // ----------------------------------------------------------------------- |
---|
| 151 | |
---|
| 152 | int fSign; |
---|
| 153 | XMLCh* fMagnitude; //null terminated |
---|
| 154 | XMLCh* fRawData; |
---|
| 155 | MemoryManager* fMemoryManager; |
---|
| 156 | }; |
---|
| 157 | |
---|
| 158 | inline int XMLBigInteger::getSign() const |
---|
| 159 | { |
---|
| 160 | return fSign; |
---|
| 161 | } |
---|
| 162 | |
---|
| 163 | inline int XMLBigInteger::getTotalDigit() const |
---|
| 164 | { |
---|
| 165 | return ((getSign() ==0) ? 0 : XMLString::stringLen(fMagnitude)); |
---|
| 166 | } |
---|
| 167 | |
---|
| 168 | inline bool XMLBigInteger::operator==(const XMLBigInteger& toCompare) const |
---|
| 169 | { |
---|
| 170 | return ( compareValues(this, &toCompare, fMemoryManager) ==0 ? true : false); |
---|
| 171 | } |
---|
| 172 | |
---|
| 173 | inline void XMLBigInteger::setSign(int newSign) |
---|
| 174 | { |
---|
| 175 | fSign = newSign; |
---|
| 176 | } |
---|
| 177 | |
---|
| 178 | inline XMLCh* XMLBigInteger::getRawData() const |
---|
| 179 | { |
---|
| 180 | return fRawData; |
---|
| 181 | } |
---|
| 182 | |
---|
| 183 | // |
---|
| 184 | // The caller needs to de-allocate the memory allocated by this function |
---|
| 185 | // |
---|
| 186 | inline XMLCh* XMLBigInteger::toString() const |
---|
| 187 | { |
---|
| 188 | // Return data using global operator new |
---|
| 189 | return XMLString::replicate(fRawData, fMemoryManager); |
---|
| 190 | } |
---|
| 191 | |
---|
| 192 | XERCES_CPP_NAMESPACE_END |
---|
| 193 | |
---|
| 194 | #endif |
---|