Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions dev/Common/Microsoft.Utf8.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.

#ifndef __MICROSOFT_UTF8_H
#define __MICROSOFT_UTF8_H

namespace Microsoft::Utf8
{
// UTF8->UTF16 conversions
template<typename T>
T ToUtf16(PCSTR utf8);

Expand Down Expand Up @@ -45,8 +49,52 @@ template<> inline winrt::hstring ToUtf16(PCSTR utf8)
return winrt::hstring(s.get());
}

inline std::wstring ToUtf16(const std::string& utf8)
{
return ToUtf16(utf8.c_str());
}

inline winrt::hstring ToHString(PCSTR utf8)
{
return ToUtf16<winrt::hstring>(utf8);
}

// UTF16->UTF8 conversions
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@DrusTheAxe could you clarify why are all these needed / why don't we use winrt::to_hstring and winrt::to_string ?

template<typename T>
T ToUtf8(PCWSTR utf16);

template<> inline std::unique_ptr<char[]> ToUtf8(PCWSTR utf16)
{
if (!utf16)
{
return std::make_unique<char[]>(0);
}

const auto lengthIncludingNullTerminator{ ::WideCharToMultiByte(CP_UTF8, 0, utf16, -1, nullptr, 0, nullptr, nullptr) };
THROW_IF_WIN32_BOOL_FALSE(lengthIncludingNullTerminator);

std::unique_ptr<char[]> s{ std::make_unique<char[]>(lengthIncludingNullTerminator) };

THROW_IF_WIN32_BOOL_FALSE(::WideCharToMultiByte(CP_UTF8, 0, utf16, -1, s.get(), lengthIncludingNullTerminator, nullptr, nullptr));
s[lengthIncludingNullTerminator - 1] = '\0';
return s;
}

template<> inline std::string ToUtf8(PCWSTR utf16)
{
if (!utf16)
{
return std::string();
}

auto s{ ToUtf8<std::unique_ptr<char[]>>(utf16) };
return std::string(s.get());
}

inline std::string ToUtf8(const std::wstring& utf16)
{
return ToUtf8<std::string>(utf16.c_str());
}
}

#endif // __MICROSOFT_UTF8_H
1 change: 0 additions & 1 deletion dev/DynamicDependency/API/DataStore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
#include "DataStore.h"

#include "DynamicDependencyDataStore_h.h"
#include "utf8.h"
#include "winrt_msixdynamicdepednency.h"

#include <wil/winrt.h>
Expand Down
1 change: 0 additions & 1 deletion dev/DynamicDependency/API/DynamicDependency.vcxitems
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@
<ClInclude Include="$(MSBuildThisFileDirectory)PackageGraphNode.h" />
<ClInclude Include="$(MSBuildThisFileDirectory)PackageId.h" />
<ClInclude Include="$(MSBuildThisFileDirectory)PackageInfo.h" />
<ClInclude Include="$(MSBuildThisFileDirectory)utf8.h" />
<ClInclude Include="$(MSBuildThisFileDirectory)wil_msixdynamicdependency.h" />
<ClInclude Include="$(MSBuildThisFileDirectory)WinRTInprocModule.h" />
<ClInclude Include="$(MSBuildThisFileDirectory)WinRTModuleManager.h" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
<ClInclude Include="$(MSBuildThisFileDirectory)MddCore.Architecture.h" />
<ClInclude Include="$(MSBuildThisFileDirectory)PackageGraphManager.h" />
<ClInclude Include="$(MSBuildThisFileDirectory)DataStore.h" />
<ClInclude Include="$(MSBuildThisFileDirectory)utf8.h" />
<ClInclude Include="$(MSBuildThisFileDirectory)winrt_msixdynamicdepednency.h" />
<ClInclude Include="$(MSBuildThisFileDirectory)MddWinRT.h" />
<ClInclude Include="$(MSBuildThisFileDirectory)WinRTInprocModule.h" />
Expand All @@ -50,6 +49,7 @@
<ClInclude Include="$(MSBuildThisFileDirectory)MddLifetimeManagement.h" />
<ClInclude Include="$(MSBuildThisFileDirectory)MddLifetimeManagementTest.h" />
<ClInclude Include="$(MSBuildThisFileDirectory)appmodel_packageinfo.h" />
<ClInclude Include="$(MSBuildThisFileDirectory)utf8.h" />
</ItemGroup>
<ItemGroup>
<Midl Include="$(MSBuildThisFileDirectory)M.AM.DynamicDependency.idl" />
Expand Down
6 changes: 2 additions & 4 deletions dev/DynamicDependency/API/PackageDependency.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@

#include "pch.h"

#include "utf8.h"

#include "PackageDependency.h"

namespace JSON = winrt::Windows::Data::Json;
Expand Down Expand Up @@ -64,7 +62,7 @@ std::wstring MddCore::PackageDependency::ToJSON() const

std::string MddCore::PackageDependency::ToJSONUtf8() const
{
return to_utf8(ToJSON());
return Microsoft::Utf8::ToUtf8(ToJSON());
}

MddCore::PackageDependency MddCore::PackageDependency::FromJSON(const winrt::hstring& json)
Expand All @@ -84,7 +82,7 @@ MddCore::PackageDependency MddCore::PackageDependency::FromJSON(const winrt::hst

MddCore::PackageDependency MddCore::PackageDependency::FromJSON(PCSTR jsonUtf8)
{
return FromJSON(utf8_to_hstring(jsonUtf8));
return FromJSON(Microsoft::Utf8::ToHString(jsonUtf8));
}

bool MddCore::PackageDependency::IsExpired() const
Expand Down
1 change: 1 addition & 0 deletions dev/DynamicDependency/API/pch.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,5 @@
#include <winrt/Windows.System.h>

#include <appmodel.identity.h>
#include <microsoft.utf8.h>
#include <security.integritylevel.h>
49 changes: 0 additions & 49 deletions dev/DynamicDependency/API/utf8.h

This file was deleted.

1 change: 1 addition & 0 deletions dev/WindowsAppRuntime_DLL/pch.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,6 @@
#include <MsixDynamicDependency.h>

#include <appmodel.identity.h>
#include <microsoft.utf8.h>
#include <security.integritylevel.h>
#include <..\WindowsAppRuntime_Insights\WindowsAppRuntimeInsights.h>