Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/HeuristicLab.Persistence/3.3/Default/Xml/Primitive/Char2XmlFormatter.cs @ 17228

Last change on this file since 17228 was 17180, checked in by swagner, 5 years ago

#2875: Removed years in copyrights

File size: 2.0 KB
RevLine 
[3742]1#region License Information
2/* HeuristicLab
[17180]3 * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[3742]4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
[10960]22using System;
23using System.Text;
24using System.Text.RegularExpressions;
[16565]25using HEAL.Attic;
[1652]26using HeuristicLab.Persistence.Interfaces;
27
28namespace HeuristicLab.Persistence.Default.Xml.Primitive {
29
[3036]30  internal sealed class Char2XmlSerializer : PrimitiveSerializerBase<char, XmlString> {
[1853]31
[10960]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
[1652]42    public override XmlString Format(char c) {
[10960]43      return new XmlString(IsSpecial(c) ? ToBase64String(c) : new string(c, 1));
[1652]44    }
45
46    public override char Parse(XmlString x) {
[10960]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");
[1652]52    }
53  }
54}
Note: See TracBrowser for help on using the repository browser.