Cleanup for NSString and NSDate, too

This commit is contained in:
Johann Studanski
2025-01-27 18:21:22 +01:00
parent 44f0fcc763
commit b507a380fb
5 changed files with 21 additions and 8 deletions

View File

@@ -13,16 +13,27 @@ namespace plistcil.test
[Fact] [Fact]
public static void TestPassiveDefaultPreprocessorsRegistered() public static void TestPassiveDefaultPreprocessorsRegistered()
{ {
byte[] testByteArray = [0x1, 0x2, 0x4, 0x8];
Assert.Equal(true, ValuePreprocessor.Preprocess(true, ValuePreprocessor.Type.BOOL)); Assert.Equal(true, ValuePreprocessor.Preprocess(true, ValuePreprocessor.Type.BOOL));
Assert.Equal(false, ValuePreprocessor.Preprocess(false, ValuePreprocessor.Type.BOOL)); Assert.Equal(false, ValuePreprocessor.Preprocess(false, ValuePreprocessor.Type.BOOL));
Assert.Equal("true", ValuePreprocessor.Preprocess("true", ValuePreprocessor.Type.BOOL)); Assert.Equal("true", ValuePreprocessor.Preprocess("true", ValuePreprocessor.Type.BOOL));
Assert.Equal("42", ValuePreprocessor.Preprocess("42", ValuePreprocessor.Type.INTEGER)); Assert.Equal("42", ValuePreprocessor.Preprocess("42", ValuePreprocessor.Type.INTEGER));
Assert.Equal(testByteArray, ValuePreprocessor.Preprocess(testByteArray, ValuePreprocessor.Type.INTEGER));
Assert.Equal("3.14159", ValuePreprocessor.Preprocess("3.14159", ValuePreprocessor.Type.FLOATING_POINT)); Assert.Equal("3.14159", ValuePreprocessor.Preprocess("3.14159", ValuePreprocessor.Type.FLOATING_POINT));
Assert.Equal(testByteArray, ValuePreprocessor.Preprocess(testByteArray, ValuePreprocessor.Type.FLOATING_POINT));
Assert.Equal("2.71828", ValuePreprocessor.Preprocess("2.71828", ValuePreprocessor.Type.UNDEFINED_NUMBER)); Assert.Equal("2.71828", ValuePreprocessor.Preprocess("2.71828", ValuePreprocessor.Type.UNDEFINED_NUMBER));
Assert.Equal("TestString", ValuePreprocessor.Preprocess("TestString", ValuePreprocessor.Type.STRING)); Assert.Equal("TestString", ValuePreprocessor.Preprocess("TestString", ValuePreprocessor.Type.STRING));
Assert.Equal(testByteArray, ValuePreprocessor.Preprocess(testByteArray, ValuePreprocessor.Type.STRING));
Assert.Equal("TestData", ValuePreprocessor.Preprocess("TestData", ValuePreprocessor.Type.DATA)); Assert.Equal("TestData", ValuePreprocessor.Preprocess("TestData", ValuePreprocessor.Type.DATA));
byte[] value = [0x1, 0x2, 0x4, 0x8]; Assert.Equal(testByteArray, ValuePreprocessor.Preprocess(testByteArray, ValuePreprocessor.Type.DATA));
Assert.Equal(value, ValuePreprocessor.Preprocess(value, ValuePreprocessor.Type.DATA));
Assert.Equal(testByteArray, ValuePreprocessor.Preprocess(testByteArray, ValuePreprocessor.Type.DATE));
Assert.Equal("01.02.1903", ValuePreprocessor.Preprocess("01.02.1903", ValuePreprocessor.Type.DATE)); Assert.Equal("01.02.1903", ValuePreprocessor.Preprocess("01.02.1903", ValuePreprocessor.Type.DATE));
Assert.Equal(23.0, ValuePreprocessor.Preprocess(23.0, ValuePreprocessor.Type.DATE)); Assert.Equal(23.0, ValuePreprocessor.Preprocess(23.0, ValuePreprocessor.Type.DATE));
} }
@@ -105,7 +116,7 @@ namespace plistcil.test
[Fact] [Fact]
public static void TestUnregisteredPreprocessorThrows() public static void TestUnregisteredPreprocessorThrows()
{ {
byte[] testArray = [0x1, 0x2, 0x4, 0x8]; int[] testArray = [1, 2, 4, 8];
// there's no registered preprocessor for byte array arguments for STRING // there's no registered preprocessor for byte array arguments for STRING
Assert.Throws<ArgumentException>(() => ValuePreprocessor.Preprocess(testArray, ValuePreprocessor.Type.STRING)); Assert.Throws<ArgumentException>(() => ValuePreprocessor.Preprocess(testArray, ValuePreprocessor.Type.STRING));

View File

@@ -260,7 +260,7 @@ namespace Claunia.PropertyList
PropertyListFormatException("The given binary property list contains a date object of an unknown type (" + PropertyListFormatException("The given binary property list contains a date object of an unknown type (" +
objInfo + ")"); objInfo + ")");
return new NSDate(bytes.Slice(offset + 1, 8)); return new NSDate(ValuePreprocessor.Preprocess(bytes.Slice(offset + 1, 8).ToArray(), ValuePreprocessor.Type.DATE));
} }
case 0x4: case 0x4:
{ {
@@ -274,7 +274,7 @@ namespace Claunia.PropertyList
//ASCII String, each character is 1 byte //ASCII String, each character is 1 byte
ReadLengthAndOffset(bytes, objInfo, offset, out int length, out int stroffset); ReadLengthAndOffset(bytes, objInfo, offset, out int length, out int stroffset);
return new NSString(bytes.Slice(offset + stroffset, length), Encoding.ASCII); return new NSString(ValuePreprocessor.Preprocess(bytes.Slice(offset + stroffset, length).ToArray(), ValuePreprocessor.Type.STRING), Encoding.ASCII);
} }
case 0x6: case 0x6:
{ {

View File

@@ -52,7 +52,7 @@ namespace Claunia.PropertyList
public NSDate(ReadOnlySpan<byte> bytes) => public NSDate(ReadOnlySpan<byte> bytes) =>
//dates are 8 byte big-endian double, seconds since the epoch //dates are 8 byte big-endian double, seconds since the epoch
Date = EPOCH.AddSeconds(ValuePreprocessor.Preprocess(BinaryPropertyListParser.ParseDouble(bytes), ValuePreprocessor.Type.DATE)); Date = EPOCH.AddSeconds(BinaryPropertyListParser.ParseDouble(bytes));
/// <summary> /// <summary>
/// Parses a date from its textual representation. That representation has the following pattern: /// Parses a date from its textual representation. That representation has the following pattern:

View File

@@ -48,9 +48,9 @@ namespace Claunia.PropertyList
public NSString(ReadOnlySpan<byte> bytes, Encoding encoding) public NSString(ReadOnlySpan<byte> bytes, Encoding encoding)
{ {
#if NATIVE_SPAN #if NATIVE_SPAN
Content = ValuePreprocessor.Preprocess(encoding.GetString(bytes), ValuePreprocessor.Type.STRING); Content = encoding.GetString(bytes);
#else #else
Content = ValuePreprocessor.Preprocess(encoding.GetString(bytes.ToArray()), ValuePreprocessor.Type.STRING); Content = encoding.GetString(bytes.ToArray());
#endif #endif
} }

View File

@@ -40,10 +40,12 @@ namespace Claunia.PropertyList
{ new TypeIdentifier(Type.FLOATING_POINT, typeof(byte[])), NullPreprocessor<byte[]> }, { new TypeIdentifier(Type.FLOATING_POINT, typeof(byte[])), NullPreprocessor<byte[]> },
{ new TypeIdentifier(Type.UNDEFINED_NUMBER, typeof(string)), NullPreprocessor<string> }, { new TypeIdentifier(Type.UNDEFINED_NUMBER, typeof(string)), NullPreprocessor<string> },
{ new TypeIdentifier(Type.STRING, typeof(string)), NullPreprocessor<string> }, { new TypeIdentifier(Type.STRING, typeof(string)), NullPreprocessor<string> },
{ new TypeIdentifier(Type.STRING, typeof(byte[])), NullPreprocessor<byte[]> },
{ new TypeIdentifier(Type.DATA, typeof(string)), NullPreprocessor<string> }, { new TypeIdentifier(Type.DATA, typeof(string)), NullPreprocessor<string> },
{ new TypeIdentifier(Type.DATA, typeof(byte[])), NullPreprocessor<byte[]> }, { new TypeIdentifier(Type.DATA, typeof(byte[])), NullPreprocessor<byte[]> },
{ new TypeIdentifier(Type.DATE, typeof(string)), NullPreprocessor<string> }, { new TypeIdentifier(Type.DATE, typeof(string)), NullPreprocessor<string> },
{ new TypeIdentifier(Type.DATE, typeof(double)), NullPreprocessor<double> }, { new TypeIdentifier(Type.DATE, typeof(double)), NullPreprocessor<double> },
{ new TypeIdentifier(Type.DATE, typeof(byte[])), NullPreprocessor<byte[]> },
}; };
/// <summary> /// <summary>