Skip to content

Commit

Permalink
v1.2.9 - fix: int.Parse CultureInfo
Browse files Browse the repository at this point in the history
  • Loading branch information
2881099 committed Dec 4, 2023
1 parent 67f40cf commit 55b37a0
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 51 deletions.
7 changes: 6 additions & 1 deletion examples/console_net8_client_side_caching/Program.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using FreeRedis;
using Newtonsoft.Json;
using System;
using System.Globalization;
using System.Threading;

namespace console_net8_client_side_caching
{
Expand All @@ -27,7 +29,10 @@ class Program

static void Main(string[] args)
{
cli.UseClientSideCaching(new ClientSideCachingOptions
Thread.CurrentThread.CurrentCulture = new CultureInfo("nb");
var test = long.Parse("-1");

cli.UseClientSideCaching(new ClientSideCachingOptions
{
//本地缓存的容量
Capacity = 3,
Expand Down
2 changes: 1 addition & 1 deletion src/FreeRedis/FreeRedis.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<AssemblyName>FreeRedis</AssemblyName>
<PackageId>FreeRedis</PackageId>
<RootNamespace>FreeRedis</RootNamespace>
<Version>1.2.8</Version>
<Version>1.2.9</Version>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<PackageProjectUrl>https://github.com/2881099/FreeRedis</PackageProjectUrl>
<Description>FreeRedis is .NET redis client, supports cluster, sentinel, master-slave, pipeline, transaction and connection pool.</Description>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@
// static RedisResult ReadBigNumber(ref ReadOnlySequence<byte> buffer, ref long offset, RedisMessageType msgtype)
// {
// if (!TryReadLine(ref buffer, ref offset, out var line)) return null;
// if (!BigInteger.TryParse(line, NumberStyles.Any, null, out var num)) throw new ProtocolViolationException($"Expecting fail BigNumber '{msgtype}0', got '{msgtype}{line}'");
// if (!BigInteger.TryParse(line, NumberStyles.Any, CultureInfo.InvariantCulture.NumberFormat, out var num)) throw new ProtocolViolationException($"Expecting fail BigNumber '{msgtype}0', got '{msgtype}{line}'");
// return new RedisResult(num, false, msgtype);
// }
// static RedisResult ReadNull(ref ReadOnlySequence<byte> buffer, ref long offset, RedisMessageType msgtype)
Expand All @@ -400,7 +400,7 @@
// case "inf": num = double.PositiveInfinity; break;
// case "-inf": num = double.NegativeInfinity; break;
// default:
// if (!double.TryParse(line, NumberStyles.Any, null, out num)) throw new ProtocolViolationException($"Expecting fail Double '{msgtype}1.23', got '{msgtype}{line}'");
// if (!double.TryParse(line, NumberStyles.Any, CultureInfo.InvariantCulture.NumberFormat, out num)) throw new ProtocolViolationException($"Expecting fail Double '{msgtype}1.23', got '{msgtype}{line}'");
// break;
// }
// return new RedisResult(num, false, msgtype);
Expand Down
62 changes: 31 additions & 31 deletions src/FreeRedis/Internal/RespHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ byte[] ReadClob()
{
if (destination == null) destination = ms = new MemoryStream();
var lenstr = ReadLine(null);
if (int.TryParse(lenstr, out var len))
if (int.TryParse(lenstr, NumberStyles.Any, CultureInfo.InvariantCulture.NumberFormat, out var len))
{
if (len < 0) return null;
if (len > 0) Read(destination, len, bufferSize);
Expand All @@ -67,7 +67,7 @@ byte[] ReadClob()
char c = (char)_stream.ReadByte();
if (c != ';') throw new ProtocolViolationException($"Expecting fail Streamed strings ';', got '{c}'");
var clenstr = ReadLine(null);
if (int.TryParse(clenstr, out var clen))
if (int.TryParse(clenstr, NumberStyles.Any, CultureInfo.InvariantCulture.NumberFormat, out var clen))
{
if (clen == 0) break;
if (clen > 0)
Expand Down Expand Up @@ -113,13 +113,13 @@ string ReadSimpleString()
long ReadNumber(char msgtype)
{
var numstr = ReadLine(null);
if (long.TryParse(numstr, out var num)) return num;
if (long.TryParse(numstr, NumberStyles.Any, CultureInfo.InvariantCulture.NumberFormat, out var num)) return num;
throw new ProtocolViolationException($"Expecting fail Number '{msgtype}0', got '{msgtype}{numstr}'");
}
BigInteger ReadBigNumber(char msgtype)
{
var numstr = ReadLine(null);
if (BigInteger.TryParse(numstr, NumberStyles.Any, null, out var num)) return num;
if (BigInteger.TryParse(numstr, NumberStyles.Any, CultureInfo.InvariantCulture.NumberFormat, out var num)) return num;
throw new ProtocolViolationException($"Expecting fail BigNumber '{msgtype}0', got '{msgtype}{numstr}'");
}
double ReadDouble(char msgtype)
Expand All @@ -130,7 +130,7 @@ double ReadDouble(char msgtype)
case "inf": return double.PositiveInfinity;
case "-inf": return double.NegativeInfinity;
}
if (double.TryParse(numstr, NumberStyles.Any, null, out var num)) return num;
if (double.TryParse(numstr, NumberStyles.Any, CultureInfo.InvariantCulture.NumberFormat, out var num)) return num;
throw new ProtocolViolationException($"Expecting fail Double '{msgtype}1.23', got '{msgtype}{numstr}'");
}
bool ReadBoolean(char msgtype)
Expand All @@ -147,7 +147,7 @@ bool ReadBoolean(char msgtype)
object[] ReadArray(char msgtype, Encoding encoding)
{
var lenstr = ReadLine(null);
if (int.TryParse(lenstr, out var len))
if (int.TryParse(lenstr, NumberStyles.Any, CultureInfo.InvariantCulture.NumberFormat, out var len))
{
if (len < 0) return null;
var arr = new object[len];
Expand All @@ -172,7 +172,7 @@ object[] ReadArray(char msgtype, Encoding encoding)
object[] ReadMap(char msgtype, Encoding encoding)
{
var lenstr = ReadLine(null);
if (int.TryParse(lenstr, out var len))
if (int.TryParse(lenstr, NumberStyles.Any, CultureInfo.InvariantCulture.NumberFormat, out var len))
{
if (len < 0) return null;
var arr = new object[len * 2];
Expand Down Expand Up @@ -900,28 +900,28 @@ public static object FromObject(this Type targetType, object value, Encoding enc
}
return null;
};
if (tt == typeof(byte)) return vs => vs == null ? 0 : (byte.TryParse(vs, NumberStyles.Any, null, out var tryval) ? tryval : 0);
if (tt == typeof(byte?)) return vs => vs == null ? null : (byte.TryParse(vs, NumberStyles.Any, null, out var tryval) ? (byte?)tryval : null);
if (tt == typeof(decimal)) return vs => vs == null ? 0 : (decimal.TryParse(vs, NumberStyles.Any, null, out var tryval) ? tryval : 0);
if (tt == typeof(decimal?)) return vs => vs == null ? null : (decimal.TryParse(vs, NumberStyles.Any, null, out var tryval) ? (decimal?)tryval : null);
if (tt == typeof(double)) return vs => vs == null ? 0 : (double.TryParse(vs, NumberStyles.Any, null, out var tryval) ? tryval : 0);
if (tt == typeof(double?)) return vs => vs == null ? null : (double.TryParse(vs, NumberStyles.Any, null, out var tryval) ? (double?)tryval : null);
if (tt == typeof(float)) return vs => vs == null ? 0 : (float.TryParse(vs, NumberStyles.Any, null, out var tryval) ? tryval : 0);
if (tt == typeof(float?)) return vs => vs == null ? null : (float.TryParse(vs, NumberStyles.Any, null, out var tryval) ? (float?)tryval : null);
if (tt == typeof(int)) return vs => vs == null ? 0 : (int.TryParse(vs, NumberStyles.Any, null, out var tryval) ? tryval : 0);
if (tt == typeof(int?)) return vs => vs == null ? null : (int.TryParse(vs, NumberStyles.Any, null, out var tryval) ? (int?)tryval : null);
if (tt == typeof(long)) return vs => vs == null ? 0 : (long.TryParse(vs, NumberStyles.Any, null, out var tryval) ? tryval : 0);
if (tt == typeof(long?)) return vs => vs == null ? null : (long.TryParse(vs, NumberStyles.Any, null, out var tryval) ? (long?)tryval : null);
if (tt == typeof(sbyte)) return vs => vs == null ? 0 : (sbyte.TryParse(vs, NumberStyles.Any, null, out var tryval) ? tryval : 0);
if (tt == typeof(sbyte?)) return vs => vs == null ? null : (sbyte.TryParse(vs, NumberStyles.Any, null, out var tryval) ? (sbyte?)tryval : null);
if (tt == typeof(short)) return vs => vs == null ? 0 : (short.TryParse(vs, NumberStyles.Any, null, out var tryval) ? tryval : 0);
if (tt == typeof(short?)) return vs => vs == null ? null : (short.TryParse(vs, NumberStyles.Any, null, out var tryval) ? (short?)tryval : null);
if (tt == typeof(uint)) return vs => vs == null ? 0 : (uint.TryParse(vs, NumberStyles.Any, null, out var tryval) ? tryval : 0);
if (tt == typeof(uint?)) return vs => vs == null ? null : (uint.TryParse(vs, NumberStyles.Any, null, out var tryval) ? (uint?)tryval : null);
if (tt == typeof(ulong)) return vs => vs == null ? 0 : (ulong.TryParse(vs, NumberStyles.Any, null, out var tryval) ? tryval : 0);
if (tt == typeof(ulong?)) return vs => vs == null ? null : (ulong.TryParse(vs, NumberStyles.Any, null, out var tryval) ? (ulong?)tryval : null);
if (tt == typeof(ushort)) return vs => vs == null ? 0 : (ushort.TryParse(vs, NumberStyles.Any, null, out var tryval) ? tryval : 0);
if (tt == typeof(ushort?)) return vs => vs == null ? null : (ushort.TryParse(vs, NumberStyles.Any, null, out var tryval) ? (ushort?)tryval : null);
if (tt == typeof(byte)) return vs => vs == null ? 0 : (byte.TryParse(vs, NumberStyles.Any, CultureInfo.InvariantCulture.NumberFormat, out var tryval) ? tryval : 0);
if (tt == typeof(byte?)) return vs => vs == null ? null : (byte.TryParse(vs, NumberStyles.Any, CultureInfo.InvariantCulture.NumberFormat, out var tryval) ? (byte?)tryval : null);
if (tt == typeof(decimal)) return vs => vs == null ? 0 : (decimal.TryParse(vs, NumberStyles.Any, CultureInfo.InvariantCulture.NumberFormat, out var tryval) ? tryval : 0);
if (tt == typeof(decimal?)) return vs => vs == null ? null : (decimal.TryParse(vs, NumberStyles.Any, CultureInfo.InvariantCulture.NumberFormat, out var tryval) ? (decimal?)tryval : null);
if (tt == typeof(double)) return vs => vs == null ? 0 : (double.TryParse(vs, NumberStyles.Any, CultureInfo.InvariantCulture.NumberFormat, out var tryval) ? tryval : 0);
if (tt == typeof(double?)) return vs => vs == null ? null : (double.TryParse(vs, NumberStyles.Any, CultureInfo.InvariantCulture.NumberFormat, out var tryval) ? (double?)tryval : null);
if (tt == typeof(float)) return vs => vs == null ? 0 : (float.TryParse(vs, NumberStyles.Any, CultureInfo.InvariantCulture.NumberFormat, out var tryval) ? tryval : 0);
if (tt == typeof(float?)) return vs => vs == null ? null : (float.TryParse(vs, NumberStyles.Any, CultureInfo.InvariantCulture.NumberFormat, out var tryval) ? (float?)tryval : null);
if (tt == typeof(int)) return vs => vs == null ? 0 : (int.TryParse(vs, NumberStyles.Any, CultureInfo.InvariantCulture.NumberFormat, out var tryval) ? tryval : 0);
if (tt == typeof(int?)) return vs => vs == null ? null : (int.TryParse(vs, NumberStyles.Any, CultureInfo.InvariantCulture.NumberFormat, out var tryval) ? (int?)tryval : null);
if (tt == typeof(long)) return vs => vs == null ? 0 : (long.TryParse(vs, NumberStyles.Any, CultureInfo.InvariantCulture.NumberFormat, out var tryval) ? tryval : 0);
if (tt == typeof(long?)) return vs => vs == null ? null : (long.TryParse(vs, NumberStyles.Any, CultureInfo.InvariantCulture.NumberFormat, out var tryval) ? (long?)tryval : null);
if (tt == typeof(sbyte)) return vs => vs == null ? 0 : (sbyte.TryParse(vs, NumberStyles.Any, CultureInfo.InvariantCulture.NumberFormat, out var tryval) ? tryval : 0);
if (tt == typeof(sbyte?)) return vs => vs == null ? null : (sbyte.TryParse(vs, NumberStyles.Any, CultureInfo.InvariantCulture.NumberFormat, out var tryval) ? (sbyte?)tryval : null);
if (tt == typeof(short)) return vs => vs == null ? 0 : (short.TryParse(vs, NumberStyles.Any, CultureInfo.InvariantCulture.NumberFormat, out var tryval) ? tryval : 0);
if (tt == typeof(short?)) return vs => vs == null ? null : (short.TryParse(vs, NumberStyles.Any, CultureInfo.InvariantCulture.NumberFormat, out var tryval) ? (short?)tryval : null);
if (tt == typeof(uint)) return vs => vs == null ? 0 : (uint.TryParse(vs, NumberStyles.Any, CultureInfo.InvariantCulture.NumberFormat, out var tryval) ? tryval : 0);
if (tt == typeof(uint?)) return vs => vs == null ? null : (uint.TryParse(vs, NumberStyles.Any, CultureInfo.InvariantCulture.NumberFormat, out var tryval) ? (uint?)tryval : null);
if (tt == typeof(ulong)) return vs => vs == null ? 0 : (ulong.TryParse(vs, NumberStyles.Any, CultureInfo.InvariantCulture.NumberFormat, out var tryval) ? tryval : 0);
if (tt == typeof(ulong?)) return vs => vs == null ? null : (ulong.TryParse(vs, NumberStyles.Any, CultureInfo.InvariantCulture.NumberFormat, out var tryval) ? (ulong?)tryval : null);
if (tt == typeof(ushort)) return vs => vs == null ? 0 : (ushort.TryParse(vs, NumberStyles.Any, CultureInfo.InvariantCulture.NumberFormat, out var tryval) ? tryval : 0);
if (tt == typeof(ushort?)) return vs => vs == null ? null : (ushort.TryParse(vs, NumberStyles.Any, CultureInfo.InvariantCulture.NumberFormat, out var tryval) ? (ushort?)tryval : null);
if (tt == typeof(DateTime)) return vs => vs == null ? DateTime.MinValue : (DateTime.TryParse(vs, out var tryval) ? tryval : DateTime.MinValue);
if (tt == typeof(DateTime?)) return vs => vs == null ? null : (DateTime.TryParse(vs, out var tryval) ? (DateTime?)tryval : null);
if (tt == typeof(DateTimeOffset)) return vs => vs == null ? DateTimeOffset.MinValue : (DateTimeOffset.TryParse(vs, out var tryval) ? tryval : DateTimeOffset.MinValue);
Expand All @@ -930,8 +930,8 @@ public static object FromObject(this Type targetType, object value, Encoding enc
if (tt == typeof(TimeSpan?)) return vs => vs == null ? null : (TimeSpan.TryParse(vs, out var tryval) ? (TimeSpan?)tryval : null);
if (tt == typeof(Guid)) return vs => vs == null ? Guid.Empty : (Guid.TryParse(vs, out var tryval) ? tryval : Guid.Empty);
if (tt == typeof(Guid?)) return vs => vs == null ? null : (Guid.TryParse(vs, out var tryval) ? (Guid?)tryval : null);
if (tt == typeof(BigInteger)) return vs => vs == null ? 0 : (BigInteger.TryParse(vs, NumberStyles.Any, null, out var tryval) ? tryval : 0);
if (tt == typeof(BigInteger?)) return vs => vs == null ? null : (BigInteger.TryParse(vs, NumberStyles.Any, null, out var tryval) ? (BigInteger?)tryval : null);
if (tt == typeof(BigInteger)) return vs => vs == null ? 0 : (BigInteger.TryParse(vs, NumberStyles.Any, CultureInfo.InvariantCulture.NumberFormat, out var tryval) ? tryval : 0);
if (tt == typeof(BigInteger?)) return vs => vs == null ? null : (BigInteger.TryParse(vs, NumberStyles.Any, CultureInfo.InvariantCulture.NumberFormat, out var tryval) ? (BigInteger?)tryval : null);
if (tt.NullableTypeOrThis().IsEnum)
{
var tttype = tt.NullableTypeOrThis();
Expand Down
9 changes: 5 additions & 4 deletions src/FreeRedis/Internal/RespHelperAsync.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#if isasync
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Net;
using System.Text;
Expand Down Expand Up @@ -38,7 +39,7 @@ async Task<byte[]> ReadClobAsync()
{
if (destination == null) destination = ms = new MemoryStream();
var lenstr = ReadLine(null);
if (int.TryParse(lenstr, out var len))
if (int.TryParse(lenstr, NumberStyles.Any, CultureInfo.InvariantCulture.NumberFormat, out var len))
{
if (len < 0) return null;
if (len > 0) await ReadAsync(destination, len, bufferSize);
Expand All @@ -53,7 +54,7 @@ async Task<byte[]> ReadClobAsync()
char c = (char)_stream.ReadByte();
if (c != ';') throw new ProtocolViolationException($"Expecting fail Streamed strings ';', got '{c}'");
var clenstr = ReadLine(null);
if (int.TryParse(clenstr, out var clen))
if (int.TryParse(clenstr, NumberStyles.Any, CultureInfo.InvariantCulture.NumberFormat, out var clen))
{
if (clen == 0) break;
if (clen > 0)
Expand All @@ -80,7 +81,7 @@ async Task<byte[]> ReadClobAsync()
async Task<object[]> ReadArrayAsync(char msgtype, Encoding encoding)
{
var lenstr = ReadLine(null);
if (int.TryParse(lenstr, out var len))
if (int.TryParse(lenstr, NumberStyles.Any, CultureInfo.InvariantCulture.NumberFormat, out var len))
{
if (len < 0) return null;
var arr = new object[len];
Expand All @@ -105,7 +106,7 @@ async Task<object[]> ReadArrayAsync(char msgtype, Encoding encoding)
async Task<object[]> ReadMapAsync(char msgtype, Encoding encoding)
{
var lenstr = ReadLine(null);
if (int.TryParse(lenstr, out var len))
if (int.TryParse(lenstr, NumberStyles.Any, CultureInfo.InvariantCulture.NumberFormat, out var len))
{
if (len < 0) return null;
var arr = new object[len * 2];
Expand Down
Loading

0 comments on commit 55b37a0

Please sign in to comment.