public void EvalIndexOfLast(RCRunner runner, RCClosure closure, RCString left, RCString right) { if (left.Count != 1) { throw new Exception("indexoflast can only take one string on the left."); } RCArray <long> result = new RCArray <long> (); string value = left[0]; int totalIndex = 0; for (int i = 0; i < right.Count; ++i) { int startIndex = 0; while (true) { startIndex = right[i].LastIndexOf(value, startIndex, right[i].Length - startIndex, StringComparison.InvariantCulture); if (startIndex > -1) { result.Write(totalIndex + startIndex); startIndex += value.Length; } else { break; } } totalIndex += right[i].Length; } runner.Yield(closure, new RCLong(result)); }
public void GetReadersForSymbol(ref Dictionary <long, RCClosure> fibers, RCArray <RCSymbolScalar> symbol) { Queue <RCClosure> symbolWaiters; for (int i = 0; i < symbol.Count; ++i) { // It is possible to have the same closure represented more than once if the // operator requested more than one symbol. This is deduping the closures // based on the fiber number, if necessary. // But maybe this whole thing should be part of MapQueue.Dequeue for clarity. if ((symbolWaiters = Dequeue(symbol[i])) != null) { while (symbolWaiters.Count > 0) { RCClosure waiter = symbolWaiters.Dequeue(); if (fibers == null) { fibers = new Dictionary <long, RCClosure> (); } fibers[waiter.Fiber] = waiter; } } } }
/* * public static void ListFibers (RCRunner runner, RCClosure closure) * { * RCArray<Fiber> modules = new RCArray<Fiber> (); * RCArray<long> keys = new RCArray<long> (); * lock (runner._botLock) * { * foreach (KeyValuePair<long, RCBot> kv in runner._bots) * { * keys.Write (kv.Key); * modules.Write ((Fiber) runner._bots[kv.Key].GetModule (typeof (Fiber))); * } * } * RCArray<long> bots = new RCArray<long> (); * RCArray<long> fibers = new RCArray<long> (); * RCArray<string> states = new RCArray<string> (); * for (int i = 0; i < modules.Count; ++i) * { * lock (modules[i]._fiberLock) * { * foreach (KeyValuePair<long, Fiber.FiberState> kv in modules[i]._fibers) * { * bots.Write (keys[i]); * fibers.Write (kv.Key); * states.Write (kv.Value.State); * } * } * } * RCBlock result = RCBlock.Empty; * if (states.Count > 0) * { * result = new RCBlock (result, "bot", ":", new RCLong (bots)); * result = new RCBlock (result, "fiber", ":", new RCLong (fibers)); * result = new RCBlock (result, "state", ":", new RCString (states)); * } * * runner.Yield (closure, result); * } */ public static void ListFibers(RCRunner runner, RCClosure closure) { RCArray <Fiber> modules = new RCArray <Fiber> (); RCArray <long> keys = new RCArray <long> (); lock (runner._botLock) { foreach (KeyValuePair <long, RCBot> kv in runner._bots) { keys.Write(kv.Key); modules.Write((Fiber)runner._bots[kv.Key].GetModule(typeof(Fiber))); } } RCCube result = new RCCube("S"); for (int i = 0; i < modules.Count; ++i) { lock (modules[i]._fiberLock) { foreach (KeyValuePair <long, Fiber.FiberState> kv in modules[i]._fibers) { RCSymbolScalar sym = RCSymbolScalar.From("fiber", keys[i], kv.Key); result.WriteCell("bot", sym, keys[i]); result.WriteCell("fiber", sym, kv.Key); result.WriteCell("state", sym, kv.Value.State); result.Write(sym); } } } runner.Yield(closure, result); }
public static void ListRequest(RCRunner runner, RCClosure closure) { RCArray <HttpServer> modules = new RCArray <HttpServer> (); RCArray <long> keys = new RCArray <long> (); lock (runner._botLock) { foreach (KeyValuePair <long, RCBot> kv in runner._bots) { keys.Write(kv.Key); modules.Write((HttpServer)runner._bots[kv.Key].GetModule(typeof(HttpServer))); } } RCCube result = new RCCube("S"); for (int i = 0; i < modules.Count; ++i) { lock (modules[i]._lock) { foreach (KeyValuePair <int, HttpServer.RequestInfo> kv in modules[i]._contexts) { RCSymbolScalar urlsym = RCSymbolScalar.From("request", keys[i], (long)kv.Key); result.WriteCell("bot", urlsym, keys[i]); result.WriteCell("handle", urlsym, (long)kv.Key); result.WriteCell("url", urlsym, kv.Value.Context.Request.RawUrl); result.Write(urlsym); } } } runner.Yield(closure, result); }
public static void ListExec(RCRunner runner, RCClosure closure) { RCArray <Exec> modules = new RCArray <Exec> (); RCArray <long> keys = new RCArray <long> (); lock (runner._botLock) { foreach (KeyValuePair <long, RCBot> kv in runner._bots) { keys.Write(kv.Key); modules.Write((Exec)runner._bots[kv.Key].GetModule(typeof(Exec))); } } RCCube result = new RCCube("S"); for (int i = 0; i < modules.Count; ++i) { lock (modules[i]._lock) { foreach (KeyValuePair <long, Exec.ChildProcess> kv in modules[i]._process) { RCSymbolScalar sym = RCSymbolScalar.From("exec", keys[i], kv.Key); result.WriteCell("bot", sym, keys[i]); result.WriteCell("handle", sym, (long)kv.Key); result.WriteCell("pid", sym, (long)kv.Value._pid); result.WriteCell("program", sym, kv.Value._program); result.WriteCell("arguments", sym, kv.Value._arguments); result.WriteCell("exit", sym, kv.Value._exitCode); result.Write(sym); } } } runner.Yield(closure, result); }
protected virtual void WriteToFiles(RCRunner runner, RCClosure closure, RCSymbol symbol, RCBlock data) { for (int i = 0; i < data.Count; ++i) { FileStream stream = null; RCBlock column = data.GetName(i); if (!_files.TryGetValue(column.Name, out stream)) { string path = Path.Combine(_dir.Name, column.Name); stream = new FileStream(path, FileMode.CreateNew, FileAccess.Write); _files[column.Name] = stream; } RCArray <byte> array = new RCArray <byte> (); column.Value.ToByte(array); // throw new NotImplementedException ("This is where it's at baby, get er done."); // RCAsyncState state = new RCAsyncState (runner, closure, stream); // stream.BeginWrite (array, 0, array.Length, new AsyncCallback (EndWrite), // state); } }
public void Enqueue(RCSymbol symbol, RCClosure closure) { if (_symbolsByFiber.ContainsKey(closure.Fiber)) { throw new Exception("Fiber " + closure.Fiber.ToString() + " is already waiting, something is wrong."); } RCArray <RCSymbolScalar> stripped = new RCArray <RCSymbolScalar> (symbol.Count); for (int i = 0; i < symbol.Count; ++i) { if (symbol[i].Key.Equals("*")) { stripped.Write(symbol[i].Previous); } else { stripped.Write(symbol[i]); } } _symbolsByFiber.Add(closure.Fiber, new HashSet <RCSymbolScalar> (stripped)); for (int i = 0; i < stripped.Count; ++i) { HashSet <long> fibers; if (!_fibersBySymbol.TryGetValue(stripped[i], out fibers)) { _fibersBySymbol[stripped[i]] = fibers = new HashSet <long> (); } fibers.Add(closure.Fiber); } _waitOrder.Enqueue(closure); }
public static RCArray <O> ContextualOp <C, L, R, O> (RCArray <L> left, RCArray <R> right, ConScalarOp <C, L, R, O> op) where C : Context <L>, new () { // Things left to do to make this work: // Add the equivalent on CubeMath. // Update both type constructors. // I think it will work well, perhaps a little overengineered... // Most people would just expand these two operations by hand rather than adding a // new profile. // I have a feeling I will use this facility for other operations... I dunno, I'm // going skiing. C c = new C(); c.Init(left); RCArray <O> output = new RCArray <O> (); for (int i = 0; i < right.Count; ++i) { output.Write(op(c, right[i])); } return(output); }
public void EvalPartsAfter(RCRunner runner, RCClosure closure, RCSymbol left, RCLong right) { RCArray <RCSymbolScalar> result = new RCArray <RCSymbolScalar> (left.Count); if (right.Count == 1) { for (int i = 0; i < left.Count; ++i) { result.Write(left[i].PartsAfter(right[0])); } } else if (right.Count == left.Count) { for (int i = 0; i < left.Count; ++i) { result.Write(left[i].PartsAfter(right[i])); } } else { throw new RCException(closure, RCErrors.Count, "partsBefore requires a single part index or an array having the same count as the left argument"); } runner.Yield(closure, new RCSymbol(result)); }
public void EvalSplitw(RCRunner runner, RCClosure closure, RCString left, RCString right) { RCArray <string> result = new RCArray <string> (); for (int i = 0; i < right.Count; ++i) { int start = 0; int end; while (true) { end = right[i].IndexOf(left[0], start); if (end >= 0) { result.Write(right[i].Substring(start, end - start)); start = end + left[0].Length; } else { result.Write(right[i].Substring(start)); break; } } } runner.Yield(closure, new RCString(result)); }
public void EvalMatches(RCRunner runner, RCClosure closure, RCString left, RCString right) { RCBlock result = RCBlock.Empty; Regex[] regexes = new Regex[left.Count]; for (int i = 0; i < left.Count; ++i) { regexes[i] = new Regex(left[i].ToString(), RegexOptions.Multiline); } for (int i = 0; i < right.Count; ++i) { RCArray <string> resulti = null; for (int j = 0; j < regexes.Length; ++j) { MatchCollection matchesij = regexes[j].Matches(right[i]); if (matchesij.Count > 0) { if (resulti == null) { resulti = new RCArray <string> (matchesij.Count + 1); } for (int k = 0; k < matchesij.Count; ++k) { resulti.Write(matchesij[k].Value); } } } if (resulti == null) { resulti = new RCArray <string> (0); } result = new RCBlock(result, "", ":", new RCString(resulti)); } runner.Yield(closure, result); }
public static RCArray <O> MonadicOp <R, O> (RCArray <R> right, ScalarOp <R, O> op) { RCArray <O> output = new RCArray <O> (right.Count); for (int i = 0; i < right.Count; ++i) { output.Write(op(right[i])); } return(output); }
public virtual void EvalHas(RCRunner runner, RCClosure closure, RCBlock left, RCString right) { RCArray <bool> result = new RCArray <bool> (); for (int i = 0; i < right.Count; ++i) { result.Write(left.Get(right[i]) != null); } runner.Yield(closure, new RCBoolean(result)); }
public void EvalFile(RCRunner runner, RCClosure closure, RCSymbol right) { RCArray <bool> result = new RCArray <bool> (right.Count); for (int i = 0; i < right.Count; ++i) { result.Write(File.Exists(PathSymbolToLocalString(right[i]))); } runner.Yield(closure, new RCBoolean(result)); }
public void DisplayFormat(RCRunner runner, RCClosure closure, RCCube right) { RCArray <string> column = right.DoColof <string> ("column", ""); RCArray <string> format = right.DoColof <string> ("format", ""); RCSystem.Log.UpdateColmap(column, format); // RCSystem.Log.Record (runner, closure, "display", 0, "format", "set to " + // right[0]); runner.Yield(closure, right); }
public void EvalPath(RCRunner runner, RCClosure closure, RCSymbol right) { RCArray <string> result = new RCArray <string> (right.Count); for (int i = 0; i < right.Count; ++i) { result.Write(PathSymbolToLocalString(right[i])); } runner.Yield(closure, new RCString(result)); }
public void EvalDir(RCRunner runner, RCClosure closure, RCString right) { RCArray <bool> result = new RCArray <bool> (right.Count); for (int i = 0; i < right.Count; ++i) { result.Write(Directory.Exists(right[i])); } runner.Yield(closure, new RCBoolean(result)); }
public void EvalIsName(RCRunner runner, RCClosure closure, RCString right) { RCArray <bool> result = new RCArray <bool> (right.Count); for (int i = 0; i < right.Count; ++i) { result.Write(RCTokenType.LengthOfStrictIdentifier(right[i], 0) == right[i].Length); } runner.Yield(closure, new RCBoolean(result)); }
public void EvalTrimEnd(RCRunner runner, RCClosure closure, RCString right) { RCArray <string> result = new RCArray <string> (right.Count); for (int i = 0; i < right.Count; ++i) { result.Write(right[i].TrimEnd()); } runner.Yield(closure, new RCString(result)); }
protected RCArray <T> DoReverse <T> (RCVector <T> right) { RCArray <T> results = new RCArray <T> (right.Count); for (int i = right.Count - 1; i >= 0; --i) { results.Write(right[i]); } return(results); }
public void UrlDecode(RCRunner runner, RCClosure closure, RCString right) { RCArray <string> result = new RCArray <string> (right.Count); for (int i = 0; i < right.Count; ++i) { result.Write(HttpUtility.UrlDecode(right[i])); } runner.Yield(closure, new RCString(result)); }
public void EvalParseTime(RCRunner runner, RCClosure closure, RCString left, RCString right) { RCArray <RCTimeScalar> result = new RCArray <RCTimeScalar> (); for (int i = 0; i < right.Count; ++i) { result.Write(TimeToken.ParseTime(right[i], left.ToArray(), smartType: false)); } runner.Yield(closure, new RCTime(result)); }
protected RCArray <T> DoPop <T> (RCArray <T> right) { RCArray <T> results = new RCArray <T> (right.Count); for (int i = 0; i < right.Count - 1; ++i) { results.Write(right[i]); } return(results); }
protected void StripControlChars(RCArray <string> result) { // So far this only comes into play on Windows when using wsl to execute shell scripts. // This is the output of the bash 'clear' command. // Let's be minimally aggressive to start. if (result.Count > 0 && result[result.Count - 1] == "\u001b[H\u001b[2J") { result.RemoveAt(result.Count - 1); } }
public void EvalDayOfWeek(RCRunner runner, RCClosure closure, RCTime right) { RCArray <string> result = new RCArray <string> (right.Count); for (int i = 0; i < right.Count; ++i) { DateTime date = new DateTime(right[i].Ticks); result.Write(date.DayOfWeek.ToString()); } runner.Yield(closure, new RCString(result)); }
protected RCArray <T> DoRepeat <T> (RCVector <long> left, RCVector <T> right) { long count = left[0]; RCArray <T> result = new RCArray <T> ((int)count * right.Count); for (int i = 0; i < count; ++i) { result.Write(right.Data); } return(result); }
public void EvalSplit(RCRunner runner, RCClosure closure, RCString left, RCString right) { // Would be nice to support muliple strings on the left, like "\" "/" split $blah RCArray <string> result = new RCArray <string> (); for (int i = 0; i < right.Count; ++i) { result.Write(right[i].Split(left[0].ToCharArray())); } runner.Yield(closure, new RCString(result)); }
public void EvalPad(RCRunner runner, RCClosure closure, RCString left, RCLong right) { RCArray <string> result = new RCArray <string> (right.Count); for (int i = 0; i < right.Count; ++i) { string padding = "".PadRight((int)right[i], left[0][0]); result.Write(padding); } runner.Yield(closure, new RCString(result)); }
public void EvalNow(RCRunner runner, RCClosure closure, RCTime right) { RCArray <RCTimeScalar> result = new RCArray <RCTimeScalar> (right.Count); for (int i = 0; i < right.Count; ++i) { DateTime date = new DateTime(right[i].Ticks); DateTime adjustedDate = date.ToLocalTime(); result.Write(new RCTimeScalar(adjustedDate, right[i].Type)); } runner.Yield(closure, new RCTime(result)); }
public void EvalTime(RCRunner runner, RCClosure closure, RCSymbol left, RCTime right) { RCArray <RCTimeScalar> result = new RCArray <RCTimeScalar> (); string name = left[0].Part(0).ToString(); RCTimeType type = (RCTimeType)Enum.Parse(typeof(RCTimeType), name, true); for (int i = 0; i < right.Count; ++i) { result.Write(new RCTimeScalar(new DateTime(right[i].Ticks), type)); } runner.Yield(closure, new RCTime(result)); }