-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathSprotoHelper.cs
72 lines (65 loc) · 1.69 KB
/
SprotoHelper.cs
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
69
70
71
72
using System;
using System.Text;
using System.Collections.Generic;
using System.Diagnostics;
namespace Sproto {
public class SprotoHelper {
public static void Error (string fmt,params object[] args) {
string errmsg = String.Format(fmt,args);
throw new Exception(errmsg);
}
public static void Assert(bool condition,string errmsg=null) {
if (!condition) {
throw new Exception(errmsg);
}
}
public static bool IsBuildInType(string type) {
switch (type) {
case "integer":
return true;
case "boolean":
return true;
case "string":
return true;
case "binary":
return true;
default:
return false;
}
}
public static string DumpList<T> (List<T> list) {
StringBuilder sb = new StringBuilder();
sb.Append("{");
foreach (var item in list) {
sb.Append(item + ",");
}
sb.Append("}");
return sb.ToString();
}
public static string DumpDict<TKey,TValue> (Dictionary<TKey,TValue> dict) {
StringBuilder sb = new StringBuilder();
sb.Append("{");
foreach (var item in dict) {
sb.Append(String.Format("{0}={1},",item.Key,item.Value));
}
sb.Append("}");
return sb.ToString();
}
public static string DumpBytes(byte[] bytes,int start=0,int length=0) {
if (length == 0) {
length = bytes.Length;
}
StringBuilder sb = new StringBuilder();
sb.Append(String.Format("len: {0}\n",length));
for (int i = 0; i < length; i++) {
sb.Append(String.Format("{0:x2} ",bytes[start+i]));
if ((i+1) % 8 == 0)
sb.Append("\n");
}
return sb.ToString();
}
public static void PrintBytes(byte[] bytes,int start=0,int length=0) {
Console.WriteLine(SprotoHelper.DumpBytes(bytes,start,length));
}
}
}