Skip to content

Commit 81c8fe2

Browse files
committed
update C++ version control & cmake script
1 parent a838c5c commit 81c8fe2

3 files changed

Lines changed: 18 additions & 0 deletions

File tree

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall")
2020

2121

2222
install(TARGETS pstring
23+
EXPORT pstringTargets
2324
ARCHIVE DESTINATION lib
2425
LIBRARY DESTINATION lib
2526
RUNTIME DESTINATION bin

include/pstring.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,11 @@
66
#include <iostream>
77
#include <unordered_map>
88
#if __cplusplus >= 202002L
9+
#include <version>
10+
#ifdef __cpp_lib_format
911
#include <format>
1012
#endif
13+
#endif
1114

1215
class PString {
1316
public:
@@ -43,10 +46,12 @@ class PString {
4346
int find(const PString& sub, size_t start=0, size_t end=std::string::npos) const;
4447

4548
#if __cplusplus >= 202002L
49+
#ifdef __cpp_lib_format
4650
template <typename... Args>
4751
inline PString format(Args&&... args) const{
4852
return PString(std::vformat(std::string_view(this->str_), std::make_format_args(args...)));
4953
}
54+
#endif
5055
#endif
5156

5257
int index(const PString& sub, size_t start=0, size_t end=std::string::npos) const;
@@ -201,6 +206,7 @@ namespace std {
201206
}
202207

203208
#if __cplusplus >= 202002L
209+
#ifdef __cpp_lib_format
204210
template <>
205211
struct std::formatter<PString> {
206212
constexpr auto parse(std::format_parse_context& ctx) {
@@ -211,6 +217,7 @@ struct std::formatter<PString> {
211217
}
212218
};
213219
#endif
220+
#endif
214221

215222

216223

test/test.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,13 @@
22
#include <vector>
33
#include <string>
44
#include <ostream>
5+
#include <version>
6+
#if __cplusplus >= 202002L
7+
#include <version>
8+
#ifdef __cpp_lib_format
59
#include <format>
10+
#endif
11+
#endif
612
#include <cassert>
713
#include "pstring.h"
814

@@ -112,9 +118,11 @@ int main(){
112118
cout << str_count.repr() + ".count(\"abc\",1,9) = " << str_count.count("abc",1,9) << endl;
113119

114120
#if __cplusplus >= 202002L
121+
#ifdef __cpp_lib_format
115122
PString fmt = "Hello, {}!";
116123
cout << fmt.repr() + ".format(\"World\") = " << fmt.format("World").repr() << endl;
117124
#endif
125+
#endif
118126

119127

120128
PString str_istitle1 = "Hello World!";
@@ -234,7 +242,9 @@ int main(){
234242
cout << str_equal_.repr() + " == " << str_equal_other3 << " = " << (str_equal_ == str_equal_other3) << std::endl;
235243

236244
#if __cplusplus >= 202002L
245+
#ifdef __cpp_lib_format
237246
cout << std::format("str_equal: {}",str_equal) << std::endl;
247+
#endif
238248
#endif
239249
const_iter(PString("123abc"));
240250

0 commit comments

Comments
 (0)