Add copyright notices to all source files and put a license in LICENSE.
[versaplex.git] / wvdotnet / wvextensions.cs
blob06f579bc435b7d0bd49a47a24a1da2918af874bd
1 /*
2 * Versaplex:
3 * Copyright (C)2007-2008 Versabanq Innovations Inc. and contributors.
4 * See the included file named LICENSE for license information.
5 */
6 using System;
7 using System.Linq;
8 using System.Collections;
9 using System.Collections.Generic;
10 using System.Data;
11 using System.IO;
12 using System.Text;
14 namespace Wv.Extensions
16 public static class StreamHelper
18 public static void write(this Stream s, WvBytes buffer)
20 s.Write(buffer.bytes, buffer.start, buffer.len);
24 public static class ExceptionHelper
26 public static string Short(this Exception e)
28 if (e == null)
29 return "Success";
30 else
31 return e.Message;
35 public static class WvContExtensions
37 public static Action ToAction(this IEnumerator ie)
39 return new Action(delegate() {
40 ie.MoveNext();
41 });
44 public static Action ToAction(this IEnumerable aie)
46 bool must_reset = false;
47 IEnumerator ie = aie.GetEnumerator();
48 return new Action(delegate() {
49 if (must_reset)
50 ie = aie.GetEnumerator();
51 must_reset = !ie.MoveNext();
52 });
56 public static class WvStreamExtensions
58 public static byte[] ToUTF8(this Object o)
60 return Encoding.UTF8.GetBytes(o.ToString());
63 public static string FromUTF8(this WvBytes b)
65 return Encoding.UTF8.GetString(b.bytes, b.start, b.len);
68 public static string ToHex(this WvBytes bytes)
70 StringBuilder sb = new StringBuilder();
71 foreach (byte b in bytes)
72 sb.Append(b.ToString("X2"));
73 return sb.ToString();
77 public static class DictExtensions
79 public static string getstr<T1,T2>(this Dictionary<T1,T2> dict,
80 T1 key)
82 if (dict.ContainsKey(key))
83 return dict[key].ToString();
84 else
85 return "";
88 public static bool has<T1,T2>(this Dictionary<T1,T2> dict,
89 T1 key)
91 return dict.ContainsKey(key);
95 public static class DataExtensions
97 // true if a string is (e)mpty (null or blank)
98 public static bool e(this string s)
100 return wv.isempty(s);
103 // true if a string is (n)on(e)mpty (nonnull and nonblank)
104 public static bool ne(this string s)
106 return !wv.isempty(s);
109 public static IEnumerable<T2> map<T1,T2>(this IEnumerable<T1> list,
110 Func<T1,T2> f)
112 foreach (T1 t in list)
113 yield return f(t);
116 public static string[] ToStringArray<T>(this IEnumerable<T> l)
118 List<string> tmp = new List<string>();
119 foreach (T t in l)
120 tmp.Add(t.ToString());
121 return tmp.ToArray();
124 public static T only<T>(this IEnumerable<T> l)
125 where T: class
127 foreach (T t in l)
128 return t;
129 return (T)null;
132 public static string join<T>(this IEnumerable<T> list, string sep)
134 return String.Join(sep, list.ToStringArray());
137 public static string join<T>(this IEnumerable<string> list, string sep)
139 return String.Join(sep, list.ToArray());
142 // Note: it would be nice to take "params string[] splitwords" here,
143 // but Mono 1.2 apparently has a bug where that won't get picked up
144 // properly.
145 public static string[] split(this string s, string splitword)
147 return s.Split(new string[] {splitword}, StringSplitOptions.None);
150 public static string[] split(this string s, string splitword, int max)
152 return s.Split(new string[] {splitword}, max,
153 StringSplitOptions.None);
156 public static string[] split(this string s, char[] splitchars)
158 return s.Split(splitchars, StringSplitOptions.None);
161 public static string[] split(this string s, char[] splitchars, int max)
163 return s.Split(splitchars, max, StringSplitOptions.None);
166 public static string shorten(this string s, int maxlen)
168 if (s.Length <= maxlen)
169 return s;
170 else
171 return s.Substring(0, maxlen) + "...";
174 public static int atoi(this object o)
176 return wv.atoi(o);
179 public static long atol(this object o)
181 return wv.atol(o);
184 public static double atod(this object o)
186 return wv.atod(o);
189 public static T pop<T>(this IEnumerator<T> list)
191 if (list.MoveNext())
192 return list.Current;
193 else
194 return default(T);
197 // pray that you never need to use this.
198 public static WvAutoCast autocast(this object o)
200 if (o is WvAutoCast)
201 return (WvAutoCast)o;
202 else
203 return new WvAutoCast(o);
206 public static V tryget<K,V>(this IDictionary<K,V> dict, K key)
208 return dict.tryget(key, default(V));
211 public static V tryget<K,V>(this IDictionary<K,V> dict, K key,
212 V defval)
214 V v;
215 if (dict.TryGetValue(key, out v))
216 return v;
217 else
218 return defval;
221 // This works if b is a byte[], too, because of the implicit
222 // cast.
223 public static WvBytes sub(this WvBytes b, int start, int len)
225 return b.sub(start, len);
228 public static void put(this WvBytes dest, int offset, WvBytes src)
230 dest.put(offset, src);
233 // usage: val1.or(val2)
234 // Returns val2 if val1 is default(T). So you could write:
235 // int port = get_port().or(80);
236 public static T or<T>(this T val1, T val2) where T: IComparable<T>
238 if (val1.Equals(default(T)))
239 return val2;
240 else
241 return val1;
244 public static string or(this string val1, string val2)
246 if (val1.e())
247 return val2;
248 else
249 return val1;
252 static int acto<TI,T>(this IEnumerator<TI> e, out T o)
254 if (e.MoveNext())
256 o = (T)e.Current.autocast().to(typeof(T));
257 return 1;
259 return 0;
262 public static int assignto<T,T1>
263 (this IEnumerable<T> ie,
264 out T1 a)
266 using (var en = ie.GetEnumerator())
267 return en.acto(out a);
270 public static int assignto<T,T1,T2>
271 (this IEnumerable<T> ie,
272 out T1 a, out T2 b)
274 using (var en = ie.GetEnumerator())
275 return en.acto(out a)
276 + en.acto(out b);
279 public static int assignto<T,T1,T2,T3>
280 (this IEnumerable<T> ie,
281 out T1 a, out T2 b, out T3 c)
283 using (var en = ie.GetEnumerator())
284 return en.acto(out a)
285 + en.acto(out b)
286 + en.acto(out c);
289 public static int assignto<T,T1,T2,T3,T4>
290 (this IEnumerable<T> ie,
291 out T1 a, out T2 b, out T3 c, out T4 d)
293 using (var en = ie.GetEnumerator())
294 return en.acto(out a)
295 + en.acto(out b)
296 + en.acto(out c)
297 + en.acto(out d);
300 public static int assignto<T,T1,T2,T3,T4,T5>
301 (this IEnumerable<T> ie,
302 out T1 a, out T2 b, out T3 c, out T4 d, out T5 e)
304 using (var en = ie.GetEnumerator())
305 return en.acto(out a)
306 + en.acto(out b)
307 + en.acto(out c)
308 + en.acto(out d)
309 + en.acto(out e);
312 public static int assignto<T,T1,T2,T3,T4,T5,T6>
313 (this IEnumerable<T> ie,
314 out T1 a, out T2 b, out T3 c, out T4 d,
315 out T5 e, out T6 f)
317 using (var en = ie.GetEnumerator())
318 return en.acto(out a)
319 + en.acto(out b)
320 + en.acto(out c)
321 + en.acto(out d)
322 + en.acto(out e)
323 + en.acto(out f);
326 public static int assignto<T,T1,T2,T3,T4,T5,T6,T7>
327 (this IEnumerable<T> ie,
328 out T1 a, out T2 b, out T3 c, out T4 d,
329 out T5 e, out T6 f, out T7 g)
331 using (var en = ie.GetEnumerator())
332 return en.acto(out a)
333 + en.acto(out b)
334 + en.acto(out c)
335 + en.acto(out d)
336 + en.acto(out e)
337 + en.acto(out f)
338 + en.acto(out g);
341 public static int assignto<T,T1,T2,T3,T4,T5,T6,T7,T8>
342 (this IEnumerable<T> ie,
343 out T1 a, out T2 b, out T3 c, out T4 d,
344 out T5 e, out T6 f, out T7 g, out T8 h)
346 using (var en = ie.GetEnumerator())
347 return en.acto(out a)
348 + en.acto(out b)
349 + en.acto(out c)
350 + en.acto(out d)
351 + en.acto(out e)
352 + en.acto(out f)
353 + en.acto(out g)
354 + en.acto(out h);