tdf#130857 qt weld: Implement QtInstanceWidget::strip_mnemonic
[LibreOffice.git] / svl / source / misc / lngmisc.cxx
blob7ccf0aed7215c076ffc2dc408a0ccf77312bea0f
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 <svl/lngmisc.hxx>
23 #include <comphelper/string.hxx>
24 #include <rtl/ustrbuf.hxx>
25 #include <tools/debug.hxx>
27 namespace linguistic
29 sal_Int32 GetNumControlChars(std::u16string_view rTxt)
31 sal_Int32 nCnt = 0;
32 for (size_t i = 0; i < rTxt.size(); ++i)
33 if (IsControlChar(rTxt[i]))
34 ++nCnt;
35 return nCnt;
38 bool RemoveHyphens(OUString &rTxt)
40 sal_Int32 n = rTxt.getLength();
41 rTxt = rTxt.replaceAll(OUStringChar(SVT_SOFT_HYPHEN), "");
42 rTxt = rTxt.replaceAll(OUStringChar(SVT_HARD_HYPHEN), "");
43 return n != rTxt.getLength();
46 bool RemoveControlChars(OUString &rTxt)
48 sal_Int32 nSize = rTxt.getLength() - GetNumControlChars(rTxt);
49 if(nSize == rTxt.getLength())
50 return false;
52 OUStringBuffer aBuf(nSize);
53 aBuf.setLength(nSize);
54 for (sal_Int32 i = 0, j = 0; i < rTxt.getLength() && j < nSize; ++i)
55 if (!IsControlChar(rTxt[i]))
56 aBuf[j++] = rTxt[i];
58 rTxt = aBuf.makeStringAndClear();
59 DBG_ASSERT(rTxt.getLength() == nSize, "GetNumControlChars returned a different number of control characters than were actually removed.");
61 return true;
64 bool ReplaceControlChars(OUString &rTxt)
66 // non breaking field character
67 static const char CH_TXTATR_INWORD = static_cast<char>(0x02);
69 // the resulting string looks like this:
70 // 1. non breaking field characters get removed
71 // 2. remaining control characters will be replaced by ' '
73 if (GetNumControlChars(rTxt) == 0)
74 return false;
76 sal_Int32 n = rTxt.getLength();
78 OUStringBuffer aBuf(n);
79 aBuf.setLength(n);
81 sal_Int32 j = 0;
82 for (sal_Int32 i = 0; i < n && j < n; ++i)
84 if (CH_TXTATR_INWORD == rTxt[i])
85 continue;
87 aBuf[j++] = IsControlChar(rTxt[i]) ? ' ' : rTxt[i];
90 aBuf.setLength(j);
91 rTxt = aBuf.makeStringAndClear();
93 return true;
96 OUString GetThesaurusReplaceText(const OUString &rText)
98 // The strings for synonyms returned by the thesaurus sometimes have some
99 // explanation text put in between '(' and ')' or a trailing '*'.
100 // These parts should not be put in the ReplaceEdit Text that may get
101 // inserted into the document. Thus we strip them from the text.
103 OUString aText(rText);
105 sal_Int32 nPos = aText.indexOf('(');
106 while (nPos >= 0)
108 sal_Int32 nEnd = aText.indexOf(')', nPos);
109 if (nEnd >= 0)
111 OUStringBuffer aTextBuf(aText);
112 aTextBuf.remove(nPos, nEnd - nPos + 1);
113 aText = aTextBuf.makeStringAndClear();
115 else
116 break;
117 nPos = aText.indexOf('(');
120 nPos = aText.indexOf('*');
121 if(nPos == 0)
122 return OUString();
123 else if(nPos > 0)
124 aText = aText.copy(0, nPos);
126 // remove any possible remaining ' ' that may confuse the thesaurus
127 // when it gets called with the text
128 return comphelper::string::strip(aText, ' ');
130 } // namespace linguistic
132 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */