/* 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); } }
/* 'Values' => ( * 'Size' => '1045MB', * 'Files' => ( * 0 => 'Users.xml', * 1 => 'Admins.xml', * 2 => ( * 'Archive' => 'App.exe' * ) * ) * ) */ public static void CreateExampleArray(KScriptContainer container) { ArrayBase values = new ArrayBase( new ArrayCollection("Values", new List <IArray>() { new ArrayItem("Size", "1045MB"), new ArrayCollection("Files", new List <IArray>() { new ArrayItem("Users.xml"), new ArrayItem("Admins.xml"), new ArrayCollection("Archive", new List <IArray>() { new ArrayItem("App.exe") }) }) }) ); container.AddMultidimensionalArray("myArray", values); }
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); } }