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