summaryrefslogtreecommitdiff
path: root/Build/source/texk/dvisvgm/dvisvgm-src/src/XMLString.cpp
blob: 31de6a7b33825743c4528e67a2788024bd0251f6 (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
/*************************************************************************
** XMLString.cpp                                                        **
**                                                                      **
** This file is part of dvisvgm -- a fast DVI to SVG converter          **
** Copyright (C) 2005-2020 Martin Gieseking <martin.gieseking@uos.de>   **
**                                                                      **
** This program is free software; you can redistribute it and/or        **
** modify it under the terms of the GNU General Public License as       **
** published by the Free Software Foundation; either version 3 of       **
** the License, or (at your option) any later version.                  **
**                                                                      **
** This program is distributed in the hope that it will be useful, but  **
** WITHOUT ANY WARRANTY; without even the implied warranty of           **
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the         **
** GNU General Public License for more details.                         **
**                                                                      **
** You should have received a copy of the GNU General Public License    **
** along with this program; if not, see <http://www.gnu.org/licenses/>. **
*************************************************************************/

#include <cmath>
#include <cstdlib>
#include "Unicode.hpp"
#include "utility.hpp"
#include "XMLString.hpp"

using namespace std;

int XMLString::DECIMAL_PLACES = 0;


static string translate (uint32_t c) {
	switch (c) {
		case '<' : return "&lt;";
		case '&' : return "&amp;";
		case '"' : return "&quot;";
		case '\'': return "&apos;";
	}
	return Unicode::utf8(c);
}


XMLString::XMLString (const string &str, bool plain) {
	if (plain)
		assign(str);
	else {
		for (char c : str)
			append(translate(c));
	}
}


XMLString::XMLString (const char *str, bool plain) {
	if (str) {
		if (plain)
			assign(str);
		else {
			while (*str)
				append(translate(*str++));
		}
	}
}


XMLString::XMLString (int n, bool cast)
	: string(cast ? to_string(n) : translate(n))
{
}


/** Rounds a floating point value to a given number of decimal places.
 *  @param[in] x number to round
 *  @param[in] n number of decimal places (must be between 1 and 6)
 *  @return rounded value */
static inline double round (double x, int n) {
	const long pow10[] = {10L, 100L, 1000L, 10000L, 100000L, 1000000L};
	const double eps = 1e-7;
	n--;
	if (x >= 0)
		return floor(x*pow10[n]+0.5+eps)/pow10[n];
	return ceil(x*pow10[n]-0.5-eps)/pow10[n];
}


XMLString::XMLString (double x) {
	if (DECIMAL_PLACES > 0) {
		// don't use fixed and setprecision() manipulators here to avoid
		// banker's rounding applied in some STL implementations
		x = round(x, DECIMAL_PLACES);
	}
	if (std::abs(x) < 1e-6)
		x = 0;
	assign(util::to_string(x));
	size_t pos = find("0.");
	if (pos != string::npos && (pos == 0 || at(pos-1) == '-'))
		erase(pos, 1);
}