File Coverage

src/nsCharSetProber.cpp
Criterion Covered Total %
statement 23 33 69.7
branch 24 44 54.5
condition n/a
subroutine n/a
pod n/a
total 47 77 61.0


line stmt bran cond sub pod time code
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 4           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 4           newptr = *newBuf = (char*)PR_Malloc(aLen);
50 4 50         if (!newptr)
51             return PR_FALSE;
52              
53 50 100         for (curPtr = prevPtr = (char*)aBuf; curPtr < aBuf+aLen; curPtr++)
54             {
55 46 100         if (*curPtr & 0x80)
56             {
57             meetMSB = PR_TRUE;
58             }
59 18 50         else if (*curPtr < 'A' || (*curPtr > 'Z' && *curPtr < 'a') || *curPtr > 'z')
    50          
    50          
60             {
61             //current char is a symbol, most likely a punctuation. we treat it as segment delimiter
62 0 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 0         while (prevPtr < curPtr) *newptr++ = *prevPtr++;
66 0           prevPtr++;
67 0           *newptr++ = ' ';
68             meetMSB = PR_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 4 50         if (meetMSB && curPtr > prevPtr)
75 50 100         while (prevPtr < curPtr) *newptr++ = *prevPtr++;
76              
77 4           newLen = newptr - *newBuf;
78              
79 4           return PR_TRUE;
80             }
81              
82             //This filter applies to all scripts which contain both English characters and upper ASCII characters.
83 4           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 4           newptr = *newBuf = (char*)PR_Malloc(aLen);
91 4 50         if (!newptr)
92             return PR_FALSE;
93              
94 50 100         for (curPtr = prevPtr = (char*)aBuf; curPtr < aBuf+aLen; curPtr++)
95             {
96 46 50         if (*curPtr == '>')
97             isInTag = PR_FALSE;
98 46 50         else if (*curPtr == '<')
99             isInTag = PR_TRUE;
100              
101 46 100         if (!(*curPtr & 0x80) &&
    50          
102 18 50         (*curPtr < 'A' || (*curPtr > 'Z' && *curPtr < 'a') || *curPtr > 'z') )
    50          
103             {
104 0 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 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 4 50         if (!isInTag)
119 50 100         while (prevPtr < curPtr)
120 46           *newptr++ = *prevPtr++;
121              
122 4           newLen = newptr - *newBuf;
123              
124 4           return PR_TRUE;
125             }