public static object Search(object needle, PhpArray haystack, bool strict) { // result needn't to be deeply copied because it is a key of an array // if (haystack == null) { PhpException.ArgumentNull("haystack"); return false; } // using operator ===: if (strict) { using (var enumerator = haystack.GetFastEnumerator()) while (enumerator.MoveNext()) { // dereferences value (because of StrictEquality operator): object val = PhpVariable.Dereference(enumerator.CurrentValue); if (Operators.StrictEquality(needle, val)) return enumerator.CurrentKey.Object; } } else { // using operator ==: using (var enumerator = haystack.GetFastEnumerator()) while (enumerator.MoveNext()) { // comparator manages references well: if (PhpComparer.CompareEq(needle, enumerator.CurrentValue)) return enumerator.CurrentKey.Object; } } // not found: return false; }
public static PhpArray Values(PhpArray array) { if (array == null) { PhpException.ArgumentNull("array"); return null; } // references are not dereferenced: PhpArray result = new PhpArray(array.Count); using (var enumerator = array.GetFastEnumerator()) while (enumerator.MoveNext()) result.AddToEnd(enumerator.CurrentValue); // result is inplace deeply copied on return to PHP code: result.InplaceCopyOnReturn = true; return result; }
public static PhpArray Filter(PHP.Core.Reflection.DTypeDesc _, PhpArray array) { var _result = new PhpArray(); using (var enumerator = array.GetFastEnumerator()) while (enumerator.MoveNext()) if (Core.Convert.ObjectToBoolean(enumerator.CurrentValue)) _result.Add(enumerator.CurrentKey, enumerator.CurrentValue); return _result; }
public static PhpArray Keys(PhpArray array, object searchValue, bool strict) { if (array == null) { PhpException.ArgumentNull("array"); return null; } PhpArray result = new PhpArray(); if (!strict) { using (var enumerator = array.GetFastEnumerator()) while (enumerator.MoveNext()) { if (PhpComparer.CompareEq(enumerator.CurrentValue, searchValue)) result.AddToEnd(enumerator.CurrentKey.Object); } } else { using (var enumerator = array.GetFastEnumerator()) while (enumerator.MoveNext()) { if (Operators.StrictEquality(enumerator.CurrentValue, searchValue)) result.AddToEnd(enumerator.CurrentKey.Object); } } // no need to make a deep copy since keys are immutable objects (strings, ints): return result; }
public static PhpArray Keys(PhpArray array) { if (array == null) { PhpException.ArgumentNull("array"); return null; } // no need to make a deep copy since keys are immutable objects (strings, ints): var result = new PhpArray(array.Count); using (var enumerator = array.GetFastEnumerator()) while (enumerator.MoveNext()) result.AddToEnd(enumerator.CurrentKey.Object); return result; }
public static PhpArray Unique(PhpArray array, ArrayUniqueSortFlags sortFlags /*= String*/) { if (array == null) { PhpException.ArgumentNull("array"); return null; } IComparer comparer; switch(sortFlags) { case ArrayUniqueSortFlags.Regular: comparer = PhpComparer.Default; break; case ArrayUniqueSortFlags.Numeric: comparer = PhpNumericComparer.Default; break; case ArrayUniqueSortFlags.String: comparer = PhpStringComparer.Default; break; case ArrayUniqueSortFlags.LocaleString: default: PhpException.ArgumentValueNotSupported("sortFlags", (int)sortFlags); return null; } PhpArray result = new PhpArray(array.Count); HashSet<object>/*!*/identitySet = new HashSet<object>(new ObjectEqualityComparer(comparer)); // get only unique values - first found using (var enumerator = array.GetFastEnumerator()) while (enumerator.MoveNext()) { if (identitySet.Add(PhpVariable.Dereference(enumerator.CurrentValue))) result.Add(enumerator.Current); } result.InplaceCopyOnReturn = true; return result; }
/// <summary> /// Adds additional arguments before arguments currently on stack. /// Used for expanding 'use' parameters of lambda function. /// </summary> internal void ExpandFrame(PhpArray useParams) { if (useParams != null && useParams.Count > 0) { ArgCount += useParams.Count; int new_top = Top + useParams.Count; if (new_top > Items.Length) ResizeItems(new_top); var stack_offset = new_top - 1; using (var enumerator = useParams.GetFastEnumerator()) while (enumerator.MoveNext()) { Items[stack_offset--] = enumerator.CurrentValue; } Top = new_top; } }
private static bool RedirectStreams(Process/*!*/ process, PhpArray/*!*/ descriptors, PhpArray/*!*/ pipes) { using (var descriptors_enum = descriptors.GetFastEnumerator()) while (descriptors_enum.MoveNext()) { int desc_no = descriptors_enum.CurrentKey.Integer; StreamAccessOptions access; Stream stream; switch (desc_no) { case 0: stream = process.StandardInput.BaseStream; access = StreamAccessOptions.Write; break; case 1: stream = process.StandardOutput.BaseStream; access = StreamAccessOptions.Read; break; case 2: stream = process.StandardError.BaseStream; access = StreamAccessOptions.Read; break; default: Debug.Fail(null); return false; } object value = PhpVariable.Dereference(descriptors_enum.CurrentValue); PhpResource resource; PhpArray array; if ((array = PhpArray.AsPhpArray(value)) != null) { if (!array.Contains(0)) { // value must be either a resource or an array: PhpException.Throw(PhpError.Warning, LibResources.GetString("descriptor_item_missing_qualifier", desc_no)); return false; } string qualifier = Core.Convert.ObjectToString(array[0]); switch (qualifier) { case "pipe": { // mode is ignored (it's determined by the stream): PhpStream php_stream = new NativeStream(stream, null, access, String.Empty, StreamContext.Default); pipes.Add(desc_no, php_stream); break; } case "file": { if (!array.Contains(1)) { PhpException.Throw(PhpError.Warning, LibResources.GetString("descriptor_item_missing_file_name", desc_no)); return false; } if (!array.Contains(2)) { PhpException.Throw(PhpError.Warning, LibResources.GetString("descriptor_item_missing_mode", desc_no)); return false; } string path = Core.Convert.ObjectToString(array[1]); string mode = Core.Convert.ObjectToString(array[2]); PhpStream php_stream = PhpStream.Open(path, mode, StreamOpenOptions.Empty, StreamContext.Default); if (php_stream == null) return false; if (!ActivePipe.BeginIO(stream, php_stream, access, desc_no)) return false; break; } default: PhpException.Throw(PhpError.Warning, LibResources.GetString("invalid_handle_qualifier", qualifier)); return false; } } else if ((resource = value as PhpResource) != null) { PhpStream php_stream = PhpStream.GetValid(resource); if (php_stream == null) return false; if (!ActivePipe.BeginIO(stream, php_stream, access, desc_no)) return false; } else { // value must be either a resource or an array: PhpException.Throw(PhpError.Warning, LibResources.GetString("descriptor_item_not_array_nor_resource", desc_no)); return false; } } return true; }