From 3c80886fa9043cba3b0ecc0362935b014066b89f Mon Sep 17 00:00:00 2001 From: Jon Skeet Date: Wed, 9 Sep 2009 13:22:36 +0100 Subject: [PATCH] Silverlight compatibility other than SortedList --- src/ProtocolBuffers/CodedInputStream.cs | 6 ++- src/ProtocolBuffers/CodedOutputStream.cs | 14 +++--- .../Descriptors/DescriptorPool.cs | 2 +- .../SilverlightCompatibility.cs | 50 +++++++++++++++++++ src/ProtocolBuffers/TextTokenizer.cs | 21 ++++---- 5 files changed, 73 insertions(+), 20 deletions(-) create mode 100644 src/ProtocolBuffers/SilverlightCompatibility.cs diff --git a/src/ProtocolBuffers/CodedInputStream.cs b/src/ProtocolBuffers/CodedInputStream.cs index e652af0d7..ab0d32e1e 100644 --- a/src/ProtocolBuffers/CodedInputStream.cs +++ b/src/ProtocolBuffers/CodedInputStream.cs @@ -160,8 +160,12 @@ namespace Google.ProtocolBuffers { /// Read a double field from the stream. /// public double ReadDouble() { - // TODO(jonskeet): Test this on different endiannesses +#if SILVERLIGHT2 + byte[] bytes = ReadRawBytes(8); + return BitConverter.ToDouble(bytes, 0); +#else return BitConverter.Int64BitsToDouble((long) ReadRawLittleEndian64()); +#endif } /// diff --git a/src/ProtocolBuffers/CodedOutputStream.cs b/src/ProtocolBuffers/CodedOutputStream.cs index e5f890f94..241c4acba 100644 --- a/src/ProtocolBuffers/CodedOutputStream.cs +++ b/src/ProtocolBuffers/CodedOutputStream.cs @@ -114,9 +114,8 @@ namespace Google.ProtocolBuffers { /// Writes a double field value, including tag, to the stream. /// public void WriteDouble(int fieldNumber, double value) { - // TODO(jonskeet): Test this on different endiannesses WriteTag(fieldNumber, WireFormat.WireType.Fixed64); - WriteRawLittleEndian64((ulong)BitConverter.DoubleToInt64Bits(value)); + WriteDoubleNoTag(value); } /// @@ -124,10 +123,7 @@ namespace Google.ProtocolBuffers { /// public void WriteFloat(int fieldNumber, float value) { WriteTag(fieldNumber, WireFormat.WireType.Fixed32); - // TODO(jonskeet): Test this on different endiannesses - byte[] rawBytes = BitConverter.GetBytes(value); - uint asInteger = BitConverter.ToUInt32(rawBytes, 0); - WriteRawLittleEndian32(asInteger); + WriteFloatNoTag(value); } /// @@ -332,7 +328,13 @@ namespace Google.ProtocolBuffers { /// Writes a double field value, including tag, to the stream. /// public void WriteDoubleNoTag(double value) { + // TODO(jonskeet): Test this on different endiannesses +#if SILVERLIGHT2 + byte[] bytes = BitConverter.GetBytes(value); + WriteRawBytes(bytes, 0, 8); +#else WriteRawLittleEndian64((ulong)BitConverter.DoubleToInt64Bits(value)); +#endif } /// diff --git a/src/ProtocolBuffers/Descriptors/DescriptorPool.cs b/src/ProtocolBuffers/Descriptors/DescriptorPool.cs index 72dd1899b..c8654572c 100644 --- a/src/ProtocolBuffers/Descriptors/DescriptorPool.cs +++ b/src/ProtocolBuffers/Descriptors/DescriptorPool.cs @@ -139,7 +139,7 @@ namespace Google.ProtocolBuffers.Descriptors { descriptorsByName[fullName] = descriptor; } - private static readonly Regex ValidationRegex = new Regex("^[_A-Za-z][_A-Za-z0-9]*$", RegexOptions.Compiled); + private static readonly Regex ValidationRegex = new Regex("^[_A-Za-z][_A-Za-z0-9]*$", SilverlightCompatibility.CompiledRegexWhereAvailable); /// /// Verifies that the descriptor's name is valid (i.e. it contains diff --git a/src/ProtocolBuffers/SilverlightCompatibility.cs b/src/ProtocolBuffers/SilverlightCompatibility.cs new file mode 100644 index 000000000..6a7721747 --- /dev/null +++ b/src/ProtocolBuffers/SilverlightCompatibility.cs @@ -0,0 +1,50 @@ +// Protocol Buffers - Google's data interchange format +// Copyright 2008 Google Inc. All rights reserved. +// http://github.com/jskeet/dotnet-protobufs/ +// Original C++/Java/Python code: +// http://code.google.com/p/protobuf/ +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +using System.Text.RegularExpressions; + +namespace Google.ProtocolBuffers +{ + /// + /// Class containing helpful workarounds for Silverlight compatibility + /// + internal static class SilverlightCompatibility + { + +#if SILVERLIGHT2 + internal const RegexOptions CompiledRegexWhereAvailable = RegexOptions.None; +#else + internal const RegexOptions CompiledRegexWhereAvailable = RegexOptions.Compiled; +#endif + + } +} diff --git a/src/ProtocolBuffers/TextTokenizer.cs b/src/ProtocolBuffers/TextTokenizer.cs index d25a5874e..4090d1da8 100644 --- a/src/ProtocolBuffers/TextTokenizer.cs +++ b/src/ProtocolBuffers/TextTokenizer.cs @@ -69,25 +69,22 @@ namespace Google.ProtocolBuffers { /// private int previousColumn = 0; -#if SILVERLIGHT - private const RegexOptions CompiledRegexWhereAvailable = RegexOptions.None; -#else - private const RegexOptions CompiledRegexWhereAvailable = RegexOptions.Compiled; -#endif - // Note: atomic groups used to mimic possessive quantifiers in Java in both of these regexes - private static readonly Regex WhitespaceAndCommentPattern = new Regex("\\G(?>(\\s|(#.*$))+)", - CompiledRegexWhereAvailable | RegexOptions.Multiline); + internal static readonly Regex WhitespaceAndCommentPattern = new Regex("\\G(?>(\\s|(#.*$))+)", + SilverlightCompatibility.CompiledRegexWhereAvailable | RegexOptions.Multiline); private static readonly Regex TokenPattern = new Regex( "\\G[a-zA-Z_](?>[0-9a-zA-Z_+-]*)|" + // an identifier "\\G[0-9+-](?>[0-9a-zA-Z_.+-]*)|" + // a number "\\G\"(?>([^\"\\\n\\\\]|\\\\.)*)(\"|\\\\?$)|" + // a double-quoted string "\\G\'(?>([^\"\\\n\\\\]|\\\\.)*)(\'|\\\\?$)", // a single-quoted string - RegexOptions.Compiled | RegexOptions.Multiline); + SilverlightCompatibility.CompiledRegexWhereAvailable | RegexOptions.Multiline); - private static readonly Regex DoubleInfinity = new Regex("^-?inf(inity)?$", CompiledRegexWhereAvailable | RegexOptions.IgnoreCase); - private static readonly Regex FloatInfinity = new Regex("^-?inf(inity)?f?$", CompiledRegexWhereAvailable | RegexOptions.IgnoreCase); - private static readonly Regex FloatNan = new Regex("^nanf?$", CompiledRegexWhereAvailable | RegexOptions.IgnoreCase); + private static readonly Regex DoubleInfinity = new Regex("^-?inf(inity)?$", + SilverlightCompatibility.CompiledRegexWhereAvailable | RegexOptions.IgnoreCase); + private static readonly Regex FloatInfinity = new Regex("^-?inf(inity)?f?$", + SilverlightCompatibility.CompiledRegexWhereAvailable | RegexOptions.IgnoreCase); + private static readonly Regex FloatNan = new Regex("^nanf?$", + SilverlightCompatibility.CompiledRegexWhereAvailable | RegexOptions.IgnoreCase); /** Construct a tokenizer that parses tokens from the given text. */ public TextTokenizer(string text) {