<commit_before>/*
  This file is part of the Grantlee template system.

  Copyright (c) 2009 <NAME> <<EMAIL>>

  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License version 3 only, as published by the Free Software Foundation.

  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  Lesser General Public License version 3 for more details.

  You should have received a copy of the GNU Lesser General Public
  License along with this library.  If not, see <http://www.gnu.org/licenses/>.

*/

#include "lists.h"
#include <util_p.h>
#include <QDateTime>

JoinFilter::JoinFilter()
{
}

QVariant JoinFilter::doFilter( const QVariant& input, const QVariant &argument, bool autoescape ) const
{
  QVariantList varList = Util::variantToList( input );
  QListIterator<QVariant> i( varList );
  QString ret;
  while ( i.hasNext() ) {
    QVariant var = i.next();
    Grantlee::SafeString s = Util::getSafeString( var );
    if ( autoescape )
      s = Util::conditionalEscape( s );

    ret.append( s );
    if ( i.hasNext() )
    {
      SafeString argString = Util::getSafeString( argument );
      ret.append( argString );
    }
  }
  return Util::markSafe( ret );
}

LengthFilter::LengthFilter()
{
}

QVariant LengthFilter::doFilter( const QVariant& input, const QVariant &argument, bool autoescape ) const
{
  if (input.type() == QVariant::List)
    return input.toList().size();

  if (input.userType() == qMetaTypeId<SafeString>() || input.type() == QVariant::String)
    return Util::getSafeString( input ).size();

  return QVariant();
}

LengthIsFilter::LengthIsFilter()
{
}

QVariant LengthIsFilter::doFilter( const QVariant& input, const QVariant &argument, bool autoescape ) const
{
  return Util::variantToList( input ).size() == QVariant( argument ).toInt();
}

FirstFilter::FirstFilter()
{
}

QVariant FirstFilter::doFilter( const QVariant& input, const QVariant &argument, bool autoescape ) const
{
  QVariantList varList = Util::variantToList( input );

  if ( varList.isEmpty() )
    return QString();

  return varList.at( 0 );
}


LastFilter::LastFilter()
{
}
<commit_msg>Fix the length_is filter now that argument can contain a list.<commit_after>/*
  This file is part of the Grantlee template system.

  Copyright (c) 2009 <NAME> <<EMAIL>>

  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License version 3 only, as published by the Free Software Foundation.

  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  Lesser General Public License version 3 for more details.

  You should have received a copy of the GNU Lesser General Public
  License along with this library.  If not, see <http://www.gnu.org/licenses/>.

*/

#include "lists.h"
#include <util_p.h>
#include <QDateTime>

JoinFilter::JoinFilter()
{
}

QVariant JoinFilter::doFilter( const QVariant& input, const QVariant &argument, bool autoescape ) const
{
  QVariantList varList = Util::variantToList( input );
  QListIterator<QVariant> i( varList );
  QString ret;
  while ( i.hasNext() ) {
    QVariant var = i.next();
    Grantlee::SafeString s = Util::getSafeString( var );
    if ( autoescape )
      s = Util::conditionalEscape( s );

    ret.append( s );
    if ( i.hasNext() )
    {
      SafeString argString = Util::getSafeString( argument );
      ret.append( argString );
    }
  }
  return Util::markSafe( ret );
}

LengthFilter::LengthFilter()
{
}

QVariant LengthFilter::doFilter( const QVariant& input, const QVariant &argument, bool autoescape ) const
{
  if (input.type() == QVariant::List)
    return input.toList().size();

  if (input.userType() == qMetaTypeId<SafeString>() || input.type() == QVariant::String)
    return Util::getSafeString( input ).size();

  return QVariant();
}

LengthIsFilter::LengthIsFilter()
{
}

QVariant LengthIsFilter::doFilter( const QVariant& input, const QVariant &argument, bool autoescape ) const
{
  if (!input.isValid() || (input.type() == QVariant::Int) || (input.type() == QVariant::DateTime))
    return QVariant();

  int size = 0;
  if (input.type() == QVariant::List)
    size = input.toList().size();

  if (input.userType() == qMetaTypeId<SafeString>() || input.type() == QVariant::String)
    size = Util::getSafeString( input ).size();

  bool ok;
  int argInt = Util::getSafeString( argument ).toInt(&ok);

  if (!ok)
    return QVariant();

  return size == argInt;
}

FirstFilter::FirstFilter()
{
}

QVariant FirstFilter::doFilter( const QVariant& input, const QVariant &argument, bool autoescape ) const
{
  QVariantList varList = Util::variantToList( input );

  if ( varList.isEmpty() )
    return QString();

  return varList.at( 0 );
}


LastFilter::LastFilter()
{
}

{'fraction_non_alphanumeric': 0.12032296179598267, 'fraction_numerical': 0.0029539188656951556, 'mean_word_length': 4.073926073926074, 'pattern_counts': {'":': 0, '<': 20, '<?xml version=': 0, '>': 20, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4718381', 'n_tokens_mistral': 1544, 'n_tokens_neox': 1508, 'n_words': 510}