Coverage Report

Created: 2026-07-10 11:04

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libreoffice/tools/source/fsys/wldcrd.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
#include <tools/wldcrd.hxx>
21
22
/** Tests, whether a wildcard in pWild will match for pStr.
23
 *
24
 * '*' in pWild means n chars for n > 0.
25
 * '?' in pWild mean match exactly one character.
26
 *
27
 */
28
bool WildCard::ImpMatch( std::u16string_view aWild, std::u16string_view aStr )
29
0
{
30
0
    const sal_Unicode* pPosAfterAsterisk = nullptr;
31
0
    const sal_Unicode* pWild = aWild.data();
32
0
    const sal_Unicode* pWildEnd = aWild.data() + aWild.size();
33
0
    const sal_Unicode* pStr = aStr.data();
34
0
    const sal_Unicode* pStrEnd = aStr.data() + aStr.size();
35
36
0
    while (pWild != pWildEnd)
37
0
    {
38
0
        switch (*pWild)
39
0
        {
40
0
            case '?':
41
0
                if ( pStr == pStrEnd )
42
0
                    return false;
43
0
                break; // Match -> proceed to the next character
44
0
            case '\\': // Escaping '?' and '*'; don't we need to escape '\\'?
45
0
                if ((pWild + 1 != pWildEnd) && ((*(pWild + 1) == '?') || (*(pWild + 1) == '*')))
46
0
                    pWild++;
47
0
                [[fallthrough]];
48
0
            default: // No wildcard, literal match
49
0
                if (pStr == pStrEnd)
50
0
                    return false;
51
0
                if (*pWild == *pStr)
52
0
                    break; // Match -> proceed to the next character
53
0
                if (!pPosAfterAsterisk)
54
0
                    return false;
55
0
                pWild = pPosAfterAsterisk;
56
0
                [[fallthrough]];
57
0
            case '*':
58
0
                while ( pWild != pWildEnd && *pWild == '*' )
59
0
                    pWild++;
60
0
                if ( pWild == pWildEnd )
61
0
                    return true;
62
                // Consider strange things like "**?*?*"
63
0
                while (*pWild == '?')
64
0
                {
65
0
                    if (pStr == pStrEnd)
66
0
                        return false;
67
0
                    pWild++;
68
0
                    pStr++;
69
0
                    while (pWild != pWildEnd && *pWild == '*')
70
0
                        pWild++;
71
0
                    if (pWild == pWildEnd)
72
0
                        return true;
73
0
                }
74
                // At this point, we are past wildcards, and a literal match must follow
75
0
                if ( pStr == pStrEnd )
76
0
                    return false;
77
0
                pPosAfterAsterisk = pWild;
78
0
                if ((*pWild == '\\') && (pWild + 1 != pWildEnd) && ((*(pWild + 1) == '?') || (*(pWild + 1) == '*')))
79
0
                    pWild++;
80
0
                while (*pStr != *pWild)
81
0
                {
82
0
                    pStr++;
83
0
                    if ( pStr == pStrEnd )
84
0
                        return false;
85
0
                }
86
0
                break; // Match -> proceed to the next character
87
0
        }
88
        // We arrive here when the current characters in pWild and pStr match
89
0
        assert(pWild != pWildEnd);
90
0
        pWild++;
91
0
        assert(pStr != pStrEnd);
92
0
        pStr++;
93
0
        if (pWild == pWildEnd && pPosAfterAsterisk && pStr != pStrEnd)
94
0
            pWild = pPosAfterAsterisk; // Try again on the rest of pStr
95
0
    }
96
0
    assert(pWild == pWildEnd);
97
0
    return pStr == pStrEnd;
98
0
}
99
100
bool WildCard::Matches( std::u16string_view rString ) const
101
0
{
102
0
    std::u16string_view aTmpWild = aWildString;
103
104
0
    size_t nSepPos;
105
106
0
    if ( cSepSymbol != '\0' )
107
0
    {
108
0
        while ( (nSepPos = aTmpWild.find(cSepSymbol)) != std::u16string_view::npos )
109
0
        {
110
            // Check all split wildcards
111
0
            if ( ImpMatch( aTmpWild.substr( 0, nSepPos ), rString ) )
112
0
                return true;
113
0
            aTmpWild = aTmpWild.substr(nSepPos + 1); // remove separator
114
0
        }
115
0
    }
116
117
0
    return ImpMatch( aTmpWild, rString );
118
0
}
119
120
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */