summaryrefslogtreecommitdiff
path: root/src/utilcode/outstring.cpp
blob: 150ef8ab53aa62429976e1de98e5bb484a2a6789 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

/*****************************************************************/
/*                         OutString.cpp                         */
/*****************************************************************/
/* A simple, lightweight, character output stream, with very few
   external dependancies (like sprintf ... ) */

/*  
   Date :  2/1/99 				*/
/*****************************************************************/

#include "stdafx.h"
#include "outstring.h"


/*****************************************************************/
//  print out 'count' instances of the character 'c'
OutString& OutString::pad(size_t count, char c) {
	if (cur+count > end)
		Realloc(count);
	memset(cur, c, count);
	cur = cur + count;
	return(*this);
}

/*****************************************************************/
// prints out a decimal representation
OutString& OutString::operator<<(double d) {

	if (d == 0.0) {
		*this << "0.0";
		return *this;
	}

	if (d < 0) {
		d = -d;
		*this << '-';
	}

		// compute the exponent
	int exponent = 0;
	while (d > 10.0)  {
		d /= 10;
		exponent++;
		if (exponent > 500) {		// avoids a possible infinite loop
            *this << "INF";		
			return *this;
		}
	}
	while (d < 1.0)  {
		d *= 10;
		--exponent;
		if (exponent < -500) {		// avoids a possible infinite loop
			*this << "0.0";		
			return *this;
		}
	}

	// we now have a normalized d (between 1 and 10)
	double delta = .5E-10;		
	d += delta;		// round to the precision we are displaying

	unsigned trailingZeros = 0;
	for(unsigned i = 0; i < 10; i++) {
		int digit = (int) d;
		d = (d - digit) * 10;		// ISSUE: does roundoff ever bite us here?
	
		if (digit == 0)		// defer printing traiing zeros
			trailingZeros++;
		else {
			if (trailingZeros > 0) {
				this->pad(trailingZeros, '0');
				trailingZeros = 0;
			}
		*this << (char) ('0' + digit);
		}
		if (i == 0)
			*this << '.';
	
	}
	if (exponent != 0) {
		*this << 'E';
		*this << exponent;
	}
	return(*this);
}

/*****************************************************************/
// prints out a decimal representation
OutString& OutString::dec(int i, size_t minWidth) {
	char buff[12];			// big enough for any number (10 digits, - sign, null term)
	char* ptr = &buff[11];
	*ptr = 0;

	unsigned val = i;
	if (i < 0)
		val = -i;	// note this happens to also work for minint!

	for(;;) {
		if (val < 10) {
			*--ptr = '0' + val;
			break;
			}
		*--ptr = '0' + (val % 10);
		val = val / 10;
		}

	if (i < 0)
		*--ptr = '-';
	
	size_t len = &buff[11] - ptr; 	// length of string
	if (len < minWidth)
		pad(minWidth-len, ' ');
	
	*this << ptr;
	return(*this);
}

/*****************************************************************/
OutString& OutString::hex(unsigned __int64 i, int minWidth, unsigned flags) {

	unsigned hi = unsigned(i >> 32);
	unsigned low = unsigned(i);
	
	if (hi != 0) {
		minWidth -= 8;
		hex(hi, minWidth, flags);		// print upper bits
		flags = zeroFill;
		minWidth = 8;
	}
	return hex(low, minWidth, flags);	// print lower bits
}

/*****************************************************************/
OutString& OutString::hex(unsigned i, int minWidth, unsigned flags) {
	char buff[12];			// big enough for any number 
	char* ptr = &buff[11];
	*ptr = 0;

    static const char digits[] = "0123456789ABCDEF";

	for(;;) {
		if (i < 16) {
			*--ptr = digits[i];
			break;
			}
		*--ptr = digits[(i % 16)];
		i = i / 16;
		}

	size_t len = &buff[11] - ptr; 			// length of string
	if (flags & put0x) {
        if (flags & zeroFill)
		    *this << "0x";
        else
            *--ptr = 'x', *--ptr = '0';
		len += 2;
		}

	if (len < (size_t)minWidth)
		pad(minWidth-len, (flags & zeroFill) ? '0' : ' ');
	
	*this << ptr;
	return(*this);
}

/*****************************************************************/
void OutString::Realloc(size_t neededSpace)  {
    size_t oldSize = cur-start;
	size_t newSize = (oldSize + neededSpace) * 3 / 2 + 32;
	char* oldBuff = start;
	start = new char[newSize+1];
	memcpy(start, oldBuff, oldSize);
	cur = &start[oldSize];
	end = &start[newSize];
	delete [] oldBuff;
}