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