Monday, March 22, 2021

Git config includeif

 Git config "includeif" feature works only in git initialized directories. On plain directories it uses only default .gitconfig file.

Monday, February 10, 2020

Relative Uris and leading/trailing backslash

Be very accurate with leading and trailing backslashes in URLs. For instance, I have some app installed on http://some.host/path that provides api via relative uri /api/some/call

There is only one correct combination of Base and Rel Uris:

(4 items)4
Base
Rel
Result
http://some.host/path/
/api/some/call
http://some.host/api/some/call
http://some.host/path/
api/some/call
http://some.host/path/api/some/call
http://some.host/path
/api/some/call
http://some.host/api/some/call
http://some.host/path
api/some/call
http://some.host/api/some/call

var baseUrl = new Uri("http://some.host/path/");
var relUrl = new Uri("api/some/call", UriKind.Relative);

var result = new Uri(baseUrl, relUrl);


So as thumb rules: All base urls should be with trailing backslashes and all relative urls should be without trailing backslashes.

Wednesday, September 6, 2017

UriEncode

There are two static methods in System.UriEscapeDataString 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 {} \;

Tuesday, June 21, 2016

Nuget or not nuget

To switch all references from static dll to Nuget, use following snippet in Package Manager Console:

Get-Project -All | foreach-object {IF (Get-Content $_.FullName | Select-String "XXX") {Install-Package XXX -ProjectName $_.FullName}}

Monday, March 9, 2015

Group by order

Quite a common case - assume we have a collection of items and we want to iterate over it, but at first by items with some feature, then by items without such feature.

For instance, we have list of integers and want to iterate firstly all even numbers, then all odd numbers.

First approach is to group items by this feature and then iterate over group:

var ints = new[]{1,2,3,4,5};

var groups = ints.ToLookup(x=>x%2==0);

var result = groups[true].Concat(groups[false]);

foreach (var i in result){
    // ..
}



But there is a trick - a boolean value is comparable, false < true, so instead of creating lookups and concatenating we can simply order the collection by feature descendingly:

var ints = new[]{1,2,3,4,5};

var result = ints.OrderByDescending(x=>x%2==0);

foreach (var i in result){
    // ..
}