/CSV.cs |
@@ -25,11 +25,12 @@ |
public static string wasEnumerableToCSV(IEnumerable<string> l) |
{ |
string[] csv = l.Select(o => o).ToArray(); |
char[] escapeCharacters = {'"', ' ', ',', '\r', '\n'}; |
Parallel.ForEach(csv.Select((v, i) => new {i, v}), o => |
{ |
string cell = o.v.Replace("\"", "\"\""); |
|
switch (cell.IndexOfAny(new[] {'"', ' ', ',', '\r', '\n'})) |
switch (cell.IndexOfAny(escapeCharacters)) |
{ |
case -1: |
csv[o.i] = cell; |
/Collections.cs |
@@ -0,0 +1,88 @@ |
/////////////////////////////////////////////////////////////////////////// |
// 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. // |
/////////////////////////////////////////////////////////////////////////// |
|
using System.Collections.Generic; |
using System.Xml; |
using System.Xml.Schema; |
using System.Xml.Serialization; |
|
namespace wasSharp |
{ |
public class Collections |
{ |
/// <summary> |
/// A serializable dictionary class. |
/// </summary> |
/// <typeparam name="TKey">the key</typeparam> |
/// <typeparam name="TValue">the value</typeparam> |
[XmlRoot("Dictionary")] |
public class SerializableDictionary<TKey, TValue> |
: Dictionary<TKey, TValue>, IXmlSerializable |
{ |
#region IXmlSerializable Members |
|
public XmlSchema GetSchema() |
{ |
return null; |
} |
|
public void ReadXml(XmlReader reader) |
{ |
XmlSerializer keySerializer = new XmlSerializer(typeof (TKey)); |
XmlSerializer valueSerializer = new XmlSerializer(typeof (TValue)); |
|
bool wasEmpty = reader.IsEmptyElement; |
reader.Read(); |
|
if (wasEmpty) |
return; |
|
while (reader.NodeType != XmlNodeType.EndElement) |
{ |
reader.ReadStartElement("Item"); |
|
reader.ReadStartElement("Key"); |
TKey key = (TKey) keySerializer.Deserialize(reader); |
reader.ReadEndElement(); |
|
reader.ReadStartElement("Value"); |
TValue value = (TValue) valueSerializer.Deserialize(reader); |
reader.ReadEndElement(); |
|
Add(key, value); |
|
reader.ReadEndElement(); |
reader.MoveToContent(); |
} |
reader.ReadEndElement(); |
} |
|
public void WriteXml(XmlWriter writer) |
{ |
XmlSerializer keySerializer = new XmlSerializer(typeof (TKey)); |
XmlSerializer valueSerializer = new XmlSerializer(typeof (TValue)); |
|
foreach (TKey key in Keys) |
{ |
writer.WriteStartElement("Item"); |
|
writer.WriteStartElement("Key"); |
keySerializer.Serialize(writer, key); |
writer.WriteEndElement(); |
|
writer.WriteStartElement("Value"); |
TValue value = this[key]; |
valueSerializer.Serialize(writer, value); |
writer.WriteEndElement(); |
|
writer.WriteEndElement(); |
} |
} |
|
#endregion |
} |
} |
} |
/XML.cs |
@@ -0,0 +1,129 @@ |
/////////////////////////////////////////////////////////////////////////// |
// 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. // |
/////////////////////////////////////////////////////////////////////////// |
|
using System.Collections.Generic; |
using System.Linq; |
using System.Text; |
using System.Threading.Tasks; |
|
namespace wasSharp |
{ |
public class XML |
{ |
/////////////////////////////////////////////////////////////////////////// |
// Copyright (C) 2015 Wizardry and Steamworks - License: GNU GPLv3 // |
/////////////////////////////////////////////////////////////////////////// |
/// <summary> |
/// Unescapes a string used in XML. |
/// </summary> |
/// <param name="s">the string to unescape</param> |
/// <returns>an XML unescaped string</returns> |
public static string UnescapeXML(string s) |
{ |
Queue<char> t = new Queue<char>(); |
StringBuilder m = new StringBuilder(); |
foreach (char c in s) |
{ |
switch (c) |
{ |
case '&': |
if (!t.Count.Equals(0)) |
{ |
m.Append(string.Join("", t.ToArray())); |
t.Clear(); |
} |
t.Enqueue(c); |
break; |
case ';': |
if (!t.Count.Equals(0)) |
{ |
t.Enqueue(c); |
string special = string.Join("", t.ToArray()); |
switch (special) |
{ |
case "'": |
m.Append('\''); |
break; |
case """: |
m.Append('"'); |
break; |
case ">": |
m.Append('>'); |
break; |
case "<": |
m.Append('<'); |
break; |
case "&": |
m.Append('&'); |
break; |
default: // Unrecognized escape sequence |
m.Append(special); |
break; |
} |
t.Clear(); |
break; |
} |
m.Append(c); |
break; |
default: |
if (!t.Count.Equals(0)) |
{ |
t.Enqueue(c); |
if (t.Count >= 6) |
{ |
m.Append(string.Join("", t.ToArray())); |
t.Clear(); |
} |
break; |
} |
m.Append(c); |
break; |
} |
} |
return m.ToString(); |
} |
|
/////////////////////////////////////////////////////////////////////////// |
// Copyright (C) 2015 Wizardry and Steamworks - License: GNU GPLv3 // |
/////////////////////////////////////////////////////////////////////////// |
/// <summary> |
/// Escapes a string to be used in XML. |
/// </summary> |
/// <param name="s">the string to escape</param> |
/// <returns>an XML escaped string</returns> |
public static string EscapeXML(string s) |
{ |
if (string.IsNullOrEmpty(s)) return s; |
|
string[] result = new string[s.Length]; |
Parallel.ForEach(Enumerable.Range(0, s.Length), o => |
{ |
switch (s[o]) |
{ |
case '&': |
result[o] = @"&"; |
break; |
case '<': |
result[o] = @"<"; |
break; |
case '>': |
result[o] = @">"; |
break; |
case '"': |
result[o] = @"""; |
break; |
case '\'': |
result[o] = @"'"; |
break; |
default: |
result[o] = s[o].ToString(); |
break; |
} |
}); |
return string.Join("", result); |
} |
} |
} |
/wasSharp.csproj |
@@ -39,6 +39,7 @@ |
<ItemGroup> |
<Compile Include="Arrays.cs" /> |
<Compile Include="BitTwiddling.cs" /> |
<Compile Include="Collections.cs" /> |
<Compile Include="Cryptography.cs" /> |
<Compile Include="CSV.cs" /> |
<Compile Include="KeyValue.cs" /> |
@@ -47,6 +48,7 @@ |
<Compile Include="wasSharp.cs" /> |
<Compile Include="Properties\AssemblyInfo.cs" /> |
<Compile Include="Web.cs" /> |
<Compile Include="XML.cs" /> |
</ItemGroup> |
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\Portable\$(TargetFrameworkVersion)\Microsoft.Portable.CSharp.targets" /> |
<!-- To modify your build process, add your task inside one of the targets below and uncomment it. |