Skip to content

Commit

Permalink
Locale-independent float to string casting
Browse files Browse the repository at this point in the history
ref #765
  • Loading branch information
jakubmisek committed Dec 9, 2020
1 parent 0e5a954 commit a960e1f
Show file tree
Hide file tree
Showing 8 changed files with 23 additions and 32 deletions.
4 changes: 2 additions & 2 deletions src/Peachpie.CodeAnalysis/Symbols/CoreMembers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ public OperatorsHolder(CoreTypes ct)
ToString_Bool = ct.Convert.Method("ToString", ct.Boolean);
ToString_Int32 = ct.Convert.Method("ToString", ct.Int32);
ToString_Long = ct.Convert.Method("ToString", ct.Long);
ToString_Double_Context = ct.Convert.Method("ToString", ct.Double, ct.Context);
ToString_Double = ct.Convert.Method("ToString", ct.Double);
Long_ToString = ct.Long.Method("ToString");
ToChar_String = ct.Convert.Method("ToChar", ct.String);
ToNumber_PhpValue = ct.Convert.Method("ToNumber", ct.PhpValue);
Expand Down Expand Up @@ -435,7 +435,7 @@ public readonly CoreMethod
EnsureItemObject_IPhpArray_PhpValue,
IsSet_PhpValue, IsEmpty_PhpValue, IsNullOrEmpty_String, Concat_String_String,
ToBoolean_String, ToBoolean_PhpString,
ToString_Bool, ToString_Long, ToString_Int32, ToString_Double_Context, Long_ToString,
ToString_Bool, ToString_Long, ToString_Int32, ToString_Double, Long_ToString,
ToChar_String,
ToNumber_PhpValue, ToNumber_String, ToArrayOrThrow_PhpValue,
AsObject_PhpValue, AsArray_PhpValue, ToArray_PhpValue, GetArrayAccess_PhpValueRef, ToPhpString_PhpValue_Context, ToClass_PhpValue, ToClass_IPhpArray, AsCallable_PhpValue_RuntimeTypeHandle_Object, AsCallable_String_RuntimeTypeHandle_Object,
Expand Down
31 changes: 14 additions & 17 deletions src/Peachpie.Library/Variables.cs
Original file line number Diff line number Diff line change
Expand Up @@ -861,18 +861,15 @@ public static int extract(Context ctx, [ImportValue(ImportValueAttribute.ValueSp

abstract class FormatterVisitor : PhpVariableVisitor, IPhpVariableFormatter
{
readonly protected Context _ctx;
readonly protected string _nl;

protected PhpString.Blob _output;
protected int _indent;

protected const string RECURSION = "*RECURSION*";

protected FormatterVisitor(Context ctx, string newline = "\n")
protected FormatterVisitor(string newline = "\n")
{
Debug.Assert(ctx != null);
_ctx = ctx;
_nl = newline;
}

Expand Down Expand Up @@ -931,16 +928,16 @@ void OutputIndent()
}
}

public PrintFormatter(Context ctx, string newline = "\n")
: base(ctx, newline)
public PrintFormatter(string newline = "\n")
: base(newline)
{
}

public override void Accept(bool obj) => _output.Append(obj ? "1" : string.Empty);

public override void Accept(long obj) => _output.Append(obj.ToString());

public override void Accept(double obj) => _output.Append(Core.Convert.ToString(obj, _ctx));
public override void Accept(double obj) => _output.Append(Core.Convert.ToString(obj));

public override void Accept(string obj) => _output.Append(obj);

Expand Down Expand Up @@ -1067,16 +1064,16 @@ void OutputIndent()
}
}

public ExportFormatter(Context ctx, string newline = "\n")
: base(ctx, newline)
public ExportFormatter(string newline = "\n")
: base(newline)
{
}

public override void Accept(bool obj) => _output.Append(obj ? PhpVariable.True : PhpVariable.False);

public override void Accept(long obj) => _output.Append(obj.ToString());

public override void Accept(double obj) => _output.Append(Core.Convert.ToString(obj, _ctx));
public override void Accept(double obj) => _output.Append(Core.Convert.ToString(obj));

public override void Accept(string obj)
{
Expand Down Expand Up @@ -1266,8 +1263,8 @@ void OutputIndent()
}
}

public DumpFormatter(Context ctx, string newline = "\n", bool verbose = false)
: base(ctx, newline)
public DumpFormatter(string newline = "\n", bool verbose = false)
: base(newline)
{
this.Verbose = verbose;
}
Expand Down Expand Up @@ -1299,7 +1296,7 @@ public override void Accept(double obj)
{
_output.Append(PhpVariable.TypeNameDouble);
_output.Append("(");
_output.Append(Core.Convert.ToString(obj, _ctx));
_output.Append(Core.Convert.ToString(obj));
_output.Append(")");
}

Expand Down Expand Up @@ -1447,7 +1444,7 @@ public override void AcceptObject(object obj)
/// <returns>A string representation or <c>true</c> if <paramref name="returnString"/> is <c>false</c>.</returns>
public static PhpValue print_r(Context ctx, PhpValue value, bool returnString = false)
{
var output = (new PrintFormatter(ctx)).Serialize(value);
var output = new PrintFormatter().Serialize(value);

if (returnString)
{
Expand All @@ -1469,7 +1466,7 @@ public static PhpValue print_r(Context ctx, PhpValue value, bool returnString =
/// <param name="variables">Variables to be dumped.</param>
public static void var_dump(Context ctx, params PhpValue[] variables)
{
var formatter = new DumpFormatter(ctx); // TODO: HtmlDumpFormatter
var formatter = new DumpFormatter(); // TODO: HtmlDumpFormatter
for (int i = 0; i < variables.Length; i++)
{
ctx.Echo(formatter.Serialize(variables[i].GetValue()));
Expand All @@ -1483,7 +1480,7 @@ public static void var_dump(Context ctx, params PhpValue[] variables)
/// <param name="variables">Variables to be dumped.</param>
public static void debug_zval_dump(Context ctx, params PhpValue[] variables)
{
var formatter = new DumpFormatter(ctx, verbose: true);
var formatter = new DumpFormatter(verbose: true);

for (int i = 0; i < variables.Length; i++)
{
Expand All @@ -1500,7 +1497,7 @@ public static void debug_zval_dump(Context ctx, params PhpValue[] variables)
/// <returns>A string representation or a <c>null</c> reference if <paramref name="returnString"/> is <c>false</c>.</returns>
public static string var_export(Context ctx, PhpValue variable, bool returnString = false)
{
var output = (new ExportFormatter(ctx)).Serialize(variable);
var output = new ExportFormatter().Serialize(variable);

if (returnString)
{
Expand Down
2 changes: 1 addition & 1 deletion src/Peachpie.Runtime/Context.Output.cs
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ public void Echo(PhpNumber value)

public void Echo(double value)
{
Output.Write(Convert.ToString(value, this));
Output.Write(Convert.ToString(value));
}

public void Echo(long value)
Expand Down
7 changes: 1 addition & 6 deletions src/Peachpie.Runtime/Conversions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,6 @@ public static class Convert
/// </summary>
public static string ToString(double value) => value.ToString("G", Context.InvariantNumberFormatInfo);

/// <summary>
/// Gets string representation of a floating point number value.
/// </summary>
public static string ToString(double value, Context ctx) => value.ToString("G", ctx.NumberFormat);

public static string ToString(IPhpConvertible value, Context ctx) => value.ToString(ctx);

/// <summary>
Expand Down Expand Up @@ -1438,7 +1433,7 @@ public static double ToDouble(PhpValue value)
PhpTypeCode.Null => null, // TODO: support nullable conversion, target parameter can be either `string` or `string?`
PhpTypeCode.Boolean => Convert.ToString(value.Boolean),
PhpTypeCode.Long => value.Long.ToString(),
PhpTypeCode.Double => Convert.ToString(value.Double, ctx),
PhpTypeCode.Double => Convert.ToString(value.Double),
PhpTypeCode.String => value.String,
PhpTypeCode.MutableString => value.MutableStringBlob.ToString(ctx.StringEncoding),
PhpTypeCode.Object => Convert.ToString(value.Object, ctx),
Expand Down
3 changes: 1 addition & 2 deletions src/Peachpie.Runtime/Dynamic/Cache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public static class Operators
public static MethodInfo SetValue_PhpValueRef_PhpValue = typeof(Core.Operators).GetMethod("SetValue", Types.PhpValue.MakeByRefType(), Types.PhpValue);
public static MethodInfo IsSet_PhpValue = new Func<PhpValue, bool>(Core.Operators.IsSet).Method;

public static MethodInfo ToString_Double_Context = new Func<double, Context, string>(Core.Convert.ToString).Method;
public static MethodInfo ToString_Double = new Func<double, string>(Core.Convert.ToString).Method;
public static MethodInfo ToLongOrThrow_String = new Func<string, long>(Core.StrictConvert.ToLong).Method;
public static MethodInfo ToDouble_String = new Func<string, double>(Core.Convert.StringToDouble).Method;
public static MethodInfo ToPhpString_PhpValue_Context = new Func<PhpValue, Context, Core.PhpString>(Core.Convert.ToPhpString).Method;
Expand Down Expand Up @@ -166,7 +166,6 @@ public static class Object
/// <summary><see cref="System.Object"/>.</summary>
public static new MethodInfo ToString = typeof(object).GetMethod("ToString", Types.Empty);
public static readonly MethodInfo ToString_Bool = typeof(Core.Convert).GetMethod("ToString", Types.Bool);
public static readonly MethodInfo ToString_Double_Context = typeof(Core.Convert).GetMethod("ToString", Types.Double[0], typeof(Context));
}

public static class Expressions
Expand Down
4 changes: 2 additions & 2 deletions src/Peachpie.Runtime/Dynamic/ConvertExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -348,10 +348,10 @@ private static Expression BindToString(Expression expr, Expression ctx)
return Expression.Call(expr, Cache.Object.ToString);

if (source == typeof(double))
return Expression.Call(Cache.Object.ToString_Double_Context, expr, ctx);
return Expression.Call(Cache.Operators.ToString_Double, expr);

if (source == typeof(float))
return Expression.Call(Cache.Object.ToString_Double_Context, Expression.Convert(expr, typeof(double)), ctx); // ToString((double)expr, ctx)
return Expression.Call(Cache.Operators.ToString_Double, Expression.Convert(expr, typeof(double))); // ToString((double)expr)

if (source == typeof(bool))
return Expression.Call(Cache.Object.ToString_Bool, expr);
Expand Down
2 changes: 1 addition & 1 deletion src/Peachpie.Runtime/PhpNumber.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1360,7 +1360,7 @@ public Convert.NumberInfo ToNumber(out PhpNumber number)
/// </summary>
public string ToString(Context ctx)
{
return IsLong ? _long.ToString() : Convert.ToString(_double, ctx);
return IsLong ? _long.ToString() : Convert.ToString(_double);
}

public object ToClass() => ToObject();
Expand Down
2 changes: 1 addition & 1 deletion src/Peachpie.Runtime/PhpValue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,7 @@ public Convert.NumberInfo ToNumber(out PhpNumber number)
PhpTypeCode.Null => string.Empty,
PhpTypeCode.Boolean => Convert.ToString(Boolean),
PhpTypeCode.Long => Long.ToString(),
PhpTypeCode.Double => Convert.ToString(Double, ctx),
PhpTypeCode.Double => Convert.ToString(Double),
PhpTypeCode.PhpArray => (string)Array,
PhpTypeCode.String => String,
PhpTypeCode.MutableString => MutableStringBlob.ToString(ctx.StringEncoding),
Expand Down

0 comments on commit a960e1f

Please sign in to comment.