Skip to content

Commit 6283624

Browse files
committed
✨ support net8.0
1 parent 91f8f3e commit 6283624

File tree

4 files changed

+26
-2
lines changed

4 files changed

+26
-2
lines changed

Json5.Tests/Json5.Tests.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFramework>net9.0</TargetFramework>
4+
<TargetFrameworks>net8.0;net9.0</TargetFrameworks>
55
<ImplicitUsings>enable</ImplicitUsings>
66
<Nullable>enable</Nullable>
77
<IsPackable>false</IsPackable>

Json5/Internal/NumberParser.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,28 @@ public static BigInteger ParseHexNum(NumberLiteral nl) {
7373
public static BigInteger ParseBinNum(NumberLiteral nl) {
7474
var s = nl.HasMinusSign || nl.HasPlusSign ? nl.String[3..] : nl.String[2..];
7575
var sign = nl.HasMinusSign ? -1 : 1;
76+
#if NET9_0_OR_GREATER
7677
// Prepend 0 so the most significant bit is never set and the result will always be positive.
7778
return BigInteger.TryParse('0' + s, NumberStyles.AllowBinarySpecifier, Culture, out var val)
7879
? sign * val
7980
: throw new NotSupportedException($"Format of the number literal {nl.String} is not supported.");
81+
#else
82+
return sign * Chunk(s.AsMemory(), size: 128).Aggregate(
83+
BigInteger.Zero,
84+
(bigInt, chunk) => (bigInt << chunk.Length) + ParseChunk(chunk));
85+
86+
UInt128 ParseChunk(ReadOnlyMemory<char> chunk)
87+
=> UInt128.TryParse(chunk.Span, NumberStyles.AllowBinarySpecifier, Culture, out var v)
88+
? v
89+
: throw new NotSupportedException($"Format of the number literal {nl.String} is not supported.");
90+
91+
static IEnumerable<ReadOnlyMemory<char>> Chunk(ReadOnlyMemory<char> source, int size) {
92+
for (var i = 0; i < source.Length; i += size) {
93+
yield return i + size < source.Length
94+
? source.Slice(i, length: size)
95+
: source[i..];
96+
}
97+
}
98+
#endif
8099
}
81100
}

Json5/Json5.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFramework>net9.0</TargetFramework>
4+
<TargetFrameworks>net8.0;net9.0</TargetFrameworks>
55
<ImplicitUsings>enable</ImplicitUsings>
66
<Nullable>enable</Nullable>
77
<GenerateDocumentationFile>True</GenerateDocumentationFile>

Json5/Microsoft.Extensions.Configuration/Json5ConfigExtensions.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,4 +118,9 @@ static bool IsMainAppSettings(IConfigurationSource s)
118118
bool IsEnvAppSettings(IConfigurationSource s)
119119
=> ((JsonConfigurationSource)s).Path == $"appsettings.{builder.Environment.EnvironmentName}.json";
120120
}
121+
122+
#if !NET9_0_OR_GREATER
123+
private static IEnumerable<(int Index, TSource Item)> Index<TSource>(this IEnumerable<TSource> source)
124+
=> source.Select((x, i) => (i, x));
125+
#endif
121126
}

0 commit comments

Comments
 (0)