Skip to content

Commit 6a08dd4

Browse files
authored
Fix regression in hstring (#503)
1 parent 0f64f60 commit 6a08dd4

File tree

3 files changed

+26
-2
lines changed

3 files changed

+26
-2
lines changed

strings/base_string.h

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -257,12 +257,19 @@ WINRT_EXPORT namespace winrt
257257

258258
const_pointer data() const noexcept
259259
{
260-
return begin();
260+
return c_str();
261261
}
262262

263263
const_pointer c_str() const noexcept
264264
{
265-
return begin();
265+
if (!empty())
266+
{
267+
return begin();
268+
}
269+
else
270+
{
271+
return L"";
272+
}
266273
}
267274

268275
const_iterator begin() const noexcept

test/test/hstring_empty.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#include "pch.h"
2+
3+
using namespace std::literals;
4+
5+
TEST_CASE("hstring_empty")
6+
{
7+
winrt::hstring s;
8+
REQUIRE(s.empty());
9+
REQUIRE(s.size() == 0);
10+
REQUIRE(s == L""sv);
11+
REQUIRE(std::distance(s.begin(), s.end()) == 0);
12+
13+
// Using wcslen to both validate that the strings are empty *and* that they are not null.
14+
REQUIRE(wcslen(s.c_str()) == 0);
15+
REQUIRE(wcslen(s.data()) == 0);
16+
}

test/test/test.vcxproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,7 @@
363363
</ClCompile>
364364
<ClCompile Include="GetMany.cpp" />
365365
<ClCompile Include="get_activation_factory.cpp" />
366+
<ClCompile Include="hstring_empty.cpp" />
366367
<ClCompile Include="iid_ppv_args.cpp" />
367368
<ClCompile Include="inspectable_interop.cpp">
368369
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">NotUsing</PrecompiledHeader>

0 commit comments

Comments
 (0)