Skip to content

Commit

Permalink
Fix merge conflict
Browse files Browse the repository at this point in the history
  • Loading branch information
peteroupc committed Oct 20, 2023
2 parents 75d81ee + a32a3b3 commit 3e33574
Show file tree
Hide file tree
Showing 36 changed files with 439 additions and 524 deletions.
2 changes: 2 additions & 0 deletions CBOR/CBOR.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,6 @@ Note that after version 4.5x, the CBOR library's repository will stop including

<PackageReference Include="PeterO.DataUtilities" Version="1.1" />
<PackageReference Include="Microsoft.CodeAnalysis.NetAnalyzers" PrivateAssets="All" Version="7.0.3" /></ItemGroup>
<PackageReference Include='PeterO.DataUtilities' Version='1.1.0'/>
<PackageReference Include='Microsoft.CodeAnalysis.NetAnalyzers' PrivateAssets='All' Version='7.0.3'/></ItemGroup>
</Project>
2 changes: 1 addition & 1 deletion CBOR/PeterO/Cbor/CBORDataUtilitiesTextString.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ internal static CBORObject ParseJSONNumber(
return null;
}

_ = options ?? CBORDataUtilities.DefaultOptions;
options = options ?? CBORDataUtilities.DefaultOptions;
bool preserveNegativeZero = options.PreserveNegativeZero;
JSONOptions.ConversionMode kind = options.NumberConversion;
int endPos = offset + count;
Expand Down
40 changes: 21 additions & 19 deletions CBOR/PeterO/Cbor/CBORDateConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -168,9 +168,7 @@ public DateTime FromCBORObject(CBORObject obj) {
/// cannot be less than -1439 or greater than 1439. For tags 0 and 1,
/// this value is always 0.</item></list>.</param>
/// <returns>Either <c>true</c> if the method is successful, or
/// <c>false</c> otherwise. Returns <c>false</c> if the parameter
/// <paramref name='year'/> or <paramref name='lesserFields'/> is null,
/// or contains fewer elements than required.</returns>
/// <c>false</c> otherwise.</returns>
public bool TryGetDateTimeFields(CBORObject obj, EInteger[] year, int[]
lesserFields) {
if (year == null) {
Expand Down Expand Up @@ -225,7 +223,8 @@ private string TryGetDateTimeFieldsInternal(
return "\"lesserFields\" + \"'s length\" (" +
lesserFields.Length + ") is not greater or equal to 7";
}
if (this.Type == ConversionType.UntaggedNumber) {
ConversionType thisType = this.Type;
if (thisType == ConversionType.UntaggedNumber) {
if (obj.IsTagged) {
return "May not be tagged";
}
Expand All @@ -237,8 +236,8 @@ private string TryGetDateTimeFieldsInternal(
if (!num.IsFinite()) {
return "Not a finite number";
}
if (num.CompareTo(long.MinValue) < 0 ||
num.CompareTo(long.MaxValue) > 0) {
if (num.CompareTo(Int64.MinValue) < 0 ||
num.CompareTo(Int64.MaxValue) > 0) {
return "Too big or small to fit a DateTime";
}
if (num.CanFitInInt64()) {
Expand Down Expand Up @@ -389,7 +388,8 @@ public CBORObject DateTimeFieldsToCBORObject(EInteger bigYear, int[]
}
try {
CBORUtilities.CheckYearAndLesserFields(bigYear, lesserFields);
switch (this.Type) {
ConversionType thisType = this.Type;
switch (thisType) {
case ConversionType.TaggedString:
{
string str = CBORUtilities.ToAtomDateTimeString(bigYear,
Expand All @@ -404,21 +404,23 @@ public CBORObject DateTimeFieldsToCBORObject(EInteger bigYear, int[]
bigYear,
lesserFields,
status);
return status[0] == 0 ?
this.Type == ConversionType.TaggedNumber ?
CBORObject.FromEInteger(ef.ToEInteger()).WithTag(1) :
CBORObject.FromEInteger(ef.ToEInteger()) : status[0] == 1 ?
this.Type == ConversionType.TaggedNumber ?
CBORObject.FromFloatingPointBits(ef.ToDoubleBits(), 8)
.WithTag(1) :
CBORObject.FromFloatingPointBits(ef.ToDoubleBits(), 8) :
throw new CBORException("Too big or small to fit an" +
"\u0020integer" + "\u0020or floating-point number");
switch (status[0]) {
case 0:
return thisType == ConversionType.TaggedNumber ?
CBORObject.FromObjectAndTag(ef.ToEInteger(), 1) :
CBORObject.FromObject(ef.ToEInteger());
case 1:
return thisType == ConversionType.TaggedNumber ?
CBORObject.FromFloatingPointBits(ef.ToDoubleBits(), 8)
.WithTag(1) :
CBORObject.FromFloatingPointBits(ef.ToDoubleBits(), 8);
default: throw new CBORException("Too big or small to fit an" +
"\u0020integer" + "\u0020or floating-point number");
}
} catch (ArgumentException ex) {
throw new CBORException(ex.Message, ex);
}
default:
throw new CBORException("Internal error");
default: throw new CBORException("Internal error");
}
} catch (ArgumentException ex) {
throw new CBORException(ex.Message, ex);
Expand Down
21 changes: 10 additions & 11 deletions CBOR/PeterO/Cbor/CBORJson.cs
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,6 @@ private CBORObject NextJSONNegativeNumber(
this.RaiseError("JSON number can't be parsed.");
}

_ = -(c - '0');
int cstart = c;
c = this.ReadChar();
this.sb = this.sb ?? new StringBuilder();
Expand Down Expand Up @@ -279,8 +278,8 @@ private CBORObject NextJSONValue(
case 't':
{
// Parse true
if ((_ = this.ReadChar()) != 'r' || (_ = this.ReadChar()) != 'u' ||
(_ = this.ReadChar()) != 'e') {
if (this.ReadChar() != 'r' || this.ReadChar() != 'u' ||
this.ReadChar() != 'e') {
this.RaiseError("Value can't be parsed.");
}
c = this.ReadChar();
Expand All @@ -298,8 +297,8 @@ private CBORObject NextJSONValue(
case 'f':
{
// Parse false
if ((_ = this.ReadChar()) != 'a' || (_ = this.ReadChar()) != 'l' ||
(_ = this.ReadChar()) != 's' || (_ = this.ReadChar()) != 'e') {
if (this.ReadChar() != 'a' || this.ReadChar() != 'l' ||
this.ReadChar() != 's' || this.ReadChar() != 'e') {
this.RaiseError("Value can't be parsed.");
}
c = this.ReadChar();
Expand All @@ -317,8 +316,8 @@ private CBORObject NextJSONValue(
case 'n':
{
// Parse null
if ((_ = this.ReadChar()) != 'u' || (_ = this.ReadChar()) != 'l' ||
(_ = this.ReadChar()) != 'l') {
if (this.ReadChar() != 'u' || this.ReadChar() != 'l' ||
this.ReadChar() != 'l') {
this.RaiseError("Value can't be parsed.");
}
c = this.ReadChar();
Expand Down Expand Up @@ -540,7 +539,7 @@ internal static CBORObject[] ParseJSONSequence(
// a truncated JSON text
return new CBORObject[] { null };
}
var list = new List<CBORObject>();
var cborList = new List<CBORObject>();
while (true) {
CBORObject co;
try {
Expand All @@ -554,7 +553,7 @@ internal static CBORObject[] ParseJSONSequence(
cj.SkipToEnd();
co = null;
}
list.Add(co);
cborList.Add(co);
if (!cj.recordSeparatorSeen) {
// End of the stream was reached
nextChar[0] = -1;
Expand All @@ -567,12 +566,12 @@ internal static CBORObject[] ParseJSONSequence(
if (nextChar[0] < 0) {
// Rest of stream had only record separators, so we found
// a truncated JSON text
list.Add(null);
cborList.Add(null);
break;
}
}
}
return list.ToArray();
return cborList.ToArray();
}

private CBORObject ParseJSONObject(int depth) {
Expand Down
5 changes: 2 additions & 3 deletions CBOR/PeterO/Cbor/CBORJson3.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,16 +61,15 @@ private string NextJSONString() {
}
}
this.sb = this.sb ?? new StringBuilder();
_ = this.sb.Remove(0, this.sb.Length);
_ = this.sb.Append(js, startIndex, endIndex - startIndex);
this.sb.Remove(0, this.sb.Length);
this.sb.Append(js, startIndex, endIndex - startIndex);
while (true) {
c = this.index < ep ? js[this.index++] & 0xffff : -1;
if (c == -1 || c < 0x20) {
this.RaiseError("Unterminated string");
}
switch (c) {
case '\\':
_ = this.index - 1;
c = this.index < ep ? js[this.index++] & 0xffff : -1;
switch (c) {
case '\\':
Expand Down
58 changes: 33 additions & 25 deletions CBOR/PeterO/Cbor/CBORNumber.cs
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,6 @@ private static CBORNumber RationalToNumber(
if (denominator.Sign <= 0) {
return null; // "Denominator may not be negative or zero");
}
var erat = ERational.Create(numerator, denominator);
if (tagName == 270) {
if (numerator.Sign < 0) {
return null; // "Numerator may not be negative");
Expand All @@ -213,11 +212,13 @@ private static CBORNumber RationalToNumber(
return null; // "Invalid options";
}
int options = o[2].AsInt32Value();
ERational erat = null;
switch (options) {
case 0:
erat = ERational.Create(numerator, denominator);
break;
case 1:
erat = erat.Negate();
erat = ERational.Create(numerator, denominator).Negate();
break;
case 2:
if (!numerator.IsZero || denominator.CompareTo(1) != 0) {
Expand Down Expand Up @@ -245,8 +246,10 @@ private static CBORNumber RationalToNumber(
break;
default: return null; // "Invalid options");
}
return CBORNumber.FromObject(erat);
} else {
return CBORNumber.FromObject(ERational.Create(numerator, denominator));
}
return CBORNumber.FromERational(erat);
}

private static bool CheckRationalToNumber(
Expand Down Expand Up @@ -632,11 +635,15 @@ public byte ToByteUnchecked() {
/// not-a-number, is not an exact integer, or is less than 0 or greater
/// than 255.</exception>
public byte ToByteIfExact() {
return !this.IsFinite() ?
throw new OverflowException("Value is infinity or NaN") :
this.IsZero() ? (byte)0 : this.IsNegative() ? throw new
OverflowException("Value out of range") :
this.ToEIntegerIfExact().ToByteChecked();
if (!this.IsFinite()) {
throw new OverflowException("Value is infinity or NaN");
}
if (this.IsZero()) {
return (byte)0;
}
return this.IsNegative() ? throw new
OverflowException("Value out of range") :
this.ToEIntegerIfExact().ToByteChecked();
}

/// <summary>Converts a byte (from 0 to 255) to an arbitrary-precision
Expand Down Expand Up @@ -935,7 +942,7 @@ public bool CanFitInInt32() {
return false;
}
long v = icn.AsInt64(gv);
return v >= int.MinValue && v <= int.MaxValue;
return v >= Int32.MinValue && v <= Int32.MaxValue;
}

/// <summary>Returns whether this object's numerical value is an
Expand Down Expand Up @@ -1046,14 +1053,15 @@ public CBORNumber Negate() {
var longValue = (long)this.value;
return longValue == 0 ? FromEDecimal(EDecimal.NegativeZero) :
longValue == long.MinValue ?
FromEInteger(EInteger.FromInt64(longValue).Negate()) : new
CBORNumber(this.Kind, -longValue);
FromEInteger(EInteger.FromInt64(longValue).Negate()) : new
CBORNumber(this.Kind, -longValue);
}
case NumberKind.EInteger:
{
var eiValue = (EInteger)this.value;
return eiValue.IsZero ? FromEDecimal(EDecimal.NegativeZero) :
FromEInteger(eiValue.Negate());
return eiValue.IsZero ?
FromEDecimal(EDecimal.NegativeZero) :
FromEInteger(eiValue.Negate());
}
default:
return new CBORNumber(this.Kind,
Expand Down Expand Up @@ -1129,8 +1137,8 @@ public CBORNumber Add(CBORNumber b) {
if (typeA == NumberKind.Integer && typeB == NumberKind.Integer) {
var valueA = (long)objA;
var valueB = (long)objB;
if ((valueA < 0 && valueB < long.MinValue - valueA) ||
(valueA > 0 && valueB > long.MaxValue - valueA)) {
if ((valueA < 0 && valueB < Int64.MinValue - valueA) ||
(valueA > 0 && valueB > Int64.MaxValue - valueA)) {
// would overflow, convert to EInteger
return CBORNumber.FromEInteger(
EInteger.FromInt64(valueA).Add(EInteger.FromInt64(valueB)));
Expand Down Expand Up @@ -1203,8 +1211,8 @@ public CBORNumber Subtract(CBORNumber b) {
if (typeA == NumberKind.Integer && typeB == NumberKind.Integer) {
var valueA = (long)objA;
var valueB = (long)objB;
if ((valueB < 0 && long.MaxValue + valueB < valueA) ||
(valueB > 0 && long.MinValue + valueB > valueA)) {
if ((valueB < 0 && Int64.MaxValue + valueB < valueA) ||
(valueB > 0 && Int64.MinValue + valueB > valueA)) {
// would overflow, convert to EInteger
return CBORNumber.FromEInteger(
EInteger.FromInt64(valueA).Subtract(EInteger.FromInt64(
Expand Down Expand Up @@ -1242,9 +1250,9 @@ public CBORNumber Subtract(CBORNumber b) {
} else {
// DebugUtility.Log("type=" + typeA + "/" + typeB + " finite=" +
// (// this.IsFinite()) + "/" + (b.IsFinite()));
EInteger b1 = GetNumberInterface(typeA).AsEInteger(objA);
EInteger b2 = GetNumberInterface(typeB).AsEInteger(objB);
return new CBORNumber(NumberKind.EInteger, b1 - b2);
EInteger b1 = GetNumberInterface(typeA).AsEInteger(objA)
.Subtract(GetNumberInterface(typeB).AsEInteger(objB));
return new CBORNumber(NumberKind.EInteger, b1);
}
}

Expand Down Expand Up @@ -1274,11 +1282,11 @@ public CBORNumber Multiply(CBORNumber b) {
bool apos = valueA > 0L;
bool bpos = valueB > 0L;
if (
(apos && ((!bpos && (long.MinValue / valueA) > valueB) ||
(bpos && valueA > (long.MaxValue / valueB)))) ||
(apos && ((!bpos && (Int64.MinValue / valueA) > valueB) ||
(bpos && valueA > (Int64.MaxValue / valueB)))) ||
(!apos && ((!bpos && valueA != 0L &&
(long.MaxValue / valueA) > valueB) ||
(bpos && valueA < (long.MinValue / valueB))))) {
(Int64.MaxValue / valueA) > valueB) ||
(bpos && valueA < (Int64.MinValue / valueB))))) {
// would overflow, convert to EInteger
var bvalueA = (EInteger)valueA;
var bvalueB = (EInteger)valueB;
Expand Down Expand Up @@ -1342,7 +1350,7 @@ public CBORNumber Divide(CBORNumber b) {

FromEDecimal(EDecimal.PositiveInfinity));
}
if (valueA == long.MinValue && valueB == -1) {
if (valueA == Int64.MinValue && valueB == -1) {
return new CBORNumber(NumberKind.Integer, valueA).Negate();
}
long quo = valueA / valueB;
Expand Down
Loading

0 comments on commit 3e33574

Please sign in to comment.