• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

xlnt-community / xlnt / 67853142-21b5-4e24-946e-3f0c93e251de

05 Feb 2025 10:00AM UTC coverage: 81.87% (-0.1%) from 81.978%
67853142-21b5-4e24-946e-3f0c93e251de

push

circleci

web-flow
Add support for C++20 and C++23, and experimental support for C++26 (#55)

Changes made by this PR:

1. Support `char8_t` and `std::u8string(_view)` in user-facing code to
support `u8""` string literals in C++20 and newer.
2. Remove `std::aligned_storage` from `xlnt::optional` due to C++23
deprecation warnings.
3. Add experimental support for C++26 - no further changes were
necessary (not yet, at least). I think we can use this to test new C++
compiler versions, but I would not guarantee C++26 support until it is
officially released.
4. Set CI to use C++23. I think it's better to not use C++26 yet to
avoid issues when new compiler versions are released.
5. Fix Unicode file paths on Windows when checking for existence of
files and folders, and when reading a file into a string.


Note: instead of using `const std::u8string &` like we do with
`std::string` in our current code, I chose to use `std::u8string_view`
to avoid unnecessarily copying strings, as we currently do in many cases
(which we cannot properly avoid except when using C strings (bad idea
IMO) or by switching to C++17 as a minimum). We should also switch from
`const std::string &` to `std::string_view` when switching to C++17 as a
minimum in the future. But, for now, I simply assumed support for
`std::string_view` (C++17) when using C++20 features, which is something
that should hopefully always work.

108 of 150 new or added lines in 8 files covered. (72.0%)

4 existing lines in 2 files now uncovered.

11488 of 14032 relevant lines covered (81.87%)

1191666.72 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

50.0
./source/detail/serialization/open_stream.cpp
1
// Copyright (c) 2017-2022 Thomas Fussell
2
// Copyright (c) 2024-2025 xlnt-community
3
//
4
// Permission is hereby granted, free of charge, to any person obtaining a copy
5
// of this software and associated documentation files (the "Software"), to deal
6
// in the Software without restriction, including without limitation the rights
7
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
// copies of the Software, and to permit persons to whom the Software is
9
// furnished to do so, subject to the following conditions:
10
//
11
// The above copyright notice and this permission notice shall be included in
12
// all copies or substantial portions of the Software.
13
//
14
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20
// THE SOFTWARE
21
//
22
// @license: http://www.opensource.org/licenses/mit-license.php
23
// @author: see AUTHORS file
24

25
#include <xlnt/utils/path.hpp>
26
#include <detail/serialization/open_stream.hpp>
27

28
#if XLNT_HAS_INCLUDE(<filesystem>)
29
  #include <filesystem>
30
#endif
31

32
namespace xlnt {
33
namespace detail {
34

35
void open_stream(std::ifstream &stream, const std::string &path)
88✔
36
{
37
#ifdef _MSC_VER
38
    open_stream(stream, xlnt::path(path).wstring());
39
#else
40
    stream.open(path, std::ios::binary);
88✔
41
#endif
42
}
88✔
43

44
void open_stream(std::ofstream &stream, const std::string &path)
75✔
45
{
46
#ifdef _MSC_VER
47
    open_stream(stream, xlnt::path(path).wstring());
48
#else
49
    stream.open(path, std::ios::binary);
75✔
50
#endif
51
}
75✔
52

53
#ifdef _MSC_VER
54
void open_stream(std::ifstream &stream, const std::wstring &path)
55
{
56
    stream.open(path, std::ios::binary);
57
}
58

59
void open_stream(std::ofstream &stream, const std::wstring &path)
60
{
61
    stream.open(path, std::ios::binary);
62
}
63
#endif
64

65
#if XLNT_HAS_FEATURE(U8_STRING_VIEW)
NEW
66
void open_stream(std::ifstream &stream, std::u8string_view path)
×
67
{
68
#ifdef _MSC_VER
69
    open_stream(stream, xlnt::path(path).wstring());
70
#elif XLNT_HAS_FEATURE(FILESYSTEM)
NEW
71
    stream.open(std::filesystem::path(path), std::ios::binary);
×
72
#else
73
    // TODO: this cannot work if the user's locale is not UTF-8. In such cases we cannot ensure
74
    // that this always works - however, in such cases we can still attempt to do a conversion to
75
    // the locale encoding, which will still work if the string can be represented
76
    // with the user's locale encoding.
77
    // NOTE: this code will only run if C++17 is only partially implemented,
78
    // but C++17 string_view and C++20 char8_t are implemented, while C++17 filesystem is not.
79
    stream.open(to_char_ptr(path.data()), std::ios::binary);
80
#endif
UNCOV
81
}
×
82

NEW
83
void open_stream(std::ofstream &stream, std::u8string_view path)
×
84
{
85
#ifdef _MSC_VER
86
    open_stream(stream, xlnt::path(path).wstring());
87
#elif XLNT_HAS_FEATURE(FILESYSTEM)
NEW
88
    stream.open(std::filesystem::path(path), std::ios::binary);
×
89
#else
90
    // TODO: this cannot work if the user's locale is not UTF-8. In such cases we cannot ensure
91
    // that this always works - however, in such cases we can still attempt to do a conversion to
92
    // the locale encoding, which will still work if the string can be represented
93
    // with the user's locale encoding.
94
    // NOTE: this code will only run if C++17 is only partially implemented,
95
    // but C++17 string_view and C++20 char8_t are implemented, while C++17 filesystem is not.
96
    stream.open(to_char_ptr(path.data()), std::ios::binary);
97
#endif
UNCOV
98
}
×
99
#endif
100

101
} // namespace detail
102
} // namespace xlnt
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc