Example #1
0
        /* Supported formats:
         * Is Initialised by ~myArrayName[0] etc.
         * Accepted key selectors:
         *  - [number] - Index of item.
         *  - ['string'] - Key of item
         *  - [~'string'] (to-be-impl.) - Key of item (bubbles down the array from index).
         *
         *      E.g.
         *      MyArray => [a => 'puppy', b => 'dog', c => [d => 'cat', e => 'kitten', a => [a => 'cub']]]
         *
         *  Example:
         *      - MyArray[2][~'d'] returns 'cat'.
         *      - MyArray[~'e'] returns 'kitten'.
         *        Or MyArray['c'][~'a'] would return cub only bubbling down from index 'c' and not return 'puppy'
         */

        public static string HandleString(string str, KScriptContainer container)
        {
            try
            {
                MatchCollection matches = Regex.Matches(str, ArrayMatch);

                Dictionary <string, Match> Matched_Items = new Dictionary <string, Match>();

                string tmp = str;

                if (matches.Count < 1)
                {
                    return(str);
                }

                foreach (Match match in matches)
                {
                    string uID = Guid.NewGuid().ToString();
                    tmp = tmp.Replace(match.Groups[0].Value, $"[{{{uID}}}]");
                    Matched_Items.Add(uID, match);
                }

                foreach (KeyValuePair <string, Match> item in Matched_Items)
                {
                    MatchCollection m = Regex.Matches(item.Value.Groups[0].Value, RetrieveItems);

                    if (m.Count < 1)
                    {
                        return(str);
                    }

                    string    id      = Regex.Match(item.Value.Groups[0].Value, ArrayMatch).Groups[1].Value;
                    ArrayBase current = container.GetMultidimensionalArrays()[id];
                    IArray    needle  = current.Find(m.Cast <Match>().ToArray());

                    var result = needle.GetValue() is null ? item.Value.Groups[0].Value : needle.GetValue();
                    tmp = tmp.Replace($"[{{{item.Key}}}]", result);
                }
                return(tmp);
            }
            catch (System.Exception ex)
            {
                if (container.Properties.ThrowAllExceptions)
                {
                    container.HandleException(ex);
                }
                return(str);
            }
        }
Example #2
0
        public static IArray GetArrayItem(string str, KScriptContainer container)
        {
            try
            {
                Match item = Regex.Match(str, ArrayMatch);

                MatchCollection m = Regex.Matches(item.Groups[0].Value, RetrieveItems);

                var    find = Regex.Match(item.Groups[0].Value, ArrayMatch).Groups[1].Value;
                string id   = string.IsNullOrWhiteSpace(find) ? MultiArray.MultiArrayParser.StripKey(str) : find;

                ArrayBase current = container.GetMultidimensionalArrays()[id];
                IArray    needle  = current.Find(m.Cast <Match>().ToArray());

                return(needle);
            }
            catch (System.Exception ex)
            {
                container.HandleException(ex);
                return(null);
            }
        }