/src/libreoffice/svl/source/misc/lngmisc.cxx
Line | Count | Source |
1 | | /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
2 | | /* |
3 | | * This file is part of the LibreOffice project. |
4 | | * |
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/. |
8 | | * |
9 | | * This file incorporates work covered by the following license notice: |
10 | | * |
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 . |
18 | | */ |
19 | | |
20 | | |
21 | | #include <svl/lngmisc.hxx> |
22 | | |
23 | | #include <comphelper/string.hxx> |
24 | | #include <rtl/ustrbuf.hxx> |
25 | | #include <tools/debug.hxx> |
26 | | |
27 | | namespace linguistic |
28 | | { |
29 | | sal_Int32 GetNumControlChars(std::u16string_view rTxt) |
30 | 0 | { |
31 | 0 | sal_Int32 nCnt = 0; |
32 | |
|
33 | 0 | for(const auto ch : rTxt) |
34 | 0 | { |
35 | 0 | if(IsControlChar(ch)) |
36 | 0 | ++nCnt; |
37 | 0 | } |
38 | 0 | return nCnt; |
39 | 0 | } |
40 | | |
41 | | bool RemoveHyphens(OUString &rTxt) |
42 | 0 | { |
43 | 0 | sal_Int32 n = rTxt.getLength(); |
44 | 0 | rTxt = rTxt.replaceAll(OUStringChar(SVT_SOFT_HYPHEN), ""); |
45 | 0 | rTxt = rTxt.replaceAll(OUStringChar(SVT_HARD_HYPHEN), ""); |
46 | 0 | return n != rTxt.getLength(); |
47 | 0 | } |
48 | | |
49 | | bool RemoveControlChars(OUString &rTxt) |
50 | 0 | { |
51 | 0 | sal_Int32 nSize = rTxt.getLength() - GetNumControlChars(rTxt); |
52 | 0 | if(nSize == rTxt.getLength()) |
53 | 0 | return false; |
54 | | |
55 | 0 | OUStringBuffer aBuf(nSize); |
56 | 0 | aBuf.setLength(nSize); |
57 | 0 | for (sal_Int32 i = 0, j = 0; i < rTxt.getLength() && j < nSize; ++i) |
58 | 0 | if (!IsControlChar(rTxt[i])) |
59 | 0 | aBuf[j++] = rTxt[i]; |
60 | |
|
61 | 0 | rTxt = aBuf.makeStringAndClear(); |
62 | 0 | DBG_ASSERT(rTxt.getLength() == nSize, "GetNumControlChars returned a different number of control characters than were actually removed."); |
63 | |
|
64 | 0 | return true; |
65 | 0 | } |
66 | | |
67 | | bool ReplaceControlChars(OUString &rTxt) |
68 | 0 | { |
69 | | // non breaking field character |
70 | 0 | static const char CH_TXTATR_INWORD = static_cast<char>(0x02); |
71 | | |
72 | | // the resulting string looks like this: |
73 | | // 1. non breaking field characters get removed |
74 | | // 2. remaining control characters will be replaced by ' ' |
75 | |
|
76 | 0 | if (GetNumControlChars(rTxt) == 0) |
77 | 0 | return false; |
78 | | |
79 | 0 | sal_Int32 n = rTxt.getLength(); |
80 | |
|
81 | 0 | OUStringBuffer aBuf(n); |
82 | 0 | aBuf.setLength(n); |
83 | |
|
84 | 0 | sal_Int32 j = 0; |
85 | 0 | for (sal_Int32 i = 0; i < n && j < n; ++i) |
86 | 0 | { |
87 | 0 | if (CH_TXTATR_INWORD == rTxt[i]) |
88 | 0 | continue; |
89 | | |
90 | 0 | aBuf[j++] = IsControlChar(rTxt[i]) ? ' ' : rTxt[i]; |
91 | 0 | } |
92 | |
|
93 | 0 | aBuf.setLength(j); |
94 | 0 | rTxt = aBuf.makeStringAndClear(); |
95 | |
|
96 | 0 | return true; |
97 | 0 | } |
98 | | |
99 | | OUString GetThesaurusReplaceText(const OUString &rText) |
100 | 0 | { |
101 | | // The strings for synonyms returned by the thesaurus sometimes have some |
102 | | // explanation text put in between '(' and ')' or a trailing '*'. |
103 | | // These parts should not be put in the ReplaceEdit Text that may get |
104 | | // inserted into the document. Thus we strip them from the text. |
105 | |
|
106 | 0 | OUString aText(rText); |
107 | |
|
108 | 0 | sal_Int32 nPos = aText.indexOf('('); |
109 | 0 | while (nPos >= 0) |
110 | 0 | { |
111 | 0 | sal_Int32 nEnd = aText.indexOf(')', nPos); |
112 | 0 | if (nEnd >= 0) |
113 | 0 | { |
114 | 0 | OUStringBuffer aTextBuf(aText); |
115 | 0 | aTextBuf.remove(nPos, nEnd - nPos + 1); |
116 | 0 | aText = aTextBuf.makeStringAndClear(); |
117 | 0 | } |
118 | 0 | else |
119 | 0 | break; |
120 | 0 | nPos = aText.indexOf('('); |
121 | 0 | } |
122 | |
|
123 | 0 | nPos = aText.indexOf('*'); |
124 | 0 | if(nPos == 0) |
125 | 0 | return OUString(); |
126 | 0 | else if(nPos > 0) |
127 | 0 | aText = aText.copy(0, nPos); |
128 | | |
129 | | // remove any possible remaining ' ' that may confuse the thesaurus |
130 | | // when it gets called with the text |
131 | 0 | return comphelper::string::strip(aText, ' '); |
132 | 0 | } |
133 | | } // namespace linguistic |
134 | | |
135 | | /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |