arraymodule.java

来自「RESIN 3.2 最新源码」· Java 代码 · 共 2,383 行 · 第 1/5 页

JAVA
2,383
字号
/* * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved * * This file is part of Resin(R) Open Source * * Each copy or derived work must preserve the copyright notice and this * notice unmodified. * * Resin Open Source is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * Resin Open Source is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty * of NON-INFRINGEMENT.  See the GNU General Public License for more * details. * * You should have received a copy of the GNU General Public License * along with Resin Open Source; if not, write to the * *   Free Software Foundation, Inc. *   59 Temple Place, Suite 330 *   Boston, MA 02111-1307  USA * * @author Scott Ferguson */package com.caucho.quercus.lib;import com.caucho.quercus.QuercusModuleException;import com.caucho.quercus.annotation.Optional;import com.caucho.quercus.annotation.ReadOnly;import com.caucho.quercus.annotation.Reference;import com.caucho.quercus.annotation.UsesSymbolTable;import com.caucho.quercus.annotation.NotNull;import com.caucho.quercus.env.*;import com.caucho.quercus.env.ArrayValue.AbstractGet;import com.caucho.quercus.env.ArrayValue.GetKey;import com.caucho.quercus.env.ArrayValue.KeyComparator;import com.caucho.quercus.env.ArrayValue.ValueComparator;import com.caucho.quercus.module.AbstractQuercusModule;import com.caucho.quercus.program.AbstractFunction;import com.caucho.util.L10N;import com.caucho.util.RandomUtil;import java.text.Collator;import java.util.Comparator;import java.util.Iterator;import java.util.Locale;import java.util.Map;import java.util.logging.Level;import java.util.logging.Logger;/** * PHP array routines. */public class ArrayModule  extends AbstractQuercusModule{  private static final L10N L = new L10N(ArrayModule.class);  private static final Logger log =    Logger.getLogger(ArrayModule.class.getName());  public static final int CASE_UPPER = 2;  public static final int CASE_LOWER = 1;  public static final int SORT_REGULAR = 0;  public static final int SORT_NUMERIC = 1;  public static final int SORT_STRING = 2;  public static final int SORT_LOCALE_STRING = 5;  public static final int SORT_NORMAL = 1;  public static final int SORT_REVERSE = -1;  public static final int SORT_DESC = 3;  public static final int SORT_ASC = 4;    public static final int EXTR_OVERWRITE = 0;  public static final int EXTR_SKIP = 1;  public static final int EXTR_PREFIX_SAME = 2;  public static final int EXTR_PREFIX_ALL = 3;  public static final int EXTR_PREFIX_INVALID = 4;  public static final int EXTR_IF_EXISTS = 6;  public static final int EXTR_PREFIX_IF_EXISTS = 5;  public static final int EXTR_REFS = 256;  public static final boolean CASE_SENSITIVE = true;  public static final boolean CASE_INSENSITIVE = false;  public static final boolean KEY_RESET = true;  public static final boolean NO_KEY_RESET = false;  public static final boolean STRICT = true;  public static final boolean NOT_STRICT = false;  private static final CompareString CS_VALUE_NORMAL    = new CompareString(ArrayValue.GET_VALUE, SORT_NORMAL);  private static final CompareString CS_VALUE_REVERSE    = new CompareString(ArrayValue.GET_VALUE, SORT_REVERSE);  private static final CompareString CS_KEY_NORMAL    = new CompareString(ArrayValue.GET_KEY, SORT_NORMAL);  private static final CompareString CS_KEY_REVERSE    = new CompareString(ArrayValue.GET_KEY, SORT_REVERSE);  private static final CompareNumeric CN_VALUE_NORMAL    = new CompareNumeric(ArrayValue.GET_VALUE, SORT_NORMAL);  private static final CompareNumeric CN_VALUE_REVERSE    = new CompareNumeric(ArrayValue.GET_VALUE, SORT_REVERSE);  private static final CompareNumeric CN_KEY_NORMAL    = new CompareNumeric(ArrayValue.GET_KEY, SORT_NORMAL);  private static final CompareNumeric CN_KEY_REVERSE    = new CompareNumeric(ArrayValue.GET_KEY, SORT_REVERSE);  private static final CompareNormal CNO_VALUE_NORMAL    = new CompareNormal(ArrayValue.GET_VALUE, SORT_NORMAL);  private static final CompareNormal CNO_VALUE_REVERSE    = new CompareNormal(ArrayValue.GET_VALUE, SORT_REVERSE);  private static final CompareNormal CNO_KEY_NORMAL    = new CompareNormal(ArrayValue.GET_KEY, SORT_NORMAL);  private static final CompareNormal CNO_KEY_REVERSE    = new CompareNormal(ArrayValue.GET_KEY, SORT_REVERSE);  private static final CompareNatural CNA_VALUE_NORMAL_SENSITIVE    = new CompareNatural(ArrayValue.GET_VALUE, SORT_NORMAL, CASE_SENSITIVE);  private static final CompareNatural CNA_VALUE_NORMAL_INSENSITIVE    = new CompareNatural(ArrayValue.GET_VALUE, SORT_NORMAL, CASE_INSENSITIVE);  /**   * Returns true for the mysql extension.   */  public String []getLoadedExtensions()  {    return new String[] { "standard" };  }  /**   * Changes the key case   */  public Value array_change_key_case(Env env,				     ArrayValue array,                                     @Optional("CASE_LOWER") int toCase)  {    if (array == null)      return BooleanValue.FALSE;    ArrayValue newArray = new ArrayValueImpl();    for (Map.Entry<Value, Value> entry : array.entrySet()) {      Value keyValue = entry.getKey();      if (keyValue instanceof StringValue) {        String key = keyValue.toString();        if (toCase == CASE_UPPER)          key = key.toUpperCase();        else          key = key.toLowerCase();        newArray.put(env.createString(key), entry.getValue());      }      else        newArray.put(keyValue, entry.getValue());    }    return newArray;  }  /**   * Chunks the array   */  public Value array_chunk(Env env,                           ArrayValue array,                           int size,                           @Optional boolean preserveKeys)  {    if (array == null)      return NullValue.NULL;    ArrayValue newArray = new ArrayValueImpl();    ArrayValue currentArray = null;    if (size < 1) {      env.warning("Size parameter expected to be greater than 0");      return NullValue.NULL;    }    int i = 0;    for (Map.Entry<Value, Value> entry : array.entrySet()) {      Value key = entry.getKey();      Value value = entry.getKey();      if (i % size == 0) {        currentArray = new ArrayValueImpl();        newArray.put(currentArray);      }      if (preserveKeys)        currentArray.put(key, value);      else        currentArray.put(new LongValue(i % size), value);      i++;    }    return newArray;  }  /**   * Combines array   */  public Value array_combine(Env env, ArrayValue keys,                             ArrayValue values)  {    if (keys == null || values == null)      return BooleanValue.FALSE;    if (keys.getSize() < 1 || values.getSize() < 1) {      env.warning("Both parameters should have at least 1 element");      return BooleanValue.FALSE;    }    if (keys.getSize() != values.getSize()) {      env.warning("Both parameters should have equal number of elements");      return BooleanValue.FALSE;    }    Iterator<Value> keyIter = keys.values().iterator();    Iterator<Value> valueIter = values.values().iterator();    ArrayValue array = new ArrayValueImpl();    while (keyIter.hasNext() && valueIter.hasNext()) {      array.put(keyIter.next(), valueIter.next());    }    return array;  }  /**   * Counts the values   */  public Value array_count_values(Env env, ArrayValue array)  {    if (array == null)      return NullValue.NULL;    ArrayValue result = new ArrayValueImpl();    for (Value value : array.values()) {      if (! (value.isLongConvertible()) && ! (value instanceof StringValue))        env.warning("Can only count STRING and INTEGER values!");      else {        Value count = result.get(value);        if (count == null)          count = new LongValue(1);        else          count = count.add(1);        result.put(value, count);      }    }    return result;  }  /**   * Pops off the top element   */  public Value array_pop(Env env, @Reference Value value)  {    if (value.isArray()) {      ArrayValue array = value.toArrayValue(env);            if (array.getSize() <= 0)        return NullValue.NULL;            return array.pop();    }    else {      env.warning(L.l("expected an array but saw {0}",                      value.toValue().getClass().getSimpleName()));      return NullValue.NULL;    }  }  /**   * Returns the size of the array.   */  public static Value count(Env env,                            @ReadOnly Value value,                            @Optional("false") boolean recursive)  {    if (! recursive)      return LongValue.create(value.getCount(env));    else      return LongValue.create(value.getCountRecursive(env));  }  /**   * Returns the current value of the array.   */  public static Value current(@ReadOnly Value value)  {    if (value instanceof ArrayValue) {      ArrayValue array = (ArrayValue) value;      return array.current();    }    else      return BooleanValue.FALSE;  }  /**   * Returns the current key of the array.   */  public static Value key(@ReadOnly Value value)  {    if (value instanceof ArrayValue) {      ArrayValue array = (ArrayValue) value;      return array.key();    }    else      return BooleanValue.FALSE;  }  /**   * Returns the current value of the array.   */  public static Value pos(@ReadOnly Value value)  {    return current(value);  }  /**   * Returns the next value of the array.   */  public static Value next(Value value)  {    if (value instanceof ArrayValue) {      ArrayValue array = (ArrayValue) value;      return array.next();    }    else      return BooleanValue.FALSE;  }  /**   * Returns the next value of the array.   */  public static Value each(Env env, @Reference Value value)  {    if (value instanceof Var) {      value = value.toValue();            if (value.isArray())        return value.toArrayValue(env).each();      else {        L.l("each() requires argument to be an array");        env.warning("each() requires argument to be an array");        return NullValue.NULL;      }    }    else {      L.l("each() argument must be a variable");      return env.error("each() argument must be a variable");    }  }  /**   * Returns the previous value of the array.   */  public static Value prev(Value value)  {    if (value instanceof ArrayValue) {      ArrayValue array = (ArrayValue) value;      return array.prev();    }    else      return BooleanValue.FALSE;  }  /**   * Resets the pointer   */  public static Value reset(Value value)  {    if (value instanceof ArrayValue) {      ArrayValue array = (ArrayValue) value;      return array.reset();    }    else      return BooleanValue.FALSE;  }  /**   * Returns the current value of the array.   */  public static Value shuffle(ArrayValue array)  {    if (array == null)      return BooleanValue.FALSE;    array.shuffle();    return BooleanValue.TRUE;  }  /**   * Resets the pointer to the end   */  public static Value end(Value value)  {    if (value instanceof ArrayValue) {      ArrayValue array = (ArrayValue) value;      return array.end();    }    else      return BooleanValue.FALSE;  }  /**   * Checks if the key is in the given array   *   * @param key a key to check for in the array   * @param searchArray the array to search for the key in   * @return true if the key is in the array, and false otherwise   */  public static boolean array_key_exists(Env env,                                         @ReadOnly Value key,                                         @ReadOnly Value searchArray)  {        if (! searchArray.isset() || ! key.isset()) {      return false;    }    if (! (searchArray.isArray() || searchArray.isObject())) {      env.warning(L.l("'" + searchArray.toString() + "' is an unexpected argument, expected ArrayValue or ObjectValue"));      return false;    }    if (! (key.isString() || key.isLongConvertible())) {      env.warning(L.l("The first argument (a '{0}') should be either a string or an integer",                      key.getType()));      return false;    }    if (searchArray instanceof ArrayValue) {      return ((ArrayValue) searchArray).containsKey(key) != null;    }    else {      return ! searchArray.getField(env, key.toStringValue()).isNull();    }  }  /**

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?