Changeset 10960 for trunk/sources/HeuristicLab.Persistence/3.3/Default/Xml/Primitive/Char2XmlFormatter.cs
- Timestamp:
- 06/11/14 09:42:05 (10 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Persistence/3.3/Default/Xml/Primitive/Char2XmlFormatter.cs
r9456 r10960 20 20 #endregion 21 21 22 using System; 23 using System.Text; 24 using System.Text.RegularExpressions; 22 25 using HeuristicLab.Persistence.Core; 23 26 using HeuristicLab.Persistence.Interfaces; … … 27 30 internal sealed class Char2XmlSerializer : PrimitiveSerializerBase<char, XmlString> { 28 31 32 private static readonly Regex base64Regex = new Regex("<Base64>(.+)</Base64>"); 33 34 private static bool IsSpecial(char c) { 35 return c <= 0x1F && c != 0x9 && c != 0xA && c != 0xD; 36 } 37 38 private static string ToBase64String(char c) { 39 return string.Format("<Base64>{0}</Base64>", Convert.ToBase64String(Encoding.ASCII.GetBytes(new[] {c}))); 40 } 41 29 42 public override XmlString Format(char c) { 30 return new XmlString( new string(c, 1));43 return new XmlString(IsSpecial(c) ? ToBase64String(c) : new string(c, 1)); 31 44 } 32 45 33 46 public override char Parse(XmlString x) { 34 if (x.Data.Length != 1) 35 throw new PersistenceException("Invalid character format, XML string length != 1"); 36 return x.Data[0]; 47 if (x.Data.Length <= 1) return x.Data[0]; 48 var m = base64Regex.Match(x.Data); 49 if (m.Success) 50 return Encoding.ASCII.GetString(Convert.FromBase64String(m.Groups[1].Value))[0]; 51 throw new PersistenceException("Invalid character format, XML string length != 1"); 37 52 } 38 53 }
Note: See TracChangeset
for help on using the changeset viewer.