Kaydet (Commit) abf91e0f authored tarafından Kohei Yoshida's avatar Kohei Yoshida

UniString to OUString in ScRefFinder, and a bunch more cleanups.

Some extra code to ensure that OUString's copy method gets correct
sub-string lengths.  Unlike UniString, OUString's version is more
strict.

Change-Id: I7daabf2ad587ff44fd6d8c66a02dbecfe519295b
üst 947041e1
...@@ -25,31 +25,28 @@ ...@@ -25,31 +25,28 @@
class ScDocument; class ScDocument;
// -----------------------------------------------------------------------
class ScRefFinder class ScRefFinder
{ {
private: OUString maFormula;
String aFormula; formula::FormulaGrammar::AddressConvention meConv;
formula::FormulaGrammar::AddressConvention eConv; ScDocument* mpDoc;
ScDocument* pDoc; ScAddress maPos;
ScAddress maPos; sal_Int32 mnFound;
xub_StrLen nFound; sal_Int32 mnSelStart;
xub_StrLen nSelStart; sal_Int32 mnSelEnd;
xub_StrLen nSelEnd;
public: public:
ScRefFinder( const String& rFormula, const ScAddress& rPos, ScRefFinder(
ScDocument* pDocument = NULL, const OUString& rFormula, const ScAddress& rPos, ScDocument* pDoc = NULL,
formula::FormulaGrammar::AddressConvention eConvP = formula::FormulaGrammar::CONV_OOO ); formula::FormulaGrammar::AddressConvention eConvP = formula::FormulaGrammar::CONV_OOO );
~ScRefFinder(); ~ScRefFinder();
const String& GetText() const { return aFormula; } const OUString& GetText() const { return maFormula; }
xub_StrLen GetFound() const { return nFound; } sal_Int32 GetFound() const { return mnFound; }
xub_StrLen GetSelStart() const { return nSelStart; } sal_Int32 GetSelStart() const { return mnSelStart; }
xub_StrLen GetSelEnd() const { return nSelEnd; } sal_Int32 GetSelEnd() const { return mnSelEnd; }
void ToggleRel( xub_StrLen nStartPos, xub_StrLen nEndPos ); void ToggleRel( sal_Int32 nStartPos, sal_Int32 nEndPos );
}; };
......
...@@ -4973,22 +4973,22 @@ void Test::testToggleRefFlag() ...@@ -4973,22 +4973,22 @@ void Test::testToggleRefFlag()
// column absolute / row absolute -> column relative / row absolute // column absolute / row absolute -> column relative / row absolute
aFinder.ToggleRel(0, aFormula.getLength()); aFinder.ToggleRel(0, aFormula.getLength());
aFormula = aFinder.GetText(); aFormula = aFinder.GetText();
CPPUNIT_ASSERT_MESSAGE( "Wrong conversion.", aFormula == "=R2C[-3]" ); CPPUNIT_ASSERT_EQUAL(OUString("=R2C[-3]"), aFormula);
// column relative / row absolute - > column absolute / row relative // column relative / row absolute - > column absolute / row relative
aFinder.ToggleRel(0, aFormula.getLength()); aFinder.ToggleRel(0, aFormula.getLength());
aFormula = aFinder.GetText(); aFormula = aFinder.GetText();
CPPUNIT_ASSERT_MESSAGE( "Wrong conversion.", aFormula == "=R[-4]C1" ); CPPUNIT_ASSERT_EQUAL(OUString("=R[-4]C1"), aFormula);
// column absolute / row relative -> column relative / row relative // column absolute / row relative -> column relative / row relative
aFinder.ToggleRel(0, aFormula.getLength()); aFinder.ToggleRel(0, aFormula.getLength());
aFormula = aFinder.GetText(); aFormula = aFinder.GetText();
CPPUNIT_ASSERT_MESSAGE( "Wrong conversion.", aFormula == "=R[-4]C[-3]" ); CPPUNIT_ASSERT_EQUAL(OUString("=R[-4]C[-3]"), aFormula);
// column relative / row relative -> column absolute / row absolute // column relative / row relative -> column absolute / row absolute
aFinder.ToggleRel(0, aFormula.getLength()); aFinder.ToggleRel(0, aFormula.getLength());
aFormula = aFinder.GetText(); aFormula = aFinder.GetText();
CPPUNIT_ASSERT_MESSAGE( "Wrong conversion.", aFormula == "=R2C1" ); CPPUNIT_ASSERT_EQUAL(OUString("=R2C1"), aFormula);
} }
{ {
...@@ -4999,7 +4999,7 @@ void Test::testToggleRefFlag() ...@@ -4999,7 +4999,7 @@ void Test::testToggleRefFlag()
ScRefFinder aFinder(aFormula, aPos, m_pDoc, formula::FormulaGrammar::CONV_XL_R1C1); ScRefFinder aFinder(aFormula, aPos, m_pDoc, formula::FormulaGrammar::CONV_XL_R1C1);
// Original // Original
CPPUNIT_ASSERT_EQUAL(aFormula, OUString(aFinder.GetText())); CPPUNIT_ASSERT_EQUAL(aFormula, aFinder.GetText());
// Make the column relative. // Make the column relative.
sal_Int32 n = aFormula.getLength(); sal_Int32 n = aFormula.getLength();
......
...@@ -65,7 +65,7 @@ inline bool IsText( bool& bQuote, sal_Unicode c ) ...@@ -65,7 +65,7 @@ inline bool IsText( bool& bQuote, sal_Unicode c )
* considered a text when it's within the ascii range and when it's not a * considered a text when it's within the ascii range and when it's not a
* delimiter. * delimiter.
*/ */
xub_StrLen FindStartPos(const sal_Unicode* p, xub_StrLen nStartPos, xub_StrLen nEndPos) sal_Int32 FindStartPos(const sal_Unicode* p, sal_Int32 nStartPos, sal_Int32 nEndPos)
{ {
while (nStartPos <= nEndPos && !IsText(p[nStartPos])) while (nStartPos <= nEndPos && !IsText(p[nStartPos]))
++nStartPos; ++nStartPos;
...@@ -73,19 +73,19 @@ xub_StrLen FindStartPos(const sal_Unicode* p, xub_StrLen nStartPos, xub_StrLen n ...@@ -73,19 +73,19 @@ xub_StrLen FindStartPos(const sal_Unicode* p, xub_StrLen nStartPos, xub_StrLen n
return nStartPos; return nStartPos;
} }
xub_StrLen FindEndPosA1(const sal_Unicode* p, xub_StrLen nStartPos, xub_StrLen nEndPos) sal_Int32 FindEndPosA1(const sal_Unicode* p, sal_Int32 nStartPos, sal_Int32 nEndPos)
{ {
bool bQuote = false; bool bQuote = false;
xub_StrLen nNewEnd = nStartPos; sal_Int32 nNewEnd = nStartPos;
while (nNewEnd <= nEndPos && IsText(bQuote, p[nNewEnd])) while (nNewEnd <= nEndPos && IsText(bQuote, p[nNewEnd]))
++nNewEnd; ++nNewEnd;
return nNewEnd; return nNewEnd;
} }
xub_StrLen FindEndPosR1C1(const sal_Unicode* p, xub_StrLen nStartPos, xub_StrLen nEndPos) sal_Int32 FindEndPosR1C1(const sal_Unicode* p, sal_Int32 nStartPos, sal_Int32 nEndPos)
{ {
xub_StrLen nNewEnd = nStartPos; sal_Int32 nNewEnd = nStartPos;
p = &p[nStartPos]; p = &p[nStartPos];
for (; nNewEnd <= nEndPos; ++p, ++nNewEnd) for (; nNewEnd <= nEndPos; ++p, ++nNewEnd)
{ {
...@@ -95,6 +95,8 @@ xub_StrLen FindEndPosR1C1(const sal_Unicode* p, xub_StrLen nStartPos, xub_StrLen ...@@ -95,6 +95,8 @@ xub_StrLen FindEndPosR1C1(const sal_Unicode* p, xub_StrLen nStartPos, xub_StrLen
for (; nNewEnd <= nEndPos; ++p, ++nNewEnd) for (; nNewEnd <= nEndPos; ++p, ++nNewEnd)
if (*p == '\'') if (*p == '\'')
break; break;
if (nNewEnd > nEndPos)
break;
} }
else if (*p == '[') else if (*p == '[')
{ {
...@@ -102,6 +104,8 @@ xub_StrLen FindEndPosR1C1(const sal_Unicode* p, xub_StrLen nStartPos, xub_StrLen ...@@ -102,6 +104,8 @@ xub_StrLen FindEndPosR1C1(const sal_Unicode* p, xub_StrLen nStartPos, xub_StrLen
for (; nNewEnd <= nEndPos; ++p, ++nNewEnd) for (; nNewEnd <= nEndPos; ++p, ++nNewEnd)
if (*p == ']') if (*p == ']')
break; break;
if (nNewEnd > nEndPos)
break;
} }
else if (!IsText(*p)) else if (!IsText(*p))
break; break;
...@@ -114,8 +118,8 @@ xub_StrLen FindEndPosR1C1(const sal_Unicode* p, xub_StrLen nStartPos, xub_StrLen ...@@ -114,8 +118,8 @@ xub_StrLen FindEndPosR1C1(const sal_Unicode* p, xub_StrLen nStartPos, xub_StrLen
* Find last character position that is considred text, from the specified * Find last character position that is considred text, from the specified
* start position. * start position.
*/ */
xub_StrLen FindEndPos(const sal_Unicode* p, xub_StrLen nStartPos, xub_StrLen nEndPos, sal_Int32 FindEndPos(const sal_Unicode* p, sal_Int32 nStartPos, sal_Int32 nEndPos,
formula::FormulaGrammar::AddressConvention eConv) formula::FormulaGrammar::AddressConvention eConv)
{ {
switch (eConv) switch (eConv)
{ {
...@@ -128,7 +132,7 @@ xub_StrLen FindEndPos(const sal_Unicode* p, xub_StrLen nStartPos, xub_StrLen nEn ...@@ -128,7 +132,7 @@ xub_StrLen FindEndPos(const sal_Unicode* p, xub_StrLen nStartPos, xub_StrLen nEn
} }
} }
void ExpandToTextA1(const sal_Unicode* p, xub_StrLen nLen, xub_StrLen& rStartPos, xub_StrLen& rEndPos) void ExpandToTextA1(const sal_Unicode* p, sal_Int32 nLen, sal_Int32& rStartPos, sal_Int32& rEndPos)
{ {
while (rStartPos > 0 && IsText(p[rStartPos - 1]) ) while (rStartPos > 0 && IsText(p[rStartPos - 1]) )
--rStartPos; --rStartPos;
...@@ -138,7 +142,7 @@ void ExpandToTextA1(const sal_Unicode* p, xub_StrLen nLen, xub_StrLen& rStartPos ...@@ -138,7 +142,7 @@ void ExpandToTextA1(const sal_Unicode* p, xub_StrLen nLen, xub_StrLen& rStartPos
++rEndPos; ++rEndPos;
} }
void ExpandToTextR1C1(const sal_Unicode* p, xub_StrLen nLen, xub_StrLen& rStartPos, xub_StrLen& rEndPos) void ExpandToTextR1C1(const sal_Unicode* p, sal_Int32 nLen, sal_Int32& rStartPos, sal_Int32& rEndPos)
{ {
// move back the start position to the first text character. // move back the start position to the first text character.
if (rStartPos > 0) if (rStartPos > 0)
...@@ -182,7 +186,7 @@ void ExpandToTextR1C1(const sal_Unicode* p, xub_StrLen nLen, xub_StrLen& rStartP ...@@ -182,7 +186,7 @@ void ExpandToTextR1C1(const sal_Unicode* p, xub_StrLen nLen, xub_StrLen& rStartP
rEndPos = FindEndPosR1C1(p, rEndPos, nLen-1); rEndPos = FindEndPosR1C1(p, rEndPos, nLen-1);
} }
void ExpandToText(const sal_Unicode* p, xub_StrLen nLen, xub_StrLen& rStartPos, xub_StrLen& rEndPos, void ExpandToText(const sal_Unicode* p, sal_Int32 nLen, sal_Int32& rStartPos, sal_Int32& rEndPos,
formula::FormulaGrammar::AddressConvention eConv) formula::FormulaGrammar::AddressConvention eConv)
{ {
switch (eConv) switch (eConv)
...@@ -200,14 +204,16 @@ void ExpandToText(const sal_Unicode* p, xub_StrLen nLen, xub_StrLen& rStartPos, ...@@ -200,14 +204,16 @@ void ExpandToText(const sal_Unicode* p, xub_StrLen nLen, xub_StrLen& rStartPos,
} }
ScRefFinder::ScRefFinder( ScRefFinder::ScRefFinder(
const String& rFormula, const ScAddress& rPos, const OUString& rFormula, const ScAddress& rPos,
ScDocument* pDocument, formula::FormulaGrammar::AddressConvention eConvP) : ScDocument* pDoc, formula::FormulaGrammar::AddressConvention eConvP) :
aFormula( rFormula ), maFormula(rFormula),
eConv( eConvP ), meConv(eConvP),
pDoc( pDocument ), mpDoc(pDoc),
maPos(rPos) maPos(rPos),
mnFound(0),
mnSelStart(0),
mnSelEnd(0)
{ {
nSelStart = nSelEnd = nFound = 0;
} }
ScRefFinder::~ScRefFinder() ScRefFinder::~ScRefFinder()
...@@ -225,50 +231,55 @@ static sal_uInt16 lcl_NextFlags( sal_uInt16 nOld ) ...@@ -225,50 +231,55 @@ static sal_uInt16 lcl_NextFlags( sal_uInt16 nOld )
return ( nOld & 0xfff8 ) | nNew; return ( nOld & 0xfff8 ) | nNew;
} }
void ScRefFinder::ToggleRel( xub_StrLen nStartPos, xub_StrLen nEndPos ) void ScRefFinder::ToggleRel( sal_Int32 nStartPos, sal_Int32 nEndPos )
{ {
xub_StrLen nLen = aFormula.Len(); sal_Int32 nLen = maFormula.getLength();
if (!nLen) if (nLen <= 0)
return; return;
const sal_Unicode* pSource = aFormula.GetBuffer(); // for quick access const sal_Unicode* pSource = maFormula.getStr(); // for quick access
// expand selection, and instead of selection start- and end-index // expand selection, and instead of selection start- and end-index
if ( nEndPos < nStartPos ) if ( nEndPos < nStartPos )
::std::swap(nEndPos, nStartPos); ::std::swap(nEndPos, nStartPos);
ExpandToText(pSource, nLen, nStartPos, nEndPos, eConv); ExpandToText(pSource, nLen, nStartPos, nEndPos, meConv);
String aResult; OUString aResult;
String aExpr; OUString aExpr;
String aSep; OUString aSep;
ScAddress aAddr; ScAddress aAddr;
nFound = 0; mnFound = 0;
xub_StrLen nLoopStart = nStartPos; sal_Int32 nLoopStart = nStartPos;
while ( nLoopStart <= nEndPos ) while ( nLoopStart <= nEndPos )
{ {
// Determine the stard and end positions of a text segment. // Determine the stard and end positions of a text segment. Note that
xub_StrLen nEStart = FindStartPos(pSource, nLoopStart, nEndPos); // the end position returned from FindEndPos may be one position after
xub_StrLen nEEnd = FindEndPos(pSource, nEStart, nEndPos, eConv); // the last character position in case of the last segment.
sal_Int32 nEStart = FindStartPos(pSource, nLoopStart, nEndPos);
aSep = aFormula.Copy( nLoopStart, nEStart-nLoopStart ); sal_Int32 nEEnd = FindEndPos(pSource, nEStart, nEndPos, meConv);
aExpr = aFormula.Copy( nEStart, nEEnd-nEStart );
aSep = maFormula.copy(nLoopStart, nEStart-nLoopStart);
if (nEEnd < maFormula.getLength())
aExpr = maFormula.copy(nEStart, nEEnd-nEStart);
else
aExpr = maFormula.copy(nEStart);
// Check the validity of the expression, and toggle the relative flag. // Check the validity of the expression, and toggle the relative flag.
ScAddress::Details aDetails(eConv, maPos.Row(), maPos.Col()); ScAddress::Details aDetails(meConv, maPos.Row(), maPos.Col());
sal_uInt16 nResult = aAddr.Parse(aExpr, pDoc, aDetails); sal_uInt16 nResult = aAddr.Parse(aExpr, mpDoc, aDetails);
if ( nResult & SCA_VALID ) if ( nResult & SCA_VALID )
{ {
sal_uInt16 nFlags = lcl_NextFlags( nResult ); sal_uInt16 nFlags = lcl_NextFlags( nResult );
aAddr.Format(aExpr, nFlags, pDoc, aDetails); aAddr.Format(aExpr, nFlags, mpDoc, aDetails);
xub_StrLen nAbsStart = nStartPos+aResult.Len()+aSep.Len(); sal_Int32 nAbsStart = nStartPos+aResult.getLength()+aSep.getLength();
if (!nFound) // first reference ? if (!mnFound) // first reference ?
nSelStart = nAbsStart; mnSelStart = nAbsStart;
nSelEnd = nAbsStart+aExpr.Len(); // selection, no indizes mnSelEnd = nAbsStart + aExpr.getLength(); // selection, no indizes
++nFound; ++mnFound;
} }
// assemble // assemble
...@@ -279,11 +290,12 @@ void ScRefFinder::ToggleRel( xub_StrLen nStartPos, xub_StrLen nEndPos ) ...@@ -279,11 +290,12 @@ void ScRefFinder::ToggleRel( xub_StrLen nStartPos, xub_StrLen nEndPos )
nLoopStart = nEEnd; nLoopStart = nEEnd;
} }
String aTotal = aFormula.Copy( 0, nStartPos ); OUString aTotal = maFormula.copy(0, nStartPos);
aTotal += aResult; aTotal += aResult;
aTotal += aFormula.Copy( nEndPos+1 ); if (nEndPos < maFormula.getLength()-1)
aTotal += maFormula.copy(nEndPos);
aFormula = aTotal; maFormula = aTotal;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment