diff --git a/plist-cil.test/UIDTests.cs b/plist-cil.test/UIDTests.cs
index fa35bca..43441ec 100644
--- a/plist-cil.test/UIDTests.cs
+++ b/plist-cil.test/UIDTests.cs
@@ -9,65 +9,57 @@ namespace plistcil.test
[Fact]
public void ByteUidTest()
{
- var uid = new UID("byte", (byte)0xAB);
+ var uid = new UID((byte)0xAB);
Assert.Equal(new byte[] { 0xAB }, uid.Bytes);
- Assert.Equal("byte", uid.Name);
}
[Fact]
public void SByteUidTest()
{
- var uid = new UID("sbyte", unchecked((sbyte)0xAB));
- Assert.Equal(new byte[] { 0xAB }, uid.Bytes);
- Assert.Equal("sbyte", uid.Name);
+ var uid = new UID("test", unchecked((sbyte)0x0F));
+ Assert.Equal(new byte[] { 0x0F }, uid.Bytes);
}
[Fact]
public void ShortUidTest()
{
- var uid = new UID("short", unchecked((short)0xABCD));
- Assert.Equal(new byte[] { 0xAB, 0xCD }, uid.Bytes);
- Assert.Equal("short", uid.Name);
+ var uid = new UID("test", unchecked((short)0x0F0F));
+ Assert.Equal(new byte[] { 0x0F, 0x0F }, uid.Bytes);
}
[Fact]
public void UShortUidTest()
{
- var uid = new UID("ushort", 0xABCDu);
+ var uid = new UID(0xABCDu);
Assert.Equal(new byte[] { 0xAB, 0xCD }, uid.Bytes);
- Assert.Equal("ushort", uid.Name);
}
[Fact]
public void UIntUidTest()
{
- var uid = new UID("uint", 0xABCDEF00u);
+ var uid = new UID(0xABCDEF00u);
Assert.Equal(new byte[] { 0xAB, 0xCD, 0xEF, 0x00 }, uid.Bytes);
- Assert.Equal("uint", uid.Name);
}
[Fact]
public void IntUidTest()
{
- var uid = new UID("int", 0xABCDEF00);
+ var uid = new UID(0xABCDEF00);
Assert.Equal(new byte[] { 0xAB, 0xCD, 0xEF, 0x00 }, uid.Bytes);
- Assert.Equal("int", uid.Name);
}
[Fact]
public void ULongUidTest()
{
- var uid = new UID("ulong", 0xABCDEF0000EFCDABu);
+ var uid = new UID(0xABCDEF0000EFCDABu);
Assert.Equal(new byte[] { 0xAB, 0xCD, 0xEF, 0x00, 0x00, 0xEF, 0xCD, 0xAB }, uid.Bytes);
- Assert.Equal("ulong", uid.Name);
}
[Fact]
public void LongUidTest()
{
- var uid = new UID("int", 0xABCDEF0000EFCDAB);
+ var uid = new UID(0xABCDEF0000EFCDAB);
Assert.Equal(new byte[] { 0xAB, 0xCD, 0xEF, 0x00, 0x00, 0xEF, 0xCD, 0xAB }, uid.Bytes);
- Assert.Equal("int", uid.Name);
}
[Theory]
@@ -75,24 +67,16 @@ namespace plistcil.test
[InlineData(new byte[] { 0xAB, 0xCD })]
[InlineData(new byte[] { 0xAB, 0xCD, 0xEF, 0xFE })]
[InlineData(new byte[] { 0xAB, 0xCD, 0xEF, 0xFE, 0xFE, 0xEF, 0xCD, 0xAB })]
- [InlineData(new byte[] { 0x00 })]
- [InlineData(new byte[] { 0x00, 0x00 })]
- [InlineData(new byte[] { 0x00, 0xCD })]
- [InlineData(new byte[] { 0x00, 0x00, 0x00, 0x00 })]
- [InlineData(new byte[] { 0x00, 0x00, 0x00, 0xCD })]
- [InlineData(new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 })]
- [InlineData(new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xCD })]
public void UidFromArrayTest(byte[] array)
{
- var uid = new UID("array", array);
+ var uid = new UID(array);
Assert.Equal(array, uid.Bytes);
- Assert.Equal("array", uid.Name);
}
[Fact]
public void BinaryRoundTripTest()
{
- var original = new UID("0", 0xabcd);
+ var original = new UID(0xabcd);
using (MemoryStream stream = new MemoryStream())
{
@@ -106,14 +90,14 @@ namespace plistcil.test
[Fact]
public void XmlRoundTripTest()
{
- var original = new UID("0", 0xabcd);
+ var original = new UID(0xabcd);
var plist = original.ToXmlPropertyList();
// UIDs don't exist in XML property lists, but they are represented as strings
// for compability purposes
var roundtrip = XmlPropertyListParser.ParseString(plist) as NSString;
- Assert.Equal("0000abcd", roundtrip.ToObject());
+ Assert.Equal("abcd", roundtrip.ToObject());
}
}
}
diff --git a/plist-cil/BinaryPropertyListParser.cs b/plist-cil/BinaryPropertyListParser.cs
index a3c3653..acb98a3 100644
--- a/plist-cil/BinaryPropertyListParser.cs
+++ b/plist-cil/BinaryPropertyListParser.cs
@@ -316,7 +316,7 @@ namespace Claunia.PropertyList
{
//UID (v1.0 and later)
int length = objInfo + 1;
- return new UID(obj.ToString(), bytes.Slice(offset + 1, length));
+ return new UID(bytes.Slice(offset + 1, length));
}
case 0xA:
{
diff --git a/plist-cil/UID.cs b/plist-cil/UID.cs
index 665bc77..3263995 100644
--- a/plist-cil/UID.cs
+++ b/plist-cil/UID.cs
@@ -36,20 +36,28 @@ namespace Claunia.PropertyList
public class UID : NSObject
{
readonly ulong value;
- readonly byte length;
- readonly string name;
///
/// Initializes a new instance of the class.
///
/// Name.
/// Bytes.
+ [Obsolete("UIDs have not meaningful names")]
public UID(String name, ReadOnlySpan bytes)
{
if (bytes.Length != 1 && bytes.Length != 2 && bytes.Length != 4 && bytes.Length != 8)
throw new ArgumentException("Type argument is not valid.");
- this.name = name;
- this.length = (byte)bytes.Length;
+ this.value = (ulong)BinaryPropertyListParser.ParseLong(bytes);
+ }
+
+ ///
+ /// Initializes a new instance of the class.
+ ///
+ /// Bytes.
+ public UID(ReadOnlySpan bytes)
+ {
+ if (bytes.Length != 1 && bytes.Length != 2 && bytes.Length != 4 && bytes.Length != 8)
+ throw new ArgumentException("Type argument is not valid.");
this.value = (ulong)BinaryPropertyListParser.ParseLong(bytes);
}
@@ -58,11 +66,19 @@ namespace Claunia.PropertyList
///
/// Name.
/// Unsigned 8-bit number.
+ [Obsolete("UIDs have no meaningful names")]
public UID(String name, byte number)
{
- this.name = name;
this.value = number;
- this.length = 1;
+ }
+
+ ///
+ /// Initializes a new instance of the class using an unsigned 8-bit number.
+ ///
+ /// Unsigned 8-bit number.
+ public UID(byte number)
+ {
+ this.value = number;
}
///
@@ -70,11 +86,10 @@ namespace Claunia.PropertyList
///
/// Name.
/// Unsigned 8-bit number.
+ [Obsolete("UIDs must be unsigned values")]
public UID(String name, sbyte number)
{
- this.name = name;
this.value = (ulong)number;
- this.length = 1;
}
///
@@ -82,16 +97,20 @@ namespace Claunia.PropertyList
///
/// Name.
/// Unsigned 16-bit number.
+ [Obsolete("UIDs have no meaningful names")]
public UID(String name, ushort number)
{
- this.name = name;
this.value = number;
- if (number <= byte.MaxValue)
- this.length = 1;
- else
- {
- this.length = 2;
- }
+ }
+
+ ///
+ /// Initializes a new instance of the class using an unsigned 16-bit number.
+ ///
+ /// Name.
+ /// Unsigned 16-bit number.
+ public UID(ushort number)
+ {
+ this.value = number;
}
///
@@ -99,16 +118,10 @@ namespace Claunia.PropertyList
///
/// Name.
/// Signed 16-bit number.
+ [Obsolete("UIDs must be unsigned values")]
public UID(String name, short number)
{
- this.name = name;
this.value = (ulong)number;
- if (number >= sbyte.MinValue && number <= sbyte.MaxValue)
- this.length = 1;
- else
- {
- this.length = 2;
- }
}
///
@@ -116,20 +129,19 @@ namespace Claunia.PropertyList
///
/// Name.
/// Unsigned 32-bit number.
+ [Obsolete("UIDs have no meaningful names")]
public UID(String name, uint number)
{
- this.name = name;
this.value = number;
- if (number <= byte.MaxValue)
- this.length = 1;
- else if (number <= ushort.MaxValue)
- {
- this.length = 2;
- }
- else
- {
- this.length = 4;
- }
+ }
+
+ ///
+ /// Initializes a new instance of the class using an unsigned 32-bit number.
+ ///
+ /// Unsigned 32-bit number.
+ public UID(uint number)
+ {
+ this.value = number;
}
///
@@ -137,20 +149,10 @@ namespace Claunia.PropertyList
///
/// Name.
/// Signed 32-bit number.
+ [Obsolete("UIDs must be unsigned values")]
public UID(String name, int number)
{
- this.name = name;
this.value = (ulong)number;
- if (number >= sbyte.MinValue && number <= sbyte.MaxValue)
- this.length = 1;
- if (number >= short.MinValue && number <= short.MaxValue)
- {
- this.length = 2;
- }
- else
- {
- this.length = 4;
- }
}
///
@@ -158,24 +160,19 @@ namespace Claunia.PropertyList
///
/// Name.
/// Unsigned 64-bit number.
+ [Obsolete("UIDs have no meaningful names")]
public UID(String name, ulong number)
{
- this.name = name;
this.value = number;
- if (number <= byte.MaxValue)
- this.length = 1;
- else if (number <= ushort.MaxValue)
- {
- this.length = 2;
- }
- else if (number <= uint.MaxValue)
- {
- this.length = 4;
- }
- else
- {
- this.length = 8;
- }
+ }
+
+ ///
+ /// Initializes a new instance of the class using an unsigned 64-bit number.
+ ///
+ /// Unsigned 64-bit number.
+ public UID(ulong number)
+ {
+ this.value = number;
}
///
@@ -183,24 +180,10 @@ namespace Claunia.PropertyList
///
/// Name.
/// Signed 64-bit number.
+ [Obsolete("UIDs must be unsigned values")]
public UID(String name, long number)
{
- this.name = name;
this.value = (ulong)number;
- if (number >= sbyte.MinValue && number <= sbyte.MaxValue)
- this.length = 1;
- if (number >= short.MinValue && number <= short.MaxValue)
- {
- this.length = 2;
- }
- if (number >= int.MinValue && number <= int.MaxValue)
- {
- this.length = 4;
- }
- else
- {
- this.length = 8;
- }
}
///
@@ -211,15 +194,47 @@ namespace Claunia.PropertyList
{
get
{
- byte[] bytes = new byte[this.length];
+ byte[] bytes = new byte[this.ByteCount];
this.GetBytes(bytes);
return bytes;
}
}
+ ///
+ /// Gets the number of bytes required to represent this .
+ ///
+ public int ByteCount
+ {
+ get
+ {
+ if (this.value <= byte.MaxValue)
+ {
+ return 1;
+ }
+ else if (this.value <= ushort.MaxValue)
+ {
+ return 2;
+ }
+ else if (this.value <= uint.MaxValue)
+ {
+ return 4;
+ }
+ else
+ {
+ return 8;
+ }
+ }
+ }
+
+ ///
+ /// Writes the bytes required to represent this to a byte span.
+ ///
+ ///
+ /// The byte span to which to write the byte representation of this UID.
+ ///
public void GetBytes(Span bytes)
{
- switch (this.length)
+ switch (this.ByteCount)
{
case 1:
bytes[0] = (byte)this.value;
@@ -246,11 +261,12 @@ namespace Claunia.PropertyList
/// Gets the name.
///
/// The name.
+ [Obsolete("UIDs have no meaningful names")]
public string Name
{
get
{
- return name;
+ return this.value.ToString();
}
}
@@ -264,7 +280,7 @@ namespace Claunia.PropertyList
{
Indent(xml, level);
xml.Append("");
- Span bytes = stackalloc byte[this.length];
+ Span bytes = stackalloc byte[this.ByteCount];
this.GetBytes(bytes);
foreach (byte b in bytes)
xml.Append(String.Format("{0:x2}", b));
@@ -273,8 +289,8 @@ namespace Claunia.PropertyList
internal override void ToBinary(BinaryPropertyListWriter outPlist)
{
- outPlist.Write(0x80 + this.length - 1);
- Span bytes = stackalloc byte[this.length];
+ outPlist.Write(0x80 + this.ByteCount - 1);
+ Span bytes = stackalloc byte[this.ByteCount];
this.GetBytes(bytes);
outPlist.Write(bytes);
}
@@ -283,7 +299,7 @@ namespace Claunia.PropertyList
{
Indent(ascii, level);
ascii.Append("\"");
- Span bytes = stackalloc byte[this.length];
+ Span bytes = stackalloc byte[this.ByteCount];
this.GetBytes(bytes);
foreach (byte b in bytes)
ascii.Append(String.Format("{0:x2}", b));
@@ -314,17 +330,14 @@ namespace Claunia.PropertyList
if (uid == null)
return false;
- return uid.name == name
- && uid.length == length
- && uid.value == value;
+ return uid.value == value;
}
-#if HAS_HASHCODE
+ ///
public override int GetHashCode()
{
- return HashCode.Combine(this.name, this.length, this.value);
+ return this.value.GetHashCode();
}
-#endif
}
}