1 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 *
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
9 *
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
13 * License.
14 *
15 * The Original Code is Mozilla Universal charset detector code.
16 *
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 2001
20 * the Initial Developer. All Rights Reserved.
21 *
22 * Contributor(s):
23 * Shy Shalom <shooshX@gmail.com>
24 *
25 * Alternatively, the contents of this file may be used under the terms of
26 * either the GNU General Public License Version 2 or later (the "GPL"), or
27 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 * in which case the provisions of the GPL or the LGPL are applicable instead
29 * of those above. If you wish to allow use of your version of this file only
30 * under the terms of either the GPL or the LGPL, and not to allow others to
31 * use your version of this file under the terms of the MPL, indicate your
32 * decision by deleting the provisions above and replace them with the notice
33 * and other provisions required by the GPL or the LGPL. If you do not delete
34 * the provisions above, a recipient may use your version of this file under
35 * the terms of any one of the MPL, the GPL or the LGPL.
36 *
37 * ***** END LICENSE BLOCK ***** */
38
39 #include "nsCharSetProber.h"
40 #include "prmem.h"
41
42 //This filter applies to all scripts which do not use English characters
FilterWithoutEnglishLetters(const char * aBuf,PRUint32 aLen,char ** newBuf,PRUint32 & newLen)43 PRBool nsCharSetProber::FilterWithoutEnglishLetters(const char* aBuf, PRUint32 aLen, char** newBuf, PRUint32& newLen)
44 {
45 char *newptr;
46 char *prevPtr, *curPtr;
47
48 PRBool meetMSB = PR_FALSE;
49 newptr = *newBuf = (char*)PR_Malloc(aLen);
50 if (!newptr)
51 return PR_FALSE;
52
53 for (curPtr = prevPtr = (char*)aBuf; curPtr < aBuf+aLen; curPtr++)
54 {
55 if (*curPtr & 0x80)
56 {
57 meetMSB = PR_TRUE;
58 }
59 else if (*curPtr < 'A' || (*curPtr > 'Z' && *curPtr < 'a') || *curPtr > 'z')
60 {
61 //current char is a symbol, most likely a punctuation. we treat it as segment delimiter
62 if (meetMSB && curPtr > prevPtr)
63 //this segment contains more than single symbol, and it has upper ASCII, we need to keep it
64 {
65 while (prevPtr < curPtr) *newptr++ = *prevPtr++;
66 prevPtr++;
67 *newptr++ = ' ';
68 meetMSB = PR_FALSE;
69 }
70 else //ignore current segment. (either because it is just a symbol or just an English word)
71 prevPtr = curPtr+1;
72 }
73 }
74 if (meetMSB && curPtr > prevPtr)
75 while (prevPtr < curPtr) *newptr++ = *prevPtr++;
76
77 newLen = newptr - *newBuf;
78
79 return PR_TRUE;
80 }
81
82 //This filter applies to all scripts which contain both English characters and upper ASCII characters.
FilterWithEnglishLetters(const char * aBuf,PRUint32 aLen,char ** newBuf,PRUint32 & newLen)83 PRBool nsCharSetProber::FilterWithEnglishLetters(const char* aBuf, PRUint32 aLen, char** newBuf, PRUint32& newLen)
84 {
85 //do filtering to reduce load to probers
86 char *newptr;
87 char *prevPtr, *curPtr;
88 PRBool isInTag = PR_FALSE;
89
90 newptr = *newBuf = (char*)PR_Malloc(aLen);
91 if (!newptr)
92 return PR_FALSE;
93
94 for (curPtr = prevPtr = (char*)aBuf; curPtr < aBuf+aLen; curPtr++)
95 {
96 if (*curPtr == '>')
97 isInTag = PR_FALSE;
98 else if (*curPtr == '<')
99 isInTag = PR_TRUE;
100
101 if (!(*curPtr & 0x80) &&
102 (*curPtr < 'A' || (*curPtr > 'Z' && *curPtr < 'a') || *curPtr > 'z') )
103 {
104 if (curPtr > prevPtr && !isInTag) // Current segment contains more than just a symbol
105 // and it is not inside a tag, keep it.
106 {
107 while (prevPtr < curPtr) *newptr++ = *prevPtr++;
108 prevPtr++;
109 *newptr++ = ' ';
110 }
111 else
112 prevPtr = curPtr+1;
113 }
114 }
115
116 // If the current segment contains more than just a symbol
117 // and it is not inside a tag then keep it.
118 if (!isInTag)
119 while (prevPtr < curPtr)
120 *newptr++ = *prevPtr++;
121
122 newLen = newptr - *newBuf;
123
124 return PR_TRUE;
125 }
126