1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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/.
10 #include <sal/config.h>
12 #include <libxml/parser.h>
14 #include <o3tl/safeint.hxx>
15 #include <o3tl/string_view.hxx>
16 #include <rtl/strbuf.hxx>
23 std::string_view rText
, std::string_view rUnEscaped
, std::string_view rEscaped
)
25 assert( rEscaped
.size() == 2*rUnEscaped
.size() );
26 OStringBuffer sReturn
;
27 for ( size_t nIndex
= 0; nIndex
< rText
.size(); ++nIndex
)
29 size_t nUnEscapedOne
= rUnEscaped
.find(rText
[nIndex
]);
30 if( nUnEscapedOne
!= std::string_view::npos
)
32 sReturn
.append(rEscaped
.substr(nUnEscapedOne
*2,2));
35 sReturn
.append(rText
[nIndex
]);
37 return sReturn
.makeStringAndClear();
42 std::string_view rText
, std::string_view rEscaped
, std::string_view rUnEscaped
)
44 assert( rEscaped
.size() == 2*rUnEscaped
.length() );
45 OStringBuffer sReturn
;
46 const size_t nLength
= rText
.size();
47 for ( size_t nIndex
= 0; nIndex
< nLength
; ++nIndex
)
49 if( rText
[nIndex
] == '\\' && nIndex
+1 < nLength
)
51 size_t nEscapedOne
= rEscaped
.find(rText
.substr(nIndex
,2));
52 if( nEscapedOne
!= std::string_view::npos
)
54 sReturn
.append(rUnEscaped
[nEscapedOne
/2]);
59 sReturn
.append(rText
[nIndex
]);
63 sReturn
.append(rText
[nIndex
]);
65 return sReturn
.makeStringAndClear();
69 OString
QuotHTML(std::string_view rString
)
71 OStringBuffer sReturn
;
72 for (size_t i
= 0; i
< rString
.size(); ++i
)
77 sReturn
.append("<");
80 sReturn
.append(">");
83 sReturn
.append(""");
86 sReturn
.append("'");
89 if (o3tl::starts_with(rString
.substr(i
), "&"))
92 sReturn
.append("&");
95 sReturn
.append(rString
[i
]);
99 return sReturn
.makeStringAndClear();
102 OString
UnQuotHTML( std::string_view rString
)
104 OStringBuffer sReturn
;
105 for (size_t i
= 0; i
!= rString
.size();) {
106 auto tmp
= rString
.substr(i
);
107 if (o3tl::starts_with(tmp
, "&")) {
109 i
+= RTL_CONSTASCII_LENGTH("&");
110 } else if (o3tl::starts_with(tmp
, "<")) {
112 i
+= RTL_CONSTASCII_LENGTH("<");
113 } else if (o3tl::starts_with(tmp
, ">")) {
115 i
+= RTL_CONSTASCII_LENGTH(">");
116 } else if (o3tl::starts_with(tmp
, """)) {
118 i
+= RTL_CONSTASCII_LENGTH(""");
119 } else if (o3tl::starts_with(tmp
, "'")) {
120 sReturn
.append('\'');
121 i
+= RTL_CONSTASCII_LENGTH("'");
123 sReturn
.append(rString
[i
]);
127 return sReturn
.makeStringAndClear();
130 bool isWellFormedXML( std::string_view text
)
135 OString content
= OString::Concat("<root>") + text
+ "</root>";
136 doc
= xmlParseMemory(content
.getStr(),static_cast<int>(content
.getLength()));
137 if (doc
== nullptr) {
145 //Convert xmlChar* to OString
146 OString
xmlStrToOString( const xmlChar
* pString
)
148 xmlChar
* pTemp
= xmlStrdup( pString
);
149 OString sResult
= reinterpret_cast<char*>( pTemp
);
156 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */