xref: /aoo41x/main/svl/source/misc/lngmisc.cxx (revision 40df464e)
1*40df464eSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*40df464eSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*40df464eSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*40df464eSAndrew Rist  * distributed with this work for additional information
6*40df464eSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*40df464eSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*40df464eSAndrew Rist  * "License"); you may not use this file except in compliance
9*40df464eSAndrew Rist  * with the License.  You may obtain a copy of the License at
10*40df464eSAndrew Rist  *
11*40df464eSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*40df464eSAndrew Rist  *
13*40df464eSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*40df464eSAndrew Rist  * software distributed under the License is distributed on an
15*40df464eSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*40df464eSAndrew Rist  * KIND, either express or implied.  See the License for the
17*40df464eSAndrew Rist  * specific language governing permissions and limitations
18*40df464eSAndrew Rist  * under the License.
19*40df464eSAndrew Rist  *
20*40df464eSAndrew Rist  *************************************************************/
21*40df464eSAndrew Rist 
22*40df464eSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_svl.hxx"
26cdf0e10cSrcweir #include <svl/lngmisc.hxx>
27cdf0e10cSrcweir #include <tools/solar.h>
28cdf0e10cSrcweir #include <tools/string.hxx>
29cdf0e10cSrcweir #include <tools/debug.hxx>
30cdf0e10cSrcweir #include <rtl/ustrbuf.hxx>
31cdf0e10cSrcweir #include <rtl/ustring.hxx>
32cdf0e10cSrcweir 
33cdf0e10cSrcweir using namespace rtl;
34cdf0e10cSrcweir 
35cdf0e10cSrcweir namespace linguistic
36cdf0e10cSrcweir {
37cdf0e10cSrcweir 
38cdf0e10cSrcweir ///////////////////////////////////////////////////////////////////////////
39cdf0e10cSrcweir 
GetNumControlChars(const OUString & rTxt)40cdf0e10cSrcweir sal_Int32 GetNumControlChars( const OUString &rTxt )
41cdf0e10cSrcweir {
42cdf0e10cSrcweir 	sal_Int32 nCnt = 0;
43cdf0e10cSrcweir 	sal_Int32 nLen = rTxt.getLength();
44cdf0e10cSrcweir 	for (sal_Int32 i = 0;  i < nLen;  ++i)
45cdf0e10cSrcweir 	{
46cdf0e10cSrcweir 		if (IsControlChar( rTxt[i] ))
47cdf0e10cSrcweir 			++nCnt;
48cdf0e10cSrcweir 	}
49cdf0e10cSrcweir 	return nCnt;
50cdf0e10cSrcweir }
51cdf0e10cSrcweir 
52cdf0e10cSrcweir 
RemoveHyphens(OUString & rTxt)53cdf0e10cSrcweir sal_Bool RemoveHyphens( OUString &rTxt )
54cdf0e10cSrcweir {
55cdf0e10cSrcweir 	sal_Bool bModified = sal_False;
56cdf0e10cSrcweir 	if (HasHyphens( rTxt ))
57cdf0e10cSrcweir 	{
58cdf0e10cSrcweir 		String aTmp( rTxt );
59cdf0e10cSrcweir 		aTmp.EraseAllChars( SVT_SOFT_HYPHEN );
60cdf0e10cSrcweir 		aTmp.EraseAllChars( SVT_HARD_HYPHEN );
61cdf0e10cSrcweir 		rTxt = aTmp;
62cdf0e10cSrcweir 		bModified = sal_True;
63cdf0e10cSrcweir 	}
64cdf0e10cSrcweir 	return bModified;
65cdf0e10cSrcweir }
66cdf0e10cSrcweir 
67cdf0e10cSrcweir 
RemoveControlChars(OUString & rTxt)68cdf0e10cSrcweir sal_Bool RemoveControlChars( OUString &rTxt )
69cdf0e10cSrcweir {
70cdf0e10cSrcweir 	sal_Bool bModified = sal_False;
71cdf0e10cSrcweir 	sal_Int32 nCtrlChars = GetNumControlChars( rTxt );
72cdf0e10cSrcweir 	if (nCtrlChars)
73cdf0e10cSrcweir 	{
74cdf0e10cSrcweir 		sal_Int32 nLen	= rTxt.getLength();
75cdf0e10cSrcweir 		sal_Int32 nSize = nLen - nCtrlChars;
76cdf0e10cSrcweir 		OUStringBuffer aBuf( nSize );
77cdf0e10cSrcweir 		aBuf.setLength( nSize );
78cdf0e10cSrcweir 		sal_Int32 nCnt = 0;
79cdf0e10cSrcweir 		for (sal_Int32 i = 0;  i < nLen;  ++i)
80cdf0e10cSrcweir 		{
81cdf0e10cSrcweir 			sal_Unicode cChar = rTxt[i];
82cdf0e10cSrcweir 			if (!IsControlChar( cChar ))
83cdf0e10cSrcweir 			{
84cdf0e10cSrcweir 				DBG_ASSERT( nCnt < nSize, "index out of range" );
85cdf0e10cSrcweir 				aBuf.setCharAt( nCnt++, cChar );
86cdf0e10cSrcweir 			}
87cdf0e10cSrcweir 		}
88cdf0e10cSrcweir 		DBG_ASSERT( nCnt == nSize, "wrong size" );
89cdf0e10cSrcweir 		rTxt = aBuf.makeStringAndClear();
90cdf0e10cSrcweir 		bModified = sal_True;
91cdf0e10cSrcweir 	}
92cdf0e10cSrcweir 	return bModified;
93cdf0e10cSrcweir }
94cdf0e10cSrcweir 
95cdf0e10cSrcweir 
96cdf0e10cSrcweir // non breaking field character
97cdf0e10cSrcweir #define CH_TXTATR_INWORD    ((sal_Char) 0x02)
98cdf0e10cSrcweir 
ReplaceControlChars(rtl::OUString & rTxt,sal_Char)99cdf0e10cSrcweir sal_Bool ReplaceControlChars( rtl::OUString &rTxt, sal_Char /*aRplcChar*/ )
100cdf0e10cSrcweir {
101cdf0e10cSrcweir     // the resulting string looks like this:
102cdf0e10cSrcweir     // 1. non breaking field characters get removed
103cdf0e10cSrcweir     // 2. remaining control characters will be replaced by ' '
104cdf0e10cSrcweir 
105cdf0e10cSrcweir     sal_Bool bModified = sal_False;
106cdf0e10cSrcweir     sal_Int32 nCtrlChars = GetNumControlChars( rTxt );
107cdf0e10cSrcweir     if (nCtrlChars)
108cdf0e10cSrcweir     {
109cdf0e10cSrcweir         sal_Int32 nLen  = rTxt.getLength();
110cdf0e10cSrcweir         OUStringBuffer aBuf( nLen );
111cdf0e10cSrcweir         sal_Int32 nCnt = 0;
112cdf0e10cSrcweir         for (sal_Int32 i = 0;  i < nLen;  ++i)
113cdf0e10cSrcweir         {
114cdf0e10cSrcweir             sal_Unicode cChar = rTxt[i];
115cdf0e10cSrcweir             if (CH_TXTATR_INWORD != cChar)
116cdf0e10cSrcweir             {
117cdf0e10cSrcweir                 if (IsControlChar( cChar ))
118cdf0e10cSrcweir                     cChar = ' ';
119cdf0e10cSrcweir                 DBG_ASSERT( nCnt < nLen, "index out of range" );
120cdf0e10cSrcweir                 aBuf.setCharAt( nCnt++, cChar );
121cdf0e10cSrcweir             }
122cdf0e10cSrcweir         }
123cdf0e10cSrcweir         aBuf.setLength( nCnt );
124cdf0e10cSrcweir         rTxt = aBuf.makeStringAndClear();
125cdf0e10cSrcweir         bModified = sal_True;
126cdf0e10cSrcweir     }
127cdf0e10cSrcweir     return bModified;
128cdf0e10cSrcweir }
129cdf0e10cSrcweir 
130cdf0e10cSrcweir 
GetThesaurusReplaceText(const String & rText)131cdf0e10cSrcweir String GetThesaurusReplaceText( const String &rText )
132cdf0e10cSrcweir {
133cdf0e10cSrcweir     // The strings for synonyms returned by the thesaurus sometimes have some
134cdf0e10cSrcweir     // explanation text put in between '(' and ')' or a trailing '*'.
135cdf0e10cSrcweir     // These parts should not be put in the ReplaceEdit Text that may get
136cdf0e10cSrcweir     // inserted into the document. Thus we strip them from the text.
137cdf0e10cSrcweir 
138cdf0e10cSrcweir     String aText( rText );
139cdf0e10cSrcweir 
140cdf0e10cSrcweir     xub_StrLen nPos = aText.Search( sal_Unicode('(') );
141cdf0e10cSrcweir     while (STRING_NOTFOUND != nPos)
142cdf0e10cSrcweir     {
143cdf0e10cSrcweir         xub_StrLen nEnd = aText.Search( sal_Unicode(')'), nPos );
144cdf0e10cSrcweir         if (STRING_NOTFOUND != nEnd)
145cdf0e10cSrcweir             aText.Erase( nPos, nEnd-nPos+1 );
146cdf0e10cSrcweir         else
147cdf0e10cSrcweir             break;
148cdf0e10cSrcweir         nPos = aText.Search( sal_Unicode('(') );
149cdf0e10cSrcweir     }
150cdf0e10cSrcweir 
151cdf0e10cSrcweir     nPos = aText.Search( sal_Unicode('*') );
152cdf0e10cSrcweir     if (STRING_NOTFOUND != nPos)
153cdf0e10cSrcweir         aText.Erase( nPos );
154cdf0e10cSrcweir 
155cdf0e10cSrcweir     // remove any possible remaining ' ' that may confuse the thesaurus
156cdf0e10cSrcweir     // when it gets called with the text
157cdf0e10cSrcweir     aText.EraseLeadingAndTrailingChars( sal_Unicode(' ') );
158cdf0e10cSrcweir 
159cdf0e10cSrcweir     return aText;
160cdf0e10cSrcweir }
161cdf0e10cSrcweir 
162cdf0e10cSrcweir ///////////////////////////////////////////////////////////////////////////
163cdf0e10cSrcweir 
164cdf0e10cSrcweir } // namespace linguistic
165cdf0e10cSrcweir 
166