diff options
author | Thomas Benisch <tbe@openoffice.org> | 2002-09-18 12:49:44 +0000 |
---|---|---|
committer | Thomas Benisch <tbe@openoffice.org> | 2002-09-18 12:49:44 +0000 |
commit | fe9a63f3f2b9e7f87c55ca458edf32d5775612d0 (patch) | |
tree | 6e26d8bf0b819e2fc3c115b5f4cafe7b21128318 /comphelper/source/misc/accessibletexthelper.cxx | |
parent | d17a75a07921ef6dec1101ddec888972907f1850 (diff) |
#102626# text length is a valid index for getTextAtIndex, getTextBeforeIndex, getTextBehindIndex
Diffstat (limited to 'comphelper/source/misc/accessibletexthelper.cxx')
-rw-r--r-- | comphelper/source/misc/accessibletexthelper.cxx | 200 |
1 files changed, 131 insertions, 69 deletions
diff --git a/comphelper/source/misc/accessibletexthelper.cxx b/comphelper/source/misc/accessibletexthelper.cxx index 9bfc64983644..79b58ff05644 100644 --- a/comphelper/source/misc/accessibletexthelper.cxx +++ b/comphelper/source/misc/accessibletexthelper.cxx @@ -2,9 +2,9 @@ * * $RCSfile: accessibletexthelper.cxx,v $ * - * $Revision: 1.2 $ + * $Revision: 1.3 $ * - * last change: $Author: fs $ $Date: 2002-09-13 12:35:02 $ + * last change: $Author: tbe $ $Date: 2002-09-18 13:48:28 $ * * The Contents of this file are made available subject to the terms of * either of the following licenses @@ -146,6 +146,13 @@ namespace comphelper // ----------------------------------------------------------------------------- + sal_Bool OCommonAccessibleText::implIsValidBoundary( i18n::Boundary& rBoundary, sal_Int32 nLength ) + { + return ( rBoundary.startPos >= 0 ) && ( rBoundary.startPos < nLength ) && ( rBoundary.endPos >= 0 ) && ( rBoundary.endPos <= nLength ); + } + + // ----------------------------------------------------------------------------- + sal_Bool OCommonAccessibleText::implIsValidIndex( sal_Int32 nIndex, sal_Int32 nLength ) { return ( nIndex >= 0 ) && ( nIndex < nLength ); @@ -163,21 +170,30 @@ namespace comphelper void OCommonAccessibleText::implGetGlyphBoundary( i18n::Boundary& rBoundary, sal_Int32 nIndex ) { ::rtl::OUString sText( implGetText() ); - Reference < i18n::XBreakIterator > xBreakIter = implGetBreakIterator(); - if ( xBreakIter.is() ) + + if ( implIsValidIndex( nIndex, sText.getLength() ) ) { - sal_Int32 nCount = 1; - sal_Int32 nDone; - sal_Int32 nStartIndex = xBreakIter->previousCharacters( sText, nIndex, implGetLocale(), i18n::CharacterIteratorMode::SKIPCELL, nCount, nDone ); - if ( nDone != 0 ) - nStartIndex = xBreakIter->nextCharacters( sText, nStartIndex, implGetLocale(), i18n::CharacterIteratorMode::SKIPCELL, nCount, nDone ); - sal_Int32 nEndIndex = xBreakIter->nextCharacters( sText, nStartIndex, implGetLocale(), i18n::CharacterIteratorMode::SKIPCELL, nCount, nDone ); - if ( nDone != 0 ) + Reference < i18n::XBreakIterator > xBreakIter = implGetBreakIterator(); + if ( xBreakIter.is() ) { - rBoundary.startPos = nStartIndex; - rBoundary.endPos = nEndIndex; + sal_Int32 nCount = 1; + sal_Int32 nDone; + sal_Int32 nStartIndex = xBreakIter->previousCharacters( sText, nIndex, implGetLocale(), i18n::CharacterIteratorMode::SKIPCELL, nCount, nDone ); + if ( nDone != 0 ) + nStartIndex = xBreakIter->nextCharacters( sText, nStartIndex, implGetLocale(), i18n::CharacterIteratorMode::SKIPCELL, nCount, nDone ); + sal_Int32 nEndIndex = xBreakIter->nextCharacters( sText, nStartIndex, implGetLocale(), i18n::CharacterIteratorMode::SKIPCELL, nCount, nDone ); + if ( nDone != 0 ) + { + rBoundary.startPos = nStartIndex; + rBoundary.endPos = nEndIndex; + } } } + else + { + rBoundary.startPos = nIndex; + rBoundary.endPos = nIndex; + } } // ----------------------------------------------------------------------------- @@ -186,20 +202,29 @@ namespace comphelper { sal_Bool bWord = sal_False; ::rtl::OUString sText( implGetText() ); - Reference < i18n::XBreakIterator > xBreakIter = implGetBreakIterator(); - if ( xBreakIter.is() ) - { - rBoundary = xBreakIter->getWordBoundary( sText, nIndex, implGetLocale(), i18n::WordType::ANY_WORD, sal_True ); - // it's a word, if the first character is an alpha-numeric character - Reference< i18n::XCharacterClassification > xCharClass = implGetCharacterClassification(); - if ( xCharClass.is() ) + if ( implIsValidIndex( nIndex, sText.getLength() ) ) + { + Reference < i18n::XBreakIterator > xBreakIter = implGetBreakIterator(); + if ( xBreakIter.is() ) { - sal_Int32 nType = xCharClass->getCharacterType( sText, rBoundary.startPos, implGetLocale() ); - if ( ( nType & ( i18n::KCharacterType::LETTER | i18n::KCharacterType::DIGIT ) ) != 0 ) - bWord = sal_True; + rBoundary = xBreakIter->getWordBoundary( sText, nIndex, implGetLocale(), i18n::WordType::ANY_WORD, sal_True ); + + // it's a word, if the first character is an alpha-numeric character + Reference< i18n::XCharacterClassification > xCharClass = implGetCharacterClassification(); + if ( xCharClass.is() ) + { + sal_Int32 nType = xCharClass->getCharacterType( sText, rBoundary.startPos, implGetLocale() ); + if ( ( nType & ( i18n::KCharacterType::LETTER | i18n::KCharacterType::DIGIT ) ) != 0 ) + bWord = sal_True; + } } } + else + { + rBoundary.startPos = nIndex; + rBoundary.endPos = nIndex; + } return bWord; } @@ -209,12 +234,21 @@ namespace comphelper void OCommonAccessibleText::implGetSentenceBoundary( i18n::Boundary& rBoundary, sal_Int32 nIndex ) { ::rtl::OUString sText( implGetText() ); - Locale aLocale = implGetLocale(); - Reference < i18n::XBreakIterator > xBreakIter = implGetBreakIterator(); - if ( xBreakIter.is() ) + + if ( implIsValidIndex( nIndex, sText.getLength() ) ) { - rBoundary.endPos = xBreakIter->endOfSentence( sText, nIndex, aLocale ); - rBoundary.startPos = xBreakIter->beginOfSentence( sText, rBoundary.endPos, aLocale ); + Locale aLocale = implGetLocale(); + Reference < i18n::XBreakIterator > xBreakIter = implGetBreakIterator(); + if ( xBreakIter.is() ) + { + rBoundary.endPos = xBreakIter->endOfSentence( sText, nIndex, aLocale ); + rBoundary.startPos = xBreakIter->beginOfSentence( sText, rBoundary.endPos, aLocale ); + } + } + else + { + rBoundary.startPos = nIndex; + rBoundary.endPos = nIndex; } } @@ -224,24 +258,42 @@ namespace comphelper { ::rtl::OUString sText( implGetText() ); - rBoundary.startPos = 0; - rBoundary.endPos = sText.getLength(); + if ( implIsValidIndex( nIndex, sText.getLength() ) ) + { + rBoundary.startPos = 0; + rBoundary.endPos = sText.getLength(); - sal_Int32 nFound = sText.lastIndexOf( (sal_Unicode)'\n', nIndex ); - if ( nFound != -1 ) - rBoundary.startPos = nFound + 1; + sal_Int32 nFound = sText.lastIndexOf( (sal_Unicode)'\n', nIndex ); + if ( nFound != -1 ) + rBoundary.startPos = nFound + 1; - nFound = sText.indexOf( (sal_Unicode)'\n', nIndex ); - if ( nFound != -1 ) - rBoundary.endPos = nFound + 1; + nFound = sText.indexOf( (sal_Unicode)'\n', nIndex ); + if ( nFound != -1 ) + rBoundary.endPos = nFound + 1; + } + else + { + rBoundary.startPos = nIndex; + rBoundary.endPos = nIndex; + } } // ----------------------------------------------------------------------------- void OCommonAccessibleText::implGetLineBoundary( i18n::Boundary& rBoundary, sal_Int32 nIndex ) { - rBoundary.startPos = 0; - rBoundary.endPos = implGetText().getLength(); + ::rtl::OUString sText( implGetText() ); + + if ( implIsValidIndex( nIndex, sText.getLength() ) ) + { + rBoundary.startPos = 0; + rBoundary.endPos = implGetText().getLength(); + } + else + { + rBoundary.startPos = nIndex; + rBoundary.endPos = nIndex; + } } // ----------------------------------------------------------------------------- @@ -336,35 +388,33 @@ namespace comphelper { ::rtl::OUString sResult; ::rtl::OUString sText( implGetText() ); + sal_Int32 nLength = sText.getLength(); i18n::Boundary aBoundary; - // according to the interface definition, an index "length" is valid - // #103254# - 13.09.2002 - fs@openoffice.org - if ( sText.getLength() == nIndex ) - return sResult; - - if ( !implIsValidIndex( nIndex, sText.getLength() ) ) + if ( !implIsValidIndex( nIndex, nLength ) && nIndex != nLength ) throw IndexOutOfBoundsException(); switch ( aTextType ) { case AccessibleTextType::CHARACTER: { - sResult = sText.copy( nIndex, 1 ); + if ( implIsValidIndex( nIndex, nLength ) ) + sResult = sText.copy( nIndex, 1 ); } break; case AccessibleTextType::GLYPH: { // get glyph at index implGetGlyphBoundary( aBoundary, nIndex ); - sResult = sText.copy( aBoundary.startPos, aBoundary.endPos - aBoundary.startPos ); + if ( implIsValidBoundary( aBoundary, nLength ) ) + sResult = sText.copy( aBoundary.startPos, aBoundary.endPos - aBoundary.startPos ); } break; case AccessibleTextType::WORD: { // get word at index sal_Bool bWord = implGetWordBoundary( aBoundary, nIndex ); - if ( bWord ) + if ( bWord && implIsValidBoundary( aBoundary, nLength ) ) sResult = sText.copy( aBoundary.startPos, aBoundary.endPos - aBoundary.startPos ); } break; @@ -372,21 +422,24 @@ namespace comphelper { // get sentence at index implGetSentenceBoundary( aBoundary, nIndex ); - sResult = sText.copy( aBoundary.startPos, aBoundary.endPos - aBoundary.startPos ); + if ( implIsValidBoundary( aBoundary, nLength ) ) + sResult = sText.copy( aBoundary.startPos, aBoundary.endPos - aBoundary.startPos ); } break; case AccessibleTextType::PARAGRAPH: { // get paragraph at index implGetParagraphBoundary( aBoundary, nIndex ); - sResult = sText.copy( aBoundary.startPos, aBoundary.endPos - aBoundary.startPos ); + if ( implIsValidBoundary( aBoundary, nLength ) ) + sResult = sText.copy( aBoundary.startPos, aBoundary.endPos - aBoundary.startPos ); } break; case AccessibleTextType::LINE: { // get line at index implGetLineBoundary( aBoundary, nIndex ); - sResult = sText.copy( aBoundary.startPos, aBoundary.endPos - aBoundary.startPos ); + if ( implIsValidBoundary( aBoundary, nLength ) ) + sResult = sText.copy( aBoundary.startPos, aBoundary.endPos - aBoundary.startPos ); } break; default: @@ -404,16 +457,17 @@ namespace comphelper { ::rtl::OUString sResult; ::rtl::OUString sText( implGetText() ); + sal_Int32 nLength = sText.getLength(); i18n::Boundary aBoundary; - if ( !implIsValidIndex( nIndex, sText.getLength() ) ) + if ( !implIsValidIndex( nIndex, nLength ) && nIndex != nLength ) throw IndexOutOfBoundsException(); switch ( aTextType ) { case AccessibleTextType::CHARACTER: { - if ( nIndex > 0 ) + if ( implIsValidIndex( nIndex - 1, nLength ) ) sResult = sText.copy( nIndex - 1, 1 ); } break; @@ -425,7 +479,8 @@ namespace comphelper if ( aBoundary.startPos > 0 ) { implGetGlyphBoundary( aBoundary, aBoundary.startPos - 1 ); - sResult = sText.copy( aBoundary.startPos, aBoundary.endPos - aBoundary.startPos ); + if ( implIsValidBoundary( aBoundary, nLength ) ) + sResult = sText.copy( aBoundary.startPos, aBoundary.endPos - aBoundary.startPos ); } } break; @@ -437,7 +492,7 @@ namespace comphelper sal_Bool bWord = sal_False; while ( !bWord && aBoundary.startPos > 0 ) bWord = implGetWordBoundary( aBoundary, aBoundary.startPos - 1 ); - if ( bWord ) + if ( bWord && implIsValidBoundary( aBoundary, nLength ) ) sResult = sText.copy( aBoundary.startPos, aBoundary.endPos - aBoundary.startPos ); } break; @@ -449,7 +504,8 @@ namespace comphelper if ( aBoundary.startPos > 0 ) { implGetSentenceBoundary( aBoundary, aBoundary.startPos - 1 ); - sResult = sText.copy( aBoundary.startPos, aBoundary.endPos - aBoundary.startPos ); + if ( implIsValidBoundary( aBoundary, nLength ) ) + sResult = sText.copy( aBoundary.startPos, aBoundary.endPos - aBoundary.startPos ); } } break; @@ -461,7 +517,8 @@ namespace comphelper if ( aBoundary.startPos > 0 ) { implGetParagraphBoundary( aBoundary, aBoundary.startPos - 1 ); - sResult = sText.copy( aBoundary.startPos, aBoundary.endPos - aBoundary.startPos ); + if ( implIsValidBoundary( aBoundary, nLength ) ) + sResult = sText.copy( aBoundary.startPos, aBoundary.endPos - aBoundary.startPos ); } } break; @@ -473,7 +530,8 @@ namespace comphelper if ( aBoundary.startPos > 0 ) { implGetLineBoundary( aBoundary, aBoundary.startPos - 1 ); - sResult = sText.copy( aBoundary.startPos, aBoundary.endPos - aBoundary.startPos ); + if ( implIsValidBoundary( aBoundary, nLength ) ) + sResult = sText.copy( aBoundary.startPos, aBoundary.endPos - aBoundary.startPos ); } } break; @@ -492,16 +550,17 @@ namespace comphelper { ::rtl::OUString sResult; ::rtl::OUString sText( implGetText() ); + sal_Int32 nLength = sText.getLength(); i18n::Boundary aBoundary; - if ( !implIsValidIndex( nIndex, sText.getLength() ) ) + if ( !implIsValidIndex( nIndex, nLength ) && nIndex != nLength ) throw IndexOutOfBoundsException(); switch ( aTextType ) { case AccessibleTextType::CHARACTER: { - if ( nIndex < sText.getLength() - 1 ) + if ( implIsValidIndex( nIndex + 1, nLength ) ) sResult = sText.copy( nIndex + 1, 1 ); } break; @@ -510,10 +569,11 @@ namespace comphelper // get glyph at index implGetGlyphBoundary( aBoundary, nIndex ); // get next glyph - if ( aBoundary.endPos < sText.getLength() ) + if ( aBoundary.endPos < nLength ) { implGetGlyphBoundary( aBoundary, aBoundary.endPos ); - sResult = sText.copy( aBoundary.startPos, aBoundary.endPos - aBoundary.startPos ); + if ( implIsValidBoundary( aBoundary, nLength ) ) + sResult = sText.copy( aBoundary.startPos, aBoundary.endPos - aBoundary.startPos ); } } break; @@ -523,9 +583,9 @@ namespace comphelper implGetWordBoundary( aBoundary, nIndex ); // get next word sal_Bool bWord = sal_False; - while ( !bWord && aBoundary.endPos < sText.getLength() ) + while ( !bWord && aBoundary.endPos < nLength ) bWord = implGetWordBoundary( aBoundary, aBoundary.endPos ); - if ( bWord ) + if ( bWord && implIsValidBoundary( aBoundary, nLength ) ) sResult = sText.copy( aBoundary.startPos, aBoundary.endPos - aBoundary.startPos ); } break; @@ -537,12 +597,12 @@ namespace comphelper sal_Int32 nEnd = aBoundary.endPos; sal_Int32 nI = aBoundary.endPos; sal_Bool bFound = sal_False; - while ( !bFound && ++nI < sText.getLength() ) + while ( !bFound && ++nI < nLength ) { implGetSentenceBoundary( aBoundary, nI ); bFound = ( aBoundary.endPos > nEnd ); } - if ( bFound ) + if ( bFound && implIsValidBoundary( aBoundary, nLength ) ) sResult = sText.copy( aBoundary.startPos, aBoundary.endPos - aBoundary.startPos ); } break; @@ -551,10 +611,11 @@ namespace comphelper // get paragraph at index implGetParagraphBoundary( aBoundary, nIndex ); // get next paragraph - if ( aBoundary.endPos < sText.getLength() ) + if ( aBoundary.endPos < nLength ) { implGetParagraphBoundary( aBoundary, aBoundary.endPos ); - sResult = sText.copy( aBoundary.startPos, aBoundary.endPos - aBoundary.startPos ); + if ( implIsValidBoundary( aBoundary, nLength ) ) + sResult = sText.copy( aBoundary.startPos, aBoundary.endPos - aBoundary.startPos ); } } break; @@ -563,10 +624,11 @@ namespace comphelper // get line at index implGetLineBoundary( aBoundary, nIndex ); // get next line - if ( aBoundary.endPos < sText.getLength() ) + if ( aBoundary.endPos < nLength ) { implGetLineBoundary( aBoundary, aBoundary.endPos ); - sResult = sText.copy( aBoundary.startPos, aBoundary.endPos - aBoundary.startPos ); + if ( implIsValidBoundary( aBoundary, nLength ) ) + sResult = sText.copy( aBoundary.startPos, aBoundary.endPos - aBoundary.startPos ); } } break; |