コード例 #1
0
        public static EcmaValue Stringify(EcmaValue value, EcmaValue replacer, EcmaValue space)
        {
            string indentString;

            space = EcmaValueUtility.UnboxPrimitiveObject(space);
            if (space.Type == EcmaValueType.Number)
            {
                indentString = new String(' ', Math.Max(0, Math.Min(10, space.ToInt32())));
            }
            else if (space.Type == EcmaValueType.String)
            {
                indentString = space.ToStringOrThrow();
            }
            else
            {
                indentString = String.Empty;
            }
            string result;

            if (replacer.IsCallable)
            {
                result = new EcmaJsonWriter(indentString, replacer).Serialize(value);
            }
            else if (EcmaArray.IsArray(replacer))
            {
                HashSet <string> propertyList = new HashSet <string>();
                for (long i = 0, length = replacer[WellKnownProperty.Length].ToLength(); i < length; i++)
                {
                    EcmaValue item = EcmaValueUtility.UnboxPrimitiveObject(replacer[i]);
                    if (item.Type == EcmaValueType.String || item.Type == EcmaValueType.Number)
                    {
                        propertyList.Add(item.ToStringOrThrow());
                    }
                }
                result = new EcmaJsonWriter(indentString, propertyList).Serialize(value);
            }
            else
            {
                result = new EcmaJsonWriter(indentString).Serialize(value);
            }
            return(result ?? EcmaValue.Undefined);
        }
コード例 #2
0
        public static EcmaValue Escape(EcmaValue str)
        {
            StringBuilder sb = new StringBuilder();

            foreach (char ch in str.ToStringOrThrow())
            {
                if (ch == '*' || ch == '+' || ch == '-' || ch == '.' || ch == '/' || ch == '@' || ch == '_' || IsAlphaNumericCharPoint(ch))
                {
                    sb.Append(ch);
                }
                else if (ch < 256)
                {
                    sb.AppendFormat("%{0:X2}", (int)ch);
                }
                else
                {
                    sb.AppendFormat("%u{0:X4}", (int)ch);
                }
            }
            return(sb.ToString());
        }
コード例 #3
0
        public static EcmaValue ToBigInt(this EcmaValue value)
        {
            value = value.ToPrimitive(EcmaPreferredPrimitiveType.Number);
            switch (value.Type)
            {
            case EcmaValueType.Undefined:
            case EcmaValueType.Null:
            case EcmaValueType.Number:
            case EcmaValueType.Symbol:
                throw new EcmaTypeErrorException(InternalString.Error.NotConvertibleToBigInt, value);

            case EcmaValueType.Boolean:
                return(value.ToBoolean() ? value1n : value0n);

            case EcmaValueType.BigInt:
                return(value);

            case EcmaValueType.String:
                return(ToBigInt(value.ToStringOrThrow()));
            }
            throw new ArgumentException("Unknown value type", "value");
        }
コード例 #4
0
 public static EcmaValue Unescape(EcmaValue str)
 {
     return(Decode(str.ToStringOrThrow(), "", false, false));
 }
コード例 #5
0
 public static EcmaValue DecodeURIComponent(EcmaValue str)
 {
     return(Decode(str.ToStringOrThrow(), "", true, true));
 }
コード例 #6
0
 public static EcmaValue DecodeURI(EcmaValue str)
 {
     return(Decode(str.ToStringOrThrow(), ";/?:@&=+$,#", true, true));
 }
コード例 #7
0
        public static EcmaValue ParseFloat(EcmaValue str)
        {
            string inputString = str.ToStringOrThrow();

            return(EcmaValueUtility.ParseFloatInternal(inputString, true));
        }
コード例 #8
0
        public static EcmaValue ParseInt(EcmaValue str, EcmaValue radix)
        {
            string inputString = str.ToStringOrThrow();

            return(EcmaValueUtility.ParseIntInternal(inputString, radix.ToInt32(), true));
        }