Commit 0d91952a authored by csharptest's avatar csharptest Committed by rogerk

Refactoring compatibility code to use FrameworkPortability class

parent 14e011f6
...@@ -698,28 +698,5 @@ namespace Google.ProtocolBuffers.Serialization ...@@ -698,28 +698,5 @@ namespace Google.ProtocolBuffers.Serialization
} }
#endregion #endregion
internal static bool TryParseInt32(string text, out int number)
{
return TryParseInt32(text, NumberStyles.Any, CultureInfo.InvariantCulture, out number);
}
internal static bool TryParseInt32(string text, NumberStyles style, IFormatProvider format, out int number)
{
#if COMPACT_FRAMEWORK
try
{
number = int.Parse(text, NumberStyles.Integer, CultureInfo.InvariantCulture);
return true;
}
catch
{
number = 0;
return false;
}
#else
return int.TryParse(text, NumberStyles.Integer, CultureInfo.InvariantCulture, out number);
#endif
}
} }
} }
\ No newline at end of file
...@@ -163,7 +163,7 @@ namespace Google.ProtocolBuffers.Serialization ...@@ -163,7 +163,7 @@ namespace Google.ProtocolBuffers.Serialization
if (ReadAsText(ref text, typeof(Enum))) if (ReadAsText(ref text, typeof(Enum)))
{ {
int number; int number;
if (TryParseInt32(text, NumberStyles.Integer, CultureInfo.InvariantCulture, out number)) if (FrameworkPortability.TryParseInt32(text, NumberStyles.Integer, FrameworkPortability.InvariantCulture, out number))
{ {
value = number; value = number;
return true; return true;
......
...@@ -157,8 +157,8 @@ namespace Google.ProtocolBuffers.Serialization ...@@ -157,8 +157,8 @@ namespace Google.ProtocolBuffers.Serialization
} }
else if (value is IConvertible) else if (value is IConvertible)
{ {
WriteEnum(field, ((IConvertible) value).ToInt32(CultureInfo.InvariantCulture), WriteEnum(field, ((IConvertible)value).ToInt32(FrameworkPortability.InvariantCulture),
((IConvertible) value).ToString(CultureInfo.InvariantCulture)); ((IConvertible)value).ToString(FrameworkPortability.InvariantCulture));
} }
else else
{ {
......
...@@ -82,7 +82,7 @@ namespace Google.ProtocolBuffers.Serialization ...@@ -82,7 +82,7 @@ namespace Google.ProtocolBuffers.Serialization
{ {
if (obj is IConvertible) if (obj is IConvertible)
{ {
value = (T) Convert.ChangeType(obj, typeof(T), CultureInfo.InvariantCulture); value = (T)Convert.ChangeType(obj, typeof(T), FrameworkPortability.InvariantCulture);
} }
else else
{ {
......
...@@ -195,7 +195,7 @@ namespace Google.ProtocolBuffers.Serialization ...@@ -195,7 +195,7 @@ namespace Google.ProtocolBuffers.Serialization
if (!cond) if (!cond)
{ {
throw new FormatException( throw new FormatException(
String.Format(CultureInfo.InvariantCulture, String.Format(FrameworkPortability.InvariantCulture,
"({0}:{1}) error: Unexpected token {2}, expected: {3}.", "({0}:{1}) error: Unexpected token {2}, expected: {3}.",
_lineNo, _linePos, _lineNo, _linePos,
CharDisplay(_next), CharDisplay(_next),
...@@ -210,7 +210,7 @@ namespace Google.ProtocolBuffers.Serialization ...@@ -210,7 +210,7 @@ namespace Google.ProtocolBuffers.Serialization
if (!cond) if (!cond)
{ {
throw new FormatException( throw new FormatException(
String.Format(CultureInfo.InvariantCulture, String.Format(FrameworkPortability.InvariantCulture,
"({0},{1}) error: {2}", _lineNo, _linePos, message)); "({0},{1}) error: {2}", _lineNo, _linePos, message));
} }
} }
...@@ -225,7 +225,7 @@ namespace Google.ProtocolBuffers.Serialization ...@@ -225,7 +225,7 @@ namespace Google.ProtocolBuffers.Serialization
format = String.Format(format, args); format = String.Format(format, args);
} }
throw new FormatException( throw new FormatException(
String.Format(CultureInfo.InvariantCulture, String.Format(FrameworkPortability.InvariantCulture,
"({0},{1}) error: {2}", _lineNo, _linePos, format)); "({0},{1}) error: {2}", _lineNo, _linePos, format));
} }
} }
...@@ -322,7 +322,7 @@ namespace Google.ProtocolBuffers.Serialization ...@@ -322,7 +322,7 @@ namespace Google.ProtocolBuffers.Serialization
string hex = new string(new char[] {ReadChar(), ReadChar(), ReadChar(), ReadChar()}); string hex = new string(new char[] {ReadChar(), ReadChar(), ReadChar(), ReadChar()});
int result; int result;
Assert( Assert(
AbstractTextReader.TryParseInt32(hex, NumberStyles.AllowHexSpecifier, CultureInfo.InvariantCulture, FrameworkPortability.TryParseInt32(hex, NumberStyles.AllowHexSpecifier, FrameworkPortability.InvariantCulture,
out result), out result),
"Expected a 4-character hex specifier."); "Expected a 4-character hex specifier.");
sb.Add((char) result); sb.Add((char) result);
......
...@@ -269,7 +269,7 @@ namespace Google.ProtocolBuffers.Serialization ...@@ -269,7 +269,7 @@ namespace Google.ProtocolBuffers.Serialization
{ {
int number; int number;
string temp; string temp;
if (null != (temp = _input.GetAttribute("value")) && TryParseInt32(temp, out number)) if (null != (temp = _input.GetAttribute("value")) && FrameworkPortability.TryParseInt32(temp, out number))
{ {
Skip(); Skip();
value = number; value = number;
......
...@@ -575,7 +575,7 @@ namespace Google.ProtocolBuffers ...@@ -575,7 +575,7 @@ namespace Google.ProtocolBuffers
case FieldType.Enum: case FieldType.Enum:
if (value is Enum) if (value is Enum)
{ {
return ComputeEnumSize(fieldNumber, ((IConvertible) value).ToInt32(CultureInfo.InvariantCulture)); return ComputeEnumSize(fieldNumber, ((IConvertible)value).ToInt32(FrameworkPortability.InvariantCulture));
} }
else else
{ {
...@@ -631,7 +631,7 @@ namespace Google.ProtocolBuffers ...@@ -631,7 +631,7 @@ namespace Google.ProtocolBuffers
case FieldType.Enum: case FieldType.Enum:
if (value is Enum) if (value is Enum)
{ {
return ComputeEnumSizeNoTag(((IConvertible) value).ToInt32(CultureInfo.InvariantCulture)); return ComputeEnumSizeNoTag(((IConvertible)value).ToInt32(FrameworkPortability.InvariantCulture));
} }
else else
{ {
......
...@@ -50,11 +50,5 @@ namespace Google.ProtocolBuffers ...@@ -50,11 +50,5 @@ namespace Google.ProtocolBuffers
internal delegate TResult Func<T1, T2, TResult>(T1 arg1, T2 arg2); internal delegate TResult Func<T1, T2, TResult>(T1 arg1, T2 arg2);
internal delegate TResult Func<T1, T2, T3, TResult>(T1 arg1, T2 arg2, T3 arg3);
internal delegate TResult Func<T1, T2, T3, T4, TResult>(T1 arg1, T2 arg2, T3 arg3, T4 arg4);
internal delegate void Action();
internal delegate void Action<T1, T2>(T1 arg1, T2 arg2); internal delegate void Action<T1, T2>(T1 arg1, T2 arg2);
} }
\ No newline at end of file
...@@ -111,7 +111,7 @@ namespace Google.ProtocolBuffers ...@@ -111,7 +111,7 @@ namespace Google.ProtocolBuffers
} }
else else
{ {
writer.WriteLine("{0}: {1}", name, ((IConvertible) value).ToString(CultureInfo.InvariantCulture)); writer.WriteLine("{0}: {1}", name, ((IConvertible)value).ToString(FrameworkPortability.InvariantCulture));
} }
} }
......
...@@ -170,10 +170,10 @@ namespace Google.ProtocolBuffers ...@@ -170,10 +170,10 @@ namespace Google.ProtocolBuffers
// the double to/from string will trim the precision to 6 places. As with other numeric formats // the double to/from string will trim the precision to 6 places. As with other numeric formats
// below, always use the invariant culture so it's predictable. // below, always use the invariant culture so it's predictable.
case FieldType.Float: case FieldType.Float:
generator.Print(((float) value).ToString("r", CultureInfo.InvariantCulture)); generator.Print(((float)value).ToString("r", FrameworkPortability.InvariantCulture));
break; break;
case FieldType.Double: case FieldType.Double:
generator.Print(((double) value).ToString("r", CultureInfo.InvariantCulture)); generator.Print(((double)value).ToString("r", FrameworkPortability.InvariantCulture));
break; break;
case FieldType.Int32: case FieldType.Int32:
...@@ -188,7 +188,7 @@ namespace Google.ProtocolBuffers ...@@ -188,7 +188,7 @@ namespace Google.ProtocolBuffers
case FieldType.Fixed64: case FieldType.Fixed64:
// The simple Object.ToString converts using the current culture. // The simple Object.ToString converts using the current culture.
// We want to always use the invariant culture so it's predictable. // We want to always use the invariant culture so it's predictable.
generator.Print(((IConvertible) value).ToString(CultureInfo.InvariantCulture)); generator.Print(((IConvertible)value).ToString(FrameworkPortability.InvariantCulture));
break; break;
case FieldType.Bool: case FieldType.Bool:
// Explicitly use the Java true/false // Explicitly use the Java true/false
...@@ -314,7 +314,7 @@ namespace Google.ProtocolBuffers ...@@ -314,7 +314,7 @@ namespace Google.ProtocolBuffers
case "nanf": case "nanf":
return float.NaN; return float.NaN;
default: default:
return float.Parse(text, CultureInfo.InvariantCulture); return float.Parse(text, FrameworkPortability.InvariantCulture);
} }
} }
...@@ -331,7 +331,7 @@ namespace Google.ProtocolBuffers ...@@ -331,7 +331,7 @@ namespace Google.ProtocolBuffers
case "nan": case "nan":
return double.NaN; return double.NaN;
default: default:
return double.Parse(text, CultureInfo.InvariantCulture); return double.Parse(text, FrameworkPortability.InvariantCulture);
} }
} }
...@@ -708,7 +708,7 @@ namespace Google.ProtocolBuffers ...@@ -708,7 +708,7 @@ namespace Google.ProtocolBuffers
{ {
// 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.ToLower(CultureInfo.InvariantCulture); String lowerName = name.ToLower(FrameworkPortability.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!
......
...@@ -346,7 +346,7 @@ namespace Google.ProtocolBuffers ...@@ -346,7 +346,7 @@ namespace Google.ProtocolBuffers
try try
{ {
double result = double.Parse(currentToken, CultureInfo.InvariantCulture); double result = double.Parse(currentToken, FrameworkPortability.InvariantCulture);
NextToken(); NextToken();
return result; return result;
} }
...@@ -387,7 +387,7 @@ namespace Google.ProtocolBuffers ...@@ -387,7 +387,7 @@ namespace Google.ProtocolBuffers
try try
{ {
float result = float.Parse(currentToken, CultureInfo.InvariantCulture); float result = float.Parse(currentToken, FrameworkPortability.InvariantCulture);
NextToken(); NextToken();
return result; return result;
} }
......
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