8000 Update filters.cpp · jinja2cpp/Jinja2Cpp@a853f8e · GitHub
[go: up one dir, main page]

Skip to content

Commit a853f8e

Browse files
jferreyra-scrmorozov
authored andcommitted
Update filters.cpp
Adding protection to many other filters.
1 parent c5e0a3b commit a853f8e

File tree

1 file changed

+32
-24
lines changed

1 file changed

+32
-24
lines changed

src/filters.cpp

Lines changed: 32 additions & 24 deletions
-
sublist.push_back(item);
Original file line numberDiff line numberDiff line change
@@ -458,6 +458,12 @@ InternalValue SequenceAccessor::Filter(const InternalValue& baseVal, RenderConte
458458

459459
if (!isConverted)
460460
return result;
461+
462+
auto ProtectedValue = [&baseVal](InternalValue value) {
463+
if (baseVal.ShouldExtendLifetime())
464+
value.SetParentData(baseVal);
465+
return value;
466+
};
461467

462468
InternalValue attrName = GetArgumentValue("attribute", context);
463469
InternalValue isCsVal = GetArgumentValue("case_sensitive", context, InternalValue(false));
@@ -482,23 +488,23 @@ InternalValue SequenceAccessor::Filter(const InternalValue& baseVal, RenderConte
482488
{
483489
case FirstItemMode:
484490
if (listSize)
485-
result = list.GetValueByIndex(0);
491+
result = ProtectedValue( list.GetValueByIndex(0) );
486492
else
487493
{
488494
auto it = list.begin();
489495
if (it != list.end())
490-
result = *it;
496+
result = ProtectedValue(*it);
491497
}
492498
break;
493499
case LastItemMode:
494500
if (listSize)
495-
result = list.GetValueByIndex(listSize.value() - 1);
501+
result = ProtectedValue(list.GetValueByIndex(listSize.value() - 1));
496502
else
497503
{
498504
auto it = list.begin();
499505
auto end = list.end();
500506
for (; it != end; ++it)
501-
result = *it;
507+
result = ProtectedValue(*it);
502508
}
503509
break;
504510
case LengthMode:
@@ -514,7 +520,7 @@ InternalValue SequenceAccessor::Filter(const InternalValue& baseVal, RenderConte
514520
if (listSize)
515521
{
516522
std::uniform_int_distribution<> dis(0, static_cast<int>(listSize.value()) - 1);
517-
result = list.GetValueByIndex(dis(gen));
523+
result = ProtectedValue(list.GetValueByIndex(dis(gen)));
518524
}
519525
else
520526
{
@@ -525,7 +531,7 @@ InternalValue SequenceAccessor::Filter(const InternalValue& baseVal, RenderConte
525531
{
526532
bool doCopy = count == 0 || std::uniform_int_distribution<size_t>(0, count)(gen) == 0;
527533
if (doCopy)
528-
result = *it;
534+
result = ProtectedValue(*it);
529535
}
530536
}
531537
break;
@@ -535,15 +541,15 @@ InternalValue SequenceAccessor::Filter(const InternalValue& baseVal, RenderConte
535541
auto b = list.begin();
536542
auto e = list.end();
537543
auto p = std::max_element(list.begin(), list.end(), lessComparator);
538-
result = p != e ? *p : InternalValue();
544+
result = p != e ? ProtectedValue(*p) : InternalValue();
539545
break;
540546
}
541547
case MinItemMode:
542548
{
543549
auto b = list.begin();
544550
auto e = list.end();
545551
auto p = std::min_element(b, e, lessComparator);
546-
result = p != e ? *p : InternalValue();
552+
result = p != e ? ProtectedValue(*p) : InternalValue();
547553
break;
548554
}
549555
case ReverseMode:
@@ -553,12 +559,7 @@ InternalValue SequenceAccessor::Filter(const InternalValue& baseVal, RenderConte
553559
auto size = listSize.value();
554560
InternalValueList resultList(size);
555561
for (std::size_t n = 0; n < size; ++n)
556-
{
557-
auto resultVal = InternalValue(std::move( list.GetValueByIndex(n) ));
558-
if (baseVal.ShouldExtendLifetime())
559-
resultVal.SetParentData(baseVal);
560-
resultList[size - n - 1] = std::move(resultVal);
561-
}
562+
resultList[size - n - 1] = ProtectedValue( list.GetValueByIndex(n) );
562563
result = ListAdapter::CreateAdapter(std::move(resultList));
563564
}
564565
else
@@ -567,12 +568,7 @@ InternalValue SequenceAccessor::Filter(const InternalValue& baseVal, RenderConte
567568
auto it = list.begin();
568569
auto end = list.end();
569570
for (; it != end; ++it)
570-
{
571-
auto resultVal = InternalValue(std::move( *it ));
572-
if (baseVal.ShouldExtendLifetime())
573-
resultVal.SetParentData(baseVal);
574-
resultList.push_back(std::move(resultVal));
575-
}
571+
resultList.push_back( ProtectedValue(*it) );
576572

577573
std::reverse(resultList.begin(), resultList.end());
578574
result = ListAdapter::CreateAdapter(std::move(resultList));
@@ -635,7 +631,7 @@ InternalValue SequenceAccessor::Filter(const InternalValue& baseVal, RenderConte
635631
std::stable_sort(items.begin(), items.end(), [](auto& i1, auto& i2) { return i1.idx < i2.idx; });
636632

637633
for (auto& i : items)
638-
resultList.push_back(list.GetValueByIndex(i.idx));
634+
resultList.push_back( ProtectedValue( list.GetValueByIndex(i.idx) ));
639635

640636
result = ListAdapter::CreateAdapter(std::move(resultList));
641637
break;
@@ -666,6 +662,12 @@ InternalValue Slice::Filter(const InternalValue& baseVal, RenderContext& context
666662
if (!isConverted)
667663
return result;
668664

665+
auto ProtectedValue = [&baseVal](InternalValue value) {
666+
if (baseVal.ShouldExtendLifetime())
667+
value.SetParentData(baseVal);
668+
return value;
669+
};
670+
669671
InternalValue sliceLengthValue = GetArgumentValue("slices", context);
670672
int64_t sliceLength = ConvertToInt(sliceLengthValue);
671673
InternalValue fillWith = GetArgumentValue("fill_with", context);
@@ -683,7 +685,7 @@ InternalValue Slice::Filter(const InternalValue& baseVal, RenderContext& context
683685
sublist.clear();
684686
sublistItemIndex %= sliceLength;
685687
}
686
688+
sublist.push_back( ProtectedValue(item) );
687689
++sublistItemIndex;
688690
}
689691
if (!IsEmpty(fillWith))
@@ -717,7 +719,13 @@ InternalValue Slice::Batch(const InternalValue& baseVal, RenderContext& context)
717719

718720
InternalValueList resultList;
719721
resultList.reserve(linecount);
720-
722+
723+
auto ProtectedValue = [&baseVal](InternalValue value) {
724+
if (baseVal.ShouldExtendLifetime())
725+
value.SetParentData(baseVal);
726+
return value;
727+
};
728+
721729
const auto remainder = elementsCount % linecount;
722730
const auto columns = elementsCount / linecount + (remainder > 0 ? 1 : 0);
723731
for (std::size_t line = 0, idx = 0; line < linecount; ++line)
@@ -728,7 +736,7 @@ InternalValue Slice::Batch(const InternalValue& baseVal, RenderContext& context)
728736
std::fill_n(std::back_inserter(row), columns, fillWith);
729737

730738
for (std::size_t column = 0; column < elems; ++column)
731-
row[column] = list.GetValueByIndex(idx++);
739+
row[column] = ProtectedValue( list.GetValueByIndex(idx++) );
732740

733741
resultList.push_back(ListAdapter::CreateAdapter(std::move(row)));
734742
}

0 commit comments

Comments
 (0)
0