[14777] | 1 | namespace HeuristicLab.BenchmarkSuite {
|
---|
| 2 | using System;
|
---|
| 3 | using System.Collections.Generic;
|
---|
| 4 | using System.IO;
|
---|
| 5 | using System.Linq;
|
---|
| 6 |
|
---|
| 7 | public static class ExampleArgumentConverter {
|
---|
| 8 | private static readonly char[] ArrayValueSeparator = { ' ', ',', ';' };
|
---|
| 9 | private static readonly char[] ArraySymbolTrim = { '[', ']', '(', ')' };
|
---|
| 10 | public static double[] ConvertDoubles(string input) {
|
---|
| 11 | return ConvertMultiple(input, double.Parse);
|
---|
| 12 | }
|
---|
| 13 |
|
---|
| 14 | public static double ConvertDouble(string input) {
|
---|
| 15 | return double.Parse(input);
|
---|
| 16 | }
|
---|
| 17 |
|
---|
| 18 | public static long[] ConvertIntegers(string input) {
|
---|
| 19 | return ConvertMultiple(input, long.Parse);
|
---|
| 20 | }
|
---|
| 21 |
|
---|
| 22 | public static long ConvertInteger(string input) {
|
---|
| 23 |
|
---|
| 24 | return string.IsNullOrEmpty(input) ? default(long) : long.Parse(input);
|
---|
| 25 | }
|
---|
| 26 |
|
---|
| 27 | public static T[] ConvertMultiple<T>(string input, Func<string, T> converter) {
|
---|
| 28 | return input
|
---|
| 29 | .Trim(ArraySymbolTrim)
|
---|
| 30 | .Split(ArrayValueSeparator)
|
---|
| 31 | .Where(s => !string.IsNullOrWhiteSpace(s))
|
---|
| 32 | .Select(converter)
|
---|
| 33 | .ToArray();
|
---|
| 34 | }
|
---|
| 35 |
|
---|
| 36 | public static IEnumerable<string> SplitByNewLine(string input) {
|
---|
| 37 | return input.Split(new[] { Environment.NewLine, "\n", "\r\n" }, StringSplitOptions.None);
|
---|
| 38 | }
|
---|
| 39 |
|
---|
| 40 | private static string[] trueFormats = new[] { "true", "t", "1" };
|
---|
| 41 | private static string[] falseFormats = new[] { "false", "f", "0" };
|
---|
| 42 |
|
---|
| 43 | public static bool ConvertBoolean(string input) {
|
---|
| 44 | var str = input.ToLower();
|
---|
| 45 |
|
---|
| 46 | if (trueFormats.Contains(str)) return true;
|
---|
| 47 | if (falseFormats.Contains(str)) return false;
|
---|
| 48 | throw new InvalidDataException(string.Format("Unable to parse {0} as boolean", str));
|
---|
| 49 | }
|
---|
| 50 |
|
---|
| 51 | public static bool[] ConvertBooleans(string input) {
|
---|
| 52 | return input
|
---|
| 53 | .Trim(ArraySymbolTrim)
|
---|
| 54 | .Split(ArrayValueSeparator)
|
---|
| 55 | .Where(s => !string.IsNullOrWhiteSpace(s))
|
---|
| 56 | .Select(ConvertBoolean)
|
---|
| 57 | .ToArray();
|
---|
| 58 | }
|
---|
| 59 | }
|
---|
| 60 | }
|
---|