@@ -11,7 +11,8 @@ public class Config
1111
1212 public NotFoundBehaviorConfig NotFoundBehavior { get ; set ; } = new ( ) ;
1313
14- public RedirectRoute [ ] Routes { get ; set ; } = [
14+ public RedirectRoute [ ] Routes { get ; set ; } =
15+ [
1516 new RedirectRoute ( )
1617 {
1718 Route = "/instagram" ,
@@ -23,89 +24,82 @@ public class Config
2324 RedirectUrl = "https://example.com"
2425 } ,
2526 ] ;
26-
27+
2728 public class NotFoundBehaviorConfig
2829 {
2930 public bool RedirectOn404 { get ; set ; } = false ;
3031 public string RedirectUrl { get ; set ; } = "https://example.com/404" ;
3132 }
3233
33- [ JsonIgnore ]
34- public static Regex PlaceholderPattern => new ( @"\\\{(\d|\w+)\}" ) ;
35-
36- [ JsonIgnore ]
37- public CompiledRoute [ ] ? CompiledRoutes { get ; set ; }
38-
34+ [ JsonIgnore ] public CompiledRoute [ ] ? CompiledRoutes { get ; set ; }
35+
3936 public void CompileRoutes ( )
4037 {
4138 var compiledRoutes = new List < CompiledRoute > ( ) ;
42-
39+
4340 foreach ( var route in Routes )
4441 {
4542 if ( ! route . Route . StartsWith ( "/" ) )
4643 route . Route = "/" + route . Route ;
47-
44+
4845 if ( ! route . Route . EndsWith ( "/" ) )
4946 route . Route += "/" ;
50-
47+
5148 var compiled = new CompiledRoute
5249 {
5350 Route = route . Route ,
5451 RedirectUrl = route . RedirectUrl
5552 } ;
56-
53+
5754 var replacements = new List < ( int Index , int Length , string NewText ) > ( ) ;
5855
5956 var escaped = Regex . Escape ( route . Route ) ;
60-
61- var matches = PlaceholderPattern . Matches ( escaped ) ;
62-
63-
57+
58+ var pattern = new Regex ( @"\\{(\d|\w+)\}" ) ;
59+
60+ var matches = pattern . Matches ( escaped ) ;
61+
6462 foreach ( var match in matches . Select ( x => x ) )
6563 {
6664 // Check if the placeholder is immediately followed by another placeholder
67-
68- Console . WriteLine ( "matchlenght: " + ( match . Index + match . Length + 2 ) + " route lenght" + escaped . Length ) ;
69-
70- if ( escaped . Length >= match . Index + match . Length + 2 && escaped . Substring ( match . Index + match . Length , 2 ) == "\\ {" )
71- throw new InvalidOperationException (
72- $ "Placeholder { match . Groups [ 1 ] . Value } cannot be immediately followed by another placeholder. " +
73- $ "Please add a string literal as separator.") ;
65+ if ( escaped . Length >= match . Index + match . Length + 2
66+ && escaped . Substring ( match . Index + match . Length , 2 ) == "\\ {" )
67+ throw new InvalidOperationException (
68+ $ "Placeholder { match . Groups [ 1 ] . Value } cannot be immediately followed by another placeholder. " +
69+ $ "Please add any separator.") ;
7470
7571 replacements . Add ( ( match . Index , match . Length , "(.+)" ) ) ;
7672 }
77-
73+
7874 var compiledRouteBuilder = new StringBuilder ( escaped ) ;
79-
75+
8076 foreach ( var replacement in replacements . OrderByDescending ( r => r . Index ) )
8177 {
8278 compiledRouteBuilder . Remove ( replacement . Index , replacement . Length ) ;
8379 compiledRouteBuilder . Insert ( replacement . Index , replacement . NewText ) ;
8480 }
8581
86- compiled . CompiledPattern = new Regex ( compiledRouteBuilder . ToString ( ) , RegexOptions . Compiled ) ;
82+ compiled . CompiledPattern = new Regex ( compiledRouteBuilder . ToString ( ) ,
83+ RegexOptions . Compiled | RegexOptions . CultureInvariant ) ;
8784
88- Console . WriteLine ( compiled . CompiledPattern . ToString ( ) ) ;
89-
9085 var duplicate = matches
9186 . Select ( ( m , i ) => m . Groups [ 1 ] . Value )
9287 . GroupBy ( x => x )
9388 . FirstOrDefault ( x => x . Count ( ) > 1 ) ;
9489
9590 if ( duplicate != null )
9691 throw new InvalidOperationException ( "Cannot use a placeholder twice in the route: " + duplicate . Key ) ;
97-
92+
9893 compiled . Placeholders = matches
9994 . Select ( ( m , i ) => m . Groups [ 1 ] . Value )
10095 . Distinct ( )
10196 . Select ( ( name , i ) => ( name , i ) )
10297 . ToDictionary ( x => x . name , x => x . i + 1 ) ;
103-
98+
10499 compiledRoutes . Add ( compiled ) ;
105100 }
106101
107102 CompiledRoutes = compiledRoutes
108103 . ToArray ( ) ;
109104 }
110-
111105}
0 commit comments