Commit 60fb63e3 authored by Jon Skeet's avatar Jon Skeet

Initial Silverlight compatibility work

parent 36721730
...@@ -116,7 +116,7 @@ namespace Google.ProtocolBuffers { ...@@ -116,7 +116,7 @@ namespace Google.ProtocolBuffers {
} }
public string ToString(Encoding encoding) { public string ToString(Encoding encoding) {
return encoding.GetString(bytes); return encoding.GetString(bytes, 0, bytes.Length);
} }
public string ToStringUtf8() { public string ToStringUtf8() {
......
...@@ -236,7 +236,7 @@ namespace Google.ProtocolBuffers { ...@@ -236,7 +236,7 @@ namespace Google.ProtocolBuffers {
return result; return result;
} }
// Slow path: Build a byte array first then copy it. // Slow path: Build a byte array first then copy it.
return Encoding.UTF8.GetString(ReadRawBytes(size)); return Encoding.UTF8.GetString(ReadRawBytes(size), 0, size);
} }
/// <summary> /// <summary>
......
...@@ -62,8 +62,8 @@ namespace Google.ProtocolBuffers { ...@@ -62,8 +62,8 @@ namespace Google.ProtocolBuffers {
} }
public static FieldSet CreateInstance() { public static FieldSet CreateInstance() {
// Use SortedList to keep fields in the canonical order // Use SortedDictionary to keep fields in the canonical order
return new FieldSet(new SortedList<FieldDescriptor, object>()); return new FieldSet(new SortedDictionary<FieldDescriptor, object>());
} }
/// <summary> /// <summary>
...@@ -82,7 +82,7 @@ namespace Google.ProtocolBuffers { ...@@ -82,7 +82,7 @@ namespace Google.ProtocolBuffers {
} }
if (hasRepeats) { if (hasRepeats) {
var tmp = new SortedList<FieldDescriptor, object>(); var tmp = new SortedDictionary<FieldDescriptor, object>();
foreach (KeyValuePair<FieldDescriptor, object> entry in fields) { foreach (KeyValuePair<FieldDescriptor, object> entry in fields) {
IList<object> list = entry.Value as IList<object>; IList<object> list = entry.Value as IList<object>;
tmp[entry.Key] = list == null ? entry.Value : Lists.AsReadOnly(list); tmp[entry.Key] = list == null ? entry.Value : Lists.AsReadOnly(list);
......
...@@ -66,8 +66,8 @@ namespace Google.ProtocolBuffers { ...@@ -66,8 +66,8 @@ namespace Google.ProtocolBuffers {
internal IDictionary<FieldDescriptor, Object> GetMutableFieldMap() { internal IDictionary<FieldDescriptor, Object> GetMutableFieldMap() {
// Use a SortedList so we'll end up serializing fields in order // Use a SortedDictionary so we'll end up serializing fields in order
var ret = new SortedList<FieldDescriptor, object>(); var ret = new SortedDictionary<FieldDescriptor, object>();
MessageDescriptor descriptor = DescriptorForType; MessageDescriptor descriptor = DescriptorForType;
foreach (FieldDescriptor field in descriptor.Fields) { foreach (FieldDescriptor field in descriptor.Fields) {
IFieldAccessor<TMessage, TBuilder> accessor = InternalFieldAccessors[field]; IFieldAccessor<TMessage, TBuilder> accessor = InternalFieldAccessors[field];
......
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Text; using System.Text;
using System.Globalization;
namespace Google.ProtocolBuffers { namespace Google.ProtocolBuffers {
/// <summary> /// <summary>
...@@ -31,7 +32,7 @@ namespace Google.ProtocolBuffers { ...@@ -31,7 +32,7 @@ namespace Google.ProtocolBuffers {
char c = input[i]; char c = input[i];
if ('a' <= c && c <= 'z') { if ('a' <= c && c <= 'z') {
if (capitaliseNext) { if (capitaliseNext) {
result.Append(char.ToUpperInvariant(c)); result.Append(char.ToUpper(c, CultureInfo.InvariantCulture));
} else { } else {
result.Append(c); result.Append(c);
} }
...@@ -40,7 +41,7 @@ namespace Google.ProtocolBuffers { ...@@ -40,7 +41,7 @@ namespace Google.ProtocolBuffers {
if (i == 0 && !pascal) { if (i == 0 && !pascal) {
// Force first letter to lower-case unless explicitly told to // Force first letter to lower-case unless explicitly told to
// capitalize it. // capitalize it.
result.Append(char.ToLowerInvariant(c)); result.Append(char.ToLower(c, CultureInfo.InvariantCulture));
} else { } else {
// Capital letters after the first are left as-is. // Capital letters after the first are left as-is.
result.Append(c); result.Append(c);
......
...@@ -537,7 +537,7 @@ namespace Google.ProtocolBuffers { ...@@ -537,7 +537,7 @@ namespace Google.ProtocolBuffers {
if (field == null) { if (field == null) {
// Explicitly specify the invariant culture so that this code does not break when // Explicitly specify the invariant culture so that this code does not break when
// executing in Turkey. // executing in Turkey.
String lowerName = name.ToLowerInvariant(); String lowerName = name.ToLower(CultureInfo.InvariantCulture);
field = type.FindDescriptor<FieldDescriptor>(lowerName); field = type.FindDescriptor<FieldDescriptor>(lowerName);
// If the case-insensitive match worked but the field is NOT a group, // If the case-insensitive match worked but the field is NOT a group,
// TODO(jonskeet): What? Java comment ends here! // TODO(jonskeet): What? Java comment ends here!
......
...@@ -69,9 +69,15 @@ namespace Google.ProtocolBuffers { ...@@ -69,9 +69,15 @@ namespace Google.ProtocolBuffers {
/// </summary> /// </summary>
private int previousColumn = 0; 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 // Note: atomic groups used to mimic possessive quantifiers in Java in both of these regexes
private static readonly Regex WhitespaceAndCommentPattern = new Regex("\\G(?>(\\s|(#.*$))+)", private static readonly Regex WhitespaceAndCommentPattern = new Regex("\\G(?>(\\s|(#.*$))+)",
RegexOptions.Compiled | RegexOptions.Multiline); CompiledRegexWhereAvailable | RegexOptions.Multiline);
private static readonly Regex TokenPattern = new Regex( private static readonly Regex TokenPattern = new Regex(
"\\G[a-zA-Z_](?>[0-9a-zA-Z_+-]*)|" + // an identifier "\\G[a-zA-Z_](?>[0-9a-zA-Z_+-]*)|" + // an identifier
"\\G[0-9+-](?>[0-9a-zA-Z_.+-]*)|" + // a number "\\G[0-9+-](?>[0-9a-zA-Z_.+-]*)|" + // a number
...@@ -79,9 +85,9 @@ namespace Google.ProtocolBuffers { ...@@ -79,9 +85,9 @@ namespace Google.ProtocolBuffers {
"\\G\'(?>([^\"\\\n\\\\]|\\\\.)*)(\'|\\\\?$)", // a single-quoted string "\\G\'(?>([^\"\\\n\\\\]|\\\\.)*)(\'|\\\\?$)", // a single-quoted string
RegexOptions.Compiled | RegexOptions.Multiline); RegexOptions.Compiled | RegexOptions.Multiline);
private static readonly Regex DoubleInfinity = new Regex("^-?inf(inity)?$", RegexOptions.Compiled | RegexOptions.IgnoreCase); private static readonly Regex DoubleInfinity = new Regex("^-?inf(inity)?$", CompiledRegexWhereAvailable | RegexOptions.IgnoreCase);
private static readonly Regex FloatInfinity = new Regex("^-?inf(inity)?f?$", RegexOptions.Compiled | RegexOptions.IgnoreCase); private static readonly Regex FloatInfinity = new Regex("^-?inf(inity)?f?$", CompiledRegexWhereAvailable | RegexOptions.IgnoreCase);
private static readonly Regex FloatNan = new Regex("^nanf?$", RegexOptions.Compiled | RegexOptions.IgnoreCase); private static readonly Regex FloatNan = new Regex("^nanf?$", CompiledRegexWhereAvailable | RegexOptions.IgnoreCase);
/** Construct a tokenizer that parses tokens from the given text. */ /** Construct a tokenizer that parses tokens from the given text. */
public TextTokenizer(string text) { public TextTokenizer(string text) {
......
...@@ -240,10 +240,10 @@ namespace Google.ProtocolBuffers { ...@@ -240,10 +240,10 @@ namespace Google.ProtocolBuffers {
public sealed class Builder public sealed class Builder
{ {
/// <summary> /// <summary>
/// Mapping from number to field. Note that by using a SortedList we ensure /// Mapping from number to field. Note that by using a SortedDictionary we ensure
/// that the fields will be serialized in ascending order. /// that the fields will be serialized in ascending order.
/// </summary> /// </summary>
private IDictionary<int, UnknownField> fields = new SortedList<int, UnknownField>(); private IDictionary<int, UnknownField> fields = new SortedDictionary<int, UnknownField>();
// Optimization: We keep around a builder for the last field that was // Optimization: We keep around a builder for the last field that was
// modified so that we can efficiently add to it multiple times in a // modified so that we can efficiently add to it multiple times in a
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment