Blame src/nsCharSetProber.cpp

Packit 67f6e7
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
Packit 67f6e7
/* ***** BEGIN LICENSE BLOCK *****
Packit 67f6e7
 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
Packit 67f6e7
 *
Packit 67f6e7
 * The contents of this file are subject to the Mozilla Public License Version
Packit 67f6e7
 * 1.1 (the "License"); you may not use this file except in compliance with
Packit 67f6e7
 * the License. You may obtain a copy of the License at
Packit 67f6e7
 * http://www.mozilla.org/MPL/
Packit 67f6e7
 *
Packit 67f6e7
 * Software distributed under the License is distributed on an "AS IS" basis,
Packit 67f6e7
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
Packit 67f6e7
 * for the specific language governing rights and limitations under the
Packit 67f6e7
 * License.
Packit 67f6e7
 *
Packit 67f6e7
 * The Original Code is Mozilla Universal charset detector code.
Packit 67f6e7
 *
Packit 67f6e7
 * The Initial Developer of the Original Code is
Packit 67f6e7
 * Netscape Communications Corporation.
Packit 67f6e7
 * Portions created by the Initial Developer are Copyright (C) 2001
Packit 67f6e7
 * the Initial Developer. All Rights Reserved.
Packit 67f6e7
 *
Packit 67f6e7
 * Contributor(s):
Packit 67f6e7
 *          Shy Shalom <shooshX@gmail.com>
Packit 67f6e7
 *
Packit 67f6e7
 * Alternatively, the contents of this file may be used under the terms of
Packit 67f6e7
 * either the GNU General Public License Version 2 or later (the "GPL"), or
Packit 67f6e7
 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
Packit 67f6e7
 * in which case the provisions of the GPL or the LGPL are applicable instead
Packit 67f6e7
 * of those above. If you wish to allow use of your version of this file only
Packit 67f6e7
 * under the terms of either the GPL or the LGPL, and not to allow others to
Packit 67f6e7
 * use your version of this file under the terms of the MPL, indicate your
Packit 67f6e7
 * decision by deleting the provisions above and replace them with the notice
Packit 67f6e7
 * and other provisions required by the GPL or the LGPL. If you do not delete
Packit 67f6e7
 * the provisions above, a recipient may use your version of this file under
Packit 67f6e7
 * the terms of any one of the MPL, the GPL or the LGPL.
Packit 67f6e7
 *
Packit 67f6e7
 * ***** END LICENSE BLOCK ***** */
Packit 67f6e7
 
Packit 67f6e7
#include "nsCharSetProber.h"
Packit 67f6e7
#include "prmem.h"
Packit 67f6e7
Packit 67f6e7
//This filter applies to all scripts which do not use English characters
Packit 67f6e7
PRBool nsCharSetProber::FilterWithoutEnglishLetters(const char* aBuf, PRUint32 aLen, char** newBuf, PRUint32& newLen)
Packit 67f6e7
{
Packit 67f6e7
  char *newptr;
Packit 67f6e7
  char *prevPtr, *curPtr;
Packit 67f6e7
  
Packit 67f6e7
  PRBool meetMSB = PR_FALSE;   
Packit 67f6e7
  newptr = *newBuf = (char*)PR_Malloc(aLen);
Packit 67f6e7
  if (!newptr)
Packit 67f6e7
    return PR_FALSE;
Packit 67f6e7
Packit 67f6e7
  for (curPtr = prevPtr = (char*)aBuf; curPtr < aBuf+aLen; curPtr++)
Packit 67f6e7
  {
Packit 67f6e7
    if (*curPtr & 0x80)
Packit 67f6e7
    {
Packit 67f6e7
      meetMSB = PR_TRUE;
Packit 67f6e7
    }
Packit 67f6e7
    else if (*curPtr < 'A' || (*curPtr > 'Z' && *curPtr < 'a') || *curPtr > 'z') 
Packit 67f6e7
    {
Packit 67f6e7
      //current char is a symbol, most likely a punctuation. we treat it as segment delimiter
Packit 67f6e7
      if (meetMSB && curPtr > prevPtr) 
Packit 67f6e7
      //this segment contains more than single symbol, and it has upper ASCII, we need to keep it
Packit 67f6e7
      {
Packit 67f6e7
        while (prevPtr < curPtr) *newptr++ = *prevPtr++;  
Packit 67f6e7
        prevPtr++;
Packit 67f6e7
        *newptr++ = ' ';
Packit 67f6e7
        meetMSB = PR_FALSE;
Packit 67f6e7
      }
Packit 67f6e7
      else //ignore current segment. (either because it is just a symbol or just an English word)
Packit 67f6e7
        prevPtr = curPtr+1;
Packit 67f6e7
    }
Packit 67f6e7
  }
Packit 67f6e7
  if (meetMSB && curPtr > prevPtr) 
Packit 67f6e7
    while (prevPtr < curPtr) *newptr++ = *prevPtr++;  
Packit 67f6e7
Packit 67f6e7
  newLen = newptr - *newBuf;
Packit 67f6e7
Packit 67f6e7
  return PR_TRUE;
Packit 67f6e7
}
Packit 67f6e7
Packit 67f6e7
//This filter applies to all scripts which contain both English characters and upper ASCII characters.
Packit 67f6e7
PRBool nsCharSetProber::FilterWithEnglishLetters(const char* aBuf, PRUint32 aLen, char** newBuf, PRUint32& newLen)
Packit 67f6e7
{
Packit 67f6e7
  //do filtering to reduce load to probers
Packit 67f6e7
  char *newptr;
Packit 67f6e7
  char *prevPtr, *curPtr;
Packit 67f6e7
  PRBool isInTag = PR_FALSE;
Packit 67f6e7
Packit 67f6e7
  newptr = *newBuf = (char*)PR_Malloc(aLen);
Packit 67f6e7
  if (!newptr)
Packit 67f6e7
    return PR_FALSE;
Packit 67f6e7
Packit 67f6e7
  for (curPtr = prevPtr = (char*)aBuf; curPtr < aBuf+aLen; curPtr++)
Packit 67f6e7
  {
Packit 67f6e7
    if (*curPtr == '>')
Packit 67f6e7
      isInTag = PR_FALSE;
Packit 67f6e7
    else if (*curPtr == '<')
Packit 67f6e7
      isInTag = PR_TRUE;
Packit 67f6e7
Packit 67f6e7
    if (!(*curPtr & 0x80) &&
Packit 67f6e7
        (*curPtr < 'A' || (*curPtr > 'Z' && *curPtr < 'a') || *curPtr > 'z') )
Packit 67f6e7
    {
Packit 67f6e7
      if (curPtr > prevPtr && !isInTag) // Current segment contains more than just a symbol 
Packit 67f6e7
                                        // and it is not inside a tag, keep it.
Packit 67f6e7
      {
Packit 67f6e7
        while (prevPtr < curPtr) *newptr++ = *prevPtr++;  
Packit 67f6e7
        prevPtr++;
Packit 67f6e7
        *newptr++ = ' ';
Packit 67f6e7
      }
Packit 67f6e7
      else
Packit 67f6e7
        prevPtr = curPtr+1;
Packit 67f6e7
    }
Packit 67f6e7
  }
Packit 67f6e7
Packit 67f6e7
  // If the current segment contains more than just a symbol 
Packit 67f6e7
  // and it is not inside a tag then keep it.
Packit 67f6e7
  if (!isInTag)
Packit 67f6e7
    while (prevPtr < curPtr)
Packit 67f6e7
      *newptr++ = *prevPtr++;  
Packit 67f6e7
Packit 67f6e7
  newLen = newptr - *newBuf;
Packit 67f6e7
Packit 67f6e7
  return PR_TRUE;
Packit 67f6e7
}