protected sealed override Object EvaluateCore( EvaluationContext context, out ResultMemory resultMemory) { resultMemory = null; var items = Parameters[0].Evaluate(context); // Array if (items.TryGetCollectionInterface(out var collection) && collection is IReadOnlyArray array && array.Count > 0) { var result = new StringBuilder(); var memory = new MemoryCounter(this, context.Options.MaxMemory); // Append the first item var item = array[0]; var itemResult = EvaluationResult.CreateIntermediateResult(context, item); var itemString = itemResult.ConvertToString(); memory.Add(itemString); result.Append(itemString); // More items? if (array.Count > 1) { var separator = ","; if (Parameters.Count > 1) { var separatorResult = Parameters[1].Evaluate(context); if (separatorResult.IsPrimitive) { separator = separatorResult.ConvertToString(); } } for (var i = 1; i < array.Count; i++) { // Append the separator memory.Add(separator); result.Append(separator); // Append the next item var nextItem = array[i]; var nextItemResult = EvaluationResult.CreateIntermediateResult(context, nextItem); var nextItemString = nextItemResult.ConvertToString(); memory.Add(nextItemString); result.Append(nextItemString); } } return(result.ToString()); }
private void WriteEmptyMapping( StringBuilder writer, MemoryCounter memory, Stack <ICollectionEnumerator> ancestors) { var str = PrefixValue("{}", ancestors); memory.Add(str); writer.Append(str); }
private void WriteMappingEnd( StringBuilder writer, MemoryCounter memory, Stack <ICollectionEnumerator> ancestors) { var str = $"\n{new String(' ', ancestors.Count * 2)}}}"; memory.Add(str); writer.Append(str); }
private void WriteMappingKey( EvaluationContext context, StringBuilder writer, MemoryCounter memory, EvaluationResult key, Stack <ICollectionEnumerator> ancestors) { var str = PrefixValue(JsonUtility.ToString(key.ConvertToString()), ancestors, isMappingKey: true); memory.Add(str); writer.Append(str); }
public void TestMemoryCounter(string bigram, long expectedCount) { ICounter testCounter = new MemoryCounter(); testCounter.Add("the", "quick"); testCounter.Add("quick", "brown"); testCounter.Add("brown", "fox"); testCounter.Add("fox", "and"); testCounter.Add("and", "the"); testCounter.Add("the", "quick"); testCounter.Add("quick", "blue"); testCounter.Add("blue", "hare"); List <BigramCountValue> bigramCountList = testCounter.BigramCountList(); Assert.AreEqual(7, bigramCountList.Count); // 8 bigrams added but 7 values since 1 repeats var targetBigramCounts = from c in bigramCountList where c.Bigram == bigram select c.Count; Assert.AreEqual(1, targetBigramCounts.Count()); // there should be only one instance of this bigram count Assert.AreEqual(expectedCount, targetBigramCounts.First()); }
private void WriteValue( EvaluationContext context, StringBuilder writer, MemoryCounter memory, EvaluationResult value, Stack <ICollectionEnumerator> ancestors) { String str; switch (value.Kind) { case ValueKind.Null: str = "null"; break; case ValueKind.Boolean: str = (Boolean)value.Value ? "true" : "false"; break; case ValueKind.Number: str = value.ConvertToString(); break; case ValueKind.String: str = JsonUtility.ToString(value.Value); break; default: str = "{}"; // The value is an object we don't know how to traverse break; } str = PrefixValue(str, ancestors); memory.Add(str); writer.Append(str); }