[Kde-pim] Review Request: Remove all the signature blocks.

Jaime Torres jtamate at gmail.com
Thu Mar 26 18:43:18 GMT 2009


-----------------------------------------------------------
This is an automatically generated e-mail. To reply, visit:
http://reviewboard.kde.org/r/403/
-----------------------------------------------------------

(Updated 2009-03-26 11:43:18.319312)


Review request for KDE PIM.


Changes
-------

I hope this is the good one.


Summary
-------

Remove all the signature blocks.
The algorithm is:
Look for the prefixes before the "-- " marks (i.e. the text before it in the line).
foreach line, if the line contains "-- ", start to delete lines until the prefix changes.

(it should also work with c code if the -- is not followed by a space).


This addresses bug 72316.
    https://bugs.kde.org/show_bug.cgi?id=72316


Diffs (updated)
-----

  /trunk/KDE/kdepim/kmail/kmmessage.cpp 944557 

Diff: http://reviewboard.kde.org/r/403/diff


Testing (updated)
-------

#include <QtCore/QtCore>
#include <iostream>
 
using namespace std;
 
/**
 remove the Signature Blocks (SB) from prefix+"-- (end of line)" until a
 line that
 * does not starts with prefix or
 * starts with prefix+(any substring of prefix)
 */
static QString
stripSignature ( const QString & msg, bool clearSigned )
{
  QRegExp SBDelimiterSearch = clearSigned ? QRegExp( "--\\s?\n" ) :
QRegExp( "-- \n" );
 
  QString res = msg;
  QString commonPrefixChars = QString( '>' );
  QString commonPrefixCharsTmp;
  QString prefix; // the current prefix
  QString line; // the line to check if is part of the SB
  int posDeletingStart = -1;
  int posNewLine = -1;
  int posSignatureBlock = -1;
 
  // Look for prefixes, to guess the used reply prefixes
  // Only usefull for the case where the actual prefix is empty
  while ( (posSignatureBlock = res.indexOf( SBDelimiterSearch,
posSignatureBlock + 1 ))
      >= 0 )
  {
    // Look back for the line begining
    posDeletingStart = res.lastIndexOf( '\n', posSignatureBlock ) + 1;
    // The prefix before "-- "$, for example, in ">> -- " is ">> "
    prefix = res.mid( posDeletingStart, posSignatureBlock - posDeletingStart );
    // The regular expresion to look for prefix change will not contain " "
    for ( int i = 0; i < prefix.size(); i++ )
    {
      if ( prefix[i] != ' ' && !commonPrefixChars.contains( prefix[i] ) )
        commonPrefixChars.append( prefix[i] );
    }
  }
  // The regular expresion to look for prefix change
  QRegExp CommonReplySearch =
      QRegExp( QString( '[' ) + commonPrefixChars + QString( ']' ) );
 
  // While there are SB delimiters
  while ( (posSignatureBlock = res.indexOf( SBDelimiterSearch )) >= 0 )
  {
    // Look back for the line begining
    posDeletingStart = res.lastIndexOf( '\n', posSignatureBlock ) + 1;
    // The prefix before "-- "$, for example, in ">> -- " is ">> "
    prefix = res.mid( posDeletingStart, posSignatureBlock - posDeletingStart );
    posNewLine = res.indexOf( '\n', posSignatureBlock ) + 1;
    // now go to the end of the SB
    while ( posNewLine < res.size() && posNewLine > 0 )
    {
      // check when the SB ends
      line = res.mid( posNewLine, res.indexOf( '\n', posNewLine ) -
posNewLine );
      // * does not starts with prefix or
      // * starts with prefix+(any substring of prefix)
      if ( (prefix.isEmpty() && line.indexOf( CommonReplySearch ) < 0) ||
           (!prefix.isEmpty() && line.startsWith( prefix ) && !prefix.contains(
              line.mid( prefix.size(), 1 ) )) )
      {
        posNewLine = res.indexOf( '\n', posNewLine ) + 1;
      }
      else
        break;
    }
    // remove the SB or truncate when is the last SB
    if ( posNewLine > 0 )
      res.remove( posDeletingStart, posNewLine - posDeletingStart );
    else
      res.truncate( posDeletingStart );
  }
 
  return res;
}
 
int
main ( int argc, const char *argv[] )
{
 
  QStringList tests;
  tests << "text1\n-- \nSignature Block1\nSignature Block1\n\n"
    "> text2\n> -- \n> Signature Block 2\n> Signature Block 2\n"
    ">> text3 -- not a signature block\n>> text3\n"
    ">>> text4\n> -- \n> Signature Block 4\n> Signature Block 4\n"
    ">>-------------\n>>-- text5 --\n>>-------------------\n>> -- \n>>
Signature Block 5\n"
    "text6\n-- \nSignature Block 6";
  tests << "text1\n"
    "> text2\n"
    ">> text3 -- not a signature block\n>> text3\n"
    ">>> text4\n"
    ">>-------------\n>>-- text5 --\n>>-------------------\n"
    "text6\n";
 
  tests << "text1\n-- \nSignature Block1\nSignature Block1\n\n"
    ">text2\n>-- \n>Signature Block 2\n>Signature Block 2\n"
    "> >text3\n> >text3\n> >-- \n>>Signature Block 3\n> >Signature Block 3\n"
    ">text4\n>-- \n>Signature Block 4\n>Signature Block 4\n"
    "text5\n-- \nSignature Block 5";
  tests << "text1\n"
    ">text2\n"
    ">>Signature Block 3\n> >Signature Block 3\n"
    ">text4\n"
    "text5\n";
 
  tests << "Text 1\n-- \nFirst sign\n\n\n> From: bla\n"
    "> Texto 2\n\n> Aqui algo de texto.\n\n>> --\n>> Not Signature
Block 2\n\n> Adios\n"
    "\n>> Texto 3\n\n>> --\n>> Not Signature block 3\n";
  tests << "Text 1\n> From: bla\n"
    "> Texto 2\n\n> Aqui algo de texto.\n\n>> --\n>> Not Signature
Block 2\n\n> Adios\n"
    "\n>> Texto 3\n\n>> --\n>> Not Signature block 3\n";
 
  tests << "-- \n-- ACME, Inc\n-- Joe User\n-- PHB\n-- Tel.: 555 1234\n--";
  tests << "";
 
  QString t4, t5;
  for ( int i = 0; i < 100; i++ )
  {
    t4.append( tests[4] );
    t5.append( tests[5] );
  }
  tests << t4 << t5;
 
  QTime time = QTime::currentTime();
 
  for ( int j = 0; j < 500; j++ )
  {
    for ( int i = 0; i < 5; i++ )
    {
      QString res = stripSignature( tests[i * 2], 0 );
      //      cout << "result:\n" << res.toStdString() << endl <<
"expected:\n" << tests[i*2+1].toStdString() << endl;
      assert( res == tests[i * 2 + 1] );
    }
  }
 
  cout << time.elapsed() << endl;
 
}


Thanks,

Jaime

_______________________________________________
KDE PIM mailing list kde-pim at kde.org
https://mail.kde.org/mailman/listinfo/kde-pim
KDE PIM home page at http://pim.kde.org/



More information about the kde-pim mailing list