Skip to content

Commit

Permalink
C#7: Migrate from Tuple to ValueTuple
Browse files Browse the repository at this point in the history
  • Loading branch information
chronoxor committed Dec 30, 2020
1 parent a6ead8f commit 132c7af
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 27 deletions.
2 changes: 1 addition & 1 deletion .appveyor.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Specify version format
version: "5.0.4.{build}"
version: "5.0.5.{build}"

# Image to use
image: Visual Studio 2019
Expand Down
12 changes: 6 additions & 6 deletions source/NetCoreServer/FileCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,15 +88,15 @@ public class FileCache
/// </summary>
/// <param name="key">Key to find</param>
/// <returns>'true' and cache value if the cache value was found, 'false' if the given key was not found</returns>
public Tuple<bool, byte[]> Find(string key)
public (bool, byte[]) Find(string key)
{
lock (_lock)
{
// Try to find the given key
if (!_entriesByKey.TryGetValue(key, out var cacheValue))
return new Tuple<bool, byte[]>(false, new byte[0]);
return (false, new byte[0]);

return new Tuple<bool, byte[]>(true, cacheValue.Value);
return (true, cacheValue.Value);
}
}

Expand All @@ -106,19 +106,19 @@ public Tuple<bool, byte[]> Find(string key)
/// <param name="key">Key to find</param>
/// <param name="timeout">Cache timeout value</param>
/// <returns>'true' and cache value if the cache value was found, 'false' if the given key was not found</returns>
public Tuple<bool, byte[]> Find(string key, out DateTime timeout)
public (bool, byte[]) Find(string key, out DateTime timeout)
{
lock (_lock)
{
// Try to find the given key
if (!_entriesByKey.TryGetValue(key, out var cacheValue))
{
timeout = new DateTime(0);
return new Tuple<bool, byte[]>(false, new byte[0]);
return (false, new byte[0]);
}

timeout = cacheValue.Timestamp + cacheValue.Timespan;
return new Tuple<bool, byte[]>(true, cacheValue.Value);
return (true, cacheValue.Value);
}
}

Expand Down
26 changes: 13 additions & 13 deletions source/NetCoreServer/HttpRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,11 @@ public HttpRequest(string method, string url, string protocol = "HTTP/1.1")
/// <summary>
/// Get the HTTP request header by index
/// </summary>
public Tuple<string, string> Header(int i)
public (string, string) Header(int i)
{
Debug.Assert((i < _headers.Count), "Index out of bounds!");
if (i >= _headers.Count)
return new Tuple<string, string>("", "");
return ("", "");

return _headers[i];
}
Expand All @@ -72,11 +72,11 @@ public Tuple<string, string> Header(int i)
/// <summary>
/// Get the HTTP request cookie by index
/// </summary>
public Tuple<string, string> Cookie(int i)
public (string, string) Cookie(int i)
{
Debug.Assert((i < _cookies.Count), "Index out of bounds!");
if (i >= _cookies.Count)
return new Tuple<string, string>("", "");
return ("", "");

return _cookies[i];
}
Expand Down Expand Up @@ -192,7 +192,7 @@ public HttpRequest SetHeader(string key, string value)
_cache.Append("\r\n");

// Add the header to the corresponding collection
_headers.Add(new Tuple<string, string>(key, value));
_headers.Add((key, value));
return this;
}

Expand All @@ -217,9 +217,9 @@ public HttpRequest SetCookie(string name, string value)
_cache.Append("\r\n");

// Add the header to the corresponding collection
_headers.Add(new Tuple<string, string>(key, cookie));
_headers.Add((key, cookie));
// Add the cookie to the corresponding collection
_cookies.Add(new Tuple<string, string>(name, value));
_cookies.Add((name, value));
return this;
}

Expand All @@ -237,7 +237,7 @@ public HttpRequest AddCookie(string name, string value)
_cache.Append(value);

// Add the cookie to the corresponding collection
_cookies.Add(new Tuple<string, string>(name, value));
_cookies.Add((name, value));
return this;
}

Expand Down Expand Up @@ -493,9 +493,9 @@ public HttpRequest MakeTraceRequest(string url)
// HTTP request protocol
private string _protocol;
// HTTP request headers
private List<Tuple<string, string>> _headers = new List<Tuple<string, string>>();
private List<(string, string)> _headers = new List<(string, string)>();
// HTTP request cookies
private List<Tuple<string, string>> _cookies = new List<Tuple<string, string>>();
private List<(string, string)> _cookies = new List<(string, string)>();
// HTTP request body
private int _bodyIndex;
private int _bodySize;
Expand Down Expand Up @@ -641,7 +641,7 @@ internal bool ReceiveHeader(byte[] buffer, int offset, int size)
// Add a new header
string headerName = _cache.ExtractString(headerNameIndex, headerNameSize);
string headerValue = _cache.ExtractString(headerValueIndex, headerValueSize);
_headers.Add(new Tuple<string, string>(headerName, headerValue));
_headers.Add((headerName, headerValue));

// Try to find the body content length
if (string.Compare(headerName, "Content-Length", StringComparison.OrdinalIgnoreCase) == 0)
Expand Down Expand Up @@ -725,7 +725,7 @@ internal bool ReceiveHeader(byte[] buffer, int offset, int size)
if ((nameSize > 0) && (cookieSize > 0))
{
// Add the cookie to the corresponding collection
_cookies.Add(new Tuple<string, string>(_cache.ExtractString(nameIndex, nameSize), _cache.ExtractString(cookieIndex, cookieSize)));
_cookies.Add((_cache.ExtractString(nameIndex, nameSize), _cache.ExtractString(cookieIndex, cookieSize)));

// Resset the current cookie values
nameIndex = j;
Expand Down Expand Up @@ -763,7 +763,7 @@ internal bool ReceiveHeader(byte[] buffer, int offset, int size)
if ((nameSize > 0) && (cookieSize > 0))
{
// Add the cookie to the corresponding collection
_cookies.Add(new Tuple<string, string>(_cache.ExtractString(nameIndex, nameSize), _cache.ExtractString(cookieIndex, cookieSize)));
_cookies.Add((_cache.ExtractString(nameIndex, nameSize), _cache.ExtractString(cookieIndex, cookieSize)));
}
}
}
Expand Down
12 changes: 6 additions & 6 deletions source/NetCoreServer/HttpResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,11 @@ public HttpResponse(int status, string statusPhrase, string protocol)
/// <summary>
/// Get the HTTP response header by index
/// </summary>
public Tuple<string, string> Header(int i)
public (string, string) Header(int i)
{
Debug.Assert((i < _headers.Count), "Index out of bounds!");
if (i >= _headers.Count)
return new Tuple<string, string>("", "");
return ("", "");

return _headers[i];
}
Expand Down Expand Up @@ -403,7 +403,7 @@ public HttpResponse SetHeader(string key, string value)
_cache.Append("\r\n");

// Add the header to the corresponding collection
_headers.Add(new Tuple<string, string>(key, value));
_headers.Add((key, value));
return this;
}

Expand Down Expand Up @@ -457,7 +457,7 @@ public HttpResponse SetCookie(string name, string value, int maxAge = 86400, str
_cache.Append("\r\n");

// Add the header to the corresponding collection
_headers.Add(new Tuple<string, string>(key, cookie));
_headers.Add((key, cookie));
return this;
}

Expand Down Expand Up @@ -688,7 +688,7 @@ public HttpResponse MakeTraceResponse(Buffer request)
// HTTP response protocol
private string _protocol;
// HTTP response headers
private List<Tuple<string, string>> _headers = new List<Tuple<string, string>>();
private List<(string, string)> _headers = new List<(string, string)>();
// HTTP response body
private int _bodyIndex;
private int _bodySize;
Expand Down Expand Up @@ -842,7 +842,7 @@ internal bool ReceiveHeader(byte[] buffer, int offset, int size)
// Add a new header
string headerName = _cache.ExtractString(headerNameIndex, headerNameSize);
string headerValue = _cache.ExtractString(headerValueIndex, headerValueSize);
_headers.Add(new Tuple<string, string>(headerName, headerValue));
_headers.Add((headerName, headerValue));

// Try to find the body content length
if (string.Compare(headerName, "Content-Length", StringComparison.OrdinalIgnoreCase) == 0)
Expand Down
2 changes: 1 addition & 1 deletion source/NetCoreServer/NetCoreServer.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<Version>5.0.4.0</Version>
<Version>5.0.5.0</Version>
<Authors>Ivan Shynkarenka</Authors>
<Copyright>Copyright (c) 2019-2021 Ivan Shynkarenka</Copyright>
<RepositoryUrl>https://github.com/chronoxor/NetCoreServer</RepositoryUrl>
Expand Down

0 comments on commit 132c7af

Please sign in to comment.