Coverage Report

Created: 2026-04-09 11:41

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libreoffice/include/comphelper/xmlencode.hxx
Line
Count
Source
1
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
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
#pragma once
21
22
#include <rtl/ustring.hxx>
23
#include <rtl/ustrbuf.hxx>
24
25
namespace comphelper::string
26
{
27
inline OUString encodeForXml(std::u16string_view rStr)
28
0
{
29
    // encode conforming xml:
30
0
    sal_Int32 len = rStr.length();
31
0
    OUStringBuffer buf(len + 16); // it's going to be at least len
32
0
    for (sal_Int32 pos = 0; pos < len; ++pos)
33
0
    {
34
0
        sal_Unicode c = rStr[pos];
35
0
        switch (c)
36
0
        {
37
0
            case '<':
38
0
                buf.append("&lt;");
39
0
                break;
40
0
            case '>':
41
0
                buf.append("&gt;");
42
0
                break;
43
0
            case '&':
44
0
                buf.append("&amp;");
45
0
                break;
46
0
            case '\'':
47
0
                buf.append("&apos;");
48
0
                break;
49
0
            case '\"':
50
0
                buf.append("&quot;");
51
0
                break;
52
0
            default:
53
0
                buf.append(c);
54
0
                break;
55
0
        }
56
0
    }
57
58
0
    return buf.makeStringAndClear();
59
0
}
60
61
} /* Namespace */
62
63
/* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */