wasSharp

Subversion Repositories:
Compare Path: Rev
With Path: Rev
?path1? @ 2  →  ?path2? @ 3
/BitTwiddling.cs
@@ -0,0 +1,26 @@
///////////////////////////////////////////////////////////////////////////
// Copyright (C) Wizardry and Steamworks 2013 - License: GNU GPLv3 //
// Please see: http://www.gnu.org/licenses/gpl.html for legal details, //
// rights of fair usage, the disclaimer and warranty conditions. //
///////////////////////////////////////////////////////////////////////////
 
namespace wasSharp
{
public class BitTwiddling
{
///////////////////////////////////////////////////////////////////////////
// Copyright (C) 2014 Wizardry and Steamworks - License: GNU GPLv3 //
///////////////////////////////////////////////////////////////////////////
/// <summary>
/// Swaps two integers passed by reference using XOR.
/// </summary>
/// <param name="q">first integer to swap</param>
/// <param name="p">second integer to swap</param>
public static void wasXORSwap(ref int q, ref int p)
{
q ^= p;
p ^= q;
q ^= p;
}
}
}
/Properties/AssemblyInfo.cs
@@ -26,4 +26,4 @@
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
 
[assembly: AssemblyVersion("1.1.*")]
[assembly: AssemblyVersion("1.2.*")]
/Reflection.cs
@@ -8,44 +8,11 @@
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
 
namespace wasSharp
{
public class Reflection
{
 
/// <summary>
/// A generic name attribute.
/// </summary>
public class NameAttribute : Attribute
{
protected readonly string name;
 
public NameAttribute(string name)
{
this.name = name;
}
 
public string Name => name;
}
 
/// <summary>
/// A generic description attribute.
/// </summary>
public class DescriptionAttribute : Attribute
{
protected readonly string description;
 
public DescriptionAttribute(string description)
{
this.description = description;
}
 
public string Description => description;
}
 
///////////////////////////////////////////////////////////////////////////
// Copyright (C) 2014 Wizardry and Steamworks - License: GNU GPLv3 //
///////////////////////////////////////////////////////////////////////////
@@ -55,9 +22,9 @@
/// <returns>an attribute of type T</returns>
public static T wasGetAttributeFromEnumValue<T>(Enum value) where T : Attribute
{
return (T)value.GetType()
return (T) value.GetType()
.GetRuntimeField(value.ToString())
.GetCustomAttributes(typeof(T), false)
.GetCustomAttributes(typeof (T), false)
.SingleOrDefault();
}
 
@@ -72,7 +39,8 @@
public static IEnumerable<T> wasGetEnumAttributes<T>(Enum e) where T : Attribute
{
return e.GetType().GetRuntimeFields()
.AsParallel().Select(o => wasGetAttributeFromEnumValue<T>((Enum)o.GetValue(Activator.CreateInstance<T>())));
.AsParallel()
.Select(o => wasGetAttributeFromEnumValue<T>((Enum) o.GetValue(Activator.CreateInstance<T>())));
}
 
///////////////////////////////////////////////////////////////////////////
@@ -117,7 +85,7 @@
{
NameAttribute attribute = value.GetType()
.GetRuntimeField(value.ToString())
.GetCustomAttributes(typeof(NameAttribute), false)
.GetCustomAttributes(typeof (NameAttribute), false)
.SingleOrDefault() as NameAttribute;
return attribute?.Name;
}
@@ -134,7 +102,7 @@
{
DescriptionAttribute attribute = value.GetType()
.GetRuntimeField(value.ToString())
.GetCustomAttributes(typeof(DescriptionAttribute), false)
.GetCustomAttributes(typeof (DescriptionAttribute), false)
.SingleOrDefault() as DescriptionAttribute;
return attribute?.Description;
}
@@ -150,12 +118,12 @@
/// <returns>the value or the default of T if case no name attribute found</returns>
public static T wasGetEnumValueFromName<T>(string name)
{
var field = typeof(T).GetRuntimeFields()
var field = typeof (T).GetRuntimeFields()
.AsParallel().SelectMany(f => f.GetCustomAttributes(
typeof(NameAttribute), false), (
f, a) => new { Field = f, Att = a }).SingleOrDefault(a => ((NameAttribute)a.Att)
.Name.Equals(name));
return field != null ? (T)field.Field.GetValue(Activator.CreateInstance<T>()) : default(T);
typeof (NameAttribute), false), (
f, a) => new {Field = f, Att = a}).SingleOrDefault(a => ((NameAttribute) a.Att)
.Name.Equals(name));
return field != null ? (T) field.Field.GetValue(Activator.CreateInstance<T>()) : default(T);
}
 
///////////////////////////////////////////////////////////////////////////
@@ -170,10 +138,40 @@
/// <returns>the description or the empty string</returns>
public static string wasGetStructureMemberName<T>(T structure, object item) where T : struct
{
var field = typeof(T).GetRuntimeFields()
.AsParallel().SelectMany(f => f.GetCustomAttributes(typeof(NameAttribute), false),
(f, a) => new { Field = f, Att = a }).SingleOrDefault(f => f.Field.GetValue(structure).Equals(item));
return field != null ? ((NameAttribute)field.Att).Name : string.Empty;
var field = typeof (T).GetRuntimeFields()
.AsParallel().SelectMany(f => f.GetCustomAttributes(typeof (NameAttribute), false),
(f, a) => new {Field = f, Att = a}).SingleOrDefault(f => f.Field.GetValue(structure).Equals(item));
return field != null ? ((NameAttribute) field.Att).Name : string.Empty;
}
 
/// <summary>
/// A generic name attribute.
/// </summary>
public class NameAttribute : Attribute
{
protected readonly string name;
 
public NameAttribute(string name)
{
this.name = name;
}
 
public string Name => name;
}
 
/// <summary>
/// A generic description attribute.
/// </summary>
public class DescriptionAttribute : Attribute
{
protected readonly string description;
 
public DescriptionAttribute(string description)
{
this.description = description;
}
 
public string Description => description;
}
}
}
}
/Web.cs
@@ -5,9 +5,8 @@
///////////////////////////////////////////////////////////////////////////
 
using System;
using System.IO;
using System.Linq;
using System.Text;
using System.Net;
 
namespace wasSharp
{
@@ -49,104 +48,7 @@
/// <returns>an RFC1738 escaped string</returns>
public static string wasURLEscapeDataString(string data)
{
//return HttpUtility.UrlEncode(data);
StringBuilder result = new StringBuilder();
 
char[] hex = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
 
foreach (char c in data)
{
switch (c)
{
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
case 'a':
case 'b':
case 'c':
case 'd':
case 'e':
case 'f':
case 'g':
case 'h':
case 'i':
case 'j':
case 'k':
case 'l':
case 'm':
case 'n':
case 'o':
case 'p':
case 'q':
case 'r':
case 's':
case 't':
case 'u':
case 'v':
case 'w':
case 'x':
case 'y':
case 'z':
case 'A':
case 'B':
case 'C':
case 'D':
case 'E':
case 'F':
case 'G':
case 'H':
case 'I':
case 'J':
case 'K':
case 'L':
case 'M':
case 'N':
case 'O':
case 'P':
case 'Q':
case 'R':
case 'S':
case 'T':
case 'U':
case 'V':
case 'W':
case 'X':
case 'Y':
case 'Z':
case '!':
case '\'':
case '(':
case ')':
case '*':
case '-':
case '.':
case '_':
result.Append(c);
break;
case ' ':
result.Append('+');
break;
default:
StringBuilder uCode = new StringBuilder();
foreach (var b in Encoding.UTF8.GetBytes(new[] {c}))
{
uCode.Append('%');
uCode.Append(hex[b >> 4]);
uCode.Append(hex[b & 0x0F]);
}
result.Append(uCode);
break;
}
}
 
return result.ToString();
return WebUtility.UrlEncode(data);
}
 
///////////////////////////////////////////////////////////////////////////
@@ -157,103 +59,7 @@
/// <returns>an RFC1738 unescaped string</returns>
public static string wasURLUnescapeDataString(string data)
{
//return HttpUtility.UrlDecode(data);
StringBuilder result = new StringBuilder();
 
int c;
 
Func<byte, int> GetInt = o =>
{
switch ((char) o)
{
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
return o - '0';
case 'a':
case 'b':
case 'c':
case 'd':
case 'e':
case 'f':
return o - 'a' + 10;
case 'A':
case 'B':
case 'C':
case 'D':
case 'E':
case 'F':
return o - 'A' + 10;
default:
return -1;
}
};
 
Func<string, int, int, int> GetCharString = (s, o, l) =>
{
int v = 0;
int e = l + o;
for (int i = o; i < e; ++i)
{
c = GetInt((byte) s[i]);
if (c.Equals(-1)) return -1;
v = (v << 4) + c;
}
return v;
};
 
using (MemoryStream bytes = new MemoryStream())
{
for (int x = 0; x < data.Length; ++x)
{
if (data[x].Equals('%') && !data[x + 1].Equals('%') && x + 2 < data.Length)
{
c = GetCharString(data, x + 1, 2);
switch (c)
{
case -1:
result.Append('%');
break;
default:
bytes.WriteByte((byte) c);
x += 2;
break;
}
continue;
}
 
if (!bytes.Length.Equals(0))
{
result.Append(Encoding.UTF8.GetChars(bytes.ToArray()));
bytes.SetLength(0);
}
 
switch (data[x].Equals('+'))
{
case true:
result.Append(' ');
break;
default:
result.Append(data[x]);
break;
}
}
 
if (!bytes.Length.Equals(0))
{
result.Append(Encoding.UTF8.GetChars(bytes.ToArray()));
bytes.SetLength(0);
}
}
 
return result.ToString();
return WebUtility.UrlDecode(data);
}
}
}
/wasSharp.csproj
@@ -38,6 +38,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="Arrays.cs" />
<Compile Include="BitTwiddling.cs" />
<Compile Include="Cryptography.cs" />
<Compile Include="CSV.cs" />
<Compile Include="KeyValue.cs" />