Просмотр исходного кода

Silverlight compatibility other than SortedList

Jon Skeet 16 лет назад
Родитель
Сommit
3c80886fa9

+ 5 - 1
src/ProtocolBuffers/CodedInputStream.cs

@@ -160,8 +160,12 @@ namespace Google.ProtocolBuffers {
     /// Read a double field from the stream.
     /// </summary>
     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
     }
 
     /// <summary>

+ 8 - 6
src/ProtocolBuffers/CodedOutputStream.cs

@@ -114,9 +114,8 @@ namespace Google.ProtocolBuffers {
     /// Writes a double field value, including tag, to the stream.
     /// </summary>
     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);
     }
 
     /// <summary>
@@ -124,10 +123,7 @@ namespace Google.ProtocolBuffers {
     /// </summary>
     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);
     }
 
     /// <summary>
@@ -332,7 +328,13 @@ namespace Google.ProtocolBuffers {
     /// Writes a double field value, including tag, to the stream.
     /// </summary>
     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
     }
 
     /// <summary>

+ 1 - 1
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);
 
     /// <summary>
     /// Verifies that the descriptor's name is valid (i.e. it contains

+ 50 - 0
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
+{
+    /// <summary>
+    /// Class containing helpful workarounds for Silverlight compatibility
+    /// </summary>
+    internal static class SilverlightCompatibility
+    {
+
+#if SILVERLIGHT2
+        internal const RegexOptions CompiledRegexWhereAvailable = RegexOptions.None;
+#else
+        internal const RegexOptions CompiledRegexWhereAvailable = RegexOptions.Compiled;
+#endif
+
+    }
+}

+ 10 - 13
src/ProtocolBuffers/TextTokenizer.cs

@@ -69,25 +69,22 @@ namespace Google.ProtocolBuffers {
     /// </summary>
     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);
-
-    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);
+      SilverlightCompatibility.CompiledRegexWhereAvailable | RegexOptions.Multiline);
+
+    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) {