Use o3tl::convert in Math
[LibreOffice.git] / l10ntools / source / helper.cxx
blob767c04eb515298c614e4d2dc8bccfe401137900a
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/.
8 */
10 #include <sal/config.h>
12 #include <o3tl/safeint.hxx>
14 #include <helper.hxx>
16 namespace helper {
18 OString escapeAll(
19 const OString& rText, const OString& rUnEscaped, const OString& rEscaped )
21 assert( rEscaped.getLength() == 2*rUnEscaped.getLength() );
22 OStringBuffer sReturn;
23 for ( sal_Int32 nIndex = 0; nIndex < rText.getLength(); ++nIndex )
25 sal_Int32 nUnEscapedOne = rUnEscaped.indexOf(rText[nIndex]);
26 if( nUnEscapedOne != -1 )
28 sReturn.append(rEscaped.subView(nUnEscapedOne*2,2));
30 else
31 sReturn.append(rText[nIndex]);
33 return sReturn.makeStringAndClear();
37 OString unEscapeAll(
38 const OString& rText, const OString& rEscaped, std::string_view rUnEscaped)
40 assert( o3tl::make_unsigned(rEscaped.getLength()) == 2*rUnEscaped.length() );
41 OStringBuffer sReturn;
42 const sal_Int32 nLength = rText.getLength();
43 for ( sal_Int32 nIndex = 0; nIndex < nLength; ++nIndex )
45 if( rText[nIndex] == '\\' && nIndex+1 < nLength )
47 sal_Int32 nEscapedOne = rEscaped.indexOf(rText.subView(nIndex,2));
48 if( nEscapedOne != -1 )
50 sReturn.append(rUnEscaped[nEscapedOne/2]);
51 ++nIndex;
53 else
55 sReturn.append(rText[nIndex]);
58 else
59 sReturn.append(rText[nIndex]);
61 return sReturn.makeStringAndClear();
65 OString QuotHTML(const OString &rString)
67 OStringBuffer sReturn;
68 for (sal_Int32 i = 0; i < rString.getLength(); ++i)
70 switch (rString[i])
72 case '<':
73 sReturn.append("&lt;");
74 break;
75 case '>':
76 sReturn.append("&gt;");
77 break;
78 case '"':
79 sReturn.append("&quot;");
80 break;
81 case '\'':
82 sReturn.append("&apos;");
83 break;
84 case '&':
85 if (rString.match("&amp;", i))
86 sReturn.append('&');
87 else
88 sReturn.append("&amp;");
89 break;
90 default:
91 sReturn.append(rString[i]);
92 break;
95 return sReturn.makeStringAndClear();
98 OString UnQuotHTML( const OString& rString )
100 OStringBuffer sReturn;
101 for (sal_Int32 i = 0; i != rString.getLength();) {
102 if (rString.match("&amp;", i)) {
103 sReturn.append('&');
104 i += RTL_CONSTASCII_LENGTH("&amp;");
105 } else if (rString.match("&lt;", i)) {
106 sReturn.append('<');
107 i += RTL_CONSTASCII_LENGTH("&lt;");
108 } else if (rString.match("&gt;", i)) {
109 sReturn.append('>');
110 i += RTL_CONSTASCII_LENGTH("&gt;");
111 } else if (rString.match("&quot;", i)) {
112 sReturn.append('"');
113 i += RTL_CONSTASCII_LENGTH("&quot;");
114 } else if (rString.match("&apos;", i)) {
115 sReturn.append('\'');
116 i += RTL_CONSTASCII_LENGTH("&apos;");
117 } else {
118 sReturn.append(rString[i]);
119 ++i;
122 return sReturn.makeStringAndClear();
125 bool isWellFormedXML( std::string_view text )
127 xmlDocPtr doc;
128 bool result = true;
130 OString content = OString::Concat("<root>") + text + "</root>";
131 doc = xmlParseMemory(content.getStr(),static_cast<int>(content.getLength()));
132 if (doc == nullptr) {
133 result = false;
135 xmlFreeDoc(doc);
136 xmlCleanupParser();
137 return result;
140 //Convert xmlChar* to OString
141 OString xmlStrToOString( const xmlChar* pString )
143 xmlChar* pTemp = xmlStrdup( pString );
144 OString sResult = reinterpret_cast<char*>( pTemp );
145 xmlFree( pTemp );
146 return sResult;
151 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */