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