Example #1
0
        public static P5Scalar IsFile(Runtime runtime, IP5Any path)
        {
            var str = path.AsString(runtime);

            if (System.IO.File.Exists(str))
                return new P5Scalar(runtime, true);

            if (System.IO.Directory.Exists(str))
                return new P5Scalar(runtime, false);

            return new P5Scalar(runtime);
        }
Example #2
0
        public int Write(Runtime runtime, IP5Any scalar, int offset, int length)
        {
            // TODO use offset/length
            output.Write(scalar.AsString(runtime));

            return 1;
        }
Example #3
0
        public P5Scalar SpliceSubstring(Runtime runtime, int start,
                                        IP5Any replace)
        {
            var sn = ForceString(runtime);

            AdjustOffsets(sn.stringValue, ref start);

            // TODO handle the various corner cases for start/end
            var part = new P5Scalar(runtime, sn.stringValue.Substring(start, sn.stringValue.Length - start));
            sn.stringValue = sn.stringValue.Substring(0, start)
                + replace.AsString(runtime);

            return part;
        }
Example #4
0
        public static P5Scalar Bless(Runtime runtime, P5Scalar reference, IP5Any pack)
        {
            var pack_str = pack.AsString(runtime);
            var stash = runtime.SymbolTable.GetPackage(runtime, pack_str, true);

            reference.BlessReference(runtime, stash);

            return reference;
        }
Example #5
0
        public static P5Scalar Uppercase(Runtime runtime, IP5Any value)
        {
            var s = value.AsString(runtime).ToUpper();

            return new P5Scalar(runtime, s);
        }
Example #6
0
        public static P5List SplitSpaces(Runtime runtime, IP5Any value)
        {
            var str = value.AsString(runtime);
            var res = new P5List(runtime);
            int start = 0, curr = 0;

            for ( ; curr < str.Length; )
            {
                for ( ; curr < str.Length && char.IsWhiteSpace(str[curr]); ++curr)
                    ;
                start = curr;
                if (start == str.Length)
                    break;
                for ( ; curr < str.Length && !char.IsWhiteSpace(str[curr]); ++curr)
                    ;

                res.Push(runtime, new P5Scalar(runtime, str.Substring(start, curr - start)));
            }

            return res;
        }
Example #7
0
        public static P5Scalar QuoteMeta(Runtime runtime, IP5Any value)
        {
            var t = new System.Text.StringBuilder();

            foreach (char c in value.AsString(runtime))
            {
                if (!char.IsLetterOrDigit(c) && c != '_')
                    t.Append('\\');

                t.Append(c);
            }

            return new P5Scalar(runtime, t.ToString());
        }
Example #8
0
        public static P5Scalar Ord(Runtime runtime, IP5Any value)
        {
            var s = value.AsString(runtime);

            return new P5Scalar(runtime, s.Length > 0 ? (int)s[0] : 0);
        }
Example #9
0
        public static IP5Any LocalizeHashElement(Runtime runtime, P5Hash hash, IP5Any index, ref SavedValue state)
        {
            string str_index = index.AsString(runtime);
            var saved = hash.LocalizeElement(runtime, str_index);
            var new_value = new P5Scalar(runtime);

            state.container = hash;
            state.str_key = str_index;
            state.value = saved;

            hash.SetItem(runtime, str_index, new_value);

            return new_value;
        }
Example #10
0
        public static bool IsDerivedFrom(Runtime runtime, P5Scalar value, IP5Any pack)
        {
            P5SymbolTable stash = value.BlessedReferenceStash(runtime);

            if (stash == null)
                stash = runtime.SymbolTable.GetPackage(runtime, value.AsString(runtime), false);

            string pack_name = pack.AsString(runtime);
            P5SymbolTable parent = runtime.SymbolTable.GetPackage(runtime, pack_name, false);

            if (parent == null || stash == null)
                return false;

            return stash.IsDerivedFrom(runtime, parent);
        }
Example #11
0
        public static P5Scalar Index(Runtime runtime, IP5Any value,
                                     IP5Any substr, int start)
        {
            var str = value.AsString(runtime);
            var sub = substr.AsString(runtime);

            if (start < 0)
                start = 0;
            if (start > str.Length)
                start = str.Length;

            return new P5Scalar(runtime, str.IndexOf(sub, start));
        }
Example #12
0
        public static IP5Any MatchHelper(IP5Regex regex, Runtime runtime, IP5Any value, int flags,
                                   Opcode.ContextValues cxt, ref RxResult oldState)
        {
            bool match = regex.MatchString(runtime, value.AsString(runtime),
                                           -1, false, ref oldState);

            if (cxt != Opcode.ContextValues.LIST)
            {
                return new P5Scalar(runtime, match);
            }
            else if (match && runtime.LastMatch.StringCaptures.Length > 0)
            {
                var res = new IP5Any[runtime.LastMatch.StringCaptures.Length];
                for (int i = 0; i < runtime.LastMatch.StringCaptures.Length; ++i)
                    res[i] = new P5Scalar(runtime, runtime.LastMatch.StringCaptures[i]);

                return new P5List(runtime, res);
            }
            else
            {
                return new P5List(runtime, match);
            }
        }
Example #13
0
        public static IP5Any MatchGlobalHelper(IP5Regex regex, Runtime runtime, IP5Any value, int flags,
                                               Opcode.ContextValues cxt, ref RxResult oldState)
        {
            var scalar = value as P5Scalar;
            bool pos_set;
            int pos = value.GetPos(runtime, out pos_set);
            string str = value.AsString(runtime);
            bool match;
            IP5Any result;

            if (cxt != Opcode.ContextValues.LIST)
            {
                match = regex.MatchString(runtime, str, pos, pos_set,
                                          ref oldState);

                result = new P5Scalar(runtime, match);

                if (scalar != null)
                {
                    if (match)
                        scalar.SetPos(runtime, runtime.LastMatch.End, false);
                    else if ((flags & Opcode.RX_KEEP) == 0)
                        scalar.UnsetPos(runtime);
                }
            }
            else
            {
                var capt = new List<IP5Any>();

                for (;;)
                {
                    match = regex.MatchString(runtime, str,
                                              pos, pos_set, ref oldState);
                    if (match)
                    {
                        if (runtime.LastMatch.StringCaptures != null)
                        {
                            foreach (var s in runtime.LastMatch.StringCaptures)
                                capt.Add(new P5Scalar(runtime, s));
                        }
                        else
                        {
                            string s = str.Substring(runtime.LastMatch.Start,
                                                     runtime.LastMatch.End - runtime.LastMatch.Start);
                            capt.Add(new P5Scalar(runtime, s));
                        }
                    }
                    else
                        break;

                    pos = runtime.LastMatch.End;
                }

                if (scalar != null)
                {
                    if ((flags & Opcode.RX_KEEP) != 0)
                        scalar.SetPos(runtime, pos, false);
                    else
                        scalar.UnsetPos(runtime);
                }

                result = new P5List(runtime, capt);
            }

            return result;
        }