@@ -13,15 +13,194 @@ public static string SanitizeMand(this string text)
1313
1414 public static string SanitizeMand ( this string text , ref bool shortened )
1515 {
16- string output = RemoveSpecialChars ( ) . Replace ( text , "" ) . Trim ( ) ;
16+ string output = StandardSanitizationRegex ( ) . Replace ( text , "" ) . Trim ( ) ;
1717 if ( output . Length < text . Length )
1818 {
1919 shortened = true ;
2020 }
2121 return output ;
2222 }
2323
24- [ GeneratedRegex ( @"[^\w\.\*\-\:\?@/\(\)\[\]\{\}\$\+<>#\$\=\, ]" ) ]
25- private static partial Regex RemoveSpecialChars ( ) ;
24+ public static string ? SanitizeOpt ( this string ? text , ref bool shortened )
25+ {
26+ if ( text != null )
27+ {
28+ return text . SanitizeMand ( ref shortened ) ;
29+ }
30+ else return null ;
31+ }
32+
33+ public static string SanitizeLdapNameMand ( this string input , ref bool shortened )
34+ {
35+ string output = LdapNameRegex ( ) . Replace ( input , "" ) . Trim ( ) ;
36+ if ( output . Length < input . Length )
37+ {
38+ shortened = true ;
39+ }
40+ return output ;
41+ }
42+
43+ public static string ? SanitizeLdapNameOpt ( this string ? input , ref bool shortened )
44+ {
45+ if ( input != null )
46+ {
47+ return input . SanitizeLdapNameMand ( ref shortened ) ;
48+ }
49+ else return null ;
50+ }
51+
52+ public static string SanitizeLdapPathMand ( this string input , ref bool shortened )
53+ {
54+ string output = LdapPathRegex ( ) . Replace ( input , "" ) . Trim ( ) ;
55+ if ( output . Length < input . Length )
56+ {
57+ shortened = true ;
58+ }
59+ return output ;
60+ }
61+
62+ public static string ? SanitizeLdapPathOpt ( this string ? input , ref bool shortened )
63+ {
64+ if ( input != null )
65+ {
66+ return input . SanitizeLdapPathMand ( ref shortened ) ;
67+ }
68+ else return null ;
69+ }
70+
71+ public static string SanitizePasswMand ( this string input , ref bool shortened )
72+ {
73+ string output = PasswdRegex ( ) . Replace ( input , "" ) . Trim ( ) ;
74+ if ( output . Length < input . Length )
75+ {
76+ shortened = true ;
77+ }
78+ return output ;
79+ }
80+
81+ public static string ? SanitizePasswOpt ( this string ? input , ref bool shortened )
82+ {
83+ if ( input != null )
84+ {
85+ return input . SanitizePasswMand ( ref shortened ) ;
86+ }
87+ else return null ;
88+ }
89+
90+ public static string SanitizeKeyMand ( this string input , ref bool shortened )
91+ {
92+ string output = input . Trim ( ) ;
93+ if ( output . Length < input . Length )
94+ {
95+ shortened = true ;
96+ }
97+ return output ;
98+ }
99+
100+ public static string ? SanitizeKeyOpt ( this string ? input , ref bool shortened )
101+ {
102+ if ( input != null )
103+ {
104+ return input . SanitizeKeyMand ( ref shortened ) ;
105+ }
106+ else return null ;
107+ }
108+
109+ public static string SanitizeCommentMand ( this string input , ref bool shortened )
110+ {
111+ string output = CommentRegex ( ) . Replace ( input , "" ) . Trim ( ) ;
112+ string ignorableChangeCompareString = output + "\n " ;
113+ if ( input != null && output . Length < input . Length && ignorableChangeCompareString != input )
114+ {
115+ shortened = true ;
116+ }
117+ return output ;
118+ }
119+
120+ public static string ? SanitizeCommentOpt ( this string ? input , ref bool shortened )
121+ {
122+ if ( input != null )
123+ {
124+ return input . SanitizeCommentMand ( ref shortened ) ;
125+ }
126+ else return null ;
127+ }
128+
129+ public static string SanitizeCidrMand ( this string input , ref bool shortened )
130+ {
131+ string output = CidrRegex ( ) . Replace ( input , "" ) . Trim ( ) ;
132+ if ( output . Length < input . Length )
133+ {
134+ shortened = true ;
135+ }
136+ return output ;
137+ }
138+
139+ public static string ? SanitizeCidrOpt ( this string ? input , ref bool shortened )
140+ {
141+ if ( input != null )
142+ {
143+ return input . SanitizeCidrMand ( ref shortened ) ;
144+ }
145+ else return null ;
146+ }
147+
148+ public static string SanitizeJsonMand ( this string input , ref bool shortened )
149+ {
150+ string output = JsonRegex ( ) . Replace ( input , "" ) . Trim ( ) ;
151+ if ( output . Length < input . Length )
152+ {
153+ shortened = true ;
154+ }
155+ return output ;
156+ }
157+
158+ public static string SanitizeJsonFieldMand ( this string input , ref bool changed )
159+ {
160+ string output = JsonFieldRegex ( ) . Replace ( input . Trim ( ) , "_" ) ;
161+ if ( output != input )
162+ {
163+ changed = true ;
164+ }
165+ return output ;
166+ }
167+
168+ public static string SanitizeEolMand ( this string input , ref bool shortened )
169+ {
170+ string output = EolRegex ( ) . Replace ( input , " " ) . Trim ( ) ;
171+ if ( output . Length < input . Length )
172+ {
173+ shortened = true ;
174+ }
175+ return output ;
176+ }
177+
178+ [ GeneratedRegex ( @"[^\w\.\*\-\:\?@/\(\)\[\]\{\}\$\+<>#\$ ]" ) ]
179+ private static partial Regex StandardSanitizationRegex ( ) ;
180+
181+ [ GeneratedRegex ( @"[^\w\.\*\-\:\?@/\(\) ]" ) ]
182+ private static partial Regex LdapNameRegex ( ) ;
183+
184+ [ GeneratedRegex ( @"[^\w\.\*\-\:\?@/\(\)\=\, \\]" ) ]
185+ private static partial Regex LdapPathRegex ( ) ;
186+
187+ [ GeneratedRegex ( @"[^\S ]" ) ]
188+ private static partial Regex PasswdRegex ( ) ;
189+
190+ [ GeneratedRegex ( @"[""'']" ) ]
191+ private static partial Regex CommentRegex ( ) ;
192+
193+ [ GeneratedRegex ( @"[^a-fA-F0-9\.\:/]" ) ]
194+ private static partial Regex CidrRegex ( ) ;
195+
196+ [ GeneratedRegex ( @"[^\S ]" ) ]
197+ private static partial Regex JsonRegex ( ) ;
198+
199+ [ GeneratedRegex ( @"[\+\*\(\)\{\}\[\]\?\!#<>\=\,\;\/\\\t@\$\%\^\|\&\~ ]" ) ]
200+ private static partial Regex JsonFieldRegex ( ) ;
201+
202+ [ GeneratedRegex ( @"[\n\r]" ) ]
203+ private static partial Regex EolRegex ( ) ;
204+
26205 }
27206}
0 commit comments