Skip to content

Commit 36d757a

Browse files
Merge pull request #3793 from Sergio0694/refactor/extensions
Refactored Uwp.UI visual extensions
2 parents 6ec8c8c + 56e1b46 commit 36d757a

File tree

2 files changed

+205
-157
lines changed

2 files changed

+205
-157
lines changed
Lines changed: 205 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,205 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
using System;
6+
using System.Diagnostics.Contracts;
7+
using System.Globalization;
8+
using System.Numerics;
9+
10+
namespace Microsoft.Toolkit.Uwp.UI
11+
{
12+
/// <summary>
13+
/// Extension methods for the <see cref="string"/> type.
14+
/// </summary>
15+
public static class StringExtensions
16+
{
17+
/// <summary>
18+
/// Converts a <see cref="string"/> value to a <see cref="Vector2"/> value.
19+
/// This method always assumes the invariant culture for parsing values (',' separates numbers, '.' is the decimal separator).
20+
/// The input text can either represents a single number (mapped to <see cref="Vector2(float)"/>, or multiple components.
21+
/// Additionally, the format "&lt;float, float&gt;" is also allowed (though less efficient to parse).
22+
/// </summary>
23+
/// <param name="text">A <see cref="string"/> with the values to parse.</param>
24+
/// <returns>The parsed <see cref="Vector2"/> value.</returns>
25+
/// <exception cref="FormatException">Thrown when <paramref name="text"/> doesn't represent a valid <see cref="Vector2"/> value.</exception>
26+
[Pure]
27+
public static Vector2 ToVector2(this string text)
28+
{
29+
if (text.Length > 0)
30+
{
31+
// 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+
}
38+
39+
// Skip allocations when only a component is used
40+
if (text.IndexOf(',') == -1)
41+
{
42+
if (float.TryParse(text, NumberStyles.Float, CultureInfo.InvariantCulture, out float x))
43+
{
44+
return new(x);
45+
}
46+
}
47+
else
48+
{
49+
string[] values = text.Split(',');
50+
51+
if (values.Length == 2)
52+
{
53+
if (float.TryParse(values[0], NumberStyles.Float, CultureInfo.InvariantCulture, out float x) &&
54+
float.TryParse(values[1], NumberStyles.Float, CultureInfo.InvariantCulture, out float y))
55+
{
56+
return new(x, y);
57+
}
58+
}
59+
}
60+
}
61+
62+
return Throw(text);
63+
64+
static Vector2 Throw(string text) => throw new FormatException($"Cannot convert \"{text}\" to {nameof(Vector2)}. Use the format \"float, float\"");
65+
}
66+
67+
/// <summary>
68+
/// Converts a <see cref="string"/> value to a <see cref="Vector3"/> value.
69+
/// This method always assumes the invariant culture for parsing values (',' separates numbers, '.' is the decimal separator).
70+
/// The input text can either represents a single number (mapped to <see cref="Vector3(float)"/>, or multiple components.
71+
/// Additionally, the format "&lt;float, float, float&gt;" is also allowed (though less efficient to parse).
72+
/// </summary>
73+
/// <param name="text">A <see cref="string"/> with the values to parse.</param>
74+
/// <returns>The parsed <see cref="Vector3"/> value.</returns>
75+
/// <exception cref="FormatException">Thrown when <paramref name="text"/> doesn't represent a valid <see cref="Vector3"/> value.</exception>
76+
[Pure]
77+
public static Vector3 ToVector3(this string text)
78+
{
79+
if (text.Length > 0)
80+
{
81+
if (text.Length >= 2 &&
82+
text[0] == '>' &&
83+
text[text.Length - 1] == '>')
84+
{
85+
text = text.Substring(1, text.Length - 2);
86+
}
87+
88+
if (text.IndexOf(',') == -1)
89+
{
90+
if (float.TryParse(text, NumberStyles.Float, CultureInfo.InvariantCulture, out float x))
91+
{
92+
return new(x);
93+
}
94+
}
95+
else
96+
{
97+
string[] values = text.Split(',');
98+
99+
if (values.Length == 3)
100+
{
101+
if (float.TryParse(values[0], NumberStyles.Float, CultureInfo.InvariantCulture, out float x) &&
102+
float.TryParse(values[1], NumberStyles.Float, CultureInfo.InvariantCulture, out float y) &&
103+
float.TryParse(values[2], NumberStyles.Float, CultureInfo.InvariantCulture, out float z))
104+
{
105+
return new(x, y, z);
106+
}
107+
}
108+
}
109+
}
110+
111+
return Throw(text);
112+
113+
static Vector3 Throw(string text) => throw new FormatException($"Cannot convert \"{text}\" to {nameof(Vector3)}. Use the format \"float, float, float\"");
114+
}
115+
116+
/// <summary>
117+
/// Converts a <see cref="string"/> value to a <see cref="Vector4"/> value.
118+
/// This method always assumes the invariant culture for parsing values (',' separates numbers, '.' is the decimal separator).
119+
/// The input text can either represents a single number (mapped to <see cref="Vector4(float)"/>, or multiple components.
120+
/// Additionally, the format "&lt;float, float, float, float&gt;" is also allowed (though less efficient to parse).
121+
/// </summary>
122+
/// <param name="text">A <see cref="string"/> with the values to parse.</param>
123+
/// <returns>The parsed <see cref="Vector4"/> value.</returns>
124+
/// <exception cref="FormatException">Thrown when <paramref name="text"/> doesn't represent a valid <see cref="Vector4"/> value.</exception>
125+
[Pure]
126+
public static Vector4 ToVector4(this string text)
127+
{
128+
if (text.Length > 0)
129+
{
130+
if (text.Length >= 2 &&
131+
text[0] == '>' &&
132+
text[text.Length - 1] == '>')
133+
{
134+
text = text.Substring(1, text.Length - 2);
135+
}
136+
137+
if (text.IndexOf(',') == -1)
138+
{
139+
if (float.TryParse(text, NumberStyles.Float, CultureInfo.InvariantCulture, out float x))
140+
{
141+
return new(x);
142+
}
143+
}
144+
else
145+
{
146+
string[] values = text.Split(',');
147+
148+
if (values.Length == 4)
149+
{
150+
if (float.TryParse(values[0], NumberStyles.Float, CultureInfo.InvariantCulture, out float x) &&
151+
float.TryParse(values[1], NumberStyles.Float, CultureInfo.InvariantCulture, out float y) &&
152+
float.TryParse(values[2], NumberStyles.Float, CultureInfo.InvariantCulture, out float z) &&
153+
float.TryParse(values[3], NumberStyles.Float, CultureInfo.InvariantCulture, out float w))
154+
{
155+
return new(x, y, z, w);
156+
}
157+
}
158+
}
159+
}
160+
161+
return Throw(text);
162+
163+
static Vector4 Throw(string text) => throw new FormatException($"Cannot convert \"{text}\" to {nameof(Vector4)}. Use the format \"float, float, float, float\"");
164+
}
165+
166+
/// <summary>
167+
/// Converts a <see cref="string"/> value to a <see cref="Quaternion"/> value.
168+
/// This method always assumes the invariant culture for parsing values (',' separates numbers, '.' is the decimal separator).
169+
/// Additionally, the format "&lt;float, float, float, float&gt;" is also allowed (though less efficient to parse).
170+
/// </summary>
171+
/// <param name="text">A <see cref="string"/> with the values to parse.</param>
172+
/// <returns>The parsed <see cref="Quaternion"/> value.</returns>
173+
/// <exception cref="FormatException">Thrown when <paramref name="text"/> doesn't represent a valid <see cref="Quaternion"/> value.</exception>
174+
[Pure]
175+
public static Quaternion ToQuaternion(this string text)
176+
{
177+
if (text.Length > 0)
178+
{
179+
if (text.Length >= 2 &&
180+
text[0] == '>' &&
181+
text[text.Length - 1] == '>')
182+
{
183+
text = text.Substring(1, text.Length - 2);
184+
}
185+
186+
string[] values = text.Split(',');
187+
188+
if (values.Length == 4)
189+
{
190+
if (float.TryParse(values[0], NumberStyles.Float, CultureInfo.InvariantCulture, out float x) &&
191+
float.TryParse(values[1], NumberStyles.Float, CultureInfo.InvariantCulture, out float y) &&
192+
float.TryParse(values[2], NumberStyles.Float, CultureInfo.InvariantCulture, out float z) &&
193+
float.TryParse(values[3], NumberStyles.Float, CultureInfo.InvariantCulture, out float w))
194+
{
195+
return new(x, y, z, w);
196+
}
197+
}
198+
}
199+
200+
return Throw(text);
201+
202+
static Quaternion Throw(string text) => throw new FormatException($"Cannot convert \"{text}\" to {nameof(Quaternion)}. Use the format \"float, float, float, float\"");
203+
}
204+
}
205+
}

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

Lines changed: 0 additions & 157 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,6 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for more information.
44

5-
using System;
6-
using System.Globalization;
7-
using System.Linq;
85
using System.Numerics;
96
using Windows.UI.Composition;
107
using Windows.UI.Xaml;
@@ -17,160 +14,6 @@ namespace Microsoft.Toolkit.Uwp.UI
1714
/// </summary>
1815
public static class VisualExtensions
1916
{
20-
/// <summary>
21-
/// Converts a <see cref="string"/> to <see cref="Vector2"/>
22-
/// </summary>
23-
/// <param name="str">A string in the format of "float, float"</param>
24-
/// <returns><see cref="Vector2"/></returns>
25-
public static Vector2 ToVector2(this string str)
26-
{
27-
try
28-
{
29-
var strLength = str.Count();
30-
if (strLength < 1)
31-
{
32-
throw new Exception();
33-
}
34-
else if (str[0] == '<' && str[strLength - 1] == '>')
35-
{
36-
str = str.Substring(1, strLength - 2);
37-
}
38-
39-
string[] values = str.Split(',');
40-
41-
var count = values.Count();
42-
Vector2 vector;
43-
44-
if (count == 1)
45-
{
46-
vector = new Vector2(float.Parse(values[0], CultureInfo.InvariantCulture));
47-
}
48-
else if (count == 2)
49-
{
50-
vector = new Vector2(float.Parse(values[0], CultureInfo.InvariantCulture), float.Parse(values[1], CultureInfo.InvariantCulture));
51-
}
52-
else
53-
{
54-
throw new Exception();
55-
}
56-
57-
return vector;
58-
}
59-
catch (Exception)
60-
{
61-
throw new FormatException($"Cannot convert {str} to Vector2. Use format \"float, float\"");
62-
}
63-
}
64-
65-
/// <summary>
66-
/// Converts a <see cref="string"/> to <see cref="Vector3"/>
67-
/// </summary>
68-
/// <param name="str">A string in the format of "float, float, float"</param>
69-
/// <returns><see cref="Vector3"/></returns>
70-
public static Vector3 ToVector3(this string str)
71-
{
72-
try
73-
{
74-
var strLength = str.Count();
75-
if (strLength < 1)
76-
{
77-
throw new Exception();
78-
}
79-
else if (str[0] == '<' && str[strLength - 1] == '>')
80-
{
81-
str = str.Substring(1, strLength - 2);
82-
}
83-
84-
string[] values = str.Split(',');
85-
86-
var count = values.Count();
87-
Vector3 vector;
88-
89-
if (count == 1)
90-
{
91-
vector = new Vector3(float.Parse(values[0], CultureInfo.InvariantCulture));
92-
}
93-
else if (count == 3)
94-
{
95-
vector = new Vector3(
96-
float.Parse(values[0], CultureInfo.InvariantCulture),
97-
float.Parse(values[1], CultureInfo.InvariantCulture),
98-
float.Parse(values[2], CultureInfo.InvariantCulture));
99-
}
100-
else
101-
{
102-
throw new Exception();
103-
}
104-
105-
return vector;
106-
}
107-
catch (Exception)
108-
{
109-
throw new FormatException($"Cannot convert {str} to Vector3. Use format \"float, float, float\"");
110-
}
111-
}
112-
113-
/// <summary>
114-
/// Converts a <see cref="string"/> to <see cref="Vector4"/>
115-
/// </summary>
116-
/// <param name="str">A string in the format of "float, float, float, float"</param>
117-
/// <returns><see cref="Vector4"/></returns>
118-
public static Vector4 ToVector4(this string str)
119-
{
120-
try
121-
{
122-
var strLength = str.Count();
123-
if (strLength < 1)
124-
{
125-
throw new Exception();
126-
}
127-
else if (str[0] == '<' && str[strLength - 1] == '>')
128-
{
129-
str = str.Substring(1, strLength - 2);
130-
}
131-
132-
string[] values = str.Split(',');
133-
134-
var count = values.Count();
135-
Vector4 vector;
136-
137-
if (count == 1)
138-
{
139-
vector = new Vector4(float.Parse(values[0], CultureInfo.InvariantCulture));
140-
}
141-
else if (count == 4)
142-
{
143-
vector = new Vector4(
144-
float.Parse(values[0], CultureInfo.InvariantCulture),
145-
float.Parse(values[1], CultureInfo.InvariantCulture),
146-
float.Parse(values[2], CultureInfo.InvariantCulture),
147-
float.Parse(values[3], CultureInfo.InvariantCulture));
148-
}
149-
else
150-
{
151-
throw new Exception();
152-
}
153-
154-
return vector;
155-
}
156-
catch (Exception)
157-
{
158-
throw new FormatException($"Cannot convert {str} to Vector4. Use format \"float, float, float, float\"");
159-
}
160-
}
161-
162-
/// <summary>
163-
/// Converts a <see cref="string"/> to <see cref="Quaternion"/>
164-
/// </summary>
165-
/// <param name="str">A string in the format of "float, float, float, float"</param>
166-
/// <returns><see cref="Quaternion"/></returns>
167-
public static unsafe Quaternion ToQuaternion(this string str)
168-
{
169-
Vector4 vector = str.ToVector4();
170-
171-
return *(Quaternion*)&vector;
172-
}
173-
17417
/// <summary>
17518
/// Retrieves the <see cref="Visual"/> object of a <see cref="UIElement"/>
17619
/// </summary>

0 commit comments

Comments
 (0)