mirror of https://github.com/SixLabors/ImageSharp
11 changed files with 71 additions and 173 deletions
@ -1,83 +0,0 @@ |
|||
// --------------------------------------------------------------------------------------------------------------------
|
|||
// <copyright file="StringExtensionsUnitTests.cs" company="James South">
|
|||
// Copyright (c) James South.
|
|||
// Licensed under the Apache License, Version 2.0.
|
|||
// </copyright>
|
|||
// <summary>
|
|||
// Provides a test harness for the string extensions
|
|||
// </summary>
|
|||
// --------------------------------------------------------------------------------------------------------------------
|
|||
|
|||
namespace ImageProcessor.UnitTests.Extensions |
|||
{ |
|||
using System.Collections.Generic; |
|||
using ImageProcessor.Common.Extensions; |
|||
using NUnit.Framework; |
|||
|
|||
/// <summary>
|
|||
/// Test harness for the string extensions
|
|||
/// </summary>
|
|||
[TestFixture] |
|||
public class StringExtensionsUnitTests |
|||
{ |
|||
/// <summary>
|
|||
/// Tests the passing to an integer array
|
|||
/// </summary>
|
|||
[Test] |
|||
public void TestToIntegerArray() |
|||
{ |
|||
Dictionary<string, int[]> data = new Dictionary<string, int[]> |
|||
{ |
|||
{ |
|||
"123-456,78-90", |
|||
new[] { 123, 456, 78, 90 } |
|||
}, |
|||
{ |
|||
"87390174,741897498,74816,748297,57355", |
|||
new[] |
|||
{ |
|||
87390174, 741897498, 74816, |
|||
748297, 57355 |
|||
} |
|||
}, |
|||
{ "1-2-3", new[] { 1, 2, 3 } } |
|||
}; |
|||
|
|||
foreach (KeyValuePair<string, int[]> item in data) |
|||
{ |
|||
int[] result = item.Key.ToPositiveIntegerArray(); |
|||
Assert.AreEqual(item.Value, result); |
|||
} |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Tests the passing to an float array
|
|||
/// </summary>
|
|||
[Test] |
|||
public void TestToFloatArray() |
|||
{ |
|||
Dictionary<string, float[]> data = new Dictionary<string, float[]> |
|||
{ |
|||
{ |
|||
"12.3-4.56,78-9.0", |
|||
new[] { 12.3F, 4.56F, 78, 9 } |
|||
}, |
|||
{ |
|||
"87390.174,7.41897498,748.16,748297,5.7355", |
|||
new[] |
|||
{ |
|||
87390.174F, 7.41897498F, |
|||
748.16F, 748297, 5.7355F |
|||
} |
|||
}, |
|||
{ "1-2-3", new float[] { 1, 2, 3 } } |
|||
}; |
|||
|
|||
foreach (KeyValuePair<string, float[]> item in data) |
|||
{ |
|||
float[] result = item.Key.ToPositiveFloatArray(); |
|||
Assert.AreEqual(item.Value, result); |
|||
} |
|||
} |
|||
} |
|||
} |
|||
@ -1,80 +0,0 @@ |
|||
// --------------------------------------------------------------------------------------------------------------------
|
|||
// <copyright file="StringExtensions.cs" company="James South">
|
|||
// Copyright (c) James South.
|
|||
// Licensed under the Apache License, Version 2.0.
|
|||
// </copyright>
|
|||
// <summary>
|
|||
// Encapsulates a series of time saving extension methods to the <see cref="T:System.String" /> class.
|
|||
// </summary>
|
|||
// --------------------------------------------------------------------------------------------------------------------
|
|||
|
|||
namespace ImageProcessor.Common.Extensions |
|||
{ |
|||
using System; |
|||
using System.Globalization; |
|||
using System.Text.RegularExpressions; |
|||
|
|||
/// <summary>
|
|||
/// Encapsulates a series of time saving extension methods to the <see cref="T:System.String"/> class.
|
|||
/// </summary>
|
|||
public static class StringExtensions |
|||
{ |
|||
/// <summary>
|
|||
/// Creates an array of integers scraped from the String.
|
|||
/// </summary>
|
|||
/// <param name="expression">The <see cref="T:System.String">String</see> instance that this method extends.</param>
|
|||
/// <returns>An array of integers scraped from the String.</returns>
|
|||
public static int[] ToPositiveIntegerArray(this string expression) |
|||
{ |
|||
if (string.IsNullOrWhiteSpace(expression)) |
|||
{ |
|||
throw new ArgumentNullException("expression"); |
|||
} |
|||
|
|||
Regex regex = new Regex(@"[\d+]+(?=[,-])|[\d+]+(?![,-])", RegexOptions.Compiled); |
|||
|
|||
MatchCollection matchCollection = regex.Matches(expression); |
|||
|
|||
// Get the collections.
|
|||
int count = matchCollection.Count; |
|||
int[] matches = new int[count]; |
|||
|
|||
// Loop and parse the int values.
|
|||
for (int i = 0; i < count; i++) |
|||
{ |
|||
matches[i] = int.Parse(matchCollection[i].Value, CultureInfo.InvariantCulture); |
|||
} |
|||
|
|||
return matches; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Creates an array of floats scraped from the String.
|
|||
/// </summary>
|
|||
/// <param name="expression">The <see cref="T:System.String">String</see> instance that this method extends.</param>
|
|||
/// <returns>An array of floats scraped from the String.</returns>
|
|||
public static float[] ToPositiveFloatArray(this string expression) |
|||
{ |
|||
if (string.IsNullOrWhiteSpace(expression)) |
|||
{ |
|||
throw new ArgumentNullException("expression"); |
|||
} |
|||
|
|||
Regex regex = new Regex(@"[\d+\.]+(?=[,-])|[\d+\.]+(?![,-])", RegexOptions.Compiled); |
|||
|
|||
MatchCollection matchCollection = regex.Matches(expression); |
|||
|
|||
// Get the collections.
|
|||
int count = matchCollection.Count; |
|||
float[] matches = new float[count]; |
|||
|
|||
// Loop and parse the int values.
|
|||
for (int i = 0; i < count; i++) |
|||
{ |
|||
matches[i] = float.Parse(matchCollection[i].Value, CultureInfo.InvariantCulture); |
|||
} |
|||
|
|||
return matches; |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue