// TODO: this is inefficient and ugly and duplicates much of LiquidCollection
        private LiquidExpressionResult DoLookup(ITemplateContext ctx, LiquidString str, ILiquidValue indexProperty)
        {
            var    strValues          = str.StringVal.ToCharArray().Select(ch => LiquidString.Create(ch.ToString()).ToOption()).ToList();
            String propertyNameString = ValueCaster.RenderAsString(indexProperty);
            int    index;

            if (propertyNameString.ToLower().Equals("first"))
            {
                index = 0;
            }
            else if (propertyNameString.ToLower().Equals("last"))
            {
                index = strValues.Count - 1;
            }
            else if (propertyNameString.ToLower().Equals("size"))
            {
                return(LiquidExpressionResult.Success(LiquidNumeric.Create(strValues.Count)));
            }
            else
            {
                //var maybeIndexResult = ValueCaster.Cast<ILiquidValue, LiquidNumeric>(indexProperty);
                var numericIndexProperty = indexProperty as LiquidNumeric;

                if (numericIndexProperty == null)
                {
                    return(ctx.Options.ErrorWhenValueMissing ?
                           LiquidExpressionResult.Error("invalid string index: '" + propertyNameString + "'") :
                           LiquidExpressionResult.Success(new None <ILiquidValue>()));
                }
                else
                {
                    index = numericIndexProperty.IntValue;
                }
            }

            if (strValues.Count == 0)
            {
                //return LiquidExpressionResult.Error("Empty string: " + propertyNameString);
                return(LiquidExpressionResult.Success(new None <ILiquidValue>())); // not an error in Ruby liquid.
            }
            return(LiquidExpressionResult.Success(CollectionIndexer.ValueAt(strValues, index)));
        }
Exemple #2
0
        //public IList<Option<ILiquidValue>> ArrValue { get { return _values; } }

        public Option <ILiquidValue> ValueAt(int key)
        {
            return(CollectionIndexer.ValueAt(_values, key));
        }