[PR #4358] [MERGED] Remove unneeded c_str() conversions #25725

Open
opened 2026-01-31 09:11:23 +00:00 by claunia · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/microsoft/terminal/pull/4358
Author: @j4james
Created: 1/26/2020
Status: Merged
Merged: 1/27/2020
Merged by: @carlos-zamora

Base: masterHead: remove-unneeded-cstrs


📝 Commits (5)

  • c93be10 Remove unneeded c_str() calls when converting an hstring to a wstring_view.
  • e462498 Remove unneeded c_str() calls when constructing a FontInfo class with a wstring face name.
  • 80d7e6e Remove unneeded winrt::to_hstring calls when passing a wstring to a method that expects an hstring.
  • e301a3b Remove unneeded c_str() calls when passing an hstring to a method that already accepts hstrings without conversion.
  • 43e323e Remove unneeded c_str() and data() calls when explicitly constructing an hstring from a wstring.

📊 Changes

7 files changed (+17 additions, -17 deletions)

View changed files

📝 src/cascadia/PublicTerminalCore/HwndTerminal.cpp (+2 -2)
📝 src/cascadia/TerminalApp/Profile.cpp (+3 -3)
📝 src/cascadia/TerminalControl/TSFInputControl.cpp (+2 -2)
📝 src/cascadia/TerminalControl/TermControl.cpp (+7 -7)
📝 src/cascadia/TerminalCore/terminalrenderdata.cpp (+1 -1)
📝 src/cascadia/UnitTests_TerminalCore/MockTermSettings.h (+1 -1)
📝 src/cascadia/WindowsTerminal/AppHost.cpp (+1 -1)

📄 Description

Summary of the Pull Request

There are number of places in the code where we need to convert between different string types, and the conversion is achieved with a call to c_str(). However this is often just an unecessary intermediate step, since the strings are already capable of doing the conversion directly. It could also result in data loss in cases where the string could contain a null character. This PR removes many of these unneeded c_str() calls.

PR Checklist

Detailed Description of the Pull Request / Additional comments

There were five general situations where I found unnecessary c_str calls:

  • Converting from hstring to wstring_view (this could be handled by a conversion function in hstring).
  • Converting from wstring to wstring_view in FontInfo instantiations (this could be handled by a conversion function in wstring).
  • Converting from wstring to hstring using winrt::to_hstring (this could be handled by the hstring converting constructor without the need for the to_hstring or cs_str calls).
  • Passing an hstring variable to a method expecting an hstring (obviously this shouldn't require any intermediate conversion).
  • Converting from wstring to hstring with an explicit hstring constructor (this could be handled by passing the wstring directly to the constructor).

Validation Steps Performed

For every line of code that changed, I've tried to manually test the aspect of the app that passed through that code, and made sure it still worked. I also managed to reproduce the problem described in issue #1955 and confirmed that it's now fixed.


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/microsoft/terminal/pull/4358 **Author:** [@j4james](https://github.com/j4james) **Created:** 1/26/2020 **Status:** ✅ Merged **Merged:** 1/27/2020 **Merged by:** [@carlos-zamora](https://github.com/carlos-zamora) **Base:** `master` ← **Head:** `remove-unneeded-cstrs` --- ### 📝 Commits (5) - [`c93be10`](https://github.com/microsoft/terminal/commit/c93be10c11bc56eccf19fab76d0b7711ba54c8dd) Remove unneeded c_str() calls when converting an hstring to a wstring_view. - [`e462498`](https://github.com/microsoft/terminal/commit/e462498bd277690ef55edc1b2d1b5e95ea45eb79) Remove unneeded c_str() calls when constructing a FontInfo class with a wstring face name. - [`80d7e6e`](https://github.com/microsoft/terminal/commit/80d7e6e5f9ef0e381b9c3d24b1b7b51b01f985a6) Remove unneeded winrt::to_hstring calls when passing a wstring to a method that expects an hstring. - [`e301a3b`](https://github.com/microsoft/terminal/commit/e301a3bbac18eab1309a73cf84894aa7f2c5847d) Remove unneeded c_str() calls when passing an hstring to a method that already accepts hstrings without conversion. - [`43e323e`](https://github.com/microsoft/terminal/commit/43e323eaa463b84fccf489b3c5b911e51351d346) Remove unneeded c_str() and data() calls when explicitly constructing an hstring from a wstring. ### 📊 Changes **7 files changed** (+17 additions, -17 deletions) <details> <summary>View changed files</summary> 📝 `src/cascadia/PublicTerminalCore/HwndTerminal.cpp` (+2 -2) 📝 `src/cascadia/TerminalApp/Profile.cpp` (+3 -3) 📝 `src/cascadia/TerminalControl/TSFInputControl.cpp` (+2 -2) 📝 `src/cascadia/TerminalControl/TermControl.cpp` (+7 -7) 📝 `src/cascadia/TerminalCore/terminalrenderdata.cpp` (+1 -1) 📝 `src/cascadia/UnitTests_TerminalCore/MockTermSettings.h` (+1 -1) 📝 `src/cascadia/WindowsTerminal/AppHost.cpp` (+1 -1) </details> ### 📄 Description ## Summary of the Pull Request There are number of places in the code where we need to convert between different string types, and the conversion is achieved with a call to `c_str()`. However this is often just an unecessary intermediate step, since the strings are already capable of doing the conversion directly. It could also result in data loss in cases where the string could contain a null character. This PR removes many of these unneeded `c_str()` calls. ## PR Checklist * [x] Closes #1955 * [x] CLA signed. If not, go over [here](https://cla.opensource.microsoft.com/microsoft/Terminal) and sign the CLA * [ ] Tests added/passed * [ ] Requires documentation to be updated * [ ] I've discussed this with core contributors already. If not checked, I'm ready to accept this work might be rejected in favor of a different grand plan. Issue number where discussion took place: #1955 ## Detailed Description of the Pull Request / Additional comments There were five general situations where I found unnecessary c_str calls: * Converting from `hstring` to `wstring_view` (this could be handled by a conversion function in `hstring`). * Converting from `wstring` to `wstring_view` in `FontInfo` instantiations (this could be handled by a conversion function in `wstring`). * Converting from `wstring` to `hstring` using `winrt::to_hstring` (this could be handled by the `hstring` converting constructor without the need for the `to_hstring` or `cs_str` calls). * Passing an `hstring` variable to a method expecting an `hstring` (obviously this shouldn't require any intermediate conversion). * Converting from wstring to hstring with an explicit `hstring` constructor (this could be handled by passing the `wstring` directly to the constructor). ## Validation Steps Performed For every line of code that changed, I've tried to manually test the aspect of the app that passed through that code, and made sure it still worked. I also managed to reproduce the problem described in issue #1955 and confirmed that it's now fixed. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
claunia added the pull-request label 2026-01-31 09:11:23 +00:00
Sign in to join this conversation.
No Label pull-request
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/terminal#25725