merge the formfield patch from ooo-build
[ooovba.git] / svtools / source / misc1 / lngmisc.cxx
blobf365e0df1dd5e44f6ba176aa1fcfe41878951da0
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: lngmisc.cxx,v $
10 * $Revision: 1.6 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_svtools.hxx"
33 #include <lngmisc.hxx>
34 #include <tools/solar.h>
35 #include <tools/string.hxx>
36 #include <tools/debug.hxx>
37 #include <rtl/ustrbuf.hxx>
38 #include <rtl/ustring.hxx>
41 using namespace rtl;
43 namespace linguistic
46 ///////////////////////////////////////////////////////////////////////////
48 INT32 GetNumControlChars( const OUString &rTxt )
50 INT32 nCnt = 0;
51 INT32 nLen = rTxt.getLength();
52 for (INT32 i = 0; i < nLen; ++i)
54 if (IsControlChar( rTxt[i] ))
55 ++nCnt;
57 return nCnt;
61 BOOL RemoveHyphens( OUString &rTxt )
63 BOOL bModified = FALSE;
64 if (HasHyphens( rTxt ))
66 String aTmp( rTxt );
67 aTmp.EraseAllChars( SVT_SOFT_HYPHEN );
68 aTmp.EraseAllChars( SVT_HARD_HYPHEN );
69 rTxt = aTmp;
70 bModified = TRUE;
72 return bModified;
76 BOOL RemoveControlChars( OUString &rTxt )
78 BOOL bModified = FALSE;
79 INT32 nCtrlChars = GetNumControlChars( rTxt );
80 if (nCtrlChars)
82 INT32 nLen = rTxt.getLength();
83 INT32 nSize = nLen - nCtrlChars;
84 OUStringBuffer aBuf( nSize );
85 aBuf.setLength( nSize );
86 INT32 nCnt = 0;
87 for (INT32 i = 0; i < nLen; ++i)
89 sal_Unicode cChar = rTxt[i];
90 if (!IsControlChar( cChar ))
92 DBG_ASSERT( nCnt < nSize, "index out of range" );
93 aBuf.setCharAt( nCnt++, cChar );
96 DBG_ASSERT( nCnt == nSize, "wrong size" );
97 rTxt = aBuf.makeStringAndClear();
98 bModified = TRUE;
100 return bModified;
104 // non breaking field character
105 #define CH_TXTATR_INWORD ((sal_Char) 0x02)
107 BOOL ReplaceControlChars( rtl::OUString &rTxt, sal_Char /*aRplcChar*/ )
109 // the resulting string looks like this:
110 // 1. non breaking field characters get removed
111 // 2. remaining control characters will be replaced by ' '
113 BOOL bModified = FALSE;
114 INT32 nCtrlChars = GetNumControlChars( rTxt );
115 if (nCtrlChars)
117 INT32 nLen = rTxt.getLength();
118 OUStringBuffer aBuf( nLen );
119 INT32 nCnt = 0;
120 for (INT32 i = 0; i < nLen; ++i)
122 sal_Unicode cChar = rTxt[i];
123 if (CH_TXTATR_INWORD != cChar)
125 if (IsControlChar( cChar ))
126 cChar = ' ';
127 DBG_ASSERT( nCnt < nLen, "index out of range" );
128 aBuf.setCharAt( nCnt++, cChar );
131 aBuf.setLength( nCnt );
132 rTxt = aBuf.makeStringAndClear();
133 bModified = TRUE;
135 return bModified;
138 ///////////////////////////////////////////////////////////////////////////
140 } // namespace linguistic