forked from nikeee/TeamSpeak3QueryApi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStringExtensions.cs
49 lines (46 loc) · 2.01 KB
/
StringExtensions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
namespace TeamSpeak3QueryApi.Net.Extensions
{
internal static class StringExtensions
{
/// <summary>Escapes a string so it can be safely used for querying the api.</summary>
/// <param name="s">The string to escape.</param>
/// <returns>An escaped string.</returns>
public static string TeamSpeakEscape(this string s)
{
if (s == string.Empty)
return s;
s = s.Replace("\\", "\\\\"); // Backslash
s = s.Replace("/", "\\/"); // Slash
s = s.Replace("|", "\\p"); // Pipe
s = s.Replace("\n", "\\n"); // Newline
//r = r.replace("\b", "\\b"); // Info: Backspace fails
//r = r.replace("\a", "\\a"); // Info: Bell fails
s = s.Replace("\r", "\\r"); // Carriage Return
s = s.Replace("\t", "\\t"); // Tab
s = s.Replace("\v", "\\v"); // Vertical Tab
s = s.Replace("\f", "\\f"); // Formfeed
s = s.Replace(" ", "\\s"); // Whitespace
return s;
}
/// <summary>Unescapes a string so it can be used for processing the rawResponse of the api.</summary>
/// <param name="s">The string to unescape.</param>
/// <returns>An unescaped string.</returns>
public static string TeamSpeakUnescape(this string s)
{
if (s == string.Empty)
return s;
s = s.Replace("\\s", " "); // Whitespace
s = s.Replace("\\p", "|"); // Pipe
s = s.Replace("\\n", "\n"); // Newline
//r = r.replace(/\\b/g, "\b"); // Info: Backspace fails
//r = r.replace(/\\a/g, "\a"); // Info: Bell fails
s = s.Replace("\\f", "\f"); // Formfeed
s = s.Replace("\\r", "\r"); // Carriage Return
s = s.Replace("\\t", "\t"); // Tab
s = s.Replace("\\v", "\v"); // Vertical Tab
s = s.Replace("\\/", "/"); // Slash
s = s.Replace("\\\\", "\\"); // Backslash
return s;
}
}
}