wasSharp – Diff between revs 4 and 7
?pathlinks?
Rev 4 | Rev 7 | |||
---|---|---|---|---|
Line 2... | Line 2... | |||
2 | // Copyright (C) Wizardry and Steamworks 2013 - License: GNU GPLv3 // |
2 | // Copyright (C) Wizardry and Steamworks 2013 - License: GNU GPLv3 // |
|
3 | // Please see: http://www.gnu.org/licenses/gpl.html for legal details, // |
3 | // Please see: http://www.gnu.org/licenses/gpl.html for legal details, // |
|
4 | // rights of fair usage, the disclaimer and warranty conditions. // |
4 | // rights of fair usage, the disclaimer and warranty conditions. // |
|
5 | /////////////////////////////////////////////////////////////////////////// |
5 | /////////////////////////////////////////////////////////////////////////// |
|
Line -... | Line 6... | |||
- | 6 | |
||
6 | |
7 | using System; |
|
7 | using System.Collections.Generic; |
8 | using System.Collections.Generic; |
|
- | 9 | using System.Linq; |
||
8 | using System.Linq; |
10 | using System.Linq.Expressions; |
|
- | 11 | using System.Text; |
||
9 | using System.Text; |
12 | using System.Text.RegularExpressions; |
|
- | 13 | using System.Threading.Tasks; |
||
Line 10... | Line 14... | |||
10 | using System.Threading.Tasks; |
14 | using System.Xml.Linq; |
|
11 | |
15 | |
|
12 | namespace wasSharp |
16 | namespace wasSharp |
|
13 | { |
17 | { |
|
- | 18 | public static class XML |
||
- | 19 | { |
||
- | 20 | private static readonly Func<string, bool> directIsSafeXML = |
||
- | 21 | ((Expression<Func<string, bool>>) |
||
- | 22 | (data => |
||
- | 23 | Regex.Replace(data, |
||
- | 24 | @"(" + string.Join("|", @"&", @"<", @">", @""", @"'") + @")", |
||
- | 25 | @"", RegexOptions.IgnoreCase | RegexOptions.Multiline) |
||
- | 26 | .IndexOfAny(new[] {'&', '<', '>', '"', '\''}) |
||
14 | public class XML |
27 | .Equals(-1))).Compile(); |
|
15 | { |
28 | |
|
16 | /////////////////////////////////////////////////////////////////////////// |
29 | /////////////////////////////////////////////////////////////////////////// |
|
17 | // Copyright (C) 2015 Wizardry and Steamworks - License: GNU GPLv3 // |
30 | // Copyright (C) 2015 Wizardry and Steamworks - License: GNU GPLv3 // |
|
18 | /////////////////////////////////////////////////////////////////////////// |
31 | /////////////////////////////////////////////////////////////////////////// |
|
19 | /// <summary> |
32 | /// <summary> |
|
20 | /// Unescapes a string used in XML. |
33 | /// Unescapes a string used in XML. |
|
21 | /// </summary> |
34 | /// </summary> |
|
22 | /// <param name="s">the string to unescape</param> |
35 | /// <param name="s">the string to unescape</param> |
|
23 | /// <returns>an XML unescaped string</returns> |
36 | /// <returns>an XML unescaped string</returns> |
|
24 | public static string UnescapeXML(string s) |
37 | public static string UnescapeXML(string s) |
|
25 | { |
38 | { |
|
26 | Queue<char> t = new Queue<char>(); |
39 | var t = new Queue<char>(); |
|
27 | StringBuilder m = new StringBuilder(); |
40 | var m = new StringBuilder(); |
|
28 | foreach (char c in s) |
41 | foreach (var c in s) |
|
29 | { |
42 | { |
|
30 | switch (c) |
43 | switch (c) |
|
31 | { |
44 | { |
|
Line 39... | Line 52... | |||
39 | break; |
52 | break; |
|
40 | case ';': |
53 | case ';': |
|
41 | if (!t.Count.Equals(0)) |
54 | if (!t.Count.Equals(0)) |
|
42 | { |
55 | { |
|
43 | t.Enqueue(c); |
56 | t.Enqueue(c); |
|
44 | string special = string.Join("", t.ToArray()); |
57 | var special = string.Join("", t.ToArray()); |
|
45 | switch (special) |
58 | switch (special) |
|
46 | { |
59 | { |
|
47 | case "'": |
60 | case "'": |
|
48 | m.Append('\''); |
61 | m.Append('\''); |
|
49 | break; |
62 | break; |
|
Line 96... | Line 109... | |||
96 | /// <returns>an XML escaped string</returns> |
109 | /// <returns>an XML escaped string</returns> |
|
97 | public static string EscapeXML(string s) |
110 | public static string EscapeXML(string s) |
|
98 | { |
111 | { |
|
99 | if (string.IsNullOrEmpty(s)) return s; |
112 | if (string.IsNullOrEmpty(s)) return s; |
|
Line 100... | Line 113... | |||
100 | |
113 | |
|
101 | string[] result = new string[s.Length]; |
114 | var result = new string[s.Length]; |
|
102 | Parallel.ForEach(Enumerable.Range(0, s.Length), o => |
115 | Parallel.ForEach(Enumerable.Range(0, s.Length), o => |
|
103 | { |
116 | { |
|
104 | switch (s[o]) |
117 | switch (s[o]) |
|
105 | { |
118 | { |
|
Line 123... | Line 136... | |||
123 | break; |
136 | break; |
|
124 | } |
137 | } |
|
125 | }); |
138 | }); |
|
126 | return string.Join("", result); |
139 | return string.Join("", result); |
|
127 | } |
140 | } |
|
- | 141 | |
||
- | 142 | /////////////////////////////////////////////////////////////////////////// |
||
- | 143 | // Copyright (C) 2015 Wizardry and Steamworks - License: GNU GPLv3 // |
||
- | 144 | /////////////////////////////////////////////////////////////////////////// |
||
- | 145 | /// <summary> |
||
- | 146 | /// Determines whether a string is safe to use in XML |
||
- | 147 | /// </summary> |
||
- | 148 | /// <param name="data">the string to check</param> |
||
- | 149 | /// <returns>true in case the string is safe</returns> |
||
- | 150 | public static bool IsSafeXML(string data) |
||
- | 151 | { |
||
- | 152 | return directIsSafeXML(data); |
||
- | 153 | } |
||
- | 154 | |
||
- | 155 | /////////////////////////////////////////////////////////////////////////// |
||
- | 156 | // Copyright (C) 2015 Wizardry and Steamworks - License: GNU GPLv3 // |
||
- | 157 | /////////////////////////////////////////////////////////////////////////// |
||
- | 158 | /// <summary> |
||
- | 159 | /// Recursively rename a node by name. |
||
- | 160 | /// </summary> |
||
- | 161 | /// <param name="root">the root from where to start</param> |
||
- | 162 | /// <param name="name">the name to replace</param> |
||
- | 163 | /// <param name="rename">the name to replace with</param> |
||
- | 164 | public static void RenameNodes(XElement root, string name, string rename) |
||
- | 165 | { |
||
- | 166 | if (Strings.StringEquals(root.Name.LocalName, name, StringComparison.Ordinal)) |
||
- | 167 | { |
||
- | 168 | root.Name = rename; |
||
- | 169 | } |
||
- | 170 | |
||
- | 171 | foreach (var xElement in root.Elements()) |
||
- | 172 | { |
||
- | 173 | RenameNodes(xElement, name, rename); |
||
- | 174 | } |
||
- | 175 | } |
||
128 | } |
176 | } |
|
129 | } |
177 | } |
|
130 | |
178 | |