-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Prefer use of interpolated strings in PresentationCore #8616
base: main
Are you sure you want to change the base?
Conversation
src/Microsoft.DotNet.Wpf/src/PresentationCore/MS/internal/AppModel/SiteOfOriginContainer.cs
Show resolved
Hide resolved
429535a
to
4dd05ab
Compare
4dd05ab
to
4ea7631
Compare
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.
@halgab A DRT(test) is failing on this PR: DrtColorAPI
You can find this test on our public repo: https://github.com/dotnet/wpf-test
I think so the test is failing due to some missing elements in this file after change.
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.
After having another read, the difference between the current version and mine is that when nativeColorValue
is empty, the current version still appends a separator value, which is not the case with my version. I feel like my version is more correct.
Tell me if you prefer that I maintain the current behaviour.
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.
Your changes are functionally different. Floats were formatted using format
previously but are now always formatted using R
. Also, the loop in your changes always prepends the separator instead of prepending the separator only when between items: ",A,B,C" vs "A,B,C". Also, there should be a space here between {uriString}
and {scRgbColor.a:R}
, it was there before your changes.
I would recommend testing your changes because by looking at the code I don't see how your changes returns the same result as the existing code.
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.
Those are valid points. I'll rollback the functional changes. I'd like to keep the constant format though, as format
is always set to R
before this loop, and that's what makes it possible to use interpolated strings. What do you think?
b63dd61
to
e7de550
Compare
+ StrokeFIndices.GetStringRepresentation(_inSegment.BeginFIndex) + "," | ||
+ StrokeFIndices.GetStringRepresentation(_inSegment.EndFIndex) + "," | ||
+ StrokeFIndices.GetStringRepresentation(_hitSegment.EndFIndex) + "}"; | ||
return $"{{{StrokeFIndices.GetStringRepresentation(_hitSegment.BeginFIndex)},{StrokeFIndices.GetStringRepresentation(_inSegment.BeginFIndex)},{StrokeFIndices.GetStringRepresentation(_inSegment.EndFIndex)},{StrokeFIndices.GetStringRepresentation(_hitSegment.EndFIndex)}}}"; |
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.
@halgab IMO, this change makes it a bit harder to understand what's happening here. Maybe we can use string.Format here ?
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.
@dipeshmsft We can just use regular interpolation and space it out, like this:
return $"{Int32.Parse("1")}," +
$"{Int32.Parse("1")}";
It will produce the following:
DefaultInterpolatedStringHandler defaultInterpolatedStringHandler = new DefaultInterpolatedStringHandler(1, 2);
defaultInterpolatedStringHandler.AppendFormatted<int>(int.Parse("1"));
defaultInterpolatedStringHandler.AppendLiteral(",");
defaultInterpolatedStringHandler.AppendFormatted<int>(int.Parse("1"));
defaultInterpolatedStringHandler.ToStringAndClear();
So the change like this:
return $"{{{StrokeFIndices.GetStringRepresentation(_hitSegment.BeginFIndex)}," +
$"{StrokeFIndices.GetStringRepresentation(_inSegment.BeginFIndex)}," +
$"{StrokeFIndices.GetStringRepresentation(_inSegment.EndFIndex)}," +
$"{StrokeFIndices.GetStringRepresentation(_hitSegment.EndFIndex)}}}";
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.
@h3xds1nz, I was just going through DefaultInterpolatedStringHandler
in your PR and your suggestion sounds good.
((IFormattable)StartPoint).ToString(format, provider) + | ||
(segments != null ? segments.ConvertToString(format, provider) : "") + | ||
(IsClosed ? "z" : ""); | ||
return $"M{((IFormattable)StartPoint).ToString(format, provider)}{(segments != null ? segments.ConvertToString(format, provider) : "")}{(IsClosed ? "z" : "")}"; |
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.
Again, this seems hard to be understood. Maybe consider using string.Concat / string.Format here.
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.
Same here, we can just use regular string interpolation and space it out over multiple lines, no need for string.Format ugliness.
{ | ||
sb.AppendFormat(provider,"{0:" + format + "}",nativeColorValue[i]); | ||
sb.Append(provider, $"{nativeColorValue[i]:R}"); |
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.
Rather than this, it would be better to keep using format
here. Although, I don't think we are going to change static format, this keeps using format and keeps it maintainable.
sb.Append(provider, $"{nativeColorValue[i]:R}"); | |
sb.AppendFormat(provider, $"{{0:{format}}}", nativeColorValue[i]); |
sb.AppendFormat(provider, "{0}{1} ", Parsers.s_ContextColor, uriString); | ||
sb.AppendFormat(provider,"{1:" + format + "}{0}",separator,scRgbColor.a); | ||
for (int i= 0; i< nativeColorValue.GetLength(0); ++i ) | ||
sb.Append(provider, $"{Parsers.s_ContextColor}{uriString} {scRgbColor.a:R}{separator}"); |
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 split this back in two lines and use the same suggestion as below for the second line.
@halgab, can you take a look at the above PR and make the necessary changes ? |
Follow-up to #8519
Description
We replace as many manual string concatenations and string.Format with interpolated strings. This saves allocations in many instances, and makes for a more readable code.
Customer Impact
Less allocations
Regression
No
Testing
CI
Risk
Low. Most changes are automated using the IDE
Microsoft Reviewers: Open in CodeFlow