| 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 |
4 |
| PRBool meetMSB = PR_FALSE; |
| 49 |
4 |
| newptr = *newBuf = (char*)PR_Malloc(aLen); |
| 50 |
4 |
▶ | if (!newptr) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!newptr) |
0 |
4 |
4 |
|
| 51 |
0 |
| return PR_FALSE; |
| 52 |
|
| |
| 53 |
50 |
▶ | for (curPtr = prevPtr = (char*)aBuf; curPtr < aBuf+aLen; curPtr++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (curPtr = prevPtr = (char*)aBuf; curPtr < aBuf+aLen; curPtr++) |
46 |
4 |
50 |
|
| 54 |
|
| { |
| 55 |
46 |
▶ | if (*curPtr & 0x80) |
Branch: 1 branch
| Branch | True | False | Total |
| if (*curPtr & 0x80) |
28 |
18 |
46 |
|
| 56 |
|
| { |
| 57 |
28 |
| meetMSB = PR_TRUE; |
| 58 |
|
| } |
| 59 |
18 |
▶ | else if (*curPtr < 'A' || (*curPtr > 'Z' && *curPtr < 'a') || *curPtr > 'z') |
Branch: 4 branches
| Branch | True | False | Total |
| else if (*curPtr < 'A' || (*curPtr > 'Z' && *curPtr < 'a') || *curPtr > 'z') |
18 |
0 |
18 |
| else if (*curPtr < 'A' || (*curPtr > 'Z' && *curPtr < 'a') || *curPtr > 'z') |
18 |
0 |
18 |
| else if (*curPtr < 'A' || (*curPtr > 'Z' && *curPtr < 'a') || *curPtr > 'z') |
18 |
0 |
18 |
| else if (*curPtr < 'A' || (*curPtr > 'Z' && *curPtr < 'a') || *curPtr > 'z') |
0 |
18 |
18 |
|
| 60 |
|
| { |
| 61 |
|
| //current char is a symbol, most likely a punctuation. we treat it as segment delimiter |
| 62 |
0 |
▶ | if (meetMSB && curPtr > prevPtr) |
Branch: 2 branches
| Branch | True | False | Total |
| if (meetMSB && curPtr > prevPtr) |
0 |
0 |
0 |
| if (meetMSB && curPtr > prevPtr) |
0 |
0 |
0 |
|
| 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++; |
Branch: 1 branch
| Branch | True | False | Total |
| while (prevPtr < curPtr) *newptr++ = *prevPtr++; |
0 |
0 |
0 |
|
| 66 |
0 |
| prevPtr++; |
| 67 |
0 |
| *newptr++ = ' '; |
| 68 |
0 |
| 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 |
▶ | if (meetMSB && curPtr > prevPtr) |
Branch: 2 branches
| Branch | True | False | Total |
| if (meetMSB && curPtr > prevPtr) |
4 |
0 |
4 |
| if (meetMSB && curPtr > prevPtr) |
4 |
0 |
4 |
|
| 75 |
50 |
▶ | while (prevPtr < curPtr) *newptr++ = *prevPtr++; |
Branch: 1 branch
| Branch | True | False | Total |
| while (prevPtr < curPtr) *newptr++ = *prevPtr++; |
46 |
4 |
50 |
|
| 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 |
4 |
| PRBool isInTag = PR_FALSE; |
| 89 |
|
| |
| 90 |
4 |
| newptr = *newBuf = (char*)PR_Malloc(aLen); |
| 91 |
4 |
▶ | if (!newptr) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!newptr) |
0 |
4 |
4 |
|
| 92 |
0 |
| return PR_FALSE; |
| 93 |
|
| |
| 94 |
50 |
▶ | for (curPtr = prevPtr = (char*)aBuf; curPtr < aBuf+aLen; curPtr++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (curPtr = prevPtr = (char*)aBuf; curPtr < aBuf+aLen; curPtr++) |
46 |
4 |
50 |
|
| 95 |
|
| { |
| 96 |
46 |
▶ | if (*curPtr == '>') |
Branch: 1 branch
| Branch | True | False | Total |
| if (*curPtr == '>') |
0 |
46 |
46 |
|
| 97 |
0 |
| isInTag = PR_FALSE; |
| 98 |
46 |
▶ | else if (*curPtr == '<') |
Branch: 1 branch
| Branch | True | False | Total |
| else if (*curPtr == '<') |
0 |
46 |
46 |
|
| 99 |
0 |
| isInTag = PR_TRUE; |
| 100 |
|
| |
| 101 |
46 |
▶ | if (!(*curPtr & 0x80) && |
Branch: 1 branch
| Branch | True | False | Total |
| if (!(*curPtr & 0x80) && |
18 |
28 |
46 |
|
| 102 |
18 |
▶ | (*curPtr < 'A' || (*curPtr > 'Z' && *curPtr < 'a') || *curPtr > 'z') ) |
Branch: 4 branches
| Branch | True | False | Total |
| (*curPtr < 'A' || (*curPtr > 'Z' && *curPtr < 'a') || *curPtr > 'z') ) |
18 |
0 |
18 |
| (*curPtr < 'A' || (*curPtr > 'Z' && *curPtr < 'a') || *curPtr > 'z') ) |
18 |
0 |
18 |
| (*curPtr < 'A' || (*curPtr > 'Z' && *curPtr < 'a') || *curPtr > 'z') ) |
18 |
0 |
18 |
| (*curPtr < 'A' || (*curPtr > 'Z' && *curPtr < 'a') || *curPtr > 'z') ) |
0 |
18 |
18 |
|
| 103 |
|
| { |
| 104 |
0 |
▶ | if (curPtr > prevPtr && !isInTag) // Current segment contains more than just a symbol |
Branch: 2 branches
| Branch | True | False | Total |
| if (curPtr > prevPtr && !isInTag) // Current segment contains more than just a symbol |
0 |
0 |
0 |
| if (curPtr > prevPtr && !isInTag) // Current segment contains more than just a symbol |
0 |
0 |
0 |
|
| 105 |
|
| // and it is not inside a tag, keep it. |
| 106 |
|
| { |
| 107 |
0 |
▶ | while (prevPtr < curPtr) *newptr++ = *prevPtr++; |
Branch: 1 branch
| Branch | True | False | Total |
| while (prevPtr < curPtr) *newptr++ = *prevPtr++; |
0 |
0 |
0 |
|
| 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 |
▶ | if (!isInTag) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!isInTag) |
4 |
0 |
4 |
|
| 119 |
50 |
▶ | while (prevPtr < curPtr) |
Branch: 1 branch
| Branch | True | False | Total |
| while (prevPtr < curPtr) |
46 |
4 |
50 |
|
| 120 |
46 |
| *newptr++ = *prevPtr++; |
| 121 |
|
| |
| 122 |
4 |
| newLen = newptr - *newBuf; |
| 123 |
|
| |
| 124 |
4 |
| return PR_TRUE; |
| 125 |
|
| } |