michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: michael@0: #include "nsSampleWordBreaker.h" michael@0: michael@0: nsSampleWordBreaker::nsSampleWordBreaker() michael@0: { michael@0: } michael@0: nsSampleWordBreaker::~nsSampleWordBreaker() michael@0: { michael@0: } michael@0: michael@0: NS_IMPL_ISUPPORTS(nsSampleWordBreaker, nsIWordBreaker) michael@0: michael@0: bool nsSampleWordBreaker::BreakInBetween( michael@0: const char16_t* aText1 , uint32_t aTextLen1, michael@0: const char16_t* aText2 , uint32_t aTextLen2) michael@0: { michael@0: NS_PRECONDITION( nullptr != aText1, "null ptr"); michael@0: NS_PRECONDITION( nullptr != aText2, "null ptr"); michael@0: michael@0: if(!aText1 || !aText2 || (0 == aTextLen1) || (0 == aTextLen2)) michael@0: return false; michael@0: michael@0: return (this->GetClass(aText1[aTextLen1-1]) != this->GetClass(aText2[0])); michael@0: } michael@0: michael@0: michael@0: #define IS_ASCII(c) (0 == ( 0xFF80 & (c))) michael@0: #define ASCII_IS_ALPHA(c) ((( 'a' <= (c)) && ((c) <= 'z')) || (( 'A' <= (c)) && ((c) <= 'Z'))) michael@0: #define ASCII_IS_DIGIT(c) (( '0' <= (c)) && ((c) <= '9')) michael@0: #define ASCII_IS_SPACE(c) (( ' ' == (c)) || ( '\t' == (c)) || ( '\r' == (c)) || ( '\n' == (c))) michael@0: #define IS_ALPHABETICAL_SCRIPT(c) ((c) < 0x2E80) michael@0: michael@0: // we change the beginning of IS_HAN from 0x4e00 to 0x3400 to relfect Unicode 3.0 michael@0: #define IS_HAN(c) (( 0x3400 <= (c)) && ((c) <= 0x9fff))||(( 0xf900 <= (c)) && ((c) <= 0xfaff)) michael@0: #define IS_KATAKANA(c) (( 0x30A0 <= (c)) && ((c) <= 0x30FF)) michael@0: #define IS_HIRAGANA(c) (( 0x3040 <= (c)) && ((c) <= 0x309F)) michael@0: #define IS_HALFWIDTHKATAKANA(c) (( 0xFF60 <= (c)) && ((c) <= 0xFF9F)) michael@0: #define IS_THAI(c) (0x0E00 == (0xFF80 & (c) )) // Look at the higest 9 bits michael@0: michael@0: uint8_t nsSampleWordBreaker::GetClass(char16_t c) michael@0: { michael@0: // begin of the hack michael@0: michael@0: if (IS_ALPHABETICAL_SCRIPT(c)) { michael@0: if(IS_ASCII(c)) { michael@0: if(ASCII_IS_SPACE(c)) { michael@0: return kWbClassSpace; michael@0: } else if(ASCII_IS_ALPHA(c) || ASCII_IS_DIGIT(c)) { michael@0: return kWbClassAlphaLetter; michael@0: } else { michael@0: return kWbClassPunct; michael@0: } michael@0: } else if(IS_THAI(c)) { michael@0: return kWbClassThaiLetter; michael@0: } else if (c == 0x00A0/*NBSP*/) { michael@0: return kWbClassSpace; michael@0: } else { michael@0: return kWbClassAlphaLetter; michael@0: } michael@0: } else { michael@0: if(IS_HAN(c)) { michael@0: return kWbClassHanLetter; michael@0: } else if(IS_KATAKANA(c)) { michael@0: return kWbClassKatakanaLetter; michael@0: } else if(IS_HIRAGANA(c)) { michael@0: return kWbClassHiraganaLetter; michael@0: } else if(IS_HALFWIDTHKATAKANA(c)) { michael@0: return kWbClassHWKatakanaLetter; michael@0: } else { michael@0: return kWbClassAlphaLetter; michael@0: } michael@0: } michael@0: return 0; michael@0: } michael@0: michael@0: nsWordRange nsSampleWordBreaker::FindWord( michael@0: const char16_t* aText , uint32_t aTextLen, michael@0: uint32_t aOffset) michael@0: { michael@0: nsWordRange range; michael@0: NS_PRECONDITION( nullptr != aText, "null ptr"); michael@0: NS_PRECONDITION( 0 != aTextLen, "len = 0"); michael@0: NS_PRECONDITION( aOffset <= aTextLen, "aOffset > aTextLen"); michael@0: michael@0: range.mBegin = aTextLen + 1; michael@0: range.mEnd = aTextLen + 1; michael@0: michael@0: if(!aText || aOffset > aTextLen) michael@0: return range; michael@0: michael@0: uint8_t c = this->GetClass(aText[aOffset]); michael@0: uint32_t i; michael@0: // Scan forward michael@0: range.mEnd--; michael@0: for(i = aOffset +1;i <= aTextLen; i++) michael@0: { michael@0: if( c != this->GetClass(aText[i])) michael@0: { michael@0: range.mEnd = i; michael@0: break; michael@0: } michael@0: } michael@0: michael@0: // Scan backward michael@0: range.mBegin = 0; michael@0: for(i = aOffset ;i > 0; i--) michael@0: { michael@0: if( c != this->GetClass(aText[i-1])) michael@0: { michael@0: range.mBegin = i; michael@0: break; michael@0: } michael@0: } michael@0: if(kWbClassThaiLetter == c) michael@0: { michael@0: // need to call Thai word breaker from here michael@0: // we should pass the whole Thai segment to the thai word breaker to find a shorter answer michael@0: } michael@0: return range; michael@0: } michael@0: michael@0: int32_t nsSampleWordBreaker::NextWord( michael@0: const char16_t* aText, uint32_t aLen, uint32_t aPos) michael@0: { michael@0: int8_t c1, c2; michael@0: uint32_t cur = aPos; michael@0: if (cur == aLen) michael@0: return NS_WORDBREAKER_NEED_MORE_TEXT; michael@0: c1 = this->GetClass(aText[cur]); michael@0: michael@0: for(cur++; cur GetClass(aText[cur]); michael@0: if(c2 != c1) michael@0: break; michael@0: } michael@0: if(kWbClassThaiLetter == c1) michael@0: { michael@0: // need to call Thai word breaker from here michael@0: // we should pass the whole Thai segment to the thai word breaker to find a shorter answer michael@0: } michael@0: if (cur == aLen) michael@0: return NS_WORDBREAKER_NEED_MORE_TEXT; michael@0: return cur; michael@0: }