Statistics
| Branch: | Revision:

root / trunk / hammock / src / net35 / Hammock / Extensions / StringExtensions.cs @ 0eea575a

History | View | Annotate | Download (3.8 kB)

1
using System;
2
using System.Collections.Generic;
3
using System.Globalization;
4
using System.Linq;
5
using System.Text;
6
using System.Text.RegularExpressions;
7
#if SILVERLIGHT && !WindowsPhone
8
using System.Windows.Browser;
9
#endif
10

    
11
#if WindowsPhone
12
using System.Web;
13
#endif
14

    
15
#if !SILVERLIGHT && !MonoTouch && !NETCF
16
using System.Web;
17
#endif
18

    
19
namespace Hammock.Extensions
20
{
21
    internal static class StringExtensions
22
    {
23
        public static bool IsNullOrBlank(this string value)
24
        {
25
            return String.IsNullOrEmpty(value) ||
26
                   (!String.IsNullOrEmpty(value) && value.Trim() == String.Empty);
27
        }
28

    
29
        public static bool EqualsIgnoreCase(this string left, string right)
30
        {
31
            return String.Compare(left, right, StringComparison.InvariantCultureIgnoreCase) == 0;
32
        }
33

    
34
        public static bool EqualsAny(this string input, params string[] args)
35
        {
36
            return args.Aggregate(false, (current, arg) => current | input.Equals(arg));
37
        }
38

    
39
        public static string FormatWith(this string format, params object[] args)
40
        {
41
            return String.Format(format, args);
42
        }
43

    
44
        public static string FormatWithInvariantCulture(this string format, params object[] args)
45
        {
46
            return String.Format(CultureInfo.InvariantCulture, format, args);
47
        }
48

    
49
        public static string Then(this string input, string value)
50
        {
51
            return String.Concat(input, value);
52
        }
53

    
54
        public static string UrlEncode(this string value)
55
        {
56
            // [DC] This is more correct than HttpUtility; it escapes spaces as %20, not +
57
            return Uri.EscapeDataString(value);
58
        }
59

    
60
        public static string UrlDecode(this string value)
61
        {
62
            return Uri.UnescapeDataString(value);
63
        }
64

    
65
        public static Uri AsUri(this string value)
66
        {
67
            return new Uri(value);
68
        }
69

    
70
        public static string ToBase64String(this byte[] input)
71
        {
72
            return Convert.ToBase64String(input);
73
        }
74

    
75
        public static byte[] GetBytes(this string input)
76
        {
77
            return Encoding.UTF8.GetBytes(input);
78
        }
79

    
80
        public static string PercentEncode(this string s)
81
        {
82
            var bytes = s.GetBytes();
83
            var sb = new StringBuilder();
84
            foreach (var b in bytes)
85
            {
86
                // [DC]: Support proper encoding of special characters (\n\r\t\b)
87
                if((b > 7 && b < 11) || b == 13)
88
                {
89
                    sb.Append(string.Format("%0{0:X}", b));
90
                }
91
                else
92
                {
93
                    sb.Append(string.Format("%{0:X}", b));
94
                }
95
            }
96
            return sb.ToString();
97
        }
98

    
99
        public static IDictionary<string, string> ParseQueryString(this string query)
100
        {
101
            // [DC]: This method does not URL decode, and cannot handle decoded input
102
            if (query.StartsWith("?")) query = query.Substring(1);
103
            
104
            if(query.Equals(string.Empty))
105
            {
106
                return new Dictionary<string, string>();
107
            }
108

    
109
            var parts = query.Split(new[] { '&' });
110
            
111
            return parts.Select(
112
                part => part.Split(new[] { '=' })).ToDictionary(
113
                    pair => pair[0], pair => pair[1]
114
                );
115
        }
116

    
117
        public static bool TryParse(this string value, out double number)
118
        {
119
          try
120
          {
121
            number = double.Parse(value);
122
            return true;
123
          }
124
          catch
125
          {
126
            number =  0;
127
            return false;
128
          }
129
        }
130

    
131
        private const RegexOptions Options =
132
#if !SILVERLIGHT && !MonoTouch
133
            RegexOptions.Compiled | RegexOptions.IgnoreCase;
134
#else
135
            RegexOptions.IgnoreCase;
136
#endif
137
    }
138
}