Wednesday, September 6, 2017

UriEncode

There are two static methods in System.Uri: EscapeDataString and EscapeUriString. Use first to escape any string after '?' in uri and second one to escape any string before '?':

Symbol EscapeUriString EscapeDataString
aaa
bbb
ccc
space%20%20
??%3F
!!%21
@@%40
##%23
$$%24
%%25%25
^%5E%5E
&&%26
**%2A
((%28
))%29
//%2F
\%5C%5C
++%2B
___
---
<%3C%3C
>%3E%3E
,,%2C
...
''%27
"%22%22
`%60%60
~~~
а%D0%B0%D0%B0
б%D0%B1%D0%B1
в%D0%B2%D0%B2

Friday, July 21, 2017

ToArray vs ToList vs ToImmutableArray vs ToImmutableList

ToArray() is the fastest way to materialize IEnumerable on dotnet core:

BenchmarkDotNet=v0.10.8, OS=Mac OS X 10.12
Processor=Intel Core i7-4770HQ CPU 2.20GHz (Haswell), ProcessorCount=8
Frequency=1000000000 Hz, Resolution=1.0000 ns, Timer=UNKNOWN
dotnet cli version=1.0.4
  [Host]     : .NET Core 4.6.25211.01, 64bit RyuJIT
  DefaultJob : .NET Core 4.6.25211.01, 64bit RyuJIT


           Method |        Mean |     Error |    StdDev |      Median |
----------------- |------------:|----------:|----------:|------------:|
 ToImmutableArray |   165.58 us |  4.994 us | 14.247 us |   161.43 us |
          ToArray |    92.47 us |  1.823 us |  1.872 us |    91.84 us |
           ToList |   156.74 us |  1.486 us |  1.317 us |   156.87 us |
  ToImmutableList | 1,137.06 us | 16.654 us | 15.578 us | 1,136.40 us |

BenchmarkDotNet=v0.10.8, OS=amzn 2017.03
Processor=Intel Xeon CPU E5-2686 v4 2.30GHz, ProcessorCount=2
Frequency=1000000000 Hz, Resolution=1.0000 ns, Timer=UNKNOWN
dotnet cli version=1.0.4
  [Host]     : .NET Core 4.6.25211.01, 64bit RyuJIT
  DefaultJob : .NET Core 4.6.25211.01, 64bit RyuJIT


           Method |        Mean |     Error |    StdDev |
----------------- |------------:|----------:|----------:|
 ToImmutableArray |   169.26 us | 0.7734 us | 0.6856 us |
          ToArray |    99.04 us | 0.1849 us | 0.1544 us |
           ToList |   171.49 us | 0.3799 us | 0.3554 us |
  ToImmutableList | 1,234.29 us | 3.2099 us | 2.6805 us |


Friday, June 16, 2017

dotnet core + bash

To update all packages in all projects:

find . -name "*.csproj" -exec sh -c "gawk 'match(\$0, /PackageReference Include=\"(.*)\" Version/, a){print a[1]}' {} | xargs -L1 dotnet add {} package" \;

To run all tests in all Tests projects:

find . -name "*Tests*.csproj" -execdir dotnet test --no-build {} \;