Bump version to 6.0-36
[LibreOffice.git] / i18npool / source / breakiterator / breakiterator_ctl.cxx
blobe39966e6617516460632b67073792649a56d357f
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
21 #include <com/sun/star/i18n/CharacterIteratorMode.hpp>
22 #include <breakiterator_ctl.hxx>
24 using namespace ::com::sun::star;
25 using namespace ::com::sun::star::uno;
26 using namespace ::com::sun::star::i18n;
27 using namespace ::com::sun::star::lang;
29 namespace i18npool {
31 /**
32 * Constructor.
34 BreakIterator_CTL::BreakIterator_CTL() :
35 cachedText(),
36 nextCellIndex( nullptr ),
37 previousCellIndex( nullptr ),
38 cellIndexSize( 512 )
40 cBreakIterator = "com.sun.star.i18n.BreakIterator_CTL";
41 // to improve performance, alloc big enough memory in construct.
42 nextCellIndex = static_cast<sal_Int32*>(calloc(cellIndexSize, sizeof(sal_Int32)));
43 previousCellIndex = static_cast<sal_Int32*>(calloc(cellIndexSize, sizeof(sal_Int32)));
46 /**
47 * Deconstructor.
49 BreakIterator_CTL::~BreakIterator_CTL()
51 free(nextCellIndex);
52 free(previousCellIndex);
55 sal_Int32 SAL_CALL BreakIterator_CTL::previousCharacters( const OUString& Text,
56 sal_Int32 nStartPos, const lang::Locale& rLocale,
57 sal_Int16 nCharacterIteratorMode, sal_Int32 nCount, sal_Int32& nDone )
59 if (nCharacterIteratorMode == CharacterIteratorMode::SKIPCELL ) {
60 nDone = 0;
61 if (nStartPos > 0) { // for others to skip cell.
62 makeIndex(Text, nStartPos);
64 if (nextCellIndex[nStartPos-1] == 0) // not a CTL character
65 return BreakIterator_Unicode::previousCharacters(Text, nStartPos, rLocale,
66 nCharacterIteratorMode, nCount, nDone);
67 else while (nCount > 0 && nextCellIndex[nStartPos - 1] > 0) {
68 nCount--; nDone++;
69 nStartPos = previousCellIndex[nStartPos - 1];
71 } else
72 nStartPos = 0;
73 } else { // for BS to delete one char.
74 nDone = (nStartPos > nCount) ? nCount : nStartPos;
75 nStartPos -= nDone;
78 return nStartPos;
81 sal_Int32 SAL_CALL BreakIterator_CTL::nextCharacters(const OUString& Text,
82 sal_Int32 nStartPos, const lang::Locale& rLocale,
83 sal_Int16 nCharacterIteratorMode, sal_Int32 nCount, sal_Int32& nDone)
85 sal_Int32 len = Text.getLength();
86 if (nCharacterIteratorMode == CharacterIteratorMode::SKIPCELL ) {
87 nDone = 0;
88 if (nStartPos < len) {
89 makeIndex(Text, nStartPos);
91 if (nextCellIndex[nStartPos] == 0) // not a CTL character
92 return BreakIterator_Unicode::nextCharacters(Text, nStartPos, rLocale,
93 nCharacterIteratorMode, nCount, nDone);
94 else while (nCount > 0 && nextCellIndex[nStartPos] > 0) {
95 nCount--; nDone++;
96 nStartPos = nextCellIndex[nStartPos];
98 } else
99 nStartPos = len;
100 } else {
101 nDone = (len - nStartPos > nCount) ? nCount : len - nStartPos;
102 nStartPos += nDone;
105 return nStartPos;
108 // This method should be overwritten by derived language specific class.
109 void SAL_CALL BreakIterator_CTL::makeIndex(const OUString& /*text*/, sal_Int32 /*pos*/)
111 throw RuntimeException();
114 // Make sure line is broken on cell boundary if we implement cell iterator.
115 LineBreakResults SAL_CALL BreakIterator_CTL::getLineBreak(
116 const OUString& Text, sal_Int32 nStartPos,
117 const lang::Locale& rLocale, sal_Int32 nMinBreakPos,
118 const LineBreakHyphenationOptions& hOptions,
119 const LineBreakUserOptions& bOptions )
121 LineBreakResults lbr = BreakIterator_Unicode::getLineBreak(Text, nStartPos,
122 rLocale, nMinBreakPos, hOptions, bOptions );
123 if (lbr.breakIndex < Text.getLength()) {
124 makeIndex(Text, lbr.breakIndex);
125 lbr.breakIndex = previousCellIndex[ lbr.breakIndex ];
127 return lbr;
132 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */