Skip to content

Commit 0ff1745

Browse files
author
msftbot[bot]
authored
Fix wrong bracket matching when parsing Vectors (#3837)
<!-- 🚨 Please Do Not skip any instructions and information mentioned below as they are all required and essential to evaluate and test the PR. By fulfilling all the required information you will be able to reduce the volume of questions and most likely help merge the PR faster 🚨 --> <!-- 📝 It is preferred if you keep the "☑️ Allow edits by maintainers" checked in the Pull Request Template as it increases collaboration with the Toolkit maintainers by permitting commits to your PR branch (only) created from your fork. This can let us quickly make fixes for minor typos or forgotten StyleCop issues during review without needing to wait on you doing extra work. Let us help you help us! 🎉 --> ## Fixes #3834 <!-- Add the relevant issue number after the "#" mentioned above (for ex: Fixes #1234) which will automatically close the issue once the PR is merged. --> <!-- Add a brief overview here of the feature/bug & fix. --> Fixed wrong bracket matching in `StringExtensions.ToVectorN(string)` and `StringExtensions.ToQuaternion(string)` methods. ## PR Type What kind of change does this PR introduce? <!-- Please uncomment one or more that apply to this PR. --> - Bugfix <!-- - Feature --> <!-- - Code style update (formatting) --> <!-- - Refactoring (no functional changes, no api changes) --> <!-- - Build or CI related changes --> <!-- - Documentation content changes --> <!-- - Sample app changes --> <!-- - Other... Please describe: --> ## What is the current behavior? <!-- Please describe the current behavior that you are modifying, or link to a relevant issue. --> Sample app crashes. ## What is the new behavior? <!-- Describe how was this issue resolved or changed? --> Sample app does not crash. ## PR Checklist Please check if your PR fulfills the following requirements: - [ ] Tested code with current [supported SDKs](../readme.md#supported) - [ ] Pull Request has been submitted to the documentation repository [instructions](..\contributing.md#docs). Link: <!-- docs PR link --> - [ ] Sample in sample app has been added / updated (for bug fixes / features) - [ ] Icon has been created (if new sample) following the [Thumbnail Style Guide and templates](https://github.com/windows-toolkit/WindowsCommunityToolkit-design-assets) - [ ] New major technical changes in the toolkit have or will be added to the [Wiki](https://github.com/windows-toolkit/WindowsCommunityToolkit/wiki) e.g. build changes, source generators, testing infrastructure, sample creation changes, etc... - [ ] Tests for the changes have been added (for bug fixes / features) (if applicable) - [ ] Header has been added to all new source files (run *build/UpdateHeaders.bat*) - [ ] Contains **NO** breaking changes <!-- If this PR contains a breaking change, please describe the impact and migration path for existing applications below. Please note that breaking changes are likely to be rejected within minor release cycles or held until major versions. --> ## Other information
2 parents a2ed6aa + dcb22e2 commit 0ff1745

File tree

1 file changed

+22
-24
lines changed

1 file changed

+22
-24
lines changed

Microsoft.Toolkit.Uwp.UI/Extensions/StringExtensions.cs

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,7 @@ public static Vector2 ToVector2(this string text)
2929
if (text.Length > 0)
3030
{
3131
// The format <x> or <x, y> is supported
32-
if (text.Length >= 2 &&
33-
text[0] == '>' &&
34-
text[text.Length - 1] == '>')
35-
{
36-
text = text.Substring(1, text.Length - 2);
37-
}
32+
text = Unbracket(text);
3833

3934
// Skip allocations when only a component is used
4035
if (text.IndexOf(',') == -1)
@@ -78,12 +73,7 @@ public static Vector3 ToVector3(this string text)
7873
{
7974
if (text.Length > 0)
8075
{
81-
if (text.Length >= 2 &&
82-
text[0] == '>' &&
83-
text[text.Length - 1] == '>')
84-
{
85-
text = text.Substring(1, text.Length - 2);
86-
}
76+
text = Unbracket(text);
8777

8878
if (text.IndexOf(',') == -1)
8979
{
@@ -127,12 +117,7 @@ public static Vector4 ToVector4(this string text)
127117
{
128118
if (text.Length > 0)
129119
{
130-
if (text.Length >= 2 &&
131-
text[0] == '>' &&
132-
text[text.Length - 1] == '>')
133-
{
134-
text = text.Substring(1, text.Length - 2);
135-
}
120+
text = Unbracket(text);
136121

137122
if (text.IndexOf(',') == -1)
138123
{
@@ -176,12 +161,7 @@ public static Quaternion ToQuaternion(this string text)
176161
{
177162
if (text.Length > 0)
178163
{
179-
if (text.Length >= 2 &&
180-
text[0] == '>' &&
181-
text[text.Length - 1] == '>')
182-
{
183-
text = text.Substring(1, text.Length - 2);
184-
}
164+
text = Unbracket(text);
185165

186166
string[] values = text.Split(',');
187167

@@ -201,5 +181,23 @@ public static Quaternion ToQuaternion(this string text)
201181

202182
static Quaternion Throw(string text) => throw new FormatException($"Cannot convert \"{text}\" to {nameof(Quaternion)}. Use the format \"float, float, float, float\"");
203183
}
184+
185+
/// <summary>
186+
/// Converts an angle bracketed <see cref="string"/> value to its unbracketed form (e.g. "&lt;float, float&gt;" to "float, float").
187+
/// If the value is already unbracketed, this method will return the value unchanged.
188+
/// </summary>
189+
/// <param name="text">A bracketed <see cref="string"/> value.</param>
190+
/// <returns>The unbracketed <see cref="string"/> value.</returns>
191+
private static string Unbracket(string text)
192+
{
193+
if (text.Length >= 2 &&
194+
text[0] == '<' &&
195+
text[text.Length - 1] == '>')
196+
{
197+
text = text.Substring(1, text.Length - 2);
198+
}
199+
200+
return text;
201+
}
204202
}
205203
}

0 commit comments

Comments
 (0)