-
Notifications
You must be signed in to change notification settings - Fork 246
/
Copy pathbase_string.h
726 lines (605 loc) · 18.6 KB
/
base_string.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
namespace winrt::impl
{
struct atomic_ref_count
{
atomic_ref_count() noexcept = default;
explicit atomic_ref_count(uint32_t count) noexcept : m_count(count)
{
}
uint32_t operator=(uint32_t count) noexcept
{
return m_count = count;
}
uint32_t operator++() noexcept
{
return static_cast<uint32_t>(m_count.fetch_add(1, std::memory_order_relaxed) + 1);
}
uint32_t operator--() noexcept
{
auto const remaining = m_count.fetch_sub(1, std::memory_order_release) - 1;
if (remaining == 0)
{
std::atomic_thread_fence(std::memory_order_acquire);
}
else if (remaining < 0)
{
abort();
}
return static_cast<uint32_t>(remaining);
}
operator uint32_t() const noexcept
{
return static_cast<uint32_t>(m_count);
}
private:
std::atomic<int32_t> m_count;
};
constexpr uint32_t hstring_reference_flag{ 1 };
struct hstring_header
{
uint32_t flags;
uint32_t length;
uint32_t padding1;
uint32_t padding2;
wchar_t const* ptr;
};
struct shared_hstring_header : hstring_header
{
atomic_ref_count count;
wchar_t buffer[1];
};
inline void release_hstring(hstring_header* handle) noexcept
{
WINRT_ASSERT((handle->flags & hstring_reference_flag) == 0);
if (0 == --static_cast<shared_hstring_header*>(handle)->count)
{
WINRT_IMPL_HeapFree(WINRT_IMPL_GetProcessHeap(), 0, handle);
}
}
inline shared_hstring_header* precreate_hstring_on_heap(uint32_t length)
{
WINRT_ASSERT(length != 0);
uint64_t bytes_required = static_cast<uint64_t>(sizeof(shared_hstring_header)) + static_cast<uint64_t>(sizeof(wchar_t)) * static_cast<uint64_t>(length);
if (bytes_required > UINT_MAX)
{
throw std::invalid_argument("length");
}
auto header = static_cast<shared_hstring_header*>(WINRT_IMPL_HeapAlloc(WINRT_IMPL_GetProcessHeap(), 0, static_cast<std::size_t>(bytes_required)));
if (!header)
{
throw std::bad_alloc();
}
header->flags = 0;
header->length = length;
header->ptr = header->buffer;
header->count = 1;
header->buffer[length] = 0;
return header;
}
inline hstring_header* create_hstring_on_heap(wchar_t const* value, uint32_t length)
{
if (!length)
{
return nullptr;
}
auto header = precreate_hstring_on_heap(length);
memcpy_s(header->buffer, sizeof(wchar_t) * length, value, sizeof(wchar_t) * length);
return header;
}
inline void create_hstring_on_stack(hstring_header& header, wchar_t const* value, uint32_t length) noexcept
{
WINRT_ASSERT(value);
WINRT_ASSERT(length != 0);
if (value[length] != 0)
{
abort();
}
header.flags = hstring_reference_flag;
header.length = length;
header.ptr = value;
}
inline hstring_header* duplicate_hstring(hstring_header* handle)
{
if (!handle)
{
return nullptr;
}
else if ((handle->flags & hstring_reference_flag) == 0)
{
++static_cast<shared_hstring_header*>(handle)->count;
return handle;
}
else
{
return create_hstring_on_heap(handle->ptr, handle->length);
}
}
struct hstring_traits
{
using type = hstring_header*;
static void close(type value) noexcept
{
release_hstring(value);
}
static constexpr type invalid() noexcept
{
return nullptr;
}
};
}
WINRT_EXPORT namespace winrt
{
struct hstring
{
using value_type = wchar_t;
using size_type = uint32_t;
using const_reference = value_type const&;
using pointer = value_type*;
using const_pointer = value_type const*;
using const_iterator = const_pointer;
using const_reverse_iterator = std::reverse_iterator<const_iterator>;
hstring() noexcept = default;
hstring(void* ptr, take_ownership_from_abi_t) noexcept : m_handle(static_cast<impl::hstring_header*>(ptr))
{
}
hstring(hstring const& value) :
m_handle(impl::duplicate_hstring(value.m_handle.get()))
{}
hstring& operator=(hstring const& value)
{
m_handle.attach(impl::duplicate_hstring(value.m_handle.get()));
return*this;
}
hstring(hstring&&) noexcept = default;
hstring& operator=(hstring&&) = default;
hstring(std::nullptr_t) = delete;
hstring& operator=(std::nullptr_t) = delete;
hstring(std::initializer_list<wchar_t> value) :
hstring(value.begin(), static_cast<uint32_t>(value.size()))
{}
hstring(wchar_t const* value) :
hstring(std::wstring_view(value))
{}
hstring(wchar_t const* value, size_type size) :
m_handle(impl::create_hstring_on_heap(value, size))
{}
explicit hstring(std::wstring_view const& value) :
hstring(value.data(), static_cast<size_type>(value.size()))
{}
hstring& operator=(std::wstring_view const& value)
{
return *this = hstring{ value };
}
hstring& operator=(wchar_t const* const value)
{
return *this = hstring{ value };
}
hstring& operator=(std::initializer_list<wchar_t> value)
{
return *this = hstring{ value };
}
void clear() noexcept
{
m_handle.close();
}
operator std::wstring_view() const noexcept
{
if (m_handle)
{
return{ m_handle.get()->ptr, m_handle.get()->length };
}
else
{
return { L"", 0 };
}
}
const_reference operator[](size_type pos) const noexcept
{
WINRT_ASSERT(pos < size());
return*(begin() + pos);
}
const_reference front() const noexcept
{
WINRT_ASSERT(!empty());
return*begin();
}
const_reference back() const noexcept
{
WINRT_ASSERT(!empty());
return*(end() - 1);
}
const_pointer data() const noexcept
{
return c_str();
}
const_pointer c_str() const noexcept
{
if (!empty())
{
return begin();
}
else
{
return L"";
}
}
const_iterator begin() const noexcept
{
if (m_handle)
{
return m_handle.get()->ptr;
}
else
{
return {};
}
}
const_iterator cbegin() const noexcept
{
return begin();
}
const_iterator end() const noexcept
{
if (m_handle)
{
return m_handle.get()->ptr + m_handle.get()->length;
}
else
{
return {};
}
}
const_iterator cend() const noexcept
{
return end();
}
const_reverse_iterator rbegin() const noexcept
{
return const_reverse_iterator(end());
}
const_reverse_iterator crbegin() const noexcept
{
return rbegin();
}
const_reverse_iterator rend() const noexcept
{
return const_reverse_iterator(begin());
}
const_reverse_iterator crend() const noexcept
{
return rend();
}
#ifdef __cpp_lib_starts_ends_with
bool starts_with(wchar_t const value) const noexcept
{
return operator std::wstring_view().starts_with(value);
}
bool starts_with(std::wstring_view const another) const noexcept
{
return operator std::wstring_view().starts_with(another);
}
bool starts_with(const wchar_t* const pointer) const noexcept
{
return operator std::wstring_view().starts_with(pointer);
}
bool ends_with(wchar_t const value) const noexcept
{
return operator std::wstring_view().ends_with(value);
}
bool ends_with(std::wstring_view const another) const noexcept
{
return operator std::wstring_view().ends_with(another);
}
bool ends_with(const wchar_t* const pointer) const noexcept
{
return operator std::wstring_view().ends_with(pointer);
}
#endif
bool empty() const noexcept
{
return !m_handle;
}
size_type size() const noexcept
{
if (m_handle)
{
return m_handle.get()->length;
}
else
{
return 0;
}
}
friend void swap(hstring& left, hstring& right) noexcept
{
swap(left.m_handle, right.m_handle);
}
private:
handle_type<impl::hstring_traits> m_handle;
};
inline void* get_abi(hstring const& object) noexcept
{
return *(void**)(&object);
}
inline void** put_abi(hstring& object) noexcept
{
object.clear();
return reinterpret_cast<void**>(&object);
}
inline void attach_abi(hstring& object, void* value) noexcept
{
object.clear();
*put_abi(object) = value;
}
inline void* detach_abi(hstring& object) noexcept
{
void* temp = get_abi(object);
*reinterpret_cast<void**>(&object) = nullptr;
return temp;
}
inline void* detach_abi(hstring&& object) noexcept
{
return detach_abi(object);
}
inline void copy_from_abi(hstring& object, void* value)
{
attach_abi(object, impl::duplicate_hstring(static_cast<impl::hstring_header*>(value)));
}
inline void copy_to_abi(hstring const& object, void*& value)
{
WINRT_ASSERT(value == nullptr);
value = impl::duplicate_hstring(static_cast<impl::hstring_header*>(get_abi(object)));
}
inline void* detach_abi(std::wstring_view const& value)
{
return impl::create_hstring_on_heap(value.data(), static_cast<uint32_t>(value.size()));
}
inline void* detach_abi(wchar_t const* const value)
{
return impl::create_hstring_on_heap(value, static_cast<uint32_t>(wcslen(value)));
}
}
#ifdef __cpp_lib_format
template<>
struct std::formatter<winrt::hstring, wchar_t> : std::formatter<std::wstring_view, wchar_t> {};
#endif
namespace winrt::impl
{
template <> struct abi<hstring>
{
using type = void*;
};
template <> struct category<hstring>
{
using type = basic_category;
};
struct hstring_builder
{
hstring_builder(hstring_builder const&) = delete;
hstring_builder& operator=(hstring_builder const&) = delete;
explicit hstring_builder(uint32_t const size) :
m_handle(impl::precreate_hstring_on_heap(size))
{
}
wchar_t* data() noexcept
{
return const_cast<wchar_t*>(m_handle.get()->ptr);
}
hstring to_hstring()
{
return { m_handle.detach(), take_ownership_from_abi };
}
private:
handle_type<impl::hstring_traits> m_handle;
};
template <typename T>
struct bind_in
{
bind_in(T const& object) noexcept : object(object)
{
}
T const& object;
#if !defined(__GNUC__) || defined(__clang__)
template <typename R>
operator R const& () const noexcept
{
return reinterpret_cast<R const&>(object);
}
#else
// HACK: GCC does not handle template deduction of const T& conversion
// function according to CWG issue 976. To make this compile on GCC,
// we have to intentionally drop the const qualifier.
// Ref: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=61663
template <typename R>
operator R& () const noexcept
{
return const_cast<R&>(reinterpret_cast<R const&>(object));
}
#endif
};
template <typename T>
struct bind_out
{
bind_out(T& object) noexcept : object(object)
{
}
T& object;
operator void** () const noexcept
{
if constexpr (std::is_same_v<T, hstring>)
{
object.clear();
}
else
{
object = nullptr;
}
return (void**)(&object);
}
template <typename R>
operator R* () const noexcept
{
if constexpr (!std::is_trivially_destructible_v<T>)
{
object = {};
}
return reinterpret_cast<R*>(&object);
}
};
template <typename T>
inline hstring hstring_convert(T value)
{
static_assert(std::is_arithmetic_v<T>);
char temp[32];
std::to_chars_result result;
if constexpr (std::is_integral_v<T>)
{
result = std::to_chars(std::begin(temp), std::end(temp), value);
}
else
{
// Floating point
result = std::to_chars(std::begin(temp), std::end(temp), value, std::chars_format::general);
}
WINRT_ASSERT(result.ec == std::errc{});
wchar_t buffer[32];
auto end = std::copy(std::begin(temp), result.ptr, buffer);
return hstring{ std::wstring_view{ buffer, static_cast<std::size_t>(end - buffer)} };
}
#if __cpp_lib_format >= 202207L
template <typename... Args>
inline hstring base_format(Args&&... args)
{
// don't forward because an object could be moved-from, causing issues
// for the second format call.
// not forwarding lets us take both rvalues and lvalues but pass them
// further down as an lvalue ref. some types can only be formatted
// when non-const (e.g. ranges::filter_view) so taking a const reference
// as parameter wouldn't work for all scenarios.
auto const size = std::formatted_size(args...);
WINRT_ASSERT(size < UINT_MAX);
auto const size32 = static_cast<uint32_t>(size);
hstring_builder builder(size32);
WINRT_VERIFY_(size32, std::format_to_n(builder.data(), size32, args...).size);
return builder.to_hstring();
}
#endif
}
WINRT_EXPORT namespace winrt
{
#if __cpp_lib_format >= 202207L
template <typename... Args>
inline hstring format(std::wformat_string<Args&...> const fmt, Args&&... args)
{
return impl::base_format(fmt, args...);
}
template <typename... Args>
inline hstring format(std::locale const& loc, std::wformat_string<Args&...> const fmt, Args&&... args)
{
return impl::base_format(loc, fmt, args...);
}
#endif
inline bool embedded_null(hstring const& value) noexcept
{
return std::any_of(value.begin(), value.end(), [](auto item)
{
return item == 0;
});
}
inline hstring to_hstring(uint8_t value)
{
return impl::hstring_convert(value);
}
inline hstring to_hstring(int8_t value)
{
return impl::hstring_convert(value);
}
inline hstring to_hstring(uint16_t value)
{
return impl::hstring_convert(value);
}
inline hstring to_hstring(int16_t value)
{
return impl::hstring_convert(value);
}
inline hstring to_hstring(uint32_t value)
{
return impl::hstring_convert(value);
}
inline hstring to_hstring(int32_t value)
{
return impl::hstring_convert(value);
}
inline hstring to_hstring(uint64_t value)
{
return impl::hstring_convert(value);
}
inline hstring to_hstring(int64_t value)
{
return impl::hstring_convert(value);
}
#if !defined(_LIBCPP_VERSION) || _LIBCPP_VERSION >= 14000
inline hstring to_hstring(float value)
{
return impl::hstring_convert(value);
}
inline hstring to_hstring(double value)
{
return impl::hstring_convert(value);
}
#endif
inline hstring to_hstring(char16_t value)
{
wchar_t buffer[2] = { value, 0 };
return hstring{ std::wstring_view{ buffer, 1 } };
}
inline hstring to_hstring(hstring const& value) noexcept
{
return value;
}
template <typename T, std::enable_if_t<std::is_same_v<T, bool>, int> = 0>
hstring to_hstring(T const value)
{
if (value)
{
return hstring{ L"true" };
}
else
{
return hstring{ L"false" };
}
}
inline hstring to_hstring(guid const& value)
{
wchar_t buffer[40];
//{00000000-0000-0000-0000-000000000000}
swprintf_s(buffer, L"{%08x-%04hx-%04hx-%02hhx%02hhx-%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx}",
value.Data1, value.Data2, value.Data3, value.Data4[0], value.Data4[1],
value.Data4[2], value.Data4[3], value.Data4[4], value.Data4[5], value.Data4[6], value.Data4[7]);
return hstring{ buffer };
}
template <typename T, std::enable_if_t<std::is_convertible_v<T, std::string_view>, int> = 0>
hstring to_hstring(T const& value)
{
std::string_view const view(value);
int const size = WINRT_IMPL_MultiByteToWideChar(65001 /*CP_UTF8*/, 0, view.data(), static_cast<int32_t>(view.size()), nullptr, 0);
if (size == 0)
{
return{};
}
impl::hstring_builder result(size);
WINRT_VERIFY_(size, WINRT_IMPL_MultiByteToWideChar(65001 /*CP_UTF8*/, 0, view.data(), static_cast<int32_t>(view.size()), result.data(), size));
return result.to_hstring();
}
inline std::string to_string(std::wstring_view value)
{
int const size = WINRT_IMPL_WideCharToMultiByte(65001 /*CP_UTF8*/, 0, value.data(), static_cast<int32_t>(value.size()), nullptr, 0, nullptr, nullptr);
if (size == 0)
{
return{};
}
std::string result(size, '?');
WINRT_VERIFY_(size, WINRT_IMPL_WideCharToMultiByte(65001 /*CP_UTF8*/, 0, value.data(), static_cast<int32_t>(value.size()), result.data(), size, nullptr, nullptr));
return result;
}
}