Skip to content

Commit 5eaa3b0

Browse files
committed
implicit conversion PhpArray -> ReadOnlySpan<PhpValue>
1 parent d9e58d4 commit 5eaa3b0

File tree

4 files changed

+40
-0
lines changed

4 files changed

+40
-0
lines changed

src/Peachpie.CodeAnalysis/Semantics/Conversions.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,9 @@ string[] ImplicitConversionOpNames(TypeSymbol target)
368368
// ReadOnlySpan<char> == string.AsSpan()
369369
if (target.IsReadOnlySpan(_compilation.GetSpecialType(SpecialType.System_Char))) return new[] { WellKnownMemberNames.ImplicitConversionName, "ToString" };
370370

371+
// ReadOnlySpan<PhpValue>
372+
if (target.IsReadOnlySpan(_compilation.CoreTypes.PhpValue.Symbol)) return new[] { WellKnownMemberNames.ImplicitConversionName, "AsSpan", "GetSpan" };
373+
371374
// AsResource
372375
// AsObject
373376
// AsPhpValue

src/Peachpie.Runtime/Conversions.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,12 @@ public static bool IsInstanceOf(object obj, PhpTypeInfo tinfo)
445445

446446
#endregion
447447

448+
#region ToSpan
449+
450+
public static ReadOnlySpan<PhpValue> ToSpan(PhpArray array) => array.GetSpan();
451+
452+
#endregion
453+
448454
#region ToDictionary
449455

450456
public static Dictionary<K, V>/*!*/ToDictionary<K, V>(this PhpValue value)

src/Peachpie.Runtime/Dynamic/ConvertExpression.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,13 @@ public static Expression Bind(Expression arg, Type target, Expression ctx)
157157
{
158158
return BindToDateTime(arg, ctx);
159159
}
160+
161+
// ReadOnlySpan<char>
162+
if (target == typeof(ReadOnlySpan<char>))
163+
{
164+
// (ReadOnlySpan<char>)ToPhpString() // implicit operator
165+
return Expression.Convert(BindToPhpString(arg, ctx), target);
166+
}
160167
}
161168

162169
// Template: PhpValueConverter.Cast<T>( PhpValue ) : T

src/Peachpie.Runtime/PhpHashtable.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Diagnostics;
55
using System.Linq;
66
using System.Runtime.CompilerServices;
7+
using System.Runtime.InteropServices;
78
using System.Text;
89
using System.Threading.Tasks;
910
using Pchp.Core.Utilities;
@@ -927,6 +928,29 @@ public void AddTo(PhpHashtable/*!*/dst, bool deepCopy)
927928
/// </summary>
928929
public PhpValue[] GetValues() => table.GetValues();
929930

931+
/// <summary>
932+
/// Copies values to a new array and get it as a span.
933+
/// </summary>
934+
public ReadOnlySpan<PhpValue> GetSpan()
935+
{
936+
if (table.Count == 0)
937+
{
938+
return default;
939+
}
940+
941+
if (table.Count == 1)
942+
{
943+
var e = table.GetEnumerator();
944+
if (e.MoveNext()) // always true
945+
{
946+
return MemoryMarshal.CreateReadOnlySpan(ref e.CurrentValue, 1);
947+
}
948+
}
949+
950+
// copy array and get span
951+
return GetValues().AsSpan();
952+
}
953+
930954
#endregion
931955

932956
#region Misc methods: Sort, Diff, Reverse, Shuffle, Unite

0 commit comments

Comments
 (0)