示例#1
0
 private static void BuildQuery(NameValueCollection nv, PipeValue obj, string prefix)
 {
     if (prefix != null && obj.IsArray)
     {
         PipeValuePropertyEnumerator enumerator = obj.GetEnumerator();
         while (enumerator.MoveNext())
         {
             if (Regex.IsMatch(prefix, @"\[\]$"))
             {
                 nv.Add(prefix, enumerator.CurrentValue.ToString());
             }
             else
             {
                 BuildQuery(nv, enumerator.CurrentValue, prefix + "[" + (enumerator.CurrentValue.Type == PipeValueType.Object && (bool)enumerator.CurrentValue ? enumerator.CurrentKey : "") + "]");
             }
         }
     }
     else if (obj.Type == PipeValueType.Object)
     {
         PipeValuePropertyEnumerator enumerator = obj.GetEnumerator();
         while (enumerator.MoveNext())
         {
             BuildQuery(nv, enumerator.CurrentValue, prefix != null ? prefix + "[" + enumerator.CurrentKey + "]" : enumerator.CurrentKey);
         }
     }
     else
     {
         nv.Add(prefix, obj.ToString());
     }
 }
示例#2
0
        public static PipeValue RemoveEnd(PipeValue str, PipeValue needle)
        {
            string strStr    = (string)str;
            string strNeedle = (string)needle;

            return(strStr.Substring(-strNeedle.Length) == needle ? ((string)str).Substring(0, -strNeedle.Length) : strStr);
        }
示例#3
0
        public static PipeValue PadEnd(PipeValue str, PipeValue needle)
        {
            string strStr    = (string)str;
            string strNeedle = (string)needle;

            return(strStr.Substring(-strNeedle.Length) != needle ? str + needle : str);
        }
示例#4
0
        public static PipeValue RemoveStart(PipeValue str, PipeValue needle)
        {
            string strStr    = (string)str;
            string strNeedle = (string)needle;

            return(strStr.Substring(0, strNeedle.Length) == needle?strStr.Substring(strNeedle.Length) : strStr);
        }
示例#5
0
        private static PipeValue ParseValue(string str)
        {
            switch (str)
            {
            case "true":
                return(true);

            case "false":
                return(false);

            case "undefined":
                return(PipeValue.Undefined);

            case "null":
                return(PipeValue.Null);

            case "0":
                return(0);
            }
            PipeValue number = +new PipeValue(str);

            if (!Double.NaN.Equals(number.Value))
            {
                return(number);
            }
            return(str);
        }
示例#6
0
        public static PipeValue PadStart(PipeValue str, PipeValue needle)
        {
            string strStr    = (string)str;
            string strNeedle = (string)needle;

            return(strStr.Substring(0, strNeedle.Length) != needle ? needle + str : str);
        }
示例#7
0
        public static PipeValue FormatQuery(PipeValue value)
        {
            NameValueCollection nv = HttpUtility.ParseQueryString("");

            BuildQuery(nv, value, null);
            return(nv.ToString());
        }
示例#8
0
 public static PipeValue Substr(PipeValue str, PipeValue start, PipeValue len)
 {
     if (!len.IsEvallable)
     {
         return(((string)str).Substring((int)start));
     }
     return(((string)str).Substring((int)start, (int)len));
 }
        public static object Evaluate(TokenList tokens, PipeValue value, EvaluateOptions options, out PipeExecutionException[] exceptions)
        {
            CommonHelper.ConfirmNotNull(tokens, "tokens");
            CommonHelper.ConfirmNotNull(options, "options");
            EvaluationContext context     = new EvaluationContext(tokens, value, options);
            object            returnValue = context.Evaluate();

            exceptions = context.Exceptions;
            return(returnValue);
        }
示例#10
0
 public static PipeValue Length(PipeValue obj)
 {
     if (obj.IsEvallable)
     {
         if (obj.HasProperty("length"))
         {
             return(obj["length"]);
         }
     }
     return(0);
 }
示例#11
0
        public static PipeValue CutBefore(PipeValue str, PipeValue needle)
        {
            string strStr    = (string)str;
            string strNeedle = (string)needle;
            int    pos       = strStr.IndexOf((string)needle);

            if (pos >= 0)
            {
                return(strStr.Substring(0, pos));
            }
            return(strStr);
        }
示例#12
0
        public static PipeValue CutAfterLast(PipeValue str, PipeValue needle)
        {
            string strStr    = (string)str;
            string strNeedle = (string)needle;
            int    pos       = strStr.LastIndexOf((string)needle);

            if (pos >= 0)
            {
                return(strStr.Substring(strNeedle.Length + pos));
            }
            return(strStr);
        }
示例#13
0
        public static PipeValue FormatDate(PipeValue timestamp, PipeValue format)
        {
            DateTime d;

            if (timestamp.Type == PipeValueType.String)
            {
                DateTime.TryParse(timestamp.ToString(), out d);
            }
            else
            {
                d = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).AddMilliseconds(timestamp.ToDouble());
            }
            return(d.ToLocalTime().ToString((string)format));
        }
示例#14
0
        public static PipeValue Keys(PipeValue obj)
        {
            if (obj.IsArray)
            {
                return(new PipeValue(Enumerable.Range(0, (int)obj["length"])));
            }
            List <string> keys       = new List <string>();
            IEnumerator   enumerator = obj.GetEnumerator();

            while (enumerator.MoveNext())
            {
                keys.Add((string)enumerator.Current);
            }
            return(new PipeValue(keys));
        }
示例#15
0
        public PipeValue Evaluate(EvaluationContext context, bool checkValid, out bool isValid)
        {
            CommonHelper.ConfirmNotNull(context, "context");
            isValid = true;
            PipeValue value = context.CurrentValue;

            if (this.Count > 0)
            {
                ConstantObjectPath m = this[0] as ConstantObjectPath;
                if (m != null)
                {
                    switch (m.Value)
                    {
                    case "#":
                        return(value.Value is PipeValuePropertyEnumerator ? ((PipeValuePropertyEnumerator)value.Value).CurrentKey : PipeValue.Undefined);

                    case "##":
                        return(value.Value is PipeValuePropertyEnumerator ? ((PipeValuePropertyEnumerator)value.Value).CurrentIndex : PipeValue.Undefined);

                    case "#count":
                        return(value.Value is PipeValuePropertyEnumerator ? ((PipeValuePropertyEnumerator)value.Value).Count : 0);
                    }
                }
            }
            value = value.Value is PipeValuePropertyEnumerator ? ((PipeValuePropertyEnumerator)value.Value).CurrentValue : value;
            if (this.Count > 0)
            {
                ConstantObjectPath m = this[0] as ConstantObjectPath;
                if (m != null)
                {
                    int  intValue;
                    bool hasPropertyName = value.IsEvallable && (value.Type != PipeValueType.Object ? value[m.Value] != PipeValue.Undefined : (value.HasProperty(m.Value) || (value.IsArray && Int32.TryParse(m.Value, out intValue))));
                    if (checkValid && !hasPropertyName && !context.Globals.ContainsKey(m.Value))
                    {
                        isValid = false;
                        return(value);
                    }
                    value = hasPropertyName ? value[m.Value] : context.Globals[m.Value];
                }
            }
            for (int i = 1, len = this.Count; i < len && value.IsEvallable; i++)
            {
                string name = this[i] is ConstantObjectPath ? ((ConstantObjectPath)this[i]).Value : (string)this[i].Evaluate(context);
                value = value[name];
            }
            return(value);
        }
示例#16
0
        private EvaluationContext(TokenList tokens, PipeValue data, EvaluateOptions options)
        {
            CommonHelper.ConfirmNotNull(tokens, "tokens");
            CommonHelper.ConfirmNotNull(options, "options");
            this.Options = options;
            this.tokens  = tokens;
            this.data    = data;
            objStack.Push(data);

            if (options.OutputXml)
            {
                XmlDocument doc = new XmlDocument();
                xmlStack.Push(doc.DocumentElement);
            }
            this.Globals      = new PipeGlobal(options.Globals);
            this.Globals["_"] = data;
        }
示例#17
0
        public static PipeValue FormatPrintf(PipeValue value, PipeValue format)
        {
            StringBuilder sb = new StringBuilder();

            switch (value.Type)
            {
            case PipeValueType.String:
                sb.EnsureCapacity(((string)value).Length);
                _snwprintf_s(sb, (IntPtr)sb.MaxCapacity, (IntPtr)32, format.ToString(), (string)value);
                break;

            case PipeValueType.Number:
                sb.EnsureCapacity(100);
                _snwprintf_s(sb, (IntPtr)100, (IntPtr)32, format.ToString(), (double)value);
                break;

            default:
                sb.EnsureCapacity(100);
                _snwprintf_s(sb, (IntPtr)100, (IntPtr)32, format.ToString(), value.ToString());
                break;
            }
            return(sb.ToString());
        }
示例#18
0
        public static PipeValue Sum(PipeContext context)
        {
            PipeValue  result = PipeValue.Undefined;
            PipeLambda fn     = context.TakeFunction();

            if (fn == null)
            {
                result = context.TakeArgument();
                fn     = context.TakeFunction();
            }
            if (fn == null)
            {
                if (context.HasArgument())
                {
                    fn = context.TakeArgumentAsKeyFunction();
                }
                else
                {
                    fn = new PipeLambda((obj, i) => obj);
                }
            }
            PipeValuePropertyEnumerator enumerator = context.Value.GetEnumerator();

            while (enumerator.MoveNext())
            {
                if (result != PipeValue.Undefined)
                {
                    result = result + fn.Invoke(enumerator);
                }
                else
                {
                    result = fn.Invoke(enumerator);
                }
            }
            return(result);
        }
示例#19
0
 public static PipeValue Round(PipeValue value)
 {
     return(Math.Round((double)value));
 }
示例#20
0
 public static PipeValue FormatJson(PipeValue value)
 {
     return(JsonConvert.SerializeObject(value));
 }
示例#21
0
 public static PipeValue Min(PipeValue a, PipeValue b)
 {
     return(Math.Max((double)a, (double)b));
 }
示例#22
0
 public static PipeValue Sort(PipeValue value)
 {
     PipeValue[] arr = value.CopyAsArray();
     Array.Sort(arr, PipeValueObjectComparer.Default);
     return(new PipeValueObjectBuilder(arr));
 }
示例#23
0
 public static PipeValue Less(PipeValue a, PipeValue b)
 {
     return(PipeValueObjectComparer.Default.Compare(a, b) < 0);
 }
示例#24
0
 public static PipeValue Ceil(PipeValue value)
 {
     return(Math.Ceiling((double)+value));
 }
示例#25
0
 public static PipeValue Floor(PipeValue value)
 {
     return(Math.Floor((double)value));
 }
示例#26
0
 public static PipeValue Mod(PipeValue a, PipeValue b)
 {
     return((+a) % (+b));
 }
示例#27
0
 public static PipeValue More(PipeValue a, PipeValue b)
 {
     return(PipeValueObjectComparer.Default.Compare(a, b) > 0);
 }
示例#28
0
 public static PipeValue Join(PipeValue value, PipeValue str)
 {
     PipeValue[] arr = value.CopyAsArray();
     return(String.Join((string)str, arr));
 }
示例#29
0
        private void WriteJson(JsonWriter writer, PipeValue value, JsonSerializer serializer)
        {
            switch (value.Type)
            {
            case PipeValueType.Undefined:
                break;

            case PipeValueType.String:
                writer.WriteValue((string)value.Value);
                break;

            case PipeValueType.Number:
                switch (Type.GetTypeCode(value.Value.GetType()))
                {
                case TypeCode.Byte:
                    writer.WriteValue((byte)value.Value);
                    break;

                case TypeCode.Char:
                    writer.WriteValue((char)value.Value);
                    break;

                case TypeCode.Decimal:
                    writer.WriteValue((decimal)value.Value);
                    break;

                case TypeCode.Double:
                    writer.WriteValue((double)value.Value);
                    break;

                case TypeCode.Int16:
                    writer.WriteValue((short)value.Value);
                    break;

                case TypeCode.Int32:
                    writer.WriteValue((int)value.Value);
                    break;

                case TypeCode.Int64:
                    writer.WriteValue((long)value.Value);
                    break;

                case TypeCode.SByte:
                    writer.WriteValue((sbyte)value.Value);
                    break;

                case TypeCode.Single:
                    writer.WriteValue((float)value.Value);
                    break;

                case TypeCode.UInt16:
                    writer.WriteValue((ushort)value.Value);
                    break;

                case TypeCode.UInt32:
                    writer.WriteValue((uint)value.Value);
                    break;

                case TypeCode.UInt64:
                    writer.WriteValue((ulong)value.Value);
                    break;
                }
                break;

            case PipeValueType.Boolean:
                writer.WriteValue((bool)value.Value);
                break;

            case PipeValueType.Object:
                if (!value.IsEvallable)
                {
                    writer.WriteNull();
                }
                else if (value.IsArray)
                {
                    writer.WriteStartArray();
                    foreach (object item in (IEnumerable)value.Value)
                    {
                        WriteJson(writer, new PipeValue(item), serializer);
                    }
                    writer.WriteEndArray();
                }
                else
                {
                    writer.WriteStartObject();
                    PipeValuePropertyEnumerator enumerator = value.GetEnumerator();
                    while (enumerator.MoveNext())
                    {
                        if (enumerator.CurrentValue != PipeValue.Undefined)
                        {
                            writer.WritePropertyName(enumerator.CurrentKey);
                            WriteJson(writer, enumerator.CurrentValue, serializer);
                        }
                    }
                    writer.WriteEndObject();
                }
                break;
            }
        }
示例#30
0
 public static PipeValue Reverse(PipeValue value)
 {
     PipeValue[] arr = value.CopyAsArray();
     Array.Reverse(arr);
     return(new PipeValueObjectBuilder(arr));
 }