1 // Licensed to the .NET Foundation under one or more agreements.
2 // The .NET Foundation licenses this file to you under the MIT license.
3 // See the LICENSE file in the project root for more information.
5 /*****************************************************************/
7 /*****************************************************************/
8 /* A simple, lightweight, character output stream, with very few
9 external dependancies (like sprintf ... ) */
13 /*****************************************************************/
16 #include "outstring.h"
19 /*****************************************************************/
20 // print out 'count' instances of the character 'c'
21 OutString& OutString::pad(size_t count, char c) {
24 memset(cur, c, count);
29 /*****************************************************************/
30 // prints out a decimal representation
31 OutString& OutString::operator<<(double d) {
43 // compute the exponent
48 if (exponent > 500) { // avoids a possible infinite loop
56 if (exponent < -500) { // avoids a possible infinite loop
62 // we now have a normalized d (between 1 and 10)
63 double delta = .5E-10;
64 d += delta; // round to the precision we are displaying
66 unsigned trailingZeros = 0;
67 for(unsigned i = 0; i < 10; i++) {
69 d = (d - digit) * 10; // ISSUE: does roundoff ever bite us here?
71 if (digit == 0) // defer printing traiing zeros
74 if (trailingZeros > 0) {
75 this->pad(trailingZeros, '0');
78 *this << (char) ('0' + digit);
91 /*****************************************************************/
92 // prints out a decimal representation
93 OutString& OutString::dec(int i, size_t minWidth) {
94 char buff[12]; // big enough for any number (10 digits, - sign, null term)
95 char* ptr = &buff[11];
100 val = -i; // note this happens to also work for minint!
107 *--ptr = '0' + (val % 10);
114 size_t len = &buff[11] - ptr; // length of string
116 pad(minWidth-len, ' ');
122 /*****************************************************************/
123 OutString& OutString::hex(unsigned __int64 i, int minWidth, unsigned flags) {
125 unsigned hi = unsigned(i >> 32);
126 unsigned low = unsigned(i);
130 hex(hi, minWidth, flags); // print upper bits
134 return hex(low, minWidth, flags); // print lower bits
137 /*****************************************************************/
138 OutString& OutString::hex(unsigned i, int minWidth, unsigned flags) {
139 char buff[12]; // big enough for any number
140 char* ptr = &buff[11];
143 static const char digits[] = "0123456789ABCDEF";
150 *--ptr = digits[(i % 16)];
154 size_t len = &buff[11] - ptr; // length of string
156 if (flags & zeroFill)
159 *--ptr = 'x', *--ptr = '0';
163 if (len < (size_t)minWidth)
164 pad(minWidth-len, (flags & zeroFill) ? '0' : ' ');
170 /*****************************************************************/
171 void OutString::Realloc(size_t neededSpace) {
172 size_t oldSize = cur-start;
173 size_t newSize = (oldSize + neededSpace) * 3 / 2 + 32;
174 char* oldBuff = start;
175 start = new char[newSize+1];
176 memcpy(start, oldBuff, oldSize);
177 cur = &start[oldSize];
178 end = &start[newSize];