terminal/src/types/inc/utils.hpp

99 lines
3 KiB
C++
Raw Normal View History

/*++
Copyright (c) Microsoft Corporation
Module Name:
- utils.hpp
Abstract:
- Helpful cross-lib utilities
Author(s):
- Mike Griese (migrie) 12-Jun-2018
--*/
#pragma once
namespace Microsoft::Console::Utils
{
// Function Description:
// - Returns -1, 0 or +1 to indicate the sign of the passed-in value.
template<typename T>
constexpr int Sign(T val) noexcept
{
return (T{ 0 } < val) - (val < T{ 0 });
}
bool IsValidHandle(const HANDLE handle) noexcept;
// Function Description:
// - Clamps a long in between `min` and `SHRT_MAX`
// Arguments:
// - value: the value to clamp
// - min: the minimum value to clamp to
// Return Value:
// - The clamped value as a short.
constexpr short ClampToShortMax(const long value, const short min) noexcept
{
return static_cast<short>(std::clamp(value,
static_cast<long>(min),
static_cast<long>(SHRT_MAX)));
}
std::wstring GuidToString(const GUID guid);
Reduce usage of Json::Value throughout Terminal.Settings.Model (#11184) This commit reduces the code surface that interacts with raw JSON data, reducing code complexity and improving maintainability. Files that needed to be changed drastically were additionally cleaned up to remove any code cruft that has accrued over time. In order to facility this the following changes were made: * Move JSON handling from `CascadiaSettings` into `SettingsLoader` This allows us to use STL containers for data model instances. For instance profiles are now added to a hashmap for O(1) lookup. * JSON parsing within `SettingsLoader` doesn't differentiate between user, inbox and fragment JSON data, reducing code complexity and size. It also centralizes common concerns, like profile deduplication and ensuring that all profiles are assigned a GUID. * Direct JSON modification, like the insertion of dynamic profiles into settings.json were removed. This vastly reduces code complexity, but unfortunately removes support for comments in JSON on first start. * `ColorScheme`s cannot be layered. As such its `LayerJson` API was replaced with `FromJson`, allowing us to remove JSON-based color scheme validation. * `Profile`s used to test their wish to layer using `ShouldBeLayered`, which was replaced with a GUID-based hashmap lookup on previously parsed profiles. Further changes were made as improvements upon the previous changes: * Compact the JSON files embedded binary, saving 28kB * Prevent double-initialization of the color table in `ColorScheme` * Making `til::color` getters `constexpr`, allow better optimizations The result is a reduction of: * 48kB binary size for the Settings.Model.dll * 5-10% startup duration * 26% code for the `CascadiaSettings` class * 1% overall code in this project Furthermore this results in the following breaking changes: * The long deprecated "globals" settings object will not be detected and no warning will be created during load. * The initial creation of a new settings.json will not produce helpful comments. Both cases are caused by the removal of manual JSON handling and the move to representing the settings file with model objects instead ## PR Checklist * [x] Closes #5276 * [x] Closes #7421 * [x] I work here * [x] Tests added/passed ## Validation Steps Performed * Out-of-box-experience is identical to before ✔️ (Except for the settings.json file lacking comments.) * Existing user settings load correctly ✔️ * New WSL instances are added to user settings ✔️ * New fragments are added to user settings ✔️ * All profiles are assigned GUIDs ✔️
2021-09-22 18:27:31 +02:00
GUID GuidFromString(_Null_terminated_ const wchar_t* str);
Switch to v5 UUIDs as profile GUIDs for the default profiles (#913) This commit switches the GUIDs for default profiles from being randomly generated to being version 5 UUIDs. More info in #870. ## PR Checklist * [x] Closes #870 * [x] CLA signed * [x] Tests added/passed * [x] Requires documentation to be updated (#883) * [x] I've discussed this with core contributors already. ## Detailed Description of the Pull Request / Additional comments This commit has a number of changes that seem ancillary, but they're general goodness. Let me explain: * I've added a whole new Types test library with only two tests in * Since UUIDv5 generation requires SHA1, we needed to take a dependency on bcrypt * I honestly don't think we should have to link bcrypt in conhost, but LTO should take care of that * I considered adding a new Terminal-specific Utils/Types library, but that seemed like a waste * The best way to link bcrypt turned out to be in line with a discussion @miniksa and I had, where we decided we both love APISets and think that the console should link against them exclusively... so I've added `onecore_apiset.lib` to the front of the link line, where it will deflect the linker away from most of the other libs automagically. ``` StartGroup: UuidTests::TestV5UuidU8String Verify: AreEqual(uuidExpected, uuidActual) EndGroup: UuidTests::TestV5UuidU8String [Passed] StartGroup: UuidTests::TestV5UuidU16String Verify: AreEqual(uuidExpected, uuidActual) EndGroup: UuidTests::TestV5UuidU16String [Passed] ```
2019-05-21 22:29:16 +02:00
GUID CreateGuid();
std::string ColorToHexString(const til::color color);
til::color ColorFromHexString(const std::string_view wstr);
std::optional<til::color> ColorFromXTermColor(const std::wstring_view wstr) noexcept;
std::optional<til::color> ColorFromXParseColorSpec(const std::wstring_view wstr) noexcept;
bool HexToUint(const wchar_t wch, unsigned int& value) noexcept;
bool StringToUint(const std::wstring_view wstr, unsigned int& value);
std::vector<std::wstring_view> SplitString(const std::wstring_view wstr, const wchar_t delimiter) noexcept;
Switch to v5 UUIDs as profile GUIDs for the default profiles (#913) This commit switches the GUIDs for default profiles from being randomly generated to being version 5 UUIDs. More info in #870. ## PR Checklist * [x] Closes #870 * [x] CLA signed * [x] Tests added/passed * [x] Requires documentation to be updated (#883) * [x] I've discussed this with core contributors already. ## Detailed Description of the Pull Request / Additional comments This commit has a number of changes that seem ancillary, but they're general goodness. Let me explain: * I've added a whole new Types test library with only two tests in * Since UUIDv5 generation requires SHA1, we needed to take a dependency on bcrypt * I honestly don't think we should have to link bcrypt in conhost, but LTO should take care of that * I considered adding a new Terminal-specific Utils/Types library, but that seemed like a waste * The best way to link bcrypt turned out to be in line with a discussion @miniksa and I had, where we decided we both love APISets and think that the console should link against them exclusively... so I've added `onecore_apiset.lib` to the front of the link line, where it will deflect the linker away from most of the other libs automagically. ``` StartGroup: UuidTests::TestV5UuidU8String Verify: AreEqual(uuidExpected, uuidActual) EndGroup: UuidTests::TestV5UuidU8String [Passed] StartGroup: UuidTests::TestV5UuidU16String Verify: AreEqual(uuidExpected, uuidActual) EndGroup: UuidTests::TestV5UuidU16String [Passed] ```
2019-05-21 22:29:16 +02:00
enum FilterOption
{
None = 0,
// Convert CR+LF and LF-only line endings to CR-only.
CarriageReturnNewline = 1u << 0,
// For security reasons, remove most control characters.
ControlCodes = 1u << 1,
};
DEFINE_ENUM_FLAG_OPERATORS(FilterOption)
std::wstring FilterStringForPaste(const std::wstring_view wstr, const FilterOption option);
Switch to v5 UUIDs as profile GUIDs for the default profiles (#913) This commit switches the GUIDs for default profiles from being randomly generated to being version 5 UUIDs. More info in #870. ## PR Checklist * [x] Closes #870 * [x] CLA signed * [x] Tests added/passed * [x] Requires documentation to be updated (#883) * [x] I've discussed this with core contributors already. ## Detailed Description of the Pull Request / Additional comments This commit has a number of changes that seem ancillary, but they're general goodness. Let me explain: * I've added a whole new Types test library with only two tests in * Since UUIDv5 generation requires SHA1, we needed to take a dependency on bcrypt * I honestly don't think we should have to link bcrypt in conhost, but LTO should take care of that * I considered adding a new Terminal-specific Utils/Types library, but that seemed like a waste * The best way to link bcrypt turned out to be in line with a discussion @miniksa and I had, where we decided we both love APISets and think that the console should link against them exclusively... so I've added `onecore_apiset.lib` to the front of the link line, where it will deflect the linker away from most of the other libs automagically. ``` StartGroup: UuidTests::TestV5UuidU8String Verify: AreEqual(uuidExpected, uuidActual) EndGroup: UuidTests::TestV5UuidU8String [Passed] StartGroup: UuidTests::TestV5UuidU16String Verify: AreEqual(uuidExpected, uuidActual) EndGroup: UuidTests::TestV5UuidU16String [Passed] ```
2019-05-21 22:29:16 +02:00
constexpr uint16_t EndianSwap(uint16_t value)
{
return (value & 0xFF00) >> 8 |
(value & 0x00FF) << 8;
}
constexpr uint32_t EndianSwap(uint32_t value)
{
return (value & 0xFF000000) >> 24 |
(value & 0x00FF0000) >> 8 |
(value & 0x0000FF00) << 8 |
(value & 0x000000FF) << 24;
Switch to v5 UUIDs as profile GUIDs for the default profiles (#913) This commit switches the GUIDs for default profiles from being randomly generated to being version 5 UUIDs. More info in #870. ## PR Checklist * [x] Closes #870 * [x] CLA signed * [x] Tests added/passed * [x] Requires documentation to be updated (#883) * [x] I've discussed this with core contributors already. ## Detailed Description of the Pull Request / Additional comments This commit has a number of changes that seem ancillary, but they're general goodness. Let me explain: * I've added a whole new Types test library with only two tests in * Since UUIDv5 generation requires SHA1, we needed to take a dependency on bcrypt * I honestly don't think we should have to link bcrypt in conhost, but LTO should take care of that * I considered adding a new Terminal-specific Utils/Types library, but that seemed like a waste * The best way to link bcrypt turned out to be in line with a discussion @miniksa and I had, where we decided we both love APISets and think that the console should link against them exclusively... so I've added `onecore_apiset.lib` to the front of the link line, where it will deflect the linker away from most of the other libs automagically. ``` StartGroup: UuidTests::TestV5UuidU8String Verify: AreEqual(uuidExpected, uuidActual) EndGroup: UuidTests::TestV5UuidU8String [Passed] StartGroup: UuidTests::TestV5UuidU16String Verify: AreEqual(uuidExpected, uuidActual) EndGroup: UuidTests::TestV5UuidU16String [Passed] ```
2019-05-21 22:29:16 +02:00
}
constexpr unsigned long EndianSwap(unsigned long value)
{
return gsl::narrow_cast<unsigned long>(EndianSwap(gsl::narrow_cast<uint32_t>(value)));
Switch to v5 UUIDs as profile GUIDs for the default profiles (#913) This commit switches the GUIDs for default profiles from being randomly generated to being version 5 UUIDs. More info in #870. ## PR Checklist * [x] Closes #870 * [x] CLA signed * [x] Tests added/passed * [x] Requires documentation to be updated (#883) * [x] I've discussed this with core contributors already. ## Detailed Description of the Pull Request / Additional comments This commit has a number of changes that seem ancillary, but they're general goodness. Let me explain: * I've added a whole new Types test library with only two tests in * Since UUIDv5 generation requires SHA1, we needed to take a dependency on bcrypt * I honestly don't think we should have to link bcrypt in conhost, but LTO should take care of that * I considered adding a new Terminal-specific Utils/Types library, but that seemed like a waste * The best way to link bcrypt turned out to be in line with a discussion @miniksa and I had, where we decided we both love APISets and think that the console should link against them exclusively... so I've added `onecore_apiset.lib` to the front of the link line, where it will deflect the linker away from most of the other libs automagically. ``` StartGroup: UuidTests::TestV5UuidU8String Verify: AreEqual(uuidExpected, uuidActual) EndGroup: UuidTests::TestV5UuidU8String [Passed] StartGroup: UuidTests::TestV5UuidU16String Verify: AreEqual(uuidExpected, uuidActual) EndGroup: UuidTests::TestV5UuidU16String [Passed] ```
2019-05-21 22:29:16 +02:00
}
constexpr GUID EndianSwap(GUID value)
{
value.Data1 = EndianSwap(value.Data1);
value.Data2 = EndianSwap(value.Data2);
value.Data3 = EndianSwap(value.Data3);
return value;
}
GUID CreateV5Uuid(const GUID& namespaceGuid, const gsl::span<const gsl::byte> name);
2020-06-01 22:26:00 +02:00
bool IsElevated();
}