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
43 0 : bool nsCharSetProber::FilterWithoutEnglishLetters(const char* aBuf, PRUint32 aLen, char** newBuf, PRUint32& newLen)
44 : {
45 : char *newptr;
46 : char *prevPtr, *curPtr;
47 :
48 0 : bool meetMSB = false;
49 0 : newptr = *newBuf = (char*)PR_Malloc(aLen);
50 0 : if (!newptr)
51 0 : return false;
52 :
53 0 : for (curPtr = prevPtr = (char*)aBuf; curPtr < aBuf+aLen; curPtr++)
54 : {
55 0 : if (*curPtr & 0x80)
56 : {
57 0 : meetMSB = true;
58 : }
59 0 : 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 0 : if (meetMSB && curPtr > prevPtr)
63 : //this segment contains more than single symbol, and it has upper ASCII, we need to keep it
64 : {
65 0 : while (prevPtr < curPtr) *newptr++ = *prevPtr++;
66 0 : prevPtr++;
67 0 : *newptr++ = ' ';
68 0 : meetMSB = false;
69 : }
70 : else //ignore current segment. (either because it is just a symbol or just an English word)
71 0 : prevPtr = curPtr+1;
72 : }
73 : }
74 0 : if (meetMSB && curPtr > prevPtr)
75 0 : while (prevPtr < curPtr) *newptr++ = *prevPtr++;
76 :
77 0 : newLen = newptr - *newBuf;
78 :
79 0 : return true;
80 : }
81 :
82 : //This filter applies to all scripts which contain both English characters and upper ASCII characters.
83 0 : bool 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 0 : bool isInTag = false;
89 :
90 0 : newptr = *newBuf = (char*)PR_Malloc(aLen);
91 0 : if (!newptr)
92 0 : return false;
93 :
94 0 : for (curPtr = prevPtr = (char*)aBuf; curPtr < aBuf+aLen; curPtr++)
95 : {
96 0 : if (*curPtr == '>')
97 0 : isInTag = false;
98 0 : else if (*curPtr == '<')
99 0 : isInTag = true;
100 :
101 0 : if (!(*curPtr & 0x80) &&
102 : (*curPtr < 'A' || (*curPtr > 'Z' && *curPtr < 'a') || *curPtr > 'z') )
103 : {
104 0 : if (curPtr > prevPtr && !isInTag) // Current segment contains more than just a symbol
105 : // and it is not inside a tag, keep it.
106 : {
107 0 : while (prevPtr < curPtr) *newptr++ = *prevPtr++;
108 0 : prevPtr++;
109 0 : *newptr++ = ' ';
110 : }
111 : else
112 0 : 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 0 : if (!isInTag)
119 0 : while (prevPtr < curPtr)
120 0 : *newptr++ = *prevPtr++;
121 :
122 0 : newLen = newptr - *newBuf;
123 :
124 0 : return true;
125 : }
|