-
Notifications
You must be signed in to change notification settings - Fork 8.1k
[WIP] Use interpolated strings #18974
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[WIP] Use interpolated strings #18974
Conversation
| return string.Format(CultureInfo.InvariantCulture, $"{0}> {File}", | ||
| FromStream == RedirectionStream.All ? "*" : ((int)FromStream).ToString(CultureInfo.InvariantCulture)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The code can be insterted in {0}.
(In C# 11 even multilines are supported https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-11#newlines-in-string-interpolations but we should switch from C# 10 to 11 in our msbuild files.)
| { | ||
| string payLoadData = BitConverter.ToString(fragmentData.blob, fragmentData.offset, fragmentData.length); | ||
| payLoadData = string.Format(CultureInfo.InvariantCulture, "0x{0}", payLoadData.Replace("-", string.Empty)); | ||
| payLoadData = string.Format(CultureInfo.InvariantCulture, $"0x{payLoadData.Replace("-", string.Empty)}"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps follow works (or will in .Net 8, or C# 11)
$"0x{payLoadData.AsSpan().Replace("-", string.Empty)}");| CimTestCimSessionContext testCimSessionContext = context as CimTestCimSessionContext; | ||
| uint sessionId = this.sessionState.GenerateSessionId(); | ||
| string originalSessionName = testCimSessionContext.CimSessionWrapper.Name; | ||
| string sessionName = originalSessionName ?? string.Format(CultureInfo.CurrentUICulture, @"{0}{1}", CimSessionState.CimSessionClassName, sessionId); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Really we should replace all string.Format with string.Create
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Follow up PR?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, current changes haven't value. Idea is to use new API to reduce allocations. There is no string.Format with interpalated string handler. I guess I misled you. Please use string.Create.
(You can find all new APIs with interpolated handlers starting with https://source.dot.net/#System.Private.CoreLib/src/libraries/System.Private.CoreLib/src/System/Runtime/CompilerServices/InterpolatedStringHandlerAttribute.cs,382762ca0bf5bcac,references)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also please don't change our trace.WriteLine() and other such callsites - this requires changes in the APIs themselves, which is not always easy. See example #18246.
src/Microsoft.PowerShell.Commands.Diagnostics/ImportCounterCommand.cs
Outdated
Show resolved
Hide resolved
| // at a time => bufferSize.Y == 1. Then, we can safely leave bufferSize.Y unchanged | ||
| // to retry with a smaller bufferSize.X. | ||
| Dbg.Assert(bufferSize.Y == 1, string.Format(CultureInfo.InvariantCulture, "bufferSize.Y should be 1, but is {0}", bufferSize.Y)); | ||
| Dbg.Assert(bufferSize.Y == 1, string.Format(CultureInfo.InvariantCulture, $"bufferSize.Y should be 1, but is {bufferSize.Y}")); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Dbg.Assert is our API. In follow PR we could modernize it to support interpolated string handler and then remove string.Format from such callsites.
The same for StringUtil.Format API.
…mand.cs Co-authored-by: Ilya <darpa@yandex.ru>
| uint sessionId = this.sessionState.GenerateSessionId(); | ||
| string originalSessionName = testCimSessionContext.CimSessionWrapper.Name; | ||
| string sessionName = originalSessionName ?? string.Format(CultureInfo.CurrentUICulture, @"{0}{1}", CimSessionState.CimSessionClassName, sessionId); | ||
| string sessionName = originalSessionName ?? string.Create(CultureInfo.CurrentUICulture, $@"{CimSessionState.CimSessionClassName}{sessionId}"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can remove verbatim string literal @. Below I see the same too.
| } | ||
|
|
||
| parameters.Append(string.Format(CultureInfo.CurrentUICulture, @"'{0}' = {1}", key, parameterList[key])); | ||
| parameters.Append(string.Format(CultureInfo.CurrentUICulture, $@"'{key}' = {parameterList[key]}")); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can use StringBuilder.AppendFormat
src/Microsoft.Management.UI.Internal/HelpWindow/HelpParagraphBuilder.cs
Outdated
Show resolved
Hide resolved
| if (result == null) | ||
| { | ||
| throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, "DataErrorInfoValidationResult not returned by ValidationRule: {0}", rule.ToString())); | ||
| throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, $"DataErrorInfoValidationResult not returned by ValidationRule: {rule.ToString()}")); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The same. And we can remove ToString().
| } | ||
|
|
||
| patterns.Add(string.Format(CultureInfo.InvariantCulture, "(?<{0}>){1}", FullTextRuleGroupName, ValuePattern)); | ||
| patterns.Add(string.Format(CultureInfo.InvariantCulture, $"(?<{FullTextRuleGroupName}>){ValuePattern}")); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The same. I stop review. Please replace all Format with Create.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm still working on it, its [WIP]. I'll change it to draft
| { | ||
| result.AppendFormat(null, "-Culture '{0}' ", CodeGeneration.EscapeSingleQuotedStringContent(runspaceConnectionInfo.Culture.ToString())); | ||
| result.AppendFormat(null, "-UICulture '{0}' ", CodeGeneration.EscapeSingleQuotedStringContent(runspaceConnectionInfo.UICulture.ToString())); | ||
| result.AppendFormat(null, $"-Culture '{CodeGeneration.EscapeSingleQuotedStringContent(runspaceConnectionInfo.Culture.ToString())}' "); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ToString() can be removed in interpolated strings.
|
@CarloToso The PR is too large. I suggest to split it by 5-10-15 files. |
|
This PR has Quantification details
Why proper sizing of changes matters
Optimal pull request sizes drive a better predictable PR flow as they strike a
What can I do to optimize my changes
How to interpret the change counts in git diff output
Was this comment helpful? 👍 :ok_hand: :thumbsdown: (Email) |
|
@iSazonov You are right this PR is too big, I'll leave it open until until I've finished splitting it, then I'll close it |
|
PR split in 7 parts |
PR Summary
Use interpolated strings with
String.Format(),StringBuilder.AppendFormat(),String.Create()PR Context
Proposed by @iSazonov
PR Checklist
.h,.cpp,.cs,.ps1and.psm1files have the correct copyright headerWIP:or[ WIP ]to the beginning of the title (theWIPbot will keep its status check atPendingwhile the prefix is present) and remove the prefix when the PR is ready.(which runs in a different PS Host).