2019-05-03 00:29:04 +02:00
|
|
|
/*++
|
|
|
|
Copyright (c) Microsoft Corporation
|
|
|
|
|
|
|
|
Module Name:
|
|
|
|
- utils.hpp
|
|
|
|
|
|
|
|
Abstract:
|
|
|
|
- Helpful cross-lib utilities
|
|
|
|
|
|
|
|
Author(s):
|
|
|
|
- Mike Griese (migrie) 12-Jun-2018
|
|
|
|
--*/
|
|
|
|
|
2020-06-18 02:27:42 +02:00
|
|
|
#pragma once
|
|
|
|
|
2019-05-03 00:29:04 +02:00
|
|
|
namespace Microsoft::Console::Utils
|
|
|
|
{
|
2020-07-10 01:24:17 +02:00
|
|
|
// 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 });
|
|
|
|
}
|
|
|
|
|
2019-05-03 00:29:04 +02:00
|
|
|
bool IsValidHandle(const HANDLE handle) noexcept;
|
|
|
|
|
2019-09-04 19:59:18 +02:00
|
|
|
// 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)));
|
|
|
|
}
|
2019-06-05 01:31:36 +02:00
|
|
|
|
2019-05-03 00:29:04 +02:00
|
|
|
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);
|
2019-05-21 22:29:16 +02:00
|
|
|
GUID CreateGuid();
|
2019-05-03 00:29:04 +02:00
|
|
|
|
2020-05-16 00:43:00 +02:00
|
|
|
std::string ColorToHexString(const til::color color);
|
|
|
|
til::color ColorFromHexString(const std::string_view wstr);
|
2020-10-15 02:29:10 +02:00
|
|
|
std::optional<til::color> ColorFromXTermColor(const std::wstring_view wstr) noexcept;
|
|
|
|
std::optional<til::color> ColorFromXParseColorSpec(const std::wstring_view wstr) noexcept;
|
2019-05-03 00:29:04 +02:00
|
|
|
|
2020-10-15 02:29:10 +02:00
|
|
|
bool HexToUint(const wchar_t wch, unsigned int& value) noexcept;
|
|
|
|
bool StringToUint(const std::wstring_view wstr, unsigned int& value);
|
2020-12-03 01:33:29 +01:00
|
|
|
std::vector<std::wstring_view> SplitString(const std::wstring_view wstr, const wchar_t delimiter) noexcept;
|
2019-05-21 22:29:16 +02:00
|
|
|
|
2021-02-08 14:11:01 +01: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);
|
|
|
|
|
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 |
|
2019-06-11 22:27:09 +02:00
|
|
|
(value & 0x00FF0000) >> 8 |
|
|
|
|
(value & 0x0000FF00) << 8 |
|
|
|
|
(value & 0x000000FF) << 24;
|
2019-05-21 22:29:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
constexpr unsigned long EndianSwap(unsigned long value)
|
|
|
|
{
|
2019-08-29 22:19:01 +02:00
|
|
|
return gsl::narrow_cast<unsigned long>(EndianSwap(gsl::narrow_cast<uint32_t>(value)));
|
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;
|
|
|
|
}
|
|
|
|
|
2019-09-03 22:02:09 +02:00
|
|
|
GUID CreateV5Uuid(const GUID& namespaceGuid, const gsl::span<const gsl::byte> name);
|
2020-06-01 22:26:00 +02:00
|
|
|
|
2021-11-13 01:58:43 +01:00
|
|
|
bool IsElevated();
|
2019-05-03 00:29:04 +02:00
|
|
|
}
|