activemq-cpp-3.9.5
HexStringParser.h
Go to the documentation of this file.
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#ifndef _DECAF_INTERNAL_UTIL_HEXSTRINGPARSER_H_
19#define _DECAF_INTERNAL_UTIL_HEXSTRINGPARSER_H_
20
21#include <decaf/util/Config.h>
22#include <string>
23
24namespace decaf {
25namespace internal {
26namespace util {
27
29 private:
30
31 static const unsigned int DOUBLE_EXPONENT_WIDTH = 11;
32 static const unsigned int DOUBLE_MANTISSA_WIDTH = 52;
33 static const unsigned int FLOAT_EXPONENT_WIDTH = 8;
34 static const unsigned int FLOAT_MANTISSA_WIDTH = 23;
35 static const unsigned int HEX_RADIX = 16;
36 static const unsigned int MAX_SIGNIFICANT_LENGTH = 15;
37
38 static const std::string HEX_SIGNIFICANT;
39 static const std::string BINARY_EXPONENT;
40 static const std::string FLOAT_TYPE_SUFFIX;
41 static const std::string HEX_PATTERN;
42
43 // TODO
44 //static final Pattern PATTERN = Pattern.compile(HEX_PATTERN);
45
46 private:
47
48 int EXPONENT_WIDTH;
49 int MANTISSA_WIDTH;
50 long long EXPONENT_BASE;
51 long long MAX_EXPONENT;
52 long long MIN_EXPONENT;
53 long long MANTISSA_MASK;
54 long long sign;
55 long long exponent;
56 long long mantissa;
57 std::string abandonedNumber;
58
59 public:
60
66 HexStringParser(int exponentWidth, int mantissaWidth);
67
68 virtual ~HexStringParser() {
69 }
70
78 long long parse(const std::string& hexString);
79
80 private:
81
82 /*
83 * Parses the sign field.
84 * @param sign string to parse
85 */
86 void parseHexSign(const std::string& signStr) {
87 this->sign = signStr.compare("-") == 0 ? 1 : 0;
88 }
89
90 /*
91 * Parses the exponent field.
92 * @param exponent string to parse
93 */
94 void parseExponent(const std::string& exponentStr);
95
96 /*
97 * Parses the mantissa field.
98 * @param mantissa string to parse
99 */
100 void parseMantissa(const std::string& significantStr);
101
102 void setInfinite() {
103 exponent = MAX_EXPONENT;
104 mantissa = 0;
105 }
106
107 void setZero() {
108 exponent = 0;
109 mantissa = 0;
110 }
111
112 /*
113 * Sets the exponent variable to Long::MAX_VALUE or -Long::MAX_VALUE if
114 * overflow or underflow happens.
115 * @param the offset to set
116 */
117 void checkedAddExponent(long long offset);
118
119 void processNormalNumber();
120 void processSubNormalNumber();
121 int countBitsLength(long long value);
122
123 /*
124 * Adjusts the mantissa to desired width for further analysis.
125 */
126 void fitMantissaInDesiredWidth(int desiredWidth);
127
128 /*
129 * Stores the discarded bits to abandonedNumber.
130 */
131 void discardTrailingBits(long long num);
132
133 /*
134 * The value is rounded up or down to the nearest infinitely precise result.
135 * If the value is exactly halfway between two infinitely precise results,
136 * then it should be rounded up to the nearest infinitely precise even.
137 */
138 void round();
139
140 /*
141 * Returns the normalized significand after removing the leading zeros.
142 */
143 std::string getNormalizedSignificand(const std::string& strIntegerPart, const std::string& strDecimalPart);
144
145 /*
146 * Calculates the offset between the normalized number and unnormalized
147 * number. In a normalized representation, significand is represented by the
148 * characters "0x1." followed by a lower-case hexadecimal representation of
149 * the rest of the significand as a fraction.
150 */
151 int getOffset(const std::string& strIntegerPart, const std::string& strDecimalPart);
152
153 public:
154 // Statics
155
156 /*
157 * Parses the hex string to a double number.
158 * @param hexString - string to parse
159 * @return the parsed double value
160 */
161 static double parseDouble(const std::string& hexString);
162
163 /*
164 * Parses the hex string to a float number.
165 * @param hexString - string to parse
166 * @return the parsed float value
167 */
168 static float parseFloat(const std::string& hexString);
169
170 private:
171 // Static
172
173 /*
174 * Analyzes the hex string and extracts the sign and digit segments.
175 * @param hexString - string to parse
176 * @return array of three strings holding the segments caller owns
177 */
178 static std::string* getSegmentsFromHexString(const std::string& hexString);
179
180 std::string& replaceFirst(std::string& target, const std::string& find, const std::string& replace) {
181
182 std::string::size_type pos = std::string::npos;
183
184 if ((pos = target.find_first_of(find, 0)) != std::string::npos) {
185 return target.replace(pos, find.length(), replace);
186 }
187
188 return target;
189 }
190
191 std::string& replaceAll(std::string& target, const std::string& find, const std::string& replace) {
192
193 std::string::size_type pos = std::string::npos;
194 while ((pos = target.find(find)) != std::string::npos) {
195 target.replace(pos, find.length(), replace);
196 }
197
198 return target;
199 }
200
201 };
202
203}}}
204
205#endif /*_DECAF_INTERNAL_UTIL_HEXSTRINGPARSER_H_*/
static float parseFloat(const std::string &hexString)
static double parseDouble(const std::string &hexString)
long long parse(const std::string &hexString)
Parses a hex string using the specs given in the constructor and returns a long long with the bits of...
virtual ~HexStringParser()
Definition HexStringParser.h:68
HexStringParser(int exponentWidth, int mantissaWidth)
Create a new HexParser.
Definition ByteArrayAdapter.h:30
Definition AprPool.h:26
Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements.
Definition AprPool.h:25