Branch data TLA Line data Source code
1 : : // Copyright (c) 2024-2025 xlnt-community
2 : : //
3 : : // Permission is hereby granted, free of charge, to any person obtaining a copy
4 : : // of this software and associated documentation files (the "Software"), to deal
5 : : // in the Software without restriction, including without limitation the rights
6 : : // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7 : : // copies of the Software, and to permit persons to whom the Software is
8 : : // furnished to do so, subject to the following conditions:
9 : : //
10 : : // The above copyright notice and this permission notice shall be included in
11 : : // all copies or substantial portions of the Software.
12 : : //
13 : : // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 : : // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 : : // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 : : // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 : : // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18 : : // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19 : : // THE SOFTWARE
20 : : //
21 : : // @license: http://www.opensource.org/licenses/mit-license.php
22 : : // @author: see AUTHORS file
23 : :
24 : : #pragma once
25 : :
26 : : #include <string>
27 : : #include <vector>
28 : :
29 : : #include <detail/xlnt_config_impl.hpp>
30 : : #include <xlnt/internal/features.hpp>
31 : :
32 : : #if XLNT_HAS_INCLUDE(<string_view>) && XLNT_HAS_FEATURE(U8_STRING_VIEW)
33 : : #include <string_view>
34 : : #endif
35 : :
36 : : namespace xlnt {
37 : : namespace detail {
38 : :
39 : : // Prepends the string literal prefix to the provided string literal.
40 : : // Useful when defining a string literal once, then using it with multiple string types.
41 : : #define LSTRING_LITERAL2(a) L##a
42 : : #define U8STRING_LITERAL2(a) u8##a
43 : : #define U16STRING_LITERAL2(a) u##a
44 : : #define U32STRING_LITERAL2(a) U##a
45 : : #define LSTRING_LITERAL(a) LSTRING_LITERAL2(a)
46 : : #define U8STRING_LITERAL(a) U8STRING_LITERAL2(a)
47 : : #define U16STRING_LITERAL(a) U16STRING_LITERAL2(a)
48 : : #define U32STRING_LITERAL(a) U32STRING_LITERAL2(a)
49 : :
50 : : // Casts a UTF-8 string literal to a narrow string literal without changing its encoding.
51 : : #ifdef __cpp_char8_t
52 : : // For C++20 and newer, interpret as UTF-8 and then cast to string literal
53 : : #define U8_TO_CHAR_PTR(a) xlnt::detail::to_char_ptr(a)
54 : : #else
55 : : // For C++11, C++14 and C++17, simply interpret as UTF-8, which works with classic string literals.
56 : : #define U8_TO_CHAR_PTR(a) a
57 : : #endif
58 : :
59 : :
60 : : // The following weird cast ensures that the string is UTF-8 encoded at all costs!
61 : : #define ENSURE_UTF8_LITERAL(a) U8_TO_CHAR_PTR(U8STRING_LITERAL(a))
62 : :
63 : : #ifdef __cpp_char8_t
64 : : /// Casts const char8_t arrays from C++20 to const char arrays.
65 :CBC 5 : inline const char * to_char_ptr(const char8_t *utf8)
66 : : {
67 : 5 : return reinterpret_cast<const char *>(utf8);
68 : : }
69 : :
70 : : /// Casts char8_t arrays from C++20 to char arrays.
71 : : inline char * to_char_ptr(char8_t *utf8)
72 : : {
73 : : return reinterpret_cast<char *>(utf8);
74 : : }
75 : : #endif
76 : :
77 : : #if XLNT_HAS_FEATURE(U8_STRING_VIEW)
78 : : /// Casts std::u8string_view from C++20 to std::string_view.
79 : : inline std::string_view to_string_view(std::u8string_view utf8)
80 : : {
81 : : return std::string_view{to_char_ptr(utf8.data()), utf8.length()};
82 : : }
83 : :
84 : : /// Copies std::u8string(_view) from C++20 to std::string.
85 : 8 : inline std::string to_string_copy(std::u8string_view utf8)
86 : : {
87 [ + ]: 24 : return std::string{utf8.begin(), utf8.end()};
88 : : }
89 : : #endif
90 : :
91 : : /// <summary>
92 : : /// Return a vector containing string split at each delim.
93 : : /// If the input string is empty, an empty vector is returned.
94 : : /// </summary>
95 : : XLNT_API_INTERNAL std::vector<std::string> split_string(const std::string &string, char delim);
96 : :
97 : : /// <summary>
98 : : /// Concatenate all the provided items by converting them to a string using its to_string member function.
99 : : /// </summary>
100 : : template<typename T>
101 : 29 : std::string join(const std::vector<T> &items, char delim)
102 : : {
103 : 29 : std::string refs;
104 [ + + ]: 65 : for (const auto& item : items)
105 : : {
106 [ + + ]: 36 : if (!refs.empty())
107 [ + ]: 7 : refs.push_back(delim);
108 : :
109 [ + ][ + + ]: 36 : refs.append(item.to_string());
110 : : }
111 : :
112 : 29 : return refs;
113 :UBC 0 : }
114 : :
115 : : } // namespace detail
116 : : } // namespace xlnt
|