From 71335b5af1f8ea0d2414185fa6082a7ea7730af4 Mon Sep 17 00:00:00 2001 From: Nate <37554478+servusdei2018@users.noreply.github.com> Date: Tue, 14 May 2024 00:39:00 -0400 Subject: [PATCH] C++20: use view() instead of str() to avoid a copy, use starts_with (#13218) * Change files * C++20: use view() instead of str() to avoid a copy, use starts_with Signed-off-by: Nathanael Bracy * fix * Apply changefile modifications from code review Co-authored-by: Jon Thysell * Revert package.json Signed-off-by: Nathanael Bracy --------- Signed-off-by: Nathanael Bracy Co-authored-by: Andrew Coates <30809111+acoates-ms@users.noreply.github.com> Co-authored-by: Jon Thysell --- ...ative-windows-f53a2b65-d364-49a5-978c-5d64e08c22ea.json | 7 +++++++ vnext/Microsoft.ReactNative/Utils/LocalBundleReader.cpp | 5 ++--- vnext/Shared/HermesSamplingProfiler.cpp | 3 +-- vnext/Shared/OInstance.cpp | 3 +-- 4 files changed, 11 insertions(+), 7 deletions(-) create mode 100644 change/react-native-windows-f53a2b65-d364-49a5-978c-5d64e08c22ea.json diff --git a/change/react-native-windows-f53a2b65-d364-49a5-978c-5d64e08c22ea.json b/change/react-native-windows-f53a2b65-d364-49a5-978c-5d64e08c22ea.json new file mode 100644 index 00000000000..aa6b327dc83 --- /dev/null +++ b/change/react-native-windows-f53a2b65-d364-49a5-978c-5d64e08c22ea.json @@ -0,0 +1,7 @@ +{ + "type": "prerelease", + "comment": "Use view() instead of str(), starts_with() instead of _Starts_with()", + "packageName": "react-native-windows", + "email": "nate@bracy.dev", + "dependentChangeType": "patch" +} diff --git a/vnext/Microsoft.ReactNative/Utils/LocalBundleReader.cpp b/vnext/Microsoft.ReactNative/Utils/LocalBundleReader.cpp index 08e5fe4d20b..b45e48f87ea 100644 --- a/vnext/Microsoft.ReactNative/Utils/LocalBundleReader.cpp +++ b/vnext/Microsoft.ReactNative/Utils/LocalBundleReader.cpp @@ -59,11 +59,10 @@ std::future LocalBundleReader::LoadBundleAsync(const std::wstring b winrt::Windows::Storage::StorageFile file{nullptr}; // Supports "ms-appx://" or "ms-appdata://" - // TODO: Replace call to private string function with C++ 20 `starts_with` - if (bundleUri._Starts_with(L"ms-app")) { + if (bundleUri.starts_with(L"ms-app")) { winrt::Windows::Foundation::Uri uri(bundleUri); file = co_await winrt::Windows::Storage::StorageFile::GetFileFromApplicationUriAsync(uri); - } else if (bundleUri._Starts_with(L"resource://")) { + } else if (bundleUri.starts_with(L"resource://")) { winrt::Windows::Foundation::Uri uri(bundleUri); co_return GetBundleFromEmbeddedResource(uri); } else { diff --git a/vnext/Shared/HermesSamplingProfiler.cpp b/vnext/Shared/HermesSamplingProfiler.cpp index 442dba36887..d2fddb83334 100644 --- a/vnext/Shared/HermesSamplingProfiler.cpp +++ b/vnext/Shared/HermesSamplingProfiler.cpp @@ -54,8 +54,7 @@ IAsyncOperation getTraceFilePath() noexcept { .count(); os << hermesDataPath.c_str() << L"\\cpu_" << now << L".cpuprofile"; - // TODO: Use C++ 20 `os.view()` to avoid a copy - co_return winrt::hstring(os.str()); + co_return winrt::hstring(os.view()); } } // namespace diff --git a/vnext/Shared/OInstance.cpp b/vnext/Shared/OInstance.cpp index ece2c869a9c..b72c6714570 100644 --- a/vnext/Shared/OInstance.cpp +++ b/vnext/Shared/OInstance.cpp @@ -196,8 +196,7 @@ std::unique_ptr JsBigStringFromPath( return facebook::react::FileMappingBigString::fromPath(bundlePath); #else std::wstring bundlePath; - // TODO: Replace call to private string function with C++ 20 `starts_with` - if (devSettings->bundleRootPath._Starts_with("resource://")) { + if (devSettings->bundleRootPath.starts_with("resource://")) { auto uri = winrt::Windows::Foundation::Uri( winrt::to_hstring(devSettings->bundleRootPath), winrt::to_hstring(jsBundleRelativePath)); bundlePath = uri.ToString();