Avoid potential negative array index access to cached text.
[LibreOffice.git] / vcl / inc / graphic / DetectorTools.hxx
blob3a3bae012eddc43402b37ed5c863605c456cf972
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 */
11 #pragma once
13 #include <rtl/string.hxx>
15 #include <vector>
17 namespace vcl
19 const char* matchArray(const char* pSource, sal_Int32 nSourceSize, const char* pSearch,
20 sal_Int32 nSearchSize)
22 for (sal_Int32 increment = 0; increment <= (nSourceSize - nSearchSize); ++increment)
24 bool bMatch = true;
25 // search both arrays if they match
26 for (sal_Int32 index = 0; index < nSearchSize && bMatch; ++index)
28 if (pSource[index] != pSearch[index])
29 bMatch = false;
31 // match has been found
32 if (bMatch)
33 return pSource;
34 pSource++;
36 return nullptr;
39 const char* matchArrayWithString(const char* pSource, sal_Int32 nSourceSize, OString const& rString)
41 return matchArray(pSource, nSourceSize, rString.getStr(), rString.getLength());
44 bool checkArrayForMatchingStrings(const char* pSource, sal_Int32 nSourceSize,
45 std::vector<OString> const& rStrings)
47 if (rStrings.empty())
48 return false;
49 if (rStrings.size() < 2)
50 return matchArrayWithString(pSource, nSourceSize, rStrings[0]) != nullptr;
52 const char* pBegin = pSource;
53 const char* pCurrent = pSource;
54 for (OString const& rString : rStrings)
56 sal_Int32 nCurrentSize = nSourceSize - sal_Int32(pCurrent - pBegin);
57 pCurrent = matchArray(pCurrent, nCurrentSize, rString.getStr(), rString.getLength());
58 if (pCurrent == nullptr)
59 return false;
61 return true;
65 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */