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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <tools/wldcrd.hxx>
22 /** Tests, whether a wildcard in pWild will match for pStr.
24 * '*' in pWild means n chars for n > 0.
25 * '?' in pWild mean match exactly one character.
28 bool WildCard::ImpMatch( std::u16string_view aWild
, std::u16string_view aStr
)
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();
36 while (pWild
!= pWildEnd
)
41 if ( pStr
== pStrEnd
)
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) == '*')))
48 default: // No wildcard, literal match
52 break; // Match -> proceed to the next character
53 if (!pPosAfterAsterisk
)
55 pWild
= pPosAfterAsterisk
;
58 while ( pWild
!= pWildEnd
&& *pWild
== '*' )
60 if ( pWild
== pWildEnd
)
62 // Consider strange things like "**?*?*"
69 while (pWild
!= pWildEnd
&& *pWild
== '*')
71 if (pWild
== pWildEnd
)
74 // At this point, we are past wildcards, and a literal match must follow
75 if ( pStr
== pStrEnd
)
77 pPosAfterAsterisk
= pWild
;
78 if ((*pWild
== '\\') && (pWild
+ 1 != pWildEnd
) && ((*(pWild
+ 1) == '?') || (*(pWild
+ 1) == '*')))
80 while (*pStr
!= *pWild
)
83 if ( pStr
== pStrEnd
)
86 break; // Match -> proceed to the next character
88 // We arrive here when the current characters in pWild and pStr match
89 assert(pWild
!= pWildEnd
);
91 assert(pStr
!= pStrEnd
);
93 if (pWild
== pWildEnd
&& pPosAfterAsterisk
&& pStr
!= pStrEnd
)
94 pWild
= pPosAfterAsterisk
; // Try again on the rest of pStr
96 assert(pWild
== pWildEnd
);
97 return pStr
== pStrEnd
;
100 bool WildCard::Matches( std::u16string_view rString
) const
102 std::u16string_view aTmpWild
= aWildString
;
106 if ( cSepSymbol
!= '\0' )
108 while ( (nSepPos
= aTmpWild
.find(cSepSymbol
)) != std::u16string_view::npos
)
110 // Check all split wildcards
111 if ( ImpMatch( aTmpWild
.substr( 0, nSepPos
), rString
) )
113 aTmpWild
= aTmpWild
.substr(nSepPos
+ 1); // remove separator
117 return ImpMatch( aTmpWild
, rString
);
120 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */