forked from ServiceStack/ServiceStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExtensionMethods.cs
More file actions
68 lines (61 loc) · 2.06 KB
/
Copy pathExtensionMethods.cs
File metadata and controls
68 lines (61 loc) · 2.06 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
using System.Text.RegularExpressions;
using ServiceStack.Text;
namespace ServiceStack.MiniProfiler.Helpers
{
/// <summary>
/// Common extension methods to use only in this project
/// </summary>
internal static class ExtensionMethods
{
/// <summary>
/// Answers true if this String is either null or empty.
/// </summary>
internal static bool IsNullOrWhiteSpace(this string s)
{
return s == null || s.Trim() == string.Empty;
}
/// <summary>
/// Answers true if this String is neither null or empty.
/// </summary>
internal static bool HasValue(this string s)
{
return !s.IsNullOrWhiteSpace();
}
internal static string Truncate(this string s, int maxLength)
{
return s != null && s.Length > maxLength ? s.Substring(0, maxLength) : s;
}
/// <summary>
/// Removes trailing / characters from a path and leaves just one
/// </summary>
internal static string EnsureTrailingSlash(this string input)
{
if (string.IsNullOrEmpty(input)) return "";
return Regex.Replace(input, "/+$", "") + "/";
}
/// <summary>
/// Removes any leading / characters from a path
/// </summary>
internal static string RemoveLeadingSlash(this string input)
{
if (string.IsNullOrEmpty(input)) return "";
return Regex.Replace(input, "^/+", "");
}
/// <summary>
/// Removes any leading / characters from a path
/// </summary>
internal static string RemoveTrailingSlash(this string input)
{
if (string.IsNullOrEmpty(input)) return "";
return Regex.Replace(input, "/+$", "");
}
/// <summary>
/// Serializes <paramref name="o"/> to a json string.
/// </summary>
internal static string ToJson(this object o)
{
if (o == null) return null;
return JsonSerializer.SerializeToString(o);
}
}
}