forked from NancyFx/Nancy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringExtensions.cs
More file actions
118 lines (102 loc) · 4.47 KB
/
Copy pathStringExtensions.cs
File metadata and controls
118 lines (102 loc) · 4.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
namespace Nancy.Extensions
{
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text.RegularExpressions;
using Nancy.Helpers;
using Routing;
/// <summary>
/// Containing extensions for the <see cref="string"/> object.
/// </summary>
public static class StringExtensions
{
/// <summary>
/// A regular expression used to manipulate parameterized route segments.
/// </summary>
/// <value>A <see cref="Regex"/> object.</value>
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private static readonly Regex ParameterExpression =
new Regex(@"{(?<name>[A-Za-z0-9_]*)(?:\?(?<default>[A-Za-z0-9_]*))?}", RegexOptions.Compiled);
/// <summary>
/// Extracts information about the parameters in the <paramref name="segment"/>.
/// </summary>
/// <param name="segment">The segment that the information should be extracted from.</param>
/// <returns>An <see cref="IEnumerable{T}"/>, containing <see cref="ParameterSegmentInformation"/> instances for the parameters in the segment.</returns>
public static IEnumerable<ParameterSegmentInformation> GetParameterDetails(this string segment)
{
var matches = ParameterExpression
.Matches(segment);
var nameMatch = matches
.Cast<Match>()
.Select(x => x)
.ToList();
if (nameMatch.Any())
{
return nameMatch.Select(x => new ParameterSegmentInformation(x.Groups["name"].Value, x.Groups["default"].Value, x.Groups["default"].Success));
}
throw new FormatException("The segment did not contain any parameters.");
}
/// <summary>
/// Checks if a segment contains any parameters.
/// </summary>
/// <param name="segment">The segment to check for parameters.</param>
/// <returns>true if the segment contains a parameter; otherwise false.</returns>
/// <remarks>A parameter is defined as a string which is surrounded by a pair of curly brackets.</remarks>
/// <exception cref="ArgumentException">The provided value for the segment parameter was null or empty.</exception>
public static bool IsParameterized(this string segment)
{
var parameterMatch =
ParameterExpression.Match(segment);
return parameterMatch.Success;
}
/// <summary>
/// Gets a dynamic dictionary back from a Uri query string
/// </summary>
/// <param name="queryString">The query string to extract values from</param>
/// <returns>A dynamic dictionary containing the query string values</returns>
public static DynamicDictionary AsQueryDictionary(this string queryString)
{
var coll = HttpUtility.ParseQueryString(queryString);
var ret = new DynamicDictionary();
var found = 0;
foreach (var key in coll.AllKeys.Where(key => key != null))
{
ret[key] = coll[key];
found++;
if (found >= StaticConfiguration.RequestQueryFormMultipartLimit)
{
break;
}
}
return ret;
}
/// <summary>
/// Converts the value from PascalCase to camelCase.
/// </summary>
/// <param name="value">The value.</param>
/// <returns>System.String.</returns>
public static string ToCamelCase(this string value)
{
return value.ConvertFirstCharacter(x => x.ToLowerInvariant());
}
/// <summary>
/// Converts the value from camelCase to PascalCase.
/// </summary>
/// <param name="value">The value.</param>
/// <returns>System.String.</returns>
public static string ToPascalCase(this string value)
{
return value.ConvertFirstCharacter(x => x.ToUpperInvariant());
}
private static string ConvertFirstCharacter(this string value, Func<string, string> converter)
{
if (string.IsNullOrEmpty(value))
{
return string.Empty;
}
return string.Concat(converter(value.Substring(0, 1)), value.Substring(1));
}
}
}