From 3836b3549b24adfe5f118a7e4ed8f5aba26df3f0 Mon Sep 17 00:00:00 2001 From: Natalia Portillo Date: Fri, 20 Feb 2015 00:42:40 +0000 Subject: [PATCH] * plist-cil/ASCIIPropertyListParser.cs: Remove redundant items. Correct same ASCII exceptionfallback issue that appeared in NSString. * plist-cil/UID.cs: * plist-cil/NSSet.cs: * plist-cil/NSDate.cs: * plist-cil/NSData.cs: * plist-cil/NSArray.cs: * plist-cil/NSObject.cs: * plist-cil/NSNumber.cs: * plist-cil/NSString.cs: * plist-cil/NSDictionary.cs: * plist-cil/PropertyListParser.cs: * plist-cil/XmlPropertyListParser.cs: * plist-cil/BinaryPropertyListWriter.cs: * plist-cil/BinaryPropertyListParser.cs: Remove redundant items. --- plist-cil/ASCIIPropertyListParser.cs | 92 +++++++++++---------------- plist-cil/BinaryPropertyListParser.cs | 50 ++++++--------- plist-cil/BinaryPropertyListWriter.cs | 34 +++++----- plist-cil/ChangeLog | 22 +++++++ plist-cil/NSArray.cs | 10 +-- plist-cil/NSData.cs | 4 +- plist-cil/NSDate.cs | 8 +-- plist-cil/NSDictionary.cs | 10 +-- plist-cil/NSNumber.cs | 22 +++---- plist-cil/NSObject.cs | 61 ++++++------------ plist-cil/NSSet.cs | 90 ++++++++++++++------------ plist-cil/NSString.cs | 20 ++---- plist-cil/PropertyListParser.cs | 16 ++--- plist-cil/UID.cs | 14 ++-- plist-cil/XmlPropertyListParser.cs | 91 +++++++------------------- 15 files changed, 225 insertions(+), 319 deletions(-) diff --git a/plist-cil/ASCIIPropertyListParser.cs b/plist-cil/ASCIIPropertyListParser.cs index a9ad0bc..e1af2af 100644 --- a/plist-cil/ASCIIPropertyListParser.cs +++ b/plist-cil/ASCIIPropertyListParser.cs @@ -26,7 +26,6 @@ using System; using System.IO; using System.Collections.Generic; using System.Text.RegularExpressions; -using System.Globalization; using System.Text; using System.Runtime.CompilerServices; @@ -135,11 +134,11 @@ namespace Claunia.PropertyList /** * Property list source data */ - private byte[] data; + byte[] data; /** * Current parsing index */ - private int index; + int index; /** * Only allow subclasses to change instantiation. @@ -153,7 +152,7 @@ namespace Claunia.PropertyList /// Creates a new parser for the given property list content. /// /// The content of the property list that is to be parsed. - private ASCIIPropertyListParser(byte[] propertyListContent) + ASCIIPropertyListParser(byte[] propertyListContent) { data = propertyListContent; } @@ -163,7 +162,7 @@ namespace Claunia.PropertyList /// /// Whether the given tokens occur at the current parsing position. /// The sequence of tokens to look for. - private bool AcceptSequence(params char[] sequence) + bool AcceptSequence(params char[] sequence) { for (int i = 0; i < sequence.Length; i++) { @@ -179,14 +178,11 @@ namespace Claunia.PropertyList /// /// The symbols to check. /// Whether one of the symbols can be accepted or not. - private bool Accept(params char[] acceptableSymbols) + bool Accept(params char[] acceptableSymbols) { bool symbolPresent = false; foreach (char c in acceptableSymbols) - { - if (data[index] == c) - symbolPresent = true; - } + symbolPresent |= data[index] == c; return symbolPresent; } @@ -196,7 +192,7 @@ namespace Claunia.PropertyList /// /// The symbol to check. /// Whether the symbol can be accepted or not. - private bool Accept(char acceptableSymbol) + bool Accept(char acceptableSymbol) { return data[index] == acceptableSymbol; } @@ -206,15 +202,14 @@ namespace Claunia.PropertyList /// /// The expected symbols. /// If none of the expected symbols could be found. - private void Expect(params char[] expectedSymbols) + void Expect(params char[] expectedSymbols) { if (!Accept(expectedSymbols)) { String excString = "Expected '" + expectedSymbols[0] + "'"; for (int i = 1; i < expectedSymbols.Length; i++) - { excString += " or '" + expectedSymbols[i] + "'"; - } + excString += " but found '" + (char)data[index] + "'"; throw new FormatException(String.Format("{0} at {1}", excString, index)); } @@ -225,7 +220,7 @@ namespace Claunia.PropertyList /// /// The expected symbol. /// If the expected symbol could be found. - private void Expect(char expectedSymbol) + void Expect(char expectedSymbol) { if (!Accept(expectedSymbol)) throw new FormatException(String.Format("Expected '{0}' but found '{1}' at {2}", expectedSymbol, data[index], index)); @@ -236,7 +231,7 @@ namespace Claunia.PropertyList /// /// The symbol to read. /// If the expected symbol could not be read. - private void Read(char symbol) + void Read(char symbol) { Expect(symbol); index++; @@ -245,7 +240,7 @@ namespace Claunia.PropertyList /** * Skips the current symbol. */ - private void Skip() + void Skip() { index++; } @@ -254,7 +249,7 @@ namespace Claunia.PropertyList /// Skips several symbols /// /// The amount of symbols to skip. - private void Skip(int numSymbols) + void Skip(int numSymbols) { index += numSymbols; } @@ -262,7 +257,7 @@ namespace Claunia.PropertyList /** * Skips all whitespaces and comments from the current parsing position onward. */ - private void SkipWhitespacesAndComments() + void SkipWhitespacesAndComments() { bool commentSkipped; do @@ -306,7 +301,7 @@ namespace Claunia.PropertyList /// /// The input until one the given symbols. /// The symbols that can occur after the string to read. - private string ReadInputUntil(params char[] symbols) + string ReadInputUntil(params char[] symbols) { string s = ""; while (!Accept(symbols)) @@ -322,7 +317,7 @@ namespace Claunia.PropertyList /// /// The input until the given symbol. /// The symbol that can occur after the string to read. - private string ReadInputUntil(char symbol) + string ReadInputUntil(char symbol) { String s = ""; while (!Accept(symbol)) @@ -360,7 +355,7 @@ namespace Claunia.PropertyList /// /// The parsed NSObject. /// - private NSObject ParseObject() + NSObject ParseObject() { switch (data[index]) { @@ -392,10 +387,7 @@ namespace Claunia.PropertyList return new NSString(quotedString); } } - else - { - return new NSString(quotedString); - } + return new NSString(quotedString); } default: { @@ -420,7 +412,7 @@ namespace Claunia.PropertyList /// The prerequisite for calling this method is, that an array begin token has been read. /// /// The array found at the parsing position. - private NSArray ParseArray() + NSArray ParseArray() { //Skip begin token Skip(); @@ -450,7 +442,7 @@ namespace Claunia.PropertyList /// The prerequisite for calling this method is, that a dictionary begin token has been read. /// /// The dictionary found at the parsing position. - private NSDictionary ParseDictionary() + NSDictionary ParseDictionary() { //Skip begin token Skip(); @@ -461,13 +453,9 @@ namespace Claunia.PropertyList //Parse key string keyString; if (Accept(QUOTEDSTRING_BEGIN_TOKEN)) - { keyString = ParseQuotedString(); - } else - { keyString = ParseString(); - } SkipWhitespacesAndComments(); //Parse assign token @@ -491,7 +479,7 @@ namespace Claunia.PropertyList /// The prerequisite for calling this method is, that a data begin token has been read. /// /// The data object found at the parsing position. - private NSObject ParseData() + NSObject ParseData() { NSObject obj = null; //Skip begin token @@ -506,13 +494,9 @@ namespace Claunia.PropertyList Skip(); Expect(DATA_GSBOOL_TRUE_TOKEN, DATA_GSBOOL_FALSE_TOKEN); if (Accept(DATA_GSBOOL_TRUE_TOKEN)) - { obj = new NSNumber(true); - } else - { obj = new NSNumber(false); - } //Skip the parsed boolean token Skip(); } @@ -559,7 +543,7 @@ namespace Claunia.PropertyList /// Attempts to parse a plain string as a date if possible. /// /// A NSDate if the string represents such an object. Otherwise a NSString is returned. - private NSObject ParseDateString() + NSObject ParseDateString() { string numericalString = ParseString(); if (numericalString.Length > 4 && numericalString[4] == DATE_DATE_FIELD_DELIMITER) @@ -581,7 +565,7 @@ namespace Claunia.PropertyList /// The string is made up of all characters to the next whitespace, delimiter token or assignment token. /// /// The string found at the current parsing position. - private string ParseString() + string ParseString() { return ReadInputUntil(WHITESPACE_SPACE, WHITESPACE_TAB, WHITESPACE_NEWLINE, WHITESPACE_CARRIAGE_RETURN, ARRAY_ITEM_DELIMITER_TOKEN, DICTIONARY_ITEM_DELIMITER_TOKEN, DICTIONARY_ASSIGN_TOKEN, ARRAY_END_TOKEN); @@ -593,7 +577,7 @@ namespace Claunia.PropertyList /// /// The quoted string found at the parsing method with all special characters unescaped. /// If an error occured during parsing. - private string ParseQuotedString() + string ParseQuotedString() { //Skip begin token Skip(); @@ -626,7 +610,7 @@ namespace Claunia.PropertyList /** * Used to encode the parsed strings */ - private static Encoding asciiEncoder; + static Encoding asciiEncoder; /// /// Parses a string according to the format specified for ASCII property lists. @@ -670,22 +654,14 @@ namespace Claunia.PropertyList } //Build string string result = Encoding.BigEndianUnicode.GetString(bytArr); - //emoryStream charBuf = CharBuffer.wrap(result); //If the string can be represented in the ASCII codepage // --> use ASCII encoding - try - { - if (asciiEncoder == null) - asciiEncoder = Encoding.GetEncoding("ascii", EncoderExceptionFallback.ExceptionFallback, DecoderExceptionFallback.ExceptionFallback); - return asciiEncoder.GetString(Encoding.Convert(Encoding.BigEndianUnicode, asciiEncoder, bytArr)); - } - catch - { - //The string contains characters outside the ASCII codepage - // --> use the UTF-8 encoded string - return result; - } + if(IsASCIIEncodable(result)) + return Encoding.ASCII.GetString(Encoding.Convert(Encoding.BigEndianUnicode, Encoding.ASCII, bytArr)); + //The string contains characters outside the ASCII codepage + // --> use the UTF-8 encoded string + return result; } /// @@ -752,6 +728,14 @@ namespace Claunia.PropertyList return Encoding.UTF8.GetString(stringBytes); } } + + internal static bool IsASCIIEncodable(string text) + { + foreach (char c in text) + if ((int)c > 0x7F) + return false; + return true; + } } } diff --git a/plist-cil/BinaryPropertyListParser.cs b/plist-cil/BinaryPropertyListParser.cs index 7205e27..337b619 100644 --- a/plist-cil/BinaryPropertyListParser.cs +++ b/plist-cil/BinaryPropertyListParser.cs @@ -40,43 +40,43 @@ namespace Claunia.PropertyList /// @author Natalia Portillo public class BinaryPropertyListParser { - private int majorVersion, minorVersion; + int majorVersion, minorVersion; /** * property list in bytes * */ - private byte[] bytes; + byte[] bytes; /** * Length of an offset definition in bytes * */ - private int offsetSize; + int offsetSize; /** * Length of an object reference in bytes * */ - private int objectRefSize; + int objectRefSize; /** * Number of objects stored in this property list * */ - private int numObjects; + int numObjects; /** * Reference to the top object of the property list * */ - private int topObject; + int topObject; /** * Offset of the offset table from the beginning of the file * */ - private int offsetTableOffset; + int offsetTableOffset; /** * The table holding the information at which offset each object is found * */ - private int[] offsetTable; + int[] offsetTable; /// /// Protected constructor so that instantiation is fully controlled by the /// static parse methods. /// /// - public BinaryPropertyListParser() + protected BinaryPropertyListParser() { /** empty **/ } @@ -99,11 +99,11 @@ namespace Claunia.PropertyList /// The root object of the property list. This is usually a NSDictionary but can also be a NSArray. /// The binary property list's data. /// When the property list's format could not be parsed. - private NSObject DoParse(byte[] data) + NSObject DoParse(byte[] data) { bytes = data; string magic = Encoding.ASCII.GetString(CopyOfRange(bytes, 0, 8)); - if (!magic.StartsWith("bplist")) + if (!magic.StartsWith("bplist", StringComparison.Ordinal)) { throw new PropertyListFormatException("The given data is no binary property list. Wrong magic bytes: " + magic); } @@ -199,7 +199,7 @@ namespace Claunia.PropertyList /// The parsed object. /// The object ID. /// When the property list's format could not be parsed. - private NSObject ParseObject(int obj) + NSObject ParseObject(int obj) { int offset = offsetTable[obj]; byte type = bytes[offset]; @@ -399,7 +399,7 @@ namespace Claunia.PropertyList /// An array with the length two. First entry is the length, second entry the offset at which the content starts. /// Object information byte. /// Offset in the byte array at which the object is located. - private int[] ReadLengthAndOffset(int objInfo, int offset) + int[] ReadLengthAndOffset(int objInfo, int offset) { int length = objInfo; int stroffset = 1; @@ -431,7 +431,7 @@ namespace Claunia.PropertyList length = (int)new System.Numerics.BigInteger(litEBigInteger); } } - return new int[]{ length, stroffset }; + return new []{ length, stroffset }; } /// @@ -512,17 +512,10 @@ namespace Claunia.PropertyList public static double ParseDouble(byte[] bytes) { if (bytes.Length == 8) - { return BitConverter.Int64BitsToDouble(ParseLong(bytes)); - } - else if (bytes.Length == 4) - { + if (bytes.Length == 4) return BitConverter.ToSingle(BitConverter.GetBytes(ParseLong(bytes)), 0); - } - else - { - throw new ArgumentException("bad byte array length " + bytes.Length); - } + throw new ArgumentException("bad byte array length " + bytes.Length); } /// @@ -535,17 +528,10 @@ namespace Claunia.PropertyList public static double ParseDouble(byte[] bytes, int startIndex, int endIndex) { if (endIndex - startIndex == 8) - { return BitConverter.Int64BitsToDouble(ParseLong(bytes, startIndex, endIndex)); - } - else if (endIndex - startIndex == 4) - { + if (endIndex - startIndex == 4) return BitConverter.ToSingle(BitConverter.GetBytes(ParseLong(bytes, startIndex, endIndex)), 0); - } - else - { - throw new ArgumentException("endIndex (" + endIndex + ") - startIndex (" + startIndex + ") != 4 or 8"); - } + throw new ArgumentException("endIndex (" + endIndex + ") - startIndex (" + startIndex + ") != 4 or 8"); } /// diff --git a/plist-cil/BinaryPropertyListWriter.cs b/plist-cil/BinaryPropertyListWriter.cs index 4e71704..113a800 100644 --- a/plist-cil/BinaryPropertyListWriter.cs +++ b/plist-cil/BinaryPropertyListWriter.cs @@ -49,7 +49,7 @@ namespace Claunia.PropertyList /// /// Version code /// Object root. - private static int GetMinimumRequiredVersion(NSObject root) + static int GetMinimumRequiredVersion(NSObject root) { int minVersion = VERSION_00; if (root == null) @@ -137,17 +137,17 @@ namespace Claunia.PropertyList return bout.ToArray(); } - private int version = VERSION_00; + int version = VERSION_00; // raw output stream to result file - private Stream outStream; + Stream outStream; // # of bytes written so far - private long count; + long count; // map from object to its ID - private Dictionary idMap = new Dictionary(); - private int idSizeInBytes; + Dictionary idMap = new Dictionary(); + int idSizeInBytes; /// /// Creates a new binary property list writer @@ -168,29 +168,29 @@ namespace Claunia.PropertyList void Write(NSObject root) { // magic bytes - Write(new byte[]{ (byte)'b', (byte)'p', (byte)'l', (byte)'i', (byte)'s', (byte)'t' }); + Write(new []{ (byte)'b', (byte)'p', (byte)'l', (byte)'i', (byte)'s', (byte)'t' }); //version switch (version) { case VERSION_00: { - Write(new byte[]{ (byte)'0', (byte)'0' }); + Write(new []{ (byte)'0', (byte)'0' }); break; } case VERSION_10: { - Write(new byte[]{ (byte)'1', (byte)'0' }); + Write(new []{ (byte)'1', (byte)'0' }); break; } case VERSION_15: { - Write(new byte[]{ (byte)'1', (byte)'5' }); + Write(new []{ (byte)'1', (byte)'5' }); break; } case VERSION_20: { - Write(new byte[]{ (byte)'2', (byte)'0' }); + Write(new []{ (byte)'2', (byte)'0' }); break; } } @@ -264,24 +264,20 @@ namespace Claunia.PropertyList return ID; } - private static int ComputeIdSizeInBytes(int numberOfIds) + static int ComputeIdSizeInBytes(int numberOfIds) { if (numberOfIds < 256) return 1; - if (numberOfIds < 65536) - return 2; - return 4; + return numberOfIds < 65536 ? 2 : 4; } - private int ComputeOffsetSizeInBytes(long maxOffset) + static int ComputeOffsetSizeInBytes(long maxOffset) { if (maxOffset < 256) return 1; if (maxOffset < 65536) return 2; - if (maxOffset < 4294967296L) - return 4; - return 8; + return maxOffset < 4294967296L ? 4 : 8; } internal void WriteIntHeader(int kind, int value) diff --git a/plist-cil/ChangeLog b/plist-cil/ChangeLog index 285829e..8e2a24c 100644 --- a/plist-cil/ChangeLog +++ b/plist-cil/ChangeLog @@ -1,3 +1,25 @@ +2015-02-20 Natalia Portillo + + * ASCIIPropertyListParser.cs: + Remove redundant items. + Correct same ASCII exceptionfallback issue that appeared in + NSString. + + * UID.cs: + * NSSet.cs: + * NSDate.cs: + * NSData.cs: + * NSArray.cs: + * NSObject.cs: + * NSNumber.cs: + * NSString.cs: + * NSDictionary.cs: + * PropertyListParser.cs: + * XmlPropertyListParser.cs: + * BinaryPropertyListWriter.cs: + * BinaryPropertyListParser.cs: + Remove redundant items. + 2015-02-20 Natalia Portillo * UID.cs: diff --git a/plist-cil/NSArray.cs b/plist-cil/NSArray.cs index d5f2aea..c50fe4b 100644 --- a/plist-cil/NSArray.cs +++ b/plist-cil/NSArray.cs @@ -88,7 +88,7 @@ namespace Claunia.PropertyList public void SetValue(int key, Object value) { if (value == null) - throw new ArgumentNullException("Cannot add null values to an NSArray!"); + throw new ArgumentNullException("value", "Cannot add null values to an NSArray!"); array[key] = NSObject.Wrap(value); } @@ -202,14 +202,14 @@ namespace Claunia.PropertyList { if (obj.GetType().Equals(typeof(NSArray))) { - return ArrayEquals(((NSArray)obj).GetArray(), this.array); + return ArrayEquals(((NSArray)obj).GetArray(), array); } else { NSObject nso = NSObject.Wrap(obj); if (nso.GetType().Equals(typeof(NSArray))) { - return ArrayEquals(((NSArray)nso).GetArray(), this.array); + return ArrayEquals(((NSArray)nso).GetArray(), array); } } return false; @@ -289,7 +289,7 @@ namespace Claunia.PropertyList { Indent(ascii, level); ascii.Append(ASCIIPropertyListParser.ARRAY_BEGIN_TOKEN); - int indexOfLastNewLine = ascii.ToString().LastIndexOf(NEWLINE); + int indexOfLastNewLine = ascii.ToString().LastIndexOf(NEWLINE, StringComparison.Ordinal); for (int i = 0; i < array.Length; i++) { Type objClass = array[i].GetType(); @@ -323,7 +323,7 @@ namespace Claunia.PropertyList { Indent(ascii, level); ascii.Append(ASCIIPropertyListParser.ARRAY_BEGIN_TOKEN); - int indexOfLastNewLine = ascii.ToString().LastIndexOf(NEWLINE); + int indexOfLastNewLine = ascii.ToString().LastIndexOf(NEWLINE, StringComparison.Ordinal); for (int i = 0; i < array.Length; i++) { Type objClass = array[i].GetType(); diff --git a/plist-cil/NSData.cs b/plist-cil/NSData.cs index a19be23..0ee55c7 100644 --- a/plist-cil/NSData.cs +++ b/plist-cil/NSData.cs @@ -35,7 +35,7 @@ namespace Claunia.PropertyList /// @author Natalia Portillo public class NSData : NSObject { - byte[] bytes; + readonly byte[] bytes; /// /// Creates the NSData object from the binary representation of it. @@ -162,7 +162,7 @@ namespace Claunia.PropertyList { Indent(ascii, level); ascii.Append(ASCIIPropertyListParser.DATA_BEGIN_TOKEN); - int indexOfLastNewLine = ascii.ToString().LastIndexOf(NEWLINE); + int indexOfLastNewLine = ascii.ToString().LastIndexOf(NEWLINE, StringComparison.Ordinal); for (int i = 0; i < bytes.Length; i++) { int b = bytes[i] & 0xFF; diff --git a/plist-cil/NSDate.cs b/plist-cil/NSDate.cs index 0a6c173..045f72f 100644 --- a/plist-cil/NSDate.cs +++ b/plist-cil/NSDate.cs @@ -34,7 +34,7 @@ namespace Claunia.PropertyList /// @author Natalia Portillo public class NSDate : NSObject { - DateTime date; + readonly DateTime date; static readonly DateTime EPOCH = new DateTime(2001, 1, 1, 0, 0, 0, DateTimeKind.Utc); @@ -49,7 +49,7 @@ namespace Claunia.PropertyList /// The parsed Date /// The date string as found in the XML property list /// Given string cannot be parsed - private static DateTime ParseDateString(string textRepresentation) + static DateTime ParseDateString(string textRepresentation) { try { @@ -67,7 +67,7 @@ namespace Claunia.PropertyList /// /// The date which should be represented. /// The string representation of the date. - private static string MakeDateString(DateTime date) + static string MakeDateString(DateTime date) { return date.ToString(sdfDefault); } @@ -79,7 +79,7 @@ namespace Claunia.PropertyList /// /// The date which should be represented. /// The string representation of the date. - private static string MakeDateStringGnuStep(DateTime date) + static string MakeDateStringGnuStep(DateTime date) { return date.ToString(sdfGnuStep); } diff --git a/plist-cil/NSDictionary.cs b/plist-cil/NSDictionary.cs index 38163cf..e836228 100644 --- a/plist-cil/NSDictionary.cs +++ b/plist-cil/NSDictionary.cs @@ -40,7 +40,7 @@ namespace Claunia.PropertyList /// @author Natalia Portillo public class NSDictionary : NSObject, IDictionary { - Dictionary dict; + readonly Dictionary dict; /// /// Creates a new empty NSDictionary. @@ -262,12 +262,6 @@ namespace Claunia.PropertyList return false; } - public override bool Equals(Object obj) - { - bool foo = this.Equals((NSDictionary)obj); - return (obj.GetType().Equals(GetType()) && ((NSDictionary)obj).dict.Equals(dict)); - } - public override bool Equals(NSObject obj) { if (!(obj is NSDictionary)) @@ -294,7 +288,7 @@ namespace Claunia.PropertyList public override int GetHashCode() { int hash = 7; - hash = 83 * hash + (this.dict != null ? this.dict.GetHashCode() : 0); + hash = 83 * hash + (dict != null ? dict.GetHashCode() : 0); return hash; } diff --git a/plist-cil/NSNumber.cs b/plist-cil/NSNumber.cs index 65681c8..bb5b098 100644 --- a/plist-cil/NSNumber.cs +++ b/plist-cil/NSNumber.cs @@ -55,11 +55,11 @@ namespace Claunia.PropertyList public const int BOOLEAN = 2; //Holds the current type of this number - int type; + readonly int type; - long longValue; - double doubleValue; - bool boolValue; + readonly long longValue; + readonly double doubleValue; + readonly bool boolValue; /// /// Parses integers and real numbers from their binary representation. @@ -225,8 +225,7 @@ namespace Claunia.PropertyList { if (type == BOOLEAN) return boolValue; - else - return longValue != 0; + return longValue != 0; } /// @@ -285,8 +284,8 @@ namespace Claunia.PropertyList public override int GetHashCode() { int hash = type; - hash = 37 * hash + (int)(this.longValue ^ ((uint)this.longValue >> 32)); - hash = 37 * hash + (int)(BitConverter.DoubleToInt64Bits(this.doubleValue) ^ ((uint)(BitConverter.DoubleToInt64Bits(this.doubleValue) >> 32))); + hash = 37 * hash + (int)(longValue ^ ((uint)longValue >> 32)); + hash = 37 * hash + (int)(BitConverter.DoubleToInt64Bits(doubleValue) ^ ((uint)(BitConverter.DoubleToInt64Bits(doubleValue) >> 32))); hash = 37 * hash + (ToBool() ? 1 : 0); return hash; } @@ -448,15 +447,12 @@ namespace Claunia.PropertyList y = num.ToDouble(); return (x < y) ? -1 : ((x == y) ? 0 : 1); } - else if (IsNumber(o)) + if (IsNumber(o)) { y = GetDoubleFromObject(o); return (x < y) ? -1 : ((x == y) ? 0 : 1); } - else - { - return -1; - } + return -1; } /// diff --git a/plist-cil/NSObject.cs b/plist-cil/NSObject.cs index 5753654..f78c36e 100644 --- a/plist-cil/NSObject.cs +++ b/plist-cil/NSObject.cs @@ -70,7 +70,7 @@ namespace Claunia.PropertyList /// /// Assigns IDs to all the objects in this NSObject subtree. /// - /// The writer object that handles the binary serialization. + /// The writer object that handles the binary serialization. internal virtual void AssignIDs(BinaryPropertyListWriter outPlist) { outPlist.AssignID(this); @@ -79,7 +79,7 @@ namespace Claunia.PropertyList /// /// Generates the binary representation of the object. /// - /// The output stream to serialize the object to. + /// The output stream to serialize the object to. internal abstract void ToBinary(BinaryPropertyListWriter outPlist); /// @@ -124,7 +124,7 @@ namespace Claunia.PropertyList /// /// The string builder for the XML document. /// The level of identation. - internal void Indent(StringBuilder xml, int level) + internal static void Indent(StringBuilder xml, int level) { for (int i = 0; i < level; i++) xml.Append(INDENT); @@ -258,7 +258,7 @@ namespace Claunia.PropertyList } if (typeof(long).IsAssignableFrom(c)) { - return Wrap((long)(long)o); + return Wrap((long)o); } if (typeof(float).Equals(c)) { @@ -266,7 +266,7 @@ namespace Claunia.PropertyList } if (typeof(double).IsAssignableFrom(c)) { - return Wrap((double)(Double)o); + return Wrap((double)o); } if (typeof(string).Equals(c)) { @@ -283,7 +283,7 @@ namespace Claunia.PropertyList { return Wrap((byte[])o); } - else if (cc.Equals(typeof(bool))) + if (cc.Equals(typeof(bool))) { bool[] array = (bool[])o; NSArray nsa = new NSArray(array.Length); @@ -291,7 +291,7 @@ namespace Claunia.PropertyList nsa.SetValue(i, Wrap(array[i])); return nsa; } - else if (cc.Equals(typeof(float))) + if (cc.Equals(typeof(float))) { float[] array = (float[])o; NSArray nsa = new NSArray(array.Length); @@ -299,7 +299,7 @@ namespace Claunia.PropertyList nsa.SetValue(i, Wrap(array[i])); return nsa; } - else if (cc.Equals(typeof(double))) + if (cc.Equals(typeof(double))) { double[] array = (double[])o; NSArray nsa = new NSArray(array.Length); @@ -307,7 +307,7 @@ namespace Claunia.PropertyList nsa.SetValue(i, Wrap(array[i])); return nsa; } - else if (cc.Equals(typeof(short))) + if (cc.Equals(typeof(short))) { short[] array = (short[])o; NSArray nsa = new NSArray(array.Length); @@ -315,7 +315,7 @@ namespace Claunia.PropertyList nsa.SetValue(i, Wrap(array[i])); return nsa; } - else if (cc.Equals(typeof(int))) + if (cc.Equals(typeof(int))) { int[] array = (int[])o; NSArray nsa = new NSArray(array.Length); @@ -323,7 +323,7 @@ namespace Claunia.PropertyList nsa.SetValue(i, Wrap(array[i])); return nsa; } - else if (cc.Equals(typeof(long))) + if (cc.Equals(typeof(long))) { long[] array = (long[])o; NSArray nsa = new NSArray(array.Length); @@ -331,10 +331,7 @@ namespace Claunia.PropertyList nsa.SetValue(i, Wrap(array[i])); return nsa; } - else - { return Wrap((Object[])o); - } } if (typeof(Dictionary).IsAssignableFrom(c)) { @@ -347,9 +344,7 @@ namespace Claunia.PropertyList return dict; } if (typeof(List).IsAssignableFrom(c)) - { return Wrap(((List)o).ToArray()); - } return WrapSerialized(o); } @@ -404,7 +399,7 @@ namespace Claunia.PropertyList } return arrayB; } - else if (this is NSDictionary) + if (this is NSDictionary) { Dictionary dictA = ((NSDictionary)this).GetDictionary(); Dictionary dictB = new Dictionary(dictA.Count); @@ -414,7 +409,7 @@ namespace Claunia.PropertyList } return dictB; } - else if (this is NSSet) + if (this is NSSet) { List setA = ((NSSet)this).GetSet(); List setB = new List(); @@ -424,7 +419,7 @@ namespace Claunia.PropertyList } return setB; } - else if (this is NSNumber) + if (this is NSNumber) { NSNumber num = (NSNumber)this; switch (num.GetNSNumberType()) @@ -433,48 +428,34 @@ namespace Claunia.PropertyList { long longVal = num.ToLong(); if (longVal > int.MaxValue || longVal < int.MinValue) - { return longVal; - } - else - { - return num.ToInt(); - } + return num.ToInt(); } case NSNumber.REAL: - { return num.ToDouble(); - } case NSNumber.BOOLEAN: - { return num.ToBool(); - } default : - { return num.ToDouble(); - } } } - else if (this is NSString) + if (this is NSString) { return ((NSString)this).GetContent(); } - else if (this is NSData) + if (this is NSData) { return ((NSData)this).Bytes; } - else if (this is NSDate) + if (this is NSDate) { return ((NSDate)this).Date; } - else if (this is UID) + if (this is UID) { return ((UID)this).Bytes; } - else - { return this; - } } internal static bool ArrayEquals(byte[] arrayA, byte[] arrayB) @@ -482,12 +463,8 @@ namespace Claunia.PropertyList if (arrayA.Length == arrayB.Length) { for (int i = 0; i < arrayA.Length; i++) - { if (arrayA[i] != arrayB[i]) - { return false; - } - } return true; } return false; diff --git a/plist-cil/NSSet.cs b/plist-cil/NSSet.cs index 9de191c..8cfaae7 100644 --- a/plist-cil/NSSet.cs +++ b/plist-cil/NSSet.cs @@ -24,7 +24,6 @@ // SOFTWARE. using System; using System.Collections.Generic; -using System.Runtime.CompilerServices; using System.Collections; using System.Text; @@ -39,9 +38,9 @@ namespace Claunia.PropertyList /// @author Natalia Portillo public class NSSet : NSObject, IEnumerable { - List set; + readonly List set; - private bool ordered = false; + bool ordered; /// /// Creates an empty unordered set. @@ -114,34 +113,40 @@ namespace Claunia.PropertyList /// Adds an object to the set. /// /// The object to add. - [MethodImpl(MethodImplOptions.Synchronized)] public void AddObject(NSObject obj) { - set.Add(obj); - if (ordered) - set.Sort(); + lock (set) + { + set.Add(obj); + if (ordered) + set.Sort(); + } } /// /// Removes an object from the set. /// /// The object to remove. - [MethodImpl(MethodImplOptions.Synchronized)] public void RemoveObject(NSObject obj) { - set.Remove(obj); - if (ordered) - set.Sort(); + lock (set) + { + set.Remove(obj); + if (ordered) + set.Sort(); + } } /// /// Returns all objects contained in the set. /// /// An array of all objects in the set. - [MethodImpl(MethodImplOptions.Synchronized)] public NSObject[] AllObjects() { - return set.ToArray(); + lock (set) + { + return set.ToArray(); + } } /// @@ -149,13 +154,12 @@ namespace Claunia.PropertyList /// if the set contains no objects. /// /// The first object in the set, or null if the set is empty. - [MethodImpl(MethodImplOptions.Synchronized)] public NSObject AnyObject() { - if (set.Count == 0) - return null; - else - return set[0]; + lock (set) + { + return set.Count == 0 ? null : set[0]; + } } /// @@ -174,15 +178,17 @@ namespace Claunia.PropertyList /// /// The object to look for. /// The object if it is present, null otherwise. - [MethodImpl(MethodImplOptions.Synchronized)] public NSObject Member(NSObject obj) { - foreach (NSObject o in set) + lock (set) { - if (o.Equals(obj)) - return o; + foreach (NSObject o in set) + { + if (o.Equals(obj)) + return o; + } + return null; } - return null; } /// @@ -190,15 +196,17 @@ namespace Claunia.PropertyList /// /// true if the intersection of both sets is empty, false otherwise. /// The other set. - [MethodImpl(MethodImplOptions.Synchronized)] public bool IntersectsSet(NSSet otherSet) { - foreach (NSObject o in set) + lock (set) { - if (otherSet.ContainsObject(o)) - return true; + foreach (NSObject o in set) + { + if (otherSet.ContainsObject(o)) + return true; + } + return false; } - return false; } /// @@ -206,15 +214,17 @@ namespace Claunia.PropertyList /// /// true if all elements in this set are also present in the other set, falseotherwise. /// The other set. - [MethodImpl(MethodImplOptions.Synchronized)] public bool IsSubsetOfSet(NSSet otherSet) { - foreach (NSObject o in set) + lock (set) { - if (!otherSet.ContainsObject(o)) - return false; + foreach (NSObject o in set) + { + if (!otherSet.ContainsObject(o)) + return false; + } + return true; } - return true; } /// @@ -223,10 +233,12 @@ namespace Claunia.PropertyList /// of NSSet. /// /// The iterator for the set. - [MethodImpl(MethodImplOptions.Synchronized)] public IEnumerator GetEnumerator() { - return set.GetEnumerator(); + lock (set) + { + return set.GetEnumerator(); + } } /// @@ -241,7 +253,7 @@ namespace Claunia.PropertyList public override int GetHashCode() { int hash = 7; - hash = 29 * hash + (this.set != null ? this.set.GetHashCode() : 0); + hash = 29 * hash + (set != null ? set.GetHashCode() : 0); return hash; } @@ -256,7 +268,7 @@ namespace Claunia.PropertyList return false; } NSSet other = (NSSet)obj; - return !(this.set != other.set && (this.set == null || !this.set.Equals(other.set))); + return !(set != other.set && (set == null || !set.Equals(other.set))); } /// @@ -337,7 +349,7 @@ namespace Claunia.PropertyList set.Sort(); NSObject[] array = AllObjects(); ascii.Append(ASCIIPropertyListParser.ARRAY_BEGIN_TOKEN); - int indexOfLastNewLine = ascii.ToString().LastIndexOf(NEWLINE); + int indexOfLastNewLine = ascii.ToString().LastIndexOf(NEWLINE, StringComparison.Ordinal); for (int i = 0; i < array.Length; i++) { Type objClass = array[i].GetType(); @@ -381,7 +393,7 @@ namespace Claunia.PropertyList set.Sort(); NSObject[] array = AllObjects(); ascii.Append(ASCIIPropertyListParser.ARRAY_BEGIN_TOKEN); - int indexOfLastNewLine = ascii.ToString().LastIndexOf(NEWLINE); + int indexOfLastNewLine = ascii.ToString().LastIndexOf(NEWLINE, StringComparison.Ordinal); for (int i = 0; i < array.Length; i++) { Type objClass = array[i].GetType(); diff --git a/plist-cil/NSString.cs b/plist-cil/NSString.cs index 3148682..782076f 100644 --- a/plist-cil/NSString.cs +++ b/plist-cil/NSString.cs @@ -51,7 +51,7 @@ namespace Claunia.PropertyList /// /// Creates a NSString from a string. /// - /// The string that will be contained in the NSString. + /// The string that will be contained in the NSString. public NSString(string text) { content = text; @@ -228,9 +228,8 @@ namespace Claunia.PropertyList { string outString = ""; char[] cArray = s.ToCharArray(); - for (int i = 0; i < cArray.Length; i++) + foreach (char c in cArray) { - char c = cArray[i]; if (c > 127) { //non-ASCII Unicode @@ -275,17 +274,10 @@ namespace Claunia.PropertyList public int CompareTo(Object o) { if (o is NSString) - { - return GetContent().CompareTo(((NSString)o).GetContent()); - } - else if (o is String) - { - return GetContent().CompareTo(((String)o)); - } - else - { - return -1; - } + return string.Compare(GetContent(), ((NSString)o).GetContent(), StringComparison.Ordinal); + if (o is String) + return string.Compare(GetContent(), ((String)o), StringComparison.Ordinal); + return -1; } public override bool Equals(NSObject obj) diff --git a/plist-cil/PropertyListParser.cs b/plist-cil/PropertyListParser.cs index 0eaebe6..fb13138 100644 --- a/plist-cil/PropertyListParser.cs +++ b/plist-cil/PropertyListParser.cs @@ -49,7 +49,7 @@ namespace Claunia.PropertyList /// /// The type of the property list /// The very first bytes of data of the property list (minus any whitespace) as a string - private static int DetermineType(string dataBeginning) + static int DetermineType(string dataBeginning) { dataBeginning = dataBeginning.Trim(); if (dataBeginning.Length == 0) @@ -64,11 +64,7 @@ namespace Claunia.PropertyList { return TYPE_ASCII; } - if (dataBeginning.StartsWith("<")) - { - return TYPE_XML; - } - return TYPE_ERROR_UNKNOWN; + return dataBeginning.StartsWith("<") ? TYPE_XML : TYPE_ERROR_UNKNOWN; } /// @@ -76,7 +72,7 @@ namespace Claunia.PropertyList /// /// The very first bytes of data of the property list (minus any whitespace) /// The type of the property list - private static int DetermineType(byte[] bytes) + static int DetermineType(byte[] bytes) { //Skip any possible whitespace at the beginning of the file int offset = 0; @@ -94,7 +90,7 @@ namespace Claunia.PropertyList /// An input stream pointing to the beginning of the property list data. /// The stream will be reset to the beginning of the property /// list data after the type has been determined. - private static int DetermineType(Stream fs) + static int DetermineType(Stream fs) { //Skip any possible whitespace at the beginning of the file byte[] magicBytes = new byte[8]; @@ -246,7 +242,7 @@ namespace Claunia.PropertyList /// Saves a property list with the given object as root into a binary file. /// /// The root object. - /// The output file. + /// The output file. /// When an error occurs during the writing process. public static void SaveAsBinary(NSObject root, FileInfo outFile) { @@ -264,7 +260,7 @@ namespace Claunia.PropertyList /// When an error occurs during the writing process. public static void SaveAsBinary(NSObject root, Stream outStream) { - //BinaryPropertyListWriter.write(outStream, root); + BinaryPropertyListWriter.Write(outStream, root); } /// diff --git a/plist-cil/UID.cs b/plist-cil/UID.cs index 7f046db..7de3798 100644 --- a/plist-cil/UID.cs +++ b/plist-cil/UID.cs @@ -34,8 +34,8 @@ namespace Claunia.PropertyList /// @author Natalia Portillo public class UID : NSObject { - byte[] bytes; - string name; + readonly byte[] bytes; + readonly string name; public UID(String name, byte[] bytes) { @@ -69,11 +69,8 @@ namespace Claunia.PropertyList { Indent(xml, level); xml.Append(""); - for (int i = 0; i < bytes.Length; i++) - { - byte b = bytes[i]; + foreach (byte b in bytes) xml.Append(String.Format("{0:x2}", b)); - } xml.Append(""); } @@ -87,11 +84,8 @@ namespace Claunia.PropertyList { Indent(ascii, level); ascii.Append("\""); - for (int i = 0; i < bytes.Length; i++) - { - byte b = bytes[i]; + foreach (byte b in bytes) ascii.Append(String.Format("{0:x2}", b)); - } ascii.Append("\""); } diff --git a/plist-cil/XmlPropertyListParser.cs b/plist-cil/XmlPropertyListParser.cs index 2439edd..6d27c2d 100644 --- a/plist-cil/XmlPropertyListParser.cs +++ b/plist-cil/XmlPropertyListParser.cs @@ -22,7 +22,6 @@ // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE // SOFTWARE. -using System; using System.Xml; using System.Collections.Generic; using System.IO; @@ -78,7 +77,7 @@ namespace Claunia.PropertyList /// /// The root NSObject of the property list contained in the XML document. /// The XML document. - private static NSObject ParseDocument(XmlDocument doc) + static NSObject ParseDocument(XmlDocument doc) { XmlDocumentType docType = doc.DocumentType; if (docType == null) @@ -100,23 +99,15 @@ namespace Claunia.PropertyList //Root element wrapped in plist tag List rootNodes = FilterElementNodes(doc.DocumentElement.ChildNodes); if (rootNodes.Count == 0) - { throw new PropertyListFormatException("The given XML property list has no root element!"); - } - else if (rootNodes.Count == 1) - { + if (rootNodes.Count == 1) rootNode = rootNodes[0]; - } else - { throw new PropertyListFormatException("The given XML property list has more than one root element!"); - } } else - { //Root NSObject not wrapped in plist-tag rootNode = doc.DocumentElement; - } return ParseObject(rootNode); } @@ -126,7 +117,7 @@ namespace Claunia.PropertyList /// /// The corresponding NSObject. /// The XML node. - private static NSObject ParseObject(XmlNode n) + static NSObject ParseObject(XmlNode n) { if (n.Name.Equals("dict")) { @@ -143,7 +134,7 @@ namespace Claunia.PropertyList } return dict; } - else if (n.Name.Equals("array")) + if (n.Name.Equals("array")) { List children = FilterElementNodes(n.ChildNodes); NSArray array = new NSArray(children.Count); @@ -153,35 +144,19 @@ namespace Claunia.PropertyList } return array; } - else if (n.Name.Equals("true")) - { + if (n.Name.Equals("true")) return new NSNumber(true); - } - else if (n.Name.Equals("false")) - { + if (n.Name.Equals("false")) return new NSNumber(false); - } - else if (n.Name.Equals("integer")) - { + if (n.Name.Equals("integer")) return new NSNumber(GetNodeTextContents(n)); - } - else if (n.Name.Equals("real")) - { + if (n.Name.Equals("real")) return new NSNumber(GetNodeTextContents(n)); - } - else if (n.Name.Equals("string")) - { + if (n.Name.Equals("string")) return new NSString(GetNodeTextContents(n)); - } - else if (n.Name.Equals("data")) - { + if (n.Name.Equals("data")) return new NSData(GetNodeTextContents(n)); - } - else if (n.Name.Equals("date")) - { - return new NSDate(GetNodeTextContents(n)); - } - return null; + return n.Name.Equals("date") ? new NSDate(GetNodeTextContents(n)) : null; } /// @@ -189,16 +164,12 @@ namespace Claunia.PropertyList /// /// The sublist containing only nodes representing actual elements. /// The list of nodes to search. - private static List FilterElementNodes(XmlNodeList list) + static List FilterElementNodes(XmlNodeList list) { List result = new List(); foreach (XmlNode child in list) - { if (child.NodeType == XmlNodeType.Element) - { result.Add(child); - } - } return result; } @@ -209,44 +180,30 @@ namespace Claunia.PropertyList /// /// The node's text content. /// The node. - private static string GetNodeTextContents(XmlNode n) + static string GetNodeTextContents(XmlNode n) { if (n.NodeType == XmlNodeType.Text || n.NodeType == XmlNodeType.CDATA) { string content = n.Value; //This concatenates any adjacent text/cdata/entity nodes - if (content == null) - return ""; - else - return content; + return content ?? ""; } - else + if (n.HasChildNodes) { - if (n.HasChildNodes) - { - XmlNodeList children = n.ChildNodes; + XmlNodeList children = n.ChildNodes; - foreach (XmlNode child in children) + foreach (XmlNode child in children) + { + //Skip any non-text nodes, like comments or entities + if (child.NodeType == XmlNodeType.Text || child.NodeType == XmlNodeType.CDATA) { - //Skip any non-text nodes, like comments or entities - if (child.NodeType == XmlNodeType.Text || child.NodeType == XmlNodeType.CDATA) - { - string content = child.Value; //This concatenates any adjacent text/cdata/entity nodes - if (content == null) - return ""; - else - return content; - } + string content = child.Value; //This concatenates any adjacent text/cdata/entity nodes + return content ?? ""; } - - return ""; - } - else - { - return ""; } + return ""; } + return ""; } - } }