Blame lib/matchcompiler.h

Packit 2035a7
/*
Packit 2035a7
 * Cppcheck - A tool for static C/C++ code analysis
Packit 2035a7
 * Copyright (C) 2007-2016 Cppcheck team.
Packit 2035a7
 *
Packit 2035a7
 * This program is free software: you can redistribute it and/or modify
Packit 2035a7
 * it under the terms of the GNU General Public License as published by
Packit 2035a7
 * the Free Software Foundation, either version 3 of the License, or
Packit 2035a7
 * (at your option) any later version.
Packit 2035a7
 *
Packit 2035a7
 * This program is distributed in the hope that it will be useful,
Packit 2035a7
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 2035a7
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit 2035a7
 * GNU General Public License for more details.
Packit 2035a7
 *
Packit 2035a7
 * You should have received a copy of the GNU General Public License
Packit 2035a7
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
Packit 2035a7
 */
Packit 2035a7
Packit 2035a7
//---------------------------------------------------------------------------
Packit 2035a7
#ifndef matchcompilerH
Packit 2035a7
#define matchcompilerH
Packit 2035a7
Packit 2035a7
#include <string>
Packit 2035a7
Packit 2035a7
namespace MatchCompiler {
Packit 2035a7
Packit 2035a7
    template <unsigned int n>
Packit 2035a7
    class ConstString {
Packit 2035a7
    public:
Packit 2035a7
        typedef const char(&StringRef)[n];
Packit 2035a7
        explicit ConstString(StringRef s)
Packit 2035a7
            :_s(s) {
Packit 2035a7
        }
Packit 2035a7
Packit 2035a7
        operator StringRef() const {
Packit 2035a7
            return _s;
Packit 2035a7
        }
Packit 2035a7
Packit 2035a7
    private:
Packit 2035a7
        StringRef _s;
Packit 2035a7
    };
Packit 2035a7
Packit 2035a7
    template <unsigned int n>
Packit 2035a7
    inline bool equalN(const char s1[], const char s2[])
Packit 2035a7
    {
Packit 2035a7
        return (*s1 == *s2) && equalN<n-1>(s1+1, s2+1);
Packit 2035a7
    }
Packit 2035a7
Packit 2035a7
    template <>
Packit 2035a7
    inline bool equalN<0>(const char [], const char [])
Packit 2035a7
    {
Packit 2035a7
        return true;
Packit 2035a7
    }
Packit 2035a7
Packit 2035a7
    template <unsigned int n>
Packit 2035a7
    inline bool operator==(const std::string & s1, ConstString<n> const & s2)
Packit 2035a7
    {
Packit 2035a7
        return equalN<n>(s1.c_str(), s2);
Packit 2035a7
    }
Packit 2035a7
Packit 2035a7
    template <unsigned int n>
Packit 2035a7
    inline bool operator!=(const std::string & s1, ConstString<n> const & s2)
Packit 2035a7
    {
Packit 2035a7
        return !operator==(s1,s2);
Packit 2035a7
    }
Packit 2035a7
Packit 2035a7
    template <unsigned int n>
Packit 2035a7
    inline ConstString<n> makeConstString(const char(&s)[n])
Packit 2035a7
    {
Packit 2035a7
        return ConstString<n>(s);
Packit 2035a7
    }
Packit 2035a7
}
Packit 2035a7
Packit 2035a7
#endif // matchcompilerH
Packit 2035a7