intl/lwbrk/src/nsSampleWordBreaker.cpp

Wed, 31 Dec 2014 07:22:50 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 07:22:50 +0100
branch
TOR_BUG_3246
changeset 4
fc2d59ddac77
permissions
-rw-r--r--

Correct previous dual key logic pending first delivery installment.

michael@0 1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5
michael@0 6
michael@0 7 #include "nsSampleWordBreaker.h"
michael@0 8
michael@0 9 nsSampleWordBreaker::nsSampleWordBreaker()
michael@0 10 {
michael@0 11 }
michael@0 12 nsSampleWordBreaker::~nsSampleWordBreaker()
michael@0 13 {
michael@0 14 }
michael@0 15
michael@0 16 NS_IMPL_ISUPPORTS(nsSampleWordBreaker, nsIWordBreaker)
michael@0 17
michael@0 18 bool nsSampleWordBreaker::BreakInBetween(
michael@0 19 const char16_t* aText1 , uint32_t aTextLen1,
michael@0 20 const char16_t* aText2 , uint32_t aTextLen2)
michael@0 21 {
michael@0 22 NS_PRECONDITION( nullptr != aText1, "null ptr");
michael@0 23 NS_PRECONDITION( nullptr != aText2, "null ptr");
michael@0 24
michael@0 25 if(!aText1 || !aText2 || (0 == aTextLen1) || (0 == aTextLen2))
michael@0 26 return false;
michael@0 27
michael@0 28 return (this->GetClass(aText1[aTextLen1-1]) != this->GetClass(aText2[0]));
michael@0 29 }
michael@0 30
michael@0 31
michael@0 32 #define IS_ASCII(c) (0 == ( 0xFF80 & (c)))
michael@0 33 #define ASCII_IS_ALPHA(c) ((( 'a' <= (c)) && ((c) <= 'z')) || (( 'A' <= (c)) && ((c) <= 'Z')))
michael@0 34 #define ASCII_IS_DIGIT(c) (( '0' <= (c)) && ((c) <= '9'))
michael@0 35 #define ASCII_IS_SPACE(c) (( ' ' == (c)) || ( '\t' == (c)) || ( '\r' == (c)) || ( '\n' == (c)))
michael@0 36 #define IS_ALPHABETICAL_SCRIPT(c) ((c) < 0x2E80)
michael@0 37
michael@0 38 // we change the beginning of IS_HAN from 0x4e00 to 0x3400 to relfect Unicode 3.0
michael@0 39 #define IS_HAN(c) (( 0x3400 <= (c)) && ((c) <= 0x9fff))||(( 0xf900 <= (c)) && ((c) <= 0xfaff))
michael@0 40 #define IS_KATAKANA(c) (( 0x30A0 <= (c)) && ((c) <= 0x30FF))
michael@0 41 #define IS_HIRAGANA(c) (( 0x3040 <= (c)) && ((c) <= 0x309F))
michael@0 42 #define IS_HALFWIDTHKATAKANA(c) (( 0xFF60 <= (c)) && ((c) <= 0xFF9F))
michael@0 43 #define IS_THAI(c) (0x0E00 == (0xFF80 & (c) )) // Look at the higest 9 bits
michael@0 44
michael@0 45 uint8_t nsSampleWordBreaker::GetClass(char16_t c)
michael@0 46 {
michael@0 47 // begin of the hack
michael@0 48
michael@0 49 if (IS_ALPHABETICAL_SCRIPT(c)) {
michael@0 50 if(IS_ASCII(c)) {
michael@0 51 if(ASCII_IS_SPACE(c)) {
michael@0 52 return kWbClassSpace;
michael@0 53 } else if(ASCII_IS_ALPHA(c) || ASCII_IS_DIGIT(c)) {
michael@0 54 return kWbClassAlphaLetter;
michael@0 55 } else {
michael@0 56 return kWbClassPunct;
michael@0 57 }
michael@0 58 } else if(IS_THAI(c)) {
michael@0 59 return kWbClassThaiLetter;
michael@0 60 } else if (c == 0x00A0/*NBSP*/) {
michael@0 61 return kWbClassSpace;
michael@0 62 } else {
michael@0 63 return kWbClassAlphaLetter;
michael@0 64 }
michael@0 65 } else {
michael@0 66 if(IS_HAN(c)) {
michael@0 67 return kWbClassHanLetter;
michael@0 68 } else if(IS_KATAKANA(c)) {
michael@0 69 return kWbClassKatakanaLetter;
michael@0 70 } else if(IS_HIRAGANA(c)) {
michael@0 71 return kWbClassHiraganaLetter;
michael@0 72 } else if(IS_HALFWIDTHKATAKANA(c)) {
michael@0 73 return kWbClassHWKatakanaLetter;
michael@0 74 } else {
michael@0 75 return kWbClassAlphaLetter;
michael@0 76 }
michael@0 77 }
michael@0 78 return 0;
michael@0 79 }
michael@0 80
michael@0 81 nsWordRange nsSampleWordBreaker::FindWord(
michael@0 82 const char16_t* aText , uint32_t aTextLen,
michael@0 83 uint32_t aOffset)
michael@0 84 {
michael@0 85 nsWordRange range;
michael@0 86 NS_PRECONDITION( nullptr != aText, "null ptr");
michael@0 87 NS_PRECONDITION( 0 != aTextLen, "len = 0");
michael@0 88 NS_PRECONDITION( aOffset <= aTextLen, "aOffset > aTextLen");
michael@0 89
michael@0 90 range.mBegin = aTextLen + 1;
michael@0 91 range.mEnd = aTextLen + 1;
michael@0 92
michael@0 93 if(!aText || aOffset > aTextLen)
michael@0 94 return range;
michael@0 95
michael@0 96 uint8_t c = this->GetClass(aText[aOffset]);
michael@0 97 uint32_t i;
michael@0 98 // Scan forward
michael@0 99 range.mEnd--;
michael@0 100 for(i = aOffset +1;i <= aTextLen; i++)
michael@0 101 {
michael@0 102 if( c != this->GetClass(aText[i]))
michael@0 103 {
michael@0 104 range.mEnd = i;
michael@0 105 break;
michael@0 106 }
michael@0 107 }
michael@0 108
michael@0 109 // Scan backward
michael@0 110 range.mBegin = 0;
michael@0 111 for(i = aOffset ;i > 0; i--)
michael@0 112 {
michael@0 113 if( c != this->GetClass(aText[i-1]))
michael@0 114 {
michael@0 115 range.mBegin = i;
michael@0 116 break;
michael@0 117 }
michael@0 118 }
michael@0 119 if(kWbClassThaiLetter == c)
michael@0 120 {
michael@0 121 // need to call Thai word breaker from here
michael@0 122 // we should pass the whole Thai segment to the thai word breaker to find a shorter answer
michael@0 123 }
michael@0 124 return range;
michael@0 125 }
michael@0 126
michael@0 127 int32_t nsSampleWordBreaker::NextWord(
michael@0 128 const char16_t* aText, uint32_t aLen, uint32_t aPos)
michael@0 129 {
michael@0 130 int8_t c1, c2;
michael@0 131 uint32_t cur = aPos;
michael@0 132 if (cur == aLen)
michael@0 133 return NS_WORDBREAKER_NEED_MORE_TEXT;
michael@0 134 c1 = this->GetClass(aText[cur]);
michael@0 135
michael@0 136 for(cur++; cur <aLen; cur++)
michael@0 137 {
michael@0 138 c2 = this->GetClass(aText[cur]);
michael@0 139 if(c2 != c1)
michael@0 140 break;
michael@0 141 }
michael@0 142 if(kWbClassThaiLetter == c1)
michael@0 143 {
michael@0 144 // need to call Thai word breaker from here
michael@0 145 // we should pass the whole Thai segment to the thai word breaker to find a shorter answer
michael@0 146 }
michael@0 147 if (cur == aLen)
michael@0 148 return NS_WORDBREAKER_NEED_MORE_TEXT;
michael@0 149 return cur;
michael@0 150 }

mercurial