-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathIniUtilities.cs
More file actions
95 lines (60 loc) · 2.35 KB
/
Copy pathIniUtilities.cs
File metadata and controls
95 lines (60 loc) · 2.35 KB
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
using System;
using System.Linq;
using System.Text.RegularExpressions;
namespace Gsemac.Text.Ini {
public static class IniUtilities {
// Public members
public static string Escape(string input) {
return Escape(input, IniOptions.Default);
}
public static string Escape(string input, IIniOptions options) {
if (options is null)
throw new ArgumentNullException(nameof(options));
if (string.IsNullOrEmpty(input))
return input;
// Add custom delimiters to the pattern.
string pattern = string.Join("|", new[] {
@"[\\'""\x00\a\b\t\r\n\[\]]"
}
.Concat(new[] {
options.CommentMarker,
options.NameValueSeparator,
}
.Where(p => !string.IsNullOrWhiteSpace(p))
.Select(p => Regex.Escape(p))));
string result = Regex.Replace(input, pattern,
m => {
switch (m.Value) {
case "\0":
return @"\0";
case "\a":
return @"\a";
case "\b":
return @"\b";
case "\t":
return @"\t";
case "\r":
return @"\r";
case "\n":
return @"\n";
default:
return $@"\{m.Value}";
}
}, RegexOptions.IgnoreCase);
return result;
}
public static string Unescape(string input) {
return StringUtilities.Unescape(input, UnescapeOptions.UnescapeEscapeSequences);
}
// Internal members
internal static string FormatValue(string value, IIniOptions options, bool isPropertyValue, bool writingValue) {
if (string.IsNullOrEmpty(value))
return value;
if (options.EnableEscapeSequences)
value = writingValue ? Escape(value, options) : Unescape(value);
if (!(isPropertyValue && !options.TrimWhiteSpace) && !string.IsNullOrEmpty(value))
value = value.Trim();
return value;
}
}
}