nsresult nsMsgSearchAdapter::EncodeImap (char **ppOutEncoding, nsISupportsArray *searchTerms, const PRUnichar *srcCharset, const PRUnichar *destCharset, bool reallyDredd)
{
  // i've left the old code (before using CBoolExpression for debugging purposes to make sure that
  // the new code generates the same encoding string as the old code.....

  nsresult err = NS_OK;
  *ppOutEncoding = nsnull;

  PRUint32 termCount;
  searchTerms->Count(&termCount);
  PRUint32 i = 0;

  // create our expression
  nsMsgSearchBoolExpression * expression = new nsMsgSearchBoolExpression();
  if (!expression)
    return NS_ERROR_OUT_OF_MEMORY;

  for (i = 0; i < termCount && NS_SUCCEEDED(err); i++)
  {
    char *termEncoding;
    bool matchAll;
    nsCOMPtr<nsIMsgSearchTerm> pTerm;
    searchTerms->QueryElementAt(i, NS_GET_IID(nsIMsgSearchTerm),
      (void **)getter_AddRefs(pTerm));
    pTerm->GetMatchAll(&matchAll);
    if (matchAll)
      continue;
    err = EncodeImapTerm (pTerm, reallyDredd, srcCharset, destCharset, &termEncoding);
    if (NS_SUCCEEDED(err) && nsnull != termEncoding)
    {
      expression = nsMsgSearchBoolExpression::AddSearchTerm(expression, pTerm, termEncoding);
      delete [] termEncoding;
    }
  }

  if (NS_SUCCEEDED(err))
  {
    // Catenate the intermediate encodings together into a big string
    nsCAutoString encodingBuff;

    if (!reallyDredd)
      encodingBuff.Append(m_kImapUnDeleted);

    expression->GenerateEncodeStr(&encodingBuff);
    *ppOutEncoding = ToNewCString(encodingBuff);
  }

  delete expression;

  return err;
}
nsresult nsMsgSearchAdapter::EncodeImap(char **ppOutEncoding,
                                        nsIArray *searchTerms,
                                        const char16_t *srcCharset,
                                        const char16_t *destCharset,
                                        bool reallyDredd) {
  // i've left the old code (before using CBoolExpression for debugging purposes
  // to make sure that the new code generates the same encoding string as the
  // old code.....

  nsresult err = NS_OK;
  *ppOutEncoding = nullptr;

  uint32_t termCount;
  searchTerms->GetLength(&termCount);
  uint32_t i = 0;

  // create our expression
  nsMsgSearchBoolExpression *expression = new nsMsgSearchBoolExpression();
  if (!expression) return NS_ERROR_OUT_OF_MEMORY;

  for (i = 0; i < termCount && NS_SUCCEEDED(err); i++) {
    char *termEncoding;
    bool matchAll;
    nsCOMPtr<nsIMsgSearchTerm> pTerm = do_QueryElementAt(searchTerms, i);
    pTerm->GetMatchAll(&matchAll);
    if (matchAll) continue;
    err = EncodeImapTerm(pTerm, reallyDredd, srcCharset, destCharset,
                         &termEncoding);
    if (NS_SUCCEEDED(err) && nullptr != termEncoding) {
      expression = nsMsgSearchBoolExpression::AddSearchTerm(expression, pTerm,
                                                            termEncoding);
      delete[] termEncoding;
    }
  }

  if (NS_SUCCEEDED(err)) {
    // Catenate the intermediate encodings together into a big string
    nsAutoCString encodingBuff;

    if (!reallyDredd) encodingBuff.Append(m_kImapUnDeleted);

    expression->GenerateEncodeStr(&encodingBuff);
    *ppOutEncoding = ToNewCString(encodingBuff);
  }

  delete expression;

  return err;
}