use insert function instead of for loop
[LibreOffice.git] / tools / source / fsys / wldcrd.cxx
blobe8199975c6e5d80e3e1c821d8c6346588bffce09
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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)
38 switch (*pWild)
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 == '?')
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;
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)
82 pStr++;
83 if ( pStr == pStrEnd )
84 return false;
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);
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
96 assert(pWild == pWildEnd);
97 return pStr == pStrEnd;
100 bool WildCard::Matches( std::u16string_view rString ) const
102 std::u16string_view aTmpWild = aWildString;
104 size_t nSepPos;
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 ) )
112 return true;
113 aTmpWild = aTmpWild.substr(nSepPos + 1); // remove separator
117 return ImpMatch( aTmpWild, rString );
120 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */