nsCharSetProber.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. #include "nsCharSetProber.h"
  39. #include "prmem.h"
  40. //This filter applies to all scripts which do not use English characters
  41. PRBool nsCharSetProber::FilterWithoutEnglishLetters(const char* aBuf, PRUint32 aLen, char** newBuf, PRUint32& newLen)
  42. {
  43. char *newptr;
  44. char *prevPtr, *curPtr;
  45. PRBool meetMSB = PR_FALSE;
  46. newptr = *newBuf = (char*)PR_Malloc(aLen);
  47. if (!newptr)
  48. return PR_FALSE;
  49. for (curPtr = prevPtr = (char*)aBuf; curPtr < aBuf+aLen; curPtr++)
  50. {
  51. if (*curPtr & 0x80)
  52. {
  53. meetMSB = PR_TRUE;
  54. }
  55. else if (*curPtr < 'A' || (*curPtr > 'Z' && *curPtr < 'a') || *curPtr > 'z')
  56. {
  57. //current char is a symbol, most likely a punctuation. we treat it as segment delimiter
  58. if (meetMSB && curPtr > prevPtr)
  59. //this segment contains more than single symbol, and it has upper ASCII, we need to keep it
  60. {
  61. while (prevPtr < curPtr) *newptr++ = *prevPtr++;
  62. prevPtr++;
  63. *newptr++ = ' ';
  64. meetMSB = PR_FALSE;
  65. }
  66. else //ignore current segment. (either because it is just a symbol or just an English word)
  67. prevPtr = curPtr+1;
  68. }
  69. }
  70. if (meetMSB && curPtr > prevPtr)
  71. while (prevPtr < curPtr) *newptr++ = *prevPtr++;
  72. newLen = newptr - *newBuf;
  73. return PR_TRUE;
  74. }
  75. //This filter applies to all scripts which contain both English characters and upper ASCII characters.
  76. PRBool nsCharSetProber::FilterWithEnglishLetters(const char* aBuf, PRUint32 aLen, char** newBuf, PRUint32& newLen)
  77. {
  78. //do filtering to reduce load to probers
  79. char *newptr;
  80. char *prevPtr, *curPtr;
  81. PRBool isInTag = PR_FALSE;
  82. newptr = *newBuf = (char*)PR_Malloc(aLen);
  83. if (!newptr)
  84. return PR_FALSE;
  85. for (curPtr = prevPtr = (char*)aBuf; curPtr < aBuf+aLen; curPtr++)
  86. {
  87. if (*curPtr == '>')
  88. isInTag = PR_FALSE;
  89. else if (*curPtr == '<')
  90. isInTag = PR_TRUE;
  91. if (!(*curPtr & 0x80) &&
  92. (*curPtr < 'A' || (*curPtr > 'Z' && *curPtr < 'a') || *curPtr > 'z') )
  93. {
  94. if (curPtr > prevPtr && !isInTag) // Current segment contains more than just a symbol
  95. // and it is not inside a tag, keep it.
  96. {
  97. while (prevPtr < curPtr) *newptr++ = *prevPtr++;
  98. prevPtr++;
  99. *newptr++ = ' ';
  100. }
  101. else
  102. prevPtr = curPtr+1;
  103. }
  104. }
  105. // If the current segment contains more than just a symbol
  106. // and it is not inside a tag then keep it.
  107. if (!isInTag)
  108. while (prevPtr < curPtr)
  109. *newptr++ = *prevPtr++;
  110. newLen = newptr - *newBuf;
  111. return PR_TRUE;
  112. }