11using BenchmarkDotNet . Attributes ;
22using BenchmarkDotNet . Attributes . Exporters ;
33using BenchmarkDotNet . Attributes . Jobs ;
4+ using System ;
45
56namespace Benchmarks . LinkBuilder
67{
7- [ MarkdownExporter , SimpleJob ( launchCount : 3 , warmupCount : 10 , targetCount : 20 ) , MemoryDiagnoser ]
8+ [ MarkdownExporter , SimpleJob ( launchCount : 3 , warmupCount : 10 , targetCount : 20 ) , MemoryDiagnoser ]
89 public class LinkBuilder_GetNamespaceFromPath_Benchmarks
910 {
1011 private const string PATH = "/api/some-really-long-namespace-path/resources/current/articles" ;
@@ -14,7 +15,7 @@ public class LinkBuilder_GetNamespaceFromPath_Benchmarks
1415 public void UsingSplit ( ) => GetNamespaceFromPath_BySplitting ( PATH , ENTITY_NAME ) ;
1516
1617 [ Benchmark ]
17- public void Current ( ) => GetNameSpaceFromPath_Current ( PATH , ENTITY_NAME ) ;
18+ public void Current ( ) => GetNameSpaceFromPathCurrent ( PATH , ENTITY_NAME ) ;
1819
1920 public static string GetNamespaceFromPath_BySplitting ( string path , string entityName )
2021 {
@@ -32,7 +33,38 @@ public static string GetNamespaceFromPath_BySplitting(string path, string entity
3233 return nSpace ;
3334 }
3435
35- public static string GetNameSpaceFromPath_Current ( string path , string entityName )
36- => JsonApiDotNetCore . Builders . LinkBuilder . GetNamespaceFromPath ( path , entityName ) ;
36+ public static string GetNameSpaceFromPathCurrent ( string path , string entityName )
37+ {
38+
39+ var entityNameSpan = entityName . AsSpan ( ) ;
40+ var pathSpan = path . AsSpan ( ) ;
41+ const char delimiter = '/' ;
42+ for ( var i = 0 ; i < pathSpan . Length ; i ++ )
43+ {
44+ if ( pathSpan [ i ] . Equals ( delimiter ) )
45+ {
46+ var nextPosition = i + 1 ;
47+ if ( pathSpan . Length > i + entityNameSpan . Length )
48+ {
49+ var possiblePathSegment = pathSpan . Slice ( nextPosition , entityNameSpan . Length ) ;
50+ if ( entityNameSpan . SequenceEqual ( possiblePathSegment ) )
51+ {
52+ // check to see if it's the last position in the string
53+ // or if the next character is a /
54+ var lastCharacterPosition = nextPosition + entityNameSpan . Length ;
55+
56+ if ( lastCharacterPosition == pathSpan . Length || pathSpan . Length >= lastCharacterPosition + 2 && pathSpan [ lastCharacterPosition ] . Equals ( delimiter ) )
57+ {
58+ return pathSpan . Slice ( 0 , i ) . ToString ( ) ;
59+ }
60+ }
61+ }
62+ }
63+ }
64+
65+ return string . Empty ;
66+
67+
68+ }
3769 }
3870}
0 commit comments