Skip to content

Commit

Permalink
Add non-throwing result for non-crucial values
Browse files Browse the repository at this point in the history
  • Loading branch information
ScrubN committed Jan 6, 2024
1 parent a6dd716 commit 6cc8232
Showing 1 changed file with 26 additions and 9 deletions.
35 changes: 26 additions & 9 deletions TwitchDownloaderCore/Tools/M3U8.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
Expand Down Expand Up @@ -149,7 +151,7 @@ private static bool ParseM3U8Key(ReadOnlySpan<char> text, Metadata.Builder metad
}
else if (text.StartsWith(PROGRAM_DATE_TIME_KEY))
{
extProgramDateTime = ParsingHelpers.ParseDateTimeOffset(text, PROGRAM_DATE_TIME_KEY);
extProgramDateTime = ParsingHelpers.ParseDateTimeOffset(text, PROGRAM_DATE_TIME_KEY, false);
}
else if (text.StartsWith(Stream.ExtByteRange.BYTE_RANGE_KEY))
{
Expand Down Expand Up @@ -588,11 +590,11 @@ public static ExtStreamInfo Parse(ReadOnlySpan<char> text)

if (text.StartsWith(KEY_PROGRAM_ID))
{
streamInfo.ProgramId = ParsingHelpers.ParseIntValue(text, KEY_PROGRAM_ID);
streamInfo.ProgramId = ParsingHelpers.ParseIntValue(text, KEY_PROGRAM_ID, false);
}
else if (text.StartsWith(KEY_BANDWIDTH))
{
streamInfo.Bandwidth = ParsingHelpers.ParseIntValue(text, KEY_BANDWIDTH);
streamInfo.Bandwidth = ParsingHelpers.ParseIntValue(text, KEY_BANDWIDTH, false);
}
else if (text.StartsWith(KEY_CODECS))
{
Expand All @@ -608,7 +610,7 @@ public static ExtStreamInfo Parse(ReadOnlySpan<char> text)
}
else if (text.StartsWith(KEY_FRAMERATE))
{
streamInfo.Framerate = ParsingHelpers.ParseDecimalValue(text, KEY_FRAMERATE);
streamInfo.Framerate = ParsingHelpers.ParseDecimalValue(text, KEY_FRAMERATE, false);
}

var nextIndex = text.UnEscapedIndexOf(',');
Expand Down Expand Up @@ -711,40 +713,49 @@ public static string ParseStringValue(ReadOnlySpan<char> text, ReadOnlySpan<char
return temp[..closeQuote].ToString();
}

public static int ParseIntValue(ReadOnlySpan<char> text, ReadOnlySpan<char> keyName)
public static int ParseIntValue(ReadOnlySpan<char> text, ReadOnlySpan<char> keyName, bool strict = true)
{
var temp = text[keyName.Length..];
temp = temp[..NextKeyStart(temp)];

if (int.TryParse(temp, NumberStyles.Integer, CultureInfo.InvariantCulture, out var intValue))
return intValue;

if (!strict)
return default;

throw new FormatException($"Unable to parse integer from: {text}");
}

public static uint ParseUIntValue(ReadOnlySpan<char> text, ReadOnlySpan<char> keyName)
public static uint ParseUIntValue(ReadOnlySpan<char> text, ReadOnlySpan<char> keyName, bool strict = true)
{
var temp = text[keyName.Length..];
temp = temp[..NextKeyStart(temp)];

if (uint.TryParse(temp, NumberStyles.Integer, CultureInfo.InvariantCulture, out var uIntValue))
return uIntValue;

if (!strict)
return default;

throw new FormatException($"Unable to parse integer from: {text}");
}

public static decimal ParseDecimalValue(ReadOnlySpan<char> text, ReadOnlySpan<char> keyName)
public static decimal ParseDecimalValue(ReadOnlySpan<char> text, ReadOnlySpan<char> keyName, bool strict = true)
{
var temp = text[keyName.Length..];
temp = temp[..NextKeyStart(temp)];

if (decimal.TryParse(temp, NumberStyles.Number, CultureInfo.InvariantCulture, out var decimalValue))
return decimalValue;

if (!strict)
return default;

throw new FormatException($"Unable to parse decimal from: {text}");
}

public static bool ParseBooleanValue(ReadOnlySpan<char> text, ReadOnlySpan<char> keyName)
public static bool ParseBooleanValue(ReadOnlySpan<char> text, ReadOnlySpan<char> keyName, bool strict = true)
{
var temp = text[keyName.Length..];

Expand All @@ -759,6 +770,9 @@ public static bool ParseBooleanValue(ReadOnlySpan<char> text, ReadOnlySpan<char>
if (bool.TryParse(temp, out var booleanValue))
return booleanValue;

if (!strict)
return default;

throw new FormatException($"Unable to parse boolean from: {text}");
}

Expand All @@ -770,14 +784,17 @@ public static Stream.ExtStreamInfo.StreamResolution ParseResolution(ReadOnlySpan
return Stream.ExtStreamInfo.StreamResolution.Parse(temp);
}

public static DateTimeOffset ParseDateTimeOffset(ReadOnlySpan<char> text, ReadOnlySpan<char> keyName)
public static DateTimeOffset ParseDateTimeOffset(ReadOnlySpan<char> text, ReadOnlySpan<char> keyName, bool strict = true)
{
var temp = text[keyName.Length..];
temp = temp[..NextKeyStart(temp)];

if (DateTimeOffset.TryParse(temp, out var dateTimeOffset))
return dateTimeOffset;

if (!strict)
return default;

throw new FormatException($"Unable to parse DateTimeOffset from: {text}");
}

Expand Down

0 comments on commit 6cc8232

Please sign in to comment.