OpenXLSX 1.12.1
Modern C++ Excel Library (Fork of OpenXLSX by Kenneth Troldal Balslev)
Loading...
Searching...
No Matches
XLStyles_Internal.hpp
Go to the documentation of this file.
1/*
2 * OpenXLSX-NX - Modern High-Performance C++ Excel Library
3 *
4 * Original work Copyright (c) 2018-2025, Kenneth Troldal Balslev and OpenXLSX contributors
5 * Modified work Copyright (c) 2026, Curry Tang and OpenXLSX-NX contributors
6 *
7 * Distributed under the BSD 3-Clause License.
8 * See LICENSE.md in the project root or https://opensource.org/licenses/BSD-3-Clause
9 */
10
11#ifndef OPENXLSX_XLSTYLES_INTERNAL_HPP
12#define OPENXLSX_XLSTYLES_INTERNAL_HPP
13
14#include <cctype>
15#include <string>
16#include <string_view>
17
18#include <fmt/format.h>
19#include <pugixml.hpp>
20
21#include "XLException.hpp"
22#include "XLXmlParser.hpp"
23
24namespace OpenXLSX
25{
26
27 inline void copyXMLNode(XMLNode& destination, const XMLNode& source)
28 {
29 if (not source.empty()) {
30 // ===== Copy all XML child nodes
31 for (XMLNode child = source.first_child(); not child.empty(); child = child.next_sibling()) destination.append_copy(child);
32 // ===== Copy all XML attributes
33 for (XMLAttribute attr = source.first_attribute(); not attr.empty(); attr = attr.next_attribute())
34 destination.append_copy(attr);
35 }
36 }
37
45 inline void xmlNodeFingerprintImpl(const XMLNode& node, std::string& fp)
46 {
47 // Attributes first, in document order
48 for (XMLAttribute attr = node.first_attribute(); attr; attr = attr.next_attribute()) {
49 fp.append(attr.name());
50 fp.push_back('=');
51 fp.append(attr.value());
52 fp.push_back(';');
53 }
54 // Element and text children
55 for (XMLNode child = node.first_child(); child; child = child.next_sibling()) {
56 if (child.type() == pugi::node_pcdata) {
57 std::string_view v(child.value());
58 // Skip whitespace-only text (indentation artifacts)
59 bool isWhitespace = std::all_of(v.begin(), v.end(), [](char c) { return std::isspace(static_cast<unsigned char>(c)); });
60 if (isWhitespace) continue;
61 fp.push_back('[');
62 fp.append(v);
63 fp.push_back(']');
64 }
65 else {
66 fp.push_back('<');
67 fp.append(child.name());
68 fp.push_back(':');
69 xmlNodeFingerprintImpl(child, fp);
70 fp.push_back('>');
71 }
72 }
73 }
74
75 inline std::string xmlNodeFingerprint(const XMLNode& node)
76 {
77 std::string fp;
78 fp.reserve(256);
79 xmlNodeFingerprintImpl(node, fp);
80 return fp;
81 }
82
83 template<typename E>
85 {
86 std::string_view str;
87 E val;
88 };
89
90 template<typename E, size_t N>
91 constexpr E EnumFromString(std::string_view str, const EnumStringMap<E> (&mapping)[N], E invalidVal)
92 {
93 for (const auto& kv : mapping) {
94 if (kv.str == str) return kv.val;
95 }
96 return invalidVal;
97 }
98
99 template<typename E, size_t N>
100 constexpr const char* EnumToString(E val, const EnumStringMap<E> (&mapping)[N], const char* invalidStr)
101 {
102 for (const auto& kv : mapping) {
103 if (kv.val == val) return kv.str.data();
104 }
105 return invalidStr;
106 }
107
111 inline std::string formatDoubleAsString(double val, int decimalPlaces = 2) { return fmt::format("{:.{}f}", val, decimalPlaces); }
112
116 inline std::string checkAndFormatDoubleAsString(double val, double min, double max, double absTolerance, int decimalPlaces = 2)
117 {
118 if (max <= min or absTolerance < 0.0)
119 throw XLException("checkAndFormatDoubleAsString: max must be greater than min and absTolerance must be >= 0.0");
120 if (val < min - absTolerance or val > max + absTolerance) return ""; // range check
121 if (val < min)
122 val = min;
123 else if (val > max)
124 val = max; // fix rounding errors within tolerance
125 return formatDoubleAsString(val, decimalPlaces);
126 }
127
128} // namespace OpenXLSX
129
130#endif // OPENXLSX_XLSTYLES_INTERNAL_HPP
XMLAttribute first_attribute() const
Definition XLXmlParser.cpp:102
XMLNode first_child() const
Definition XLXmlParser.cpp:192
XMLNode next_sibling(const char *name_) const
Definition XLXmlParser.cpp:156
bool empty() const
Definition XLXmlParser.cpp:58
XMLNode append_copy(const XMLNode &node)
Definition XLXmlParser.cpp:427
Definition XLException.hpp:33
Definition XLXmlParser.hpp:283
bool empty() const
Definition XLXmlParser.cpp:513
XMLAttribute next_attribute() const
Definition XLXmlParser.cpp:635
Definition IXLCellProvider.hpp:24
std::string formatDoubleAsString(double val, int decimalPlaces=2)
Format val as a string with decimalPlaces.
Definition XLStyles_Internal.hpp:111
std::string xmlNodeFingerprint(const XMLNode &node)
Definition XLStyles_Internal.hpp:75
void copyXMLNode(XMLNode &destination, const XMLNode &source)
Definition XLStyles_Internal.hpp:27
constexpr E EnumFromString(std::string_view str, const EnumStringMap< E >(&mapping)[N], E invalidVal)
Definition XLStyles_Internal.hpp:91
void xmlNodeFingerprintImpl(const XMLNode &node, std::string &fp)
Compute a canonical, deterministic string fingerprint of a pugixml subtree.
Definition XLStyles_Internal.hpp:45
std::string checkAndFormatDoubleAsString(double val, double min, double max, double absTolerance, int decimalPlaces=2)
Check that a double value is within range, and format it as a string with decimalPlaces.
Definition XLStyles_Internal.hpp:116
OpenXLSX_xml_node XMLNode
Definition XLXmlParser.hpp:64
constexpr const char * EnumToString(E val, const EnumStringMap< E >(&mapping)[N], const char *invalidStr)
Definition XLStyles_Internal.hpp:100
Definition XLStyles_Internal.hpp:85
std::string_view str
Definition XLStyles_Internal.hpp:86
E val
Definition XLStyles_Internal.hpp:87