~ubuntu-branches/ubuntu/trusty/smuxi/trusty-proposed

« back to all changes in this revision

Viewing changes to lib/ServiceStack/src/ServiceStack.Common/StringExtensions.cs

  • Committer: Package Import Robot
  • Author(s): Mirco Bauer
  • Date: 2013-05-25 22:11:31 UTC
  • mfrom: (1.2.12)
  • Revision ID: package-import@ubuntu.com-20130525221131-nd2mc0kzubuwyx20
Tags: 0.8.11-1
* [22d13d5] Imported Upstream version 0.8.11
* [6d2b95a] Refreshed patches
* [89eb66e] Added ServiceStack libraries to smuxi-engine package
* [848ab10] Enable Campfire engine
* [c6dbdc7] Always build db4o for predictable build result
* [13ec489] Exclude OS X specific libraries from dh_clideps

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
using System;
 
2
using System.Collections.Generic;
 
3
using System.Globalization;
 
4
using System.Text;
 
5
using System.Text.RegularExpressions;
 
6
using ServiceStack.Common.Utils;
 
7
using ServiceStack.Text;
 
8
using ServiceStack.Text.Common;
 
9
 
 
10
namespace ServiceStack.Common
 
11
{
 
12
    public static class StringExtensions
 
13
    {
 
14
        static readonly Regex RegexSplitCamelCase = new Regex("([A-Z]|[0-9]+)", 
 
15
#if !SILVERLIGHT && !MONOTOUCH && !XBOX
 
16
            RegexOptions.Compiled
 
17
#else
 
18
            RegexOptions.None
 
19
#endif
 
20
        );
 
21
 
 
22
        public static T ToEnum<T>(this string value)
 
23
        {
 
24
            return (T)Enum.Parse(typeof(T), value, true);
 
25
        }
 
26
 
 
27
        public static T ToEnumOrDefault<T>(this string value, T defaultValue)
 
28
        {
 
29
            if (String.IsNullOrEmpty(value)) return defaultValue;
 
30
            return (T)Enum.Parse(typeof(T), value, true);
 
31
        }
 
32
 
 
33
        public static string SplitCamelCase(this string value)
 
34
        {
 
35
            return RegexSplitCamelCase.Replace(value, " $1").TrimStart();
 
36
        }
 
37
 
 
38
        public static string ToEnglish(this string camelCase)
 
39
        {
 
40
            var ucWords = camelCase.SplitCamelCase().ToLower();
 
41
            return ucWords[0].ToString(CultureInfo.InvariantCulture).ToUpper() + ucWords.Substring(1);
 
42
        }
 
43
 
 
44
        public static bool IsEmpty(this string value)
 
45
        {
 
46
            return String.IsNullOrEmpty(value);
 
47
        }
 
48
 
 
49
        public static bool IsNullOrEmpty(this string value)
 
50
        {
 
51
            return String.IsNullOrEmpty(value);
 
52
        }
 
53
 
 
54
        public static bool EqualsIgnoreCase(this string value, string other)
 
55
        {
 
56
            return String.Equals(value, other, StringComparison.CurrentCultureIgnoreCase);
 
57
        }
 
58
 
 
59
        public static string ReplaceFirst(this string haystack, string needle, string replacement)
 
60
        {
 
61
            var pos = haystack.IndexOf(needle);
 
62
            if (pos < 0) return haystack;
 
63
 
 
64
            return haystack.Substring(0, pos) + replacement + haystack.Substring(pos + needle.Length);
 
65
        }
 
66
 
 
67
        public static string ReplaceAll(this string haystack, string needle, string replacement)
 
68
        {
 
69
            int pos;
 
70
            // Avoid a possible infinite loop
 
71
            if (needle == replacement) return haystack;
 
72
            while ((pos = haystack.IndexOf(needle)) > 0)
 
73
            {
 
74
                haystack = haystack.Substring(0, pos) 
 
75
                    + replacement 
 
76
                    + haystack.Substring(pos + needle.Length);
 
77
            }
 
78
            return haystack;
 
79
        }
 
80
 
 
81
        public static bool ContainsAny(this string text, params string[] testMatches)
 
82
        {       
 
83
            foreach (var testMatch in testMatches)
 
84
            {
 
85
                if (text.Contains(testMatch)) return true;
 
86
            }
 
87
            return false;
 
88
        }
 
89
 
 
90
        private static readonly Regex InvalidVarCharsRegEx = new Regex(@"[^A-Za-z0-9]",
 
91
#if !SILVERLIGHT && !MONOTOUCH && !XBOX
 
92
            RegexOptions.Compiled
 
93
#else
 
94
 RegexOptions.None
 
95
#endif
 
96
        );
 
97
 
 
98
        public static string SafeVarName(this string text)
 
99
        {
 
100
            if (String.IsNullOrEmpty(text)) return null;
 
101
            return InvalidVarCharsRegEx.Replace(text, "_");
 
102
        }
 
103
 
 
104
        public static string Join(this List<string> items)
 
105
        {
 
106
            return String.Join(JsWriter.ItemSeperatorString, items.ToArray());
 
107
        }
 
108
 
 
109
        public static string Join(this List<string> items, string delimeter)
 
110
        {
 
111
            return String.Join(delimeter, items.ToArray());
 
112
        }
 
113
 
 
114
        public static string CombineWith(this string path, params string[] thesePaths)
 
115
        {
 
116
            if (thesePaths.Length == 1 && thesePaths[0] == null) return path;
 
117
            return PathUtils.CombinePaths(new StringBuilder(path.TrimEnd('/','\\')), thesePaths);
 
118
        }
 
119
 
 
120
        public static string ToParentPath(this string path)
 
121
        {
 
122
            var pos = path.LastIndexOf('/');
 
123
            if (pos == -1) return "/";
 
124
 
 
125
            var parentPath = path.Substring(0, pos);
 
126
            return parentPath;
 
127
        }
 
128
 
 
129
        public static string RemoveCharFlags(this string text, bool[] charFlags)
 
130
        {
 
131
            if (text == null) return null;
 
132
 
 
133
            var copy = text.ToCharArray();
 
134
            var nonWsPos = 0;
 
135
 
 
136
            for (var i = 0; i < text.Length; i++)
 
137
            {
 
138
                var @char = text[i];
 
139
                if (@char < charFlags.Length && charFlags[@char]) continue;
 
140
                copy[nonWsPos++] = @char;
 
141
            }
 
142
 
 
143
            return new String(copy, 0, nonWsPos);
 
144
        }
 
145
 
 
146
        public static string ToNullIfEmpty(this string text)
 
147
        {
 
148
            return String.IsNullOrEmpty(text) ? null : text;
 
149
        }
 
150
 
 
151
 
 
152
        private static char[] SystemTypeChars = new[] { '<', '>', '+' };
 
153
 
 
154
        public static bool IsUserType(this Type type)
 
155
        {
 
156
            return type.IsClass
 
157
                && type.Namespace != null
 
158
                && !type.Namespace.StartsWith("System.")
 
159
                && type.Name.IndexOfAny(SystemTypeChars) == -1;
 
160
        }
 
161
 
 
162
        public static bool IsInt(this string text)
 
163
        {
 
164
            if (string.IsNullOrEmpty(text)) return false;
 
165
            int ret;
 
166
            return int.TryParse(text, out ret);
 
167
        }
 
168
 
 
169
        public static int ToInt(this string text)
 
170
        {
 
171
            return int.Parse(text);
 
172
        }
 
173
 
 
174
        public static int ToInt(this string text, int defaultValue)
 
175
        {
 
176
            int ret;
 
177
            return int.TryParse(text, out ret) ? ret : defaultValue;
 
178
        }
 
179
 
 
180
        public static long ToInt64(this string text)
 
181
        {
 
182
            return long.Parse(text);
 
183
        }
 
184
 
 
185
        public static long ToInt64(this string text, long defaultValue)
 
186
        {
 
187
            long ret;
 
188
            return long.TryParse(text, out ret) ? ret : defaultValue;
 
189
        }
 
190
    }
 
191
 
 
192
}
 
 
b'\\ No newline at end of file'