mirror of
https://github.com/claunia/plist-cil.git
synced 2025-12-16 19:14:26 +00:00
Merge pull request #42 from quamotion/fixes/cache-encoding
Use cached values for ASCII, UTF8 and UTF16 encoding
This commit is contained in:
@@ -44,6 +44,8 @@ namespace Claunia.PropertyList
|
|||||||
/// @author Natalia Portillo
|
/// @author Natalia Portillo
|
||||||
public class BinaryPropertyListParser
|
public class BinaryPropertyListParser
|
||||||
{
|
{
|
||||||
|
private readonly static Encoding utf16BigEndian = Encoding.GetEncoding("UTF-16BE");
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Major version of the property list format
|
/// Major version of the property list format
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -290,7 +292,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), "ASCII");
|
return new NSString(bytes.Slice(offset + stroffset, length), Encoding.ASCII);
|
||||||
}
|
}
|
||||||
case 0x6:
|
case 0x6:
|
||||||
{
|
{
|
||||||
@@ -300,7 +302,7 @@ namespace Claunia.PropertyList
|
|||||||
//UTF-16 characters can have variable length, but the Core Foundation reference implementation
|
//UTF-16 characters can have variable length, but the Core Foundation reference implementation
|
||||||
//assumes 2 byte characters, thus only covering the Basic Multilingual Plane
|
//assumes 2 byte characters, thus only covering the Basic Multilingual Plane
|
||||||
length *= 2;
|
length *= 2;
|
||||||
return new NSString(bytes.Slice(offset + stroffset, length), "UTF-16BE");
|
return new NSString(bytes.Slice(offset + stroffset, length), utf16BigEndian);
|
||||||
}
|
}
|
||||||
case 0x7:
|
case 0x7:
|
||||||
{
|
{
|
||||||
@@ -310,7 +312,7 @@ namespace Claunia.PropertyList
|
|||||||
//UTF-8 characters can have variable length, so we need to calculate the byte length dynamically
|
//UTF-8 characters can have variable length, so we need to calculate the byte length dynamically
|
||||||
//by reading the UTF-8 characters one by one
|
//by reading the UTF-8 characters one by one
|
||||||
int length = CalculateUtf8StringLength(bytes, offset + strOffset, characters);
|
int length = CalculateUtf8StringLength(bytes, offset + strOffset, characters);
|
||||||
return new NSString(bytes.Slice(offset + strOffset, length), "UTF-8");
|
return new NSString(bytes.Slice(offset + strOffset, length), Encoding.UTF8);
|
||||||
}
|
}
|
||||||
case 0x8:
|
case 0x8:
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -43,12 +43,22 @@ namespace Claunia.PropertyList
|
|||||||
/// <param name="encoding">The encoding of the binary representation, the name of a supported charset.</param>
|
/// <param name="encoding">The encoding of the binary representation, the name of a supported charset.</param>
|
||||||
/// <exception cref="ArgumentException">The encoding charset is invalid or not supported by the underlying platform.</exception>
|
/// <exception cref="ArgumentException">The encoding charset is invalid or not supported by the underlying platform.</exception>
|
||||||
public NSString(ReadOnlySpan<byte> bytes, String encoding)
|
public NSString(ReadOnlySpan<byte> bytes, String encoding)
|
||||||
|
: this(bytes, Encoding.GetEncoding(encoding))
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Creates a NSString from its binary representation.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="bytes">The binary representation.</param>
|
||||||
|
/// <param name="encoding">The encoding of the binary representation.</param>
|
||||||
|
/// <exception cref="ArgumentException">The encoding charset is invalid or not supported by the underlying platform.</exception>
|
||||||
|
public NSString(ReadOnlySpan<byte> bytes, Encoding encoding)
|
||||||
{
|
{
|
||||||
Encoding enc = Encoding.GetEncoding(encoding);
|
|
||||||
#if NATIVE_SPAN
|
#if NATIVE_SPAN
|
||||||
content = enc.GetString(bytes);
|
content = encoding.GetString(bytes);
|
||||||
#else
|
#else
|
||||||
content = enc.GetString(bytes.ToArray());
|
content = encoding.GetString(bytes.ToArray());
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user