Skip to content

Commit 1c667b5

Browse files
committed
supports string literal
1 parent 6ddd86a commit 1c667b5

2 files changed

Lines changed: 70 additions & 0 deletions

File tree

include/ext/convert.hpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include <climits>
1313
#include <cstdint>
1414
#include <type_traits>
15+
#include <string_view>
1516

1617
#ifndef PAINFUL_CPP_STRING_CONVERSION_NAMESPACE
1718
#define PAINFUL_CPP_STRING_CONVERSION_NAMESPACE ext
@@ -291,6 +292,12 @@ namespace PAINFUL_CPP_STRING_CONVERSION_NAMESPACE {
291292
return d;
292293
}
293294
}
295+
296+
template<typename Target, typename InputChar, size_t InputLength>
297+
Target PAINFUL_CPP_STRING_CONVERSION_FUNCTION(InputChar const (&s)[InputLength], bool extended = false) {
298+
static_assert(InputLength >= 1, "null-terminate is required");
299+
return PAINFUL_CPP_STRING_CONVERSION_FUNCTION <Target>(std::basic_string_view<InputChar>{ s, InputLength - 1 }, extended);
300+
}
294301
}
295302

296303
#ifdef PAINFUL_CPP_STRING_CONVERSION_NAMESPACE_DEFAULT

test/test.cpp

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ namespace {
2828
}
2929
}
3030

31+
using std::string_view_literals::operator ""sv;
32+
3133
int main() {
3234
for (char32_t c = 0; c <= 0x10'ffff; c++) {
3335
if (ext::details::is_utf16_h(c) || ext::details::is_utf16_l(c)) {
@@ -194,6 +196,67 @@ int main() {
194196
assert_true(intermediate_string == std::string_view(string_data, string_size));
195197
assert_true(intermediate_string_utf16 == std::u16string_view(string_data_utf16, string_size_utf16));
196198
}
199+
{
200+
#define HELLO_WORLD "Hello world! 你好世界!こんにちは世界!"
201+
202+
constexpr auto c{HELLO_WORLD ""sv};
203+
constexpr auto u8{u8"" HELLO_WORLD ""sv};
204+
constexpr auto u16{u"" HELLO_WORLD ""sv};
205+
constexpr auto u32{U"" HELLO_WORLD ""sv};
206+
constexpr auto w{L"" HELLO_WORLD ""sv};
207+
208+
auto const c_u8 = ext::convert<std::u8string>(HELLO_WORLD);
209+
auto const c_u16 = ext::convert<std::u16string>(HELLO_WORLD);
210+
auto const c_u32 = ext::convert<std::u32string>(HELLO_WORLD);
211+
auto const c_w = ext::convert<std::wstring>(HELLO_WORLD);
212+
213+
auto const u8_c = ext::convert<std::string>(u8"" HELLO_WORLD);
214+
auto const u8_u16 = ext::convert<std::u16string>(u8"" HELLO_WORLD);
215+
auto const u8_u32 = ext::convert<std::u32string>(u8"" HELLO_WORLD);
216+
auto const u8_w = ext::convert<std::wstring>(u8"" HELLO_WORLD);
217+
218+
auto const u16_c = ext::convert<std::string>(u"" HELLO_WORLD);
219+
auto const u16_u8 = ext::convert<std::u8string>(u"" HELLO_WORLD);
220+
auto const u16_u32 = ext::convert<std::u32string>(u"" HELLO_WORLD);
221+
auto const u16_w = ext::convert<std::wstring>(u"" HELLO_WORLD);
222+
223+
auto const u32_c = ext::convert<std::string>(U"" HELLO_WORLD);
224+
auto const u32_u8 = ext::convert<std::u8string>(U"" HELLO_WORLD);
225+
auto const u32_u16 = ext::convert<std::u16string>(U"" HELLO_WORLD);
226+
auto const u32_w = ext::convert<std::wstring>(U"" HELLO_WORLD);
227+
228+
auto const w_c = ext::convert<std::string>(L"" HELLO_WORLD);
229+
auto const w_u8 = ext::convert<std::u8string>(L"" HELLO_WORLD);
230+
auto const w_u16 = ext::convert<std::u16string>(L"" HELLO_WORLD);
231+
auto const w_u32 = ext::convert<std::u32string>(L"" HELLO_WORLD);
232+
233+
assert_true(c == u8_c);
234+
assert_true(c == u16_c);
235+
assert_true(c == u32_c);
236+
assert_true(c == w_c);
237+
238+
#ifdef __cpp_char8_t
239+
assert_true(u8 == c_u8);
240+
assert_true(u8 == u16_u8);
241+
assert_true(u8 == u32_u8);
242+
assert_true(u8 == w_u8);
243+
#endif
244+
245+
assert_true(u16 == c_u16);
246+
assert_true(u16 == u8_u16);
247+
assert_true(u16 == u32_u16);
248+
assert_true(u16 == w_u16);
249+
250+
assert_true(u32 == c_u32);
251+
assert_true(u32 == u8_u32);
252+
assert_true(u32 == u16_u32);
253+
assert_true(u32 == w_u32);
254+
255+
assert_true(w == c_w);
256+
assert_true(w == u8_w);
257+
assert_true(w == u16_w);
258+
assert_true(w == u32_w);
259+
}
197260
std::printf("All test pass.\n");
198261
return 0;
199262
}

0 commit comments

Comments
 (0)