-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4b600eb
commit 9ff15c7
Showing
61 changed files
with
1,758 additions
and
1,657 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,42 @@ | ||
using System.Text.RegularExpressions; | ||
|
||
namespace M3U8Parser.Attributes.BaseAttribute | ||
{ | ||
public class BoolAttribute : CustomAttribute<bool> | ||
{ | ||
using System.Text.RegularExpressions; | ||
|
||
public class BoolAttribute : CustomAttribute<bool> | ||
{ | ||
private const string YesValue = "YES"; | ||
private const string NoValue = "NO"; | ||
|
||
public BoolAttribute(string attributeName) : base(attributeName) | ||
{ | ||
} | ||
public BoolAttribute(string attributeName) | ||
: base(attributeName) | ||
{ | ||
} | ||
|
||
public override string ToString() | ||
{ | ||
return $"{AttributeName}={BoolToString(Value)}"; | ||
} | ||
public override string ToString() | ||
{ | ||
return $"{AttributeName}={BoolToString(Value)}"; | ||
} | ||
|
||
public override void Read(string content) | ||
public override void Read(string content) | ||
{ | ||
var pattern = $"(?={AttributeName})(.*?)(?=,|$)"; | ||
var match = Regex.Match(content.Trim(), pattern, RegexOptions.Multiline & RegexOptions.IgnoreCase); | ||
var match = Regex.Match(content.Trim(), pattern, RegexOptions.Multiline & RegexOptions.IgnoreCase); | ||
|
||
if (match.Success) | ||
{ | ||
var valueFounded = match.Groups[0].Value.Split('=')[1]; | ||
Value = StringToBool(valueFounded); | ||
} | ||
} | ||
{ | ||
var valueFounded = match.Groups[0].Value.Split('=')[1]; | ||
Value = StringToBool(valueFounded); | ||
} | ||
} | ||
|
||
private string BoolToString(bool value) | ||
{ | ||
return value ? YesValue : NoValue; | ||
} | ||
private string BoolToString(bool value) | ||
{ | ||
return value ? YesValue : NoValue; | ||
} | ||
|
||
private bool StringToBool(string value) | ||
{ | ||
return value == YesValue; | ||
} | ||
} | ||
private bool StringToBool(string value) | ||
{ | ||
return value == YesValue; | ||
} | ||
} | ||
} |
114 changes: 57 additions & 57 deletions
114
src/M3U8Parser/Attributes/BaseAttribute/CustomAttribute.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,60 +1,60 @@ | ||
using System; | ||
using System.Text.RegularExpressions; | ||
using M3U8Parser.Interfaces; | ||
|
||
namespace M3U8Parser.Attributes.BaseAttribute | ||
{ | ||
public class CustomAttribute<T> : IAttribute | ||
{ | ||
public CustomAttribute(string attributeName) | ||
{ | ||
AttributeName = attributeName; | ||
} | ||
|
||
public T Value { get; set; } | ||
|
||
protected string AttributeName { get; } | ||
|
||
public override string ToString() | ||
{ | ||
if (Value != null) | ||
{ | ||
return $"{AttributeName}={Value}"; | ||
} | ||
|
||
return string.Empty; | ||
} | ||
|
||
public virtual void Read(string content) | ||
{ | ||
var match = Regex.Match(content.Trim(), $"[,|:](?={AttributeName})(.*?)(?=,|$)", | ||
RegexOptions.Multiline & RegexOptions.IgnoreCase); | ||
|
||
var type = typeof(T); | ||
|
||
if (match.Success) | ||
{ | ||
var valueFounded = match.Groups[0].Value.Split('=')[1]; | ||
|
||
if (type.IsGenericType && type.GetGenericTypeDefinition().Equals(typeof(Nullable<>))) | ||
{ | ||
type = Nullable.GetUnderlyingType(type); | ||
} | ||
|
||
if (typeof(ICustomAttribute).IsAssignableFrom(type)) | ||
{ | ||
var instanceAttribute = (ICustomAttribute)Activator.CreateInstance(type, false); | ||
Value = (T)instanceAttribute.ParseFromString(valueFounded); | ||
} | ||
else | ||
{ | ||
Value = (T)Convert.ChangeType(valueFounded, type); | ||
} | ||
} | ||
else | ||
{ | ||
Value = default(T); | ||
} | ||
} | ||
} | ||
using System; | ||
using System.Text.RegularExpressions; | ||
using M3U8Parser.Interfaces; | ||
|
||
public class CustomAttribute<T> : IAttribute | ||
{ | ||
public CustomAttribute(string attributeName) | ||
{ | ||
AttributeName = attributeName; | ||
} | ||
|
||
public T Value { get; set; } | ||
|
||
protected string AttributeName { get; } | ||
|
||
public override string ToString() | ||
{ | ||
if (Value != null) | ||
{ | ||
return $"{AttributeName}={Value}"; | ||
} | ||
|
||
return string.Empty; | ||
} | ||
|
||
public virtual void Read(string content) | ||
{ | ||
var match = Regex.Match(content.Trim(), $"[,|:](?={AttributeName})(.*?)(?=,|$)", | ||
RegexOptions.Multiline & RegexOptions.IgnoreCase); | ||
|
||
var type = typeof(T); | ||
|
||
if (match.Success) | ||
{ | ||
var valueFounded = match.Groups[0].Value.Split('=')[1]; | ||
|
||
if (type.IsGenericType && type.GetGenericTypeDefinition().Equals(typeof(Nullable<>))) | ||
{ | ||
type = Nullable.GetUnderlyingType(type); | ||
} | ||
|
||
if (typeof(ICustomAttribute).IsAssignableFrom(type)) | ||
{ | ||
var instanceAttribute = (ICustomAttribute)Activator.CreateInstance(type, false); | ||
Value = (T)instanceAttribute.ParseFromString(valueFounded); | ||
} | ||
else | ||
{ | ||
Value = (T)Convert.ChangeType(valueFounded, type); | ||
} | ||
} | ||
else | ||
{ | ||
Value = default; | ||
} | ||
} | ||
} | ||
} |
52 changes: 26 additions & 26 deletions
52
src/M3U8Parser/Attributes/BaseAttribute/DecimalAttribute.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,34 @@ | ||
using System.Globalization; | ||
using System.Text.RegularExpressions; | ||
|
||
namespace M3U8Parser.Attributes.BaseAttribute | ||
{ | ||
public class DecimalAttribute : CustomAttribute<decimal?> | ||
{ | ||
public DecimalAttribute(string attributeName) : base(attributeName) | ||
{ | ||
} | ||
using System.Globalization; | ||
using System.Text.RegularExpressions; | ||
|
||
public class DecimalAttribute : CustomAttribute<decimal?> | ||
{ | ||
public DecimalAttribute(string attributeName) : base(attributeName) | ||
{ | ||
} | ||
|
||
public override string ToString() | ||
{ | ||
if (Value != null) | ||
{ | ||
return $"{AttributeName}={Value.Value.ToString(CultureInfo.InvariantCulture)}"; | ||
} | ||
public override string ToString() | ||
{ | ||
if (Value != null) | ||
{ | ||
return $"{AttributeName}={Value.Value.ToString(CultureInfo.InvariantCulture)}"; | ||
} | ||
|
||
return string.Empty; | ||
} | ||
|
||
return string.Empty; | ||
} | ||
|
||
public override void Read(string content) | ||
public override void Read(string content) | ||
{ | ||
var pattern = $"(?={AttributeName})(.*?)(?=,|$)"; | ||
var match = Regex.Match(content.Trim(), pattern, RegexOptions.Multiline & RegexOptions.IgnoreCase); | ||
var match = Regex.Match(content.Trim(), pattern, RegexOptions.Multiline & RegexOptions.IgnoreCase); | ||
|
||
if (match.Success) | ||
{ | ||
var valueFounded = match.Groups[0].Value.Split('=')[1]; | ||
Value = decimal.Parse(valueFounded, CultureInfo.InvariantCulture); | ||
} | ||
} | ||
} | ||
{ | ||
var valueFounded = match.Groups[0].Value.Split('=')[1]; | ||
Value = decimal.Parse(valueFounded, CultureInfo.InvariantCulture); | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.