Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable nullable in projectfile #19

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions perf/BrowserDetector.Benchmarks/DetectorBenchmarks.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
namespace BrowserDetector.Benchmarks
{
using System;
using BenchmarkDotNet.Attributes;
using Shyjus.BrowserDetection;

Expand All @@ -13,13 +12,13 @@ public class DetectorBenchmarks
[Benchmark]
public string Chrome_Windows()
{
return Detector.GetBrowser(this.chromeWindows.AsSpan()).Name;
return Detector.GetBrowser(this.chromeWindows).Name;
}

[Benchmark]
public string Safari_Windows()
{
return Detector.GetBrowser(this.safariWindows.AsSpan()).Name;
return Detector.GetBrowser(this.safariWindows).Name;
}
}
}
1 change: 1 addition & 0 deletions src/BrowserDetector.NetCore/BrowserDetector.Net.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

<PropertyGroup>
<TargetFrameworks>net6.0;net7.0;net8.0</TargetFrameworks>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
Expand Down
13 changes: 8 additions & 5 deletions src/BrowserDetector.NetCore/Browsers/Browser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,8 @@ internal abstract class Browser : IBrowser
{
private readonly string platform;

protected Browser(ReadOnlySpan<char> userAgent, string version)
protected Browser(ReadOnlySpan<char> userAgent)
{
this.Version = version;

var platform = PlatformDetector.GetPlatformAndOS(userAgent);
this.platform = platform.Platform;
this.OS = platform.OS;
Expand All @@ -28,18 +26,23 @@ protected Browser(ReadOnlySpan<char> userAgent, string version)
public string DeviceType { get; }

/// <inheritdoc/>
public string Version { get; }
public abstract string Version { get; }

/// <inheritdoc/>
public string OS { get; }

/// <summary>
/// Check for valid parsing of user-agent-string.
/// </summary>
internal bool IsValid => !string.IsNullOrEmpty(Version);

/// <summary>
/// Gets the version segment from user agent for the key passed in.
/// </summary>
/// <param name="userAgent">The user agent value.</param>
/// <param name="key">The key to use for looking up the version segment.</param>
/// <returns>The version segment.</returns>
protected static string GetVersionIfKeyPresent(ReadOnlySpan<char> userAgent, string key)
protected static string? GetVersionIfKeyIsPresent(ReadOnlySpan<char> userAgent, string key)
{
var keyStartIndex = userAgent.IndexOf(key.AsSpan());

Expand Down
42 changes: 17 additions & 25 deletions src/BrowserDetector.NetCore/Browsers/Chrome.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,46 +9,38 @@
/// </summary>
internal class Chrome : Browser
{
public Chrome(ReadOnlySpan<char> userAgent, string version)
: base(userAgent, version)
{
}

/// <inheritdoc/>
public override string Name => BrowserNames.Chrome;

/// <summary>
/// Populates a Chrome browser object from the userAgent value passed in. A return value indicates the parsing and populating the browser instance succeeded.
/// </summary>
/// <param name="userAgent">User agent value</param>
/// <param name="result">When this method returns True, the result will contain a Chrome object populated</param>
/// <returns>True if parsing succeeded, else False</returns>
public static bool TryParse(ReadOnlySpan<char> userAgent, out Chrome result)
public Chrome(string userAgent)
: base(userAgent)
{
var chromeIndex = userAgent.IndexOf("Chrome/".AsSpan());
var safariIndex = userAgent.IndexOf("Safari/".AsSpan());
var crIOS = userAgent.IndexOf("CriOS/".AsSpan());
Version = string.Empty;

var chromeIndex = userAgent.AsSpan().IndexOf("Chrome/".AsSpan());
var safariIndex = userAgent.AsSpan().IndexOf("Safari/".AsSpan());
var crIOS = userAgent.AsSpan().IndexOf("CriOS/".AsSpan());

// Chrome should have both "Safari" and "Chrome" words in it.
if ((safariIndex > -1 && chromeIndex > -1) || (safariIndex > -1 && crIOS > -1))
{
var fireFoxVersion = GetVersionIfKeyPresent(userAgent, "Chrome/");
var fireFoxVersion = GetVersionIfKeyIsPresent(userAgent, "Chrome/");
if (fireFoxVersion != null)
{
result = new Chrome(userAgent, fireFoxVersion);
return true;
Version = fireFoxVersion;
return;
}


var chromeIosVersion = GetVersionIfKeyPresent(userAgent, "CriOS/");
var chromeIosVersion = GetVersionIfKeyIsPresent(userAgent, "CriOS/");
if (chromeIosVersion != null)
{
result = new Chrome(userAgent, chromeIosVersion);
return true;
}
Version = chromeIosVersion;
}

result = null;
return false;
}

/// <inheritdoc/>
public override string Name => BrowserNames.Chrome;
public override string Version { get; }
}
}
36 changes: 10 additions & 26 deletions src/BrowserDetector.NetCore/Browsers/Edge.cs
Original file line number Diff line number Diff line change
@@ -1,42 +1,26 @@
namespace Shyjus.BrowserDetection
{
using System;

/// <summary>
/// Represents an instance of Edge Browser.
/// </summary>
internal class Edge : Browser
{
public Edge(ReadOnlySpan<char> userAgent, string version)
: base(userAgent, version)
{
}

/// <inheritdoc/>
public override string Name => BrowserNames.Edge;

/// <summary>
/// Tries to create an Edge browser object from the user agent passed in.
/// </summary>
/// <param name="userAgent">The user agent.</param>
/// <param name="result">An instance of Edge browser, if parsing was successful.</param>
/// <returns>A boolean value indicating whether the parsing was successful.</returns>
public static bool TryParse(ReadOnlySpan<char> userAgent, out Browser result)
public Edge(string userAgent)
: base(userAgent)
{
var edgeVersion = GetVersionIfKeyPresent(userAgent, "Edge/");
var edgeIosVersion = GetVersionIfKeyPresent(userAgent, "EdgiOS/");
var edgeAndroidVersion = GetVersionIfKeyPresent(userAgent, "EdgA/");

var version = edgeVersion ?? edgeIosVersion ?? edgeAndroidVersion;

if (version == null)
{
result = null;
return false;
}
var edgeVersion = GetVersionIfKeyIsPresent(userAgent, "Edge/");
var edgeIosVersion = GetVersionIfKeyIsPresent(userAgent, "EdgiOS/");
var edgeAndroidVersion = GetVersionIfKeyIsPresent(userAgent, "EdgA/");

result = new Edge(userAgent, version);
return true;
Version = edgeVersion ?? edgeIosVersion ?? edgeAndroidVersion ?? string.Empty;
}

/// <inheritdoc/>
public override string Name => BrowserNames.Edge;
public override string Version { get; }
}
}
29 changes: 7 additions & 22 deletions src/BrowserDetector.NetCore/Browsers/EdgeChromium.cs
Original file line number Diff line number Diff line change
@@ -1,39 +1,24 @@
namespace Shyjus.BrowserDetection
{
using System;

/// <summary>
/// Represents an instance of EdgeChromium Browser.
/// </summary>
internal class EdgeChromium : Browser
{
public EdgeChromium(ReadOnlySpan<char> userAgent, string version)
: base(userAgent, version)
{
}

/// <inheritdoc/>
public override string Name => BrowserNames.EdgeChromium;

/// <summary>
/// Tries to build a EdgeChromium browser instance from the user agent passed in and
/// returns a value that indicates whether the parsing succeeded.
/// </summary>
/// <param name="userAgent">The user agent value.</param>
/// <param name="result">An EdgeChromium browser instance.</param>
/// <returns>A boolean value that indicates whether the parsing succeeded.</returns>
public static bool TryParse(ReadOnlySpan<char> userAgent, out EdgeChromium result)
public EdgeChromium(string userAgent)
: base(userAgent)
{
var edgChromiumVersion = GetVersionIfKeyPresent(userAgent, "Edg/");
Version = GetVersionIfKeyIsPresent(userAgent, "Edg/") ?? string.Empty;
}

if (edgChromiumVersion != null)
{
result = new EdgeChromium(userAgent, edgChromiumVersion);
return true;
}
/// <inheritdoc/>
public override string Name => BrowserNames.EdgeChromium;

result = null;
return false;
}
public override string Version { get; }
}
}
41 changes: 15 additions & 26 deletions src/BrowserDetector.NetCore/Browsers/Firefox.cs
Original file line number Diff line number Diff line change
@@ -1,49 +1,38 @@
namespace Shyjus.BrowserDetection
{
using System;

/// <summary>
/// A type representing the FireFox browser instance.
/// </summary>
internal class Firefox : Browser
{
private Firefox(ReadOnlySpan<char> userAgent, string version)
: base(userAgent, version)
{
}

public string Platform { get; }

/// <inheritdoc/>
public override string Name => BrowserNames.Firefox;

/// <summary>
/// Tries to build a Firefox browser instance from the user agent passed in and
/// returns a value that indicates whether the parsing succeeded.
/// </summary>
/// <param name="userAgent">The user agent value.</param>
/// <param name="result">A Firefox browser instance.</param>
/// <returns>A boolean value that indicates whether the parsing succeeded.</returns>
public static bool TryParse(ReadOnlySpan<char> userAgent, out Firefox result)
public Firefox(string userAgent)
: base(userAgent)
{
Version = string.Empty;

// Desktop version of Firefox.
var fireFoxVersion = GetVersionIfKeyPresent(userAgent, "Firefox/");
var fireFoxVersion = GetVersionIfKeyIsPresent(userAgent, "Firefox/");
if (fireFoxVersion != null)
{
result = new Firefox(userAgent, fireFoxVersion);
return true;
Version = fireFoxVersion;
return;
}

// IOS version of Firefox.
var fxiosVersion = GetVersionIfKeyPresent(userAgent, "FxiOS/");
var fxiosVersion = GetVersionIfKeyIsPresent(userAgent, "FxiOS/");
if (fxiosVersion != null)
{
result = new Firefox(userAgent, fxiosVersion);
return true;
}

result = null;
return false;
Version = fxiosVersion;

}

/// <inheritdoc/>
public override string Name => BrowserNames.Firefox;

public override string Version { get; }
}
}
28 changes: 6 additions & 22 deletions src/BrowserDetector.NetCore/Browsers/InternetExplorer.cs
Original file line number Diff line number Diff line change
@@ -1,38 +1,22 @@
namespace Shyjus.BrowserDetection
{
using System;

/// <summary>
/// Represents an instance of Edge Browser
/// </summary>
internal class InternetExplorer : Browser
{
public InternetExplorer(ReadOnlySpan<char> userAgent, string version)
: base(userAgent, version)
{
}

public override string Name => BrowserNames.InternetExplorer;

/// <summary>
/// Tries to build an instance of InternetExplorer browser from the user agent passed in and
/// returns a value that indicates whether the parsing succeeded.
/// </summary>
/// <param name="userAgent">The user agent value.</param>
/// <param name="result">An instance of EdgeChromium browser.</param>
/// <returns>A boolean value that indicates whether the parsing succeeded.</returns>
public static bool TryParse(ReadOnlySpan<char> userAgent, out InternetExplorer result)
public InternetExplorer(string userAgent)
: base(userAgent)
{
var tridentVersion = GetVersionIfKeyPresent(userAgent, "Trident/");

if (tridentVersion != null)
{
result = new InternetExplorer(userAgent, tridentVersion);
return true;
}

result = null;
return false;
Version = GetVersionIfKeyIsPresent(userAgent, "Trident/") ?? string.Empty;
}

public override string Name => BrowserNames.InternetExplorer;
public override string Version { get; }
}
}
Loading