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