3 * Copyright (C)2007-2008 Versabanq Innovations Inc. and contributors.
4 * See the included file named LICENSE for license information.
8 using System
.Collections
;
9 using System
.Collections
.Generic
;
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
)
35 public static class WvContExtensions
37 public static Action
ToAction(this IEnumerator ie
)
39 return new Action(delegate() {
44 public static Action
ToAction(this IEnumerable aie
)
46 bool must_reset
= false;
47 IEnumerator ie
= aie
.GetEnumerator();
48 return new Action(delegate() {
50 ie
= aie
.GetEnumerator();
51 must_reset
= !ie
.MoveNext();
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"));
77 public static class DictExtensions
79 public static string getstr
<T1
,T2
>(this Dictionary
<T1
,T2
> dict
,
82 if (dict
.ContainsKey(key
))
83 return dict
[key
].ToString();
88 public static bool has
<T1
,T2
>(this Dictionary
<T1
,T2
> dict
,
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
,
112 foreach (T1 t
in list
)
116 public static string[] ToStringArray
<T
>(this IEnumerable
<T
> l
)
118 List
<string> tmp
= new List
<string>();
120 tmp
.Add(t
.ToString());
121 return tmp
.ToArray();
124 public static T only
<T
>(this IEnumerable
<T
> l
)
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
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
)
171 return s
.Substring(0, maxlen
) + "...";
174 public static int atoi(this object o
)
179 public static long atol(this object o
)
184 public static double atod(this object o
)
189 public static T pop
<T
>(this IEnumerator
<T
> list
)
197 // pray that you never need to use this.
198 public static WvAutoCast
autocast(this object o
)
201 return (WvAutoCast
)o
;
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
,
215 if (dict
.TryGetValue(key
, out v
))
221 // This works if b is a byte[], too, because of the implicit
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
)))
244 public static string or(this string val1
, string val2
)
252 static int acto
<TI
,T
>(this IEnumerator
<TI
> e
, out T o
)
256 o
= (T
)e
.Current
.autocast().to(typeof(T
));
262 public static int assignto
<T
,T1
>
263 (this IEnumerable
<T
> ie
,
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
,
274 using (var en
= ie
.GetEnumerator())
275 return en
.acto(out a
)
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
)
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
)
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
)
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
,
317 using (var en
= ie
.GetEnumerator())
318 return en
.acto(out a
)
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
)
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
)