[Audit] Use std::wstring_view for all global strings #3756

Open
opened 2026-01-30 23:29:29 +00:00 by claunia · 3 comments
Owner

Originally created by @miniksa on GitHub (Sep 5, 2019).

I left this behind in the audit mode enable phase.

This is because changing the DEFAULT_FONT_FACE to a std::wstring_view cascades in a way that is troublesome.

For one, std::wstring_view is not guaranteed to be null terminated. Some of the consumers of this value throughout the codebase use the pointer to this memory location as a z-terminated string instead of a counted string. In the context of pointing to a globally allocated string, we can probably feel safe that it is null terminated and could ignore this. But I don't like ignoring things like this as they pop up later when refactoring occurs.

For two, changing the way the font face string works cascades through the entire FontInfo and friends classes in a way that touches many, many things. I don't really want to move all of that around without also addressing #602 at the same time because we've already identified that this particular value needs to change in those classes to satisfy that bug and circumstance.'

For three, we haven't even started talking about the cascading effects of doing this to the other three strings.

So for now, I've punted and left this TODO behind.

Originally created by @miniksa on GitHub (Sep 5, 2019). I left this behind in the audit mode enable phase. This is because changing the `DEFAULT_FONT_FACE` to a `std::wstring_view` cascades in a way that is troublesome. For one, `std::wstring_view` is not guaranteed to be null terminated. Some of the consumers of this value throughout the codebase use the pointer to this memory location as a z-terminated string instead of a counted string. In the context of pointing to a globally allocated string, we can probably feel safe that it is null terminated and could ignore this. But I don't like ignoring things like this as they pop up later when refactoring occurs. For two, changing the way the font face string works cascades through the entire `FontInfo` and friends classes in a way that touches many, many things. I don't really want to move all of that around without also addressing #602 at the same time because we've already identified that this particular value needs to change in those classes to satisfy that bug and circumstance.' For three, we haven't even started talking about the cascading effects of doing this to the other three strings. So for now, I've punted and left this TODO behind.
Author
Owner

@dlong11 commented on GitHub (Sep 7, 2019):

@miniksa I am not seeing why this would need to happen all at once. Couldn't you do this in steps? I believe the goal of this PR is to remove the warning 26426 for string constants. Typically when you start to switch over to std::string_view you need to do it from the bottom up. Do the utility functions and work up. Switching the globals (top down) is going to be very painful and it will could cause bugs do to lack of the null terminator.

If the goal is to remove 26426 you could do this by using non-complex objects (ie objects without destructors). We could make a pass and switch over the std::wstrings to be declared as constexpr wchar_t arrays or pointers. (ie - non complex objects.)

const std::wstring DEFAULT_FONT_FACE{ L"Consolas" };
would become
constexpr auto DEFAULT_FONT_FACE[] = L"Consolas";
or you could use const pointers.

Since these where previously std::wstrings the code would work without modifications. The down side is these stings would get implicitly converted to std::wstring when used but I don't think that would be a perf issue unless you were in a tight loop. In my day job we follow the google coding standards and here is what they say.

This approach seems safer than trying to switch over to string_view wholesale. After doing this, work could begin on switching the code base over to handle string_view.

Thoughts?

@dlong11 commented on GitHub (Sep 7, 2019): @miniksa I am not seeing why this would need to happen all at once. Couldn't you do this in steps? I believe the goal of this PR is to remove the warning 26426 for string constants. Typically when you start to switch over to std::string_view you need to do it from the bottom up. Do the utility functions and work up. Switching the globals (top down) is going to be very painful and it will could cause bugs do to lack of the null terminator. If the goal is to remove 26426 you could do this by using non-complex objects (ie objects without destructors). We could make a pass and switch over the std::wstrings to be declared as constexpr wchar_t arrays or pointers. (ie - non complex objects.) const std::wstring DEFAULT_FONT_FACE{ L"Consolas" }; would become constexpr auto DEFAULT_FONT_FACE[] = L"Consolas"; or you could use const pointers. Since these where previously std::wstrings the code would work without modifications. The down side is these stings would get implicitly converted to std::wstring when used but I don't think that would be a perf issue unless you were in a tight loop. In my day job we follow the google coding standards and [here](https://google.github.io/styleguide/cppguide.html#Static_and_Global_Variables) is what they say. This approach seems safer than trying to switch over to string_view wholesale. After doing this, work could begin on switching the code base over to handle string_view. Thoughts?
Author
Owner

@miniksa commented on GitHub (Sep 9, 2019):

That's a good thought, @dlong11. My brain was just stuck in a std::wstring_view shaped box. I'd be satisfied with just eliminating the warning by making it a wchar_t array as it is indeed very painful to propagate the whole wstring_view problem.

@miniksa commented on GitHub (Sep 9, 2019): That's a good thought, @dlong11. My brain was just stuck in a `std::wstring_view` shaped box. I'd be satisfied with just eliminating the warning by making it a wchar_t array as it is indeed very painful to propagate the whole `wstring_view` problem.
Author
Owner

@WSLUser commented on GitHub (Sep 10, 2019):

I would suppose if you're going with std, you'd want to use gsl too? Such as

int main()
{
	std::wstring s;
	auto r = gsl::make_span(s);

    return 0;

Or making use of gsl::basic_string_span as make_span sounds like it might become deprecated per this issue.

@WSLUser commented on GitHub (Sep 10, 2019): I would suppose if you're going with std, you'd want to use gsl too? Such as ``` int main() { std::wstring s; auto r = gsl::make_span(s); return 0; ``` Or making use of `gsl::basic_string_span` as make_span sounds like it might become deprecated per this [issue](https://github.com/microsoft/GSL/issues/656).
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/terminal#3756