mini-llvm 0.1.0
Loading...
Searching...
No Matches
SystemString.h
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2
3#pragma once
4
5#include <compare>
6#include <cstddef>
7#include <format>
8#include <functional>
9#include <string>
10#include <string_view>
11#include <utility>
12
13#ifdef _WIN32
15#endif
16
17namespace mini_llvm {
18
20public:
21#ifdef _WIN32
22 using value_type = wchar_t;
23 using string_type = std::wstring;
24#else
25 using value_type = char;
26 using string_type = std::string;
27#endif
28
29 SystemString() = default;
30
31 SystemString(const char *str)
32#ifdef _WIN32
33 : str_(windows::widen(str)) {}
34#else
35 : str_(str) {}
36#endif
37
38 SystemString(const std::string &str)
39#ifdef _WIN32
40 : str_(windows::widen(str)) {}
41#else
42 : str_(str) {}
43#endif
44
45 SystemString(std::string &&str)
46#ifdef _WIN32
47 : str_(windows::widen(str)) {}
48#else
49 : str_(std::move(str)) {}
50#endif
51
52 SystemString(std::string_view str)
53#ifdef _WIN32
54 : str_(windows::widen(str)) {}
55#else
56 : str_(str) {}
57#endif
58
59#ifdef _WIN32
60 SystemString(const wchar_t *str)
61 : str_(str) {}
62
63 SystemString(const std::wstring &str)
64 : str_(str) {}
65
66 SystemString(std::wstring &&str)
67 : str_(std::move(str)) {}
68
69 SystemString(std::wstring_view str)
70 : str_(str) {}
71#endif
72
73 SystemString(const SystemString &other) = default;
74 SystemString(SystemString &&other) noexcept = default;
75
77 swap(other);
78 return *this;
79 }
80
81 string_type &native() & noexcept {
82 return str_;
83 }
84
85 const string_type &native() const & noexcept {
86 return str_;
87 }
88
89 string_type &&native() && noexcept {
90 return std::move(str_);
91 }
92
93 const value_type *c_str() const noexcept {
94 return str_.c_str();
95 }
96
97 std::string to_string() const & {
98#ifdef _WIN32
99 return windows::narrow(str_);
100#else
101 return str_;
102#endif
103 }
104
105 std::string to_string() && {
106#ifdef _WIN32
107 return windows::narrow(str_);
108#else
109 return std::move(str_);
110#endif
111 }
112
113 void swap(SystemString &other) noexcept {
114 str_.swap(other.str_);
115 }
116
117private:
118 string_type str_;
119};
120
121inline void swap(SystemString &lhs, SystemString &rhs) noexcept {
122 lhs.swap(rhs);
123}
124
125inline bool operator==(const SystemString &lhs, const SystemString &rhs) noexcept {
126 return lhs.native() == rhs.native();
127}
128
129inline std::strong_ordering operator<=>(const SystemString &lhs, const SystemString &rhs) noexcept {
130 return lhs.native() <=> rhs.native();
131}
132
133} // namespace mini_llvm
134
135template <>
136struct std::hash<mini_llvm::SystemString> {
137 size_t operator()(const mini_llvm::SystemString &str) const noexcept {
138 return std::hash<mini_llvm::SystemString::string_type>()(str.native());
139 }
140};
141
142template <>
143struct std::formatter<mini_llvm::SystemString> {
144 constexpr auto parse(std::format_parse_context &ctx) {
145 return ctx.begin();
146 }
147
148 template <typename FormatContext>
149 auto format(const mini_llvm::SystemString &str, FormatContext &ctx) const {
150 return std::format_to(ctx.out(), "{}", str.to_string());
151 }
152};
Definition SystemString.h:19
char value_type
Definition SystemString.h:25
SystemString(SystemString &&other) noexcept=default
SystemString(const SystemString &other)=default
std::string to_string() &&
Definition SystemString.h:105
SystemString(const char *str)
Definition SystemString.h:31
const string_type & native() const &noexcept
Definition SystemString.h:85
SystemString(std::string_view str)
Definition SystemString.h:52
string_type & native() &noexcept
Definition SystemString.h:81
SystemString(const std::string &str)
Definition SystemString.h:38
std::string string_type
Definition SystemString.h:26
std::string to_string() const &
Definition SystemString.h:97
SystemString & operator=(SystemString other) noexcept
Definition SystemString.h:76
const value_type * c_str() const noexcept
Definition SystemString.h:93
void swap(SystemString &other) noexcept
Definition SystemString.h:113
string_type && native() &&noexcept
Definition SystemString.h:89
SystemString(std::string &&str)
Definition SystemString.h:45
Definition GraphColoringAllocator.h:13
void swap(FileHandle &lhs, FileHandle &rhs) noexcept
Definition FileHandle.h:61
MINI_LLVM_EXPORT bool operator==(const BigInteger &lhs, const BigInteger &rhs) noexcept
MINI_LLVM_EXPORT std::strong_ordering operator<=>(const BigInteger &lhs, const BigInteger &rhs) noexcept
auto format(const mini_llvm::SystemString &str, FormatContext &ctx) const
Definition SystemString.h:149
constexpr auto parse(std::format_parse_context &ctx)
Definition SystemString.h:144
size_t operator()(const mini_llvm::SystemString &str) const noexcept
Definition SystemString.h:137