Skip to content

Commit

Permalink
Added string matching function.
Browse files Browse the repository at this point in the history
This is extracted from PathMatcher and will be used as the basis for unifying string matching across Gaffer as required by GafferHQ#707.
  • Loading branch information
johnhaddon committed Apr 1, 2014
1 parent ce0f200 commit 66e6d71
Show file tree
Hide file tree
Showing 8 changed files with 355 additions and 88 deletions.
78 changes: 78 additions & 0 deletions include/Gaffer/StringAlgo.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
//////////////////////////////////////////////////////////////////////////
//
// Copyright (c) 2012, John Haddon. All rights reserved.
// Copyright (c) 2014, Image Engine Design Inc. All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above
// copyright notice, this list of conditions and the following
// disclaimer.
//
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following
// disclaimer in the documentation and/or other materials provided with
// the distribution.
//
// * Neither the name of John Haddon nor the names of
// any other contributors to this software may be used to endorse or
// promote products derived from this software without specific prior
// written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
// IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
// THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
//////////////////////////////////////////////////////////////////////////

#ifndef GAFFER_STRINGALGO_H
#define GAFFER_STRINGALGO_H

#include <string>

namespace Gaffer
{

/// A type which can be used to store a pattern to be matched against.
/// Note that the match() function can actually operate on other string
/// types as well so the use of this type is purely optional. The main
/// reason to use a MatchPattern is documentation - by including it in a function
/// signature, the use of an argument can be made more obvious.
///
/// Patterns currently support only the "*" wildcard, which matches any
/// sequence of characters.
typedef std::string MatchPattern;

/// Returns true if the string matches the pattern and false otherwise.
inline bool match( const std::string &s, const MatchPattern &pattern );
inline bool match( const char *s, const char *pattern );

/// A comparison function for strings, equivalent to std::less<> except
/// that strings are treated as equal if they have identical prefixes followed
/// by a wildcard character in at least one. This allows searches to be performed
/// to quickly find all patterns that potentially match a given string. See the
/// GafferScene::PathMatcher class for an example where this is used in conjunction
/// with std::multimap and equal_range() to perform rapid matching against multiple
/// patterns.
struct MatchPatternLess
{

inline bool operator() ( const MatchPattern &s1, const MatchPattern &s2 ) const;

};

} // namespace Gaffer

#include "Gaffer/StringAlgo.inl"

#endif // GAFFER_STRINGALGO_H
110 changes: 110 additions & 0 deletions include/Gaffer/StringAlgo.inl
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
//////////////////////////////////////////////////////////////////////////
//
// Copyright (c) 2012, John Haddon. All rights reserved.
// Copyright (c) 2014, Image Engine Design Inc. All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above
// copyright notice, this list of conditions and the following
// disclaimer.
//
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following
// disclaimer in the documentation and/or other materials provided with
// the distribution.
//
// * Neither the name of John Haddon nor the names of
// any other contributors to this software may be used to endorse or
// promote products derived from this software without specific prior
// written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
// IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
// THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
//////////////////////////////////////////////////////////////////////////

#ifndef GAFFER_STRINGALGO_INL
#define GAFFER_STRINGALGO_INL

namespace Gaffer
{

inline bool match( const std::string &string, const std::string &pattern )
{
return match( string.c_str(), pattern.c_str() );
}

inline bool match( const char *s, const char *pattern )
{
char c;
while( true )
{
switch( c = *pattern++ )
{
case '\0' :

return *s == c;

case '*' :

if( *pattern == '\0' )
{
// optimisation for when pattern
// ends with '*'.
return true;
}

// general case - recurse.
while( *s != '\0' )
{
if( match( s, pattern ) )
{
return true;
}
s++;
}
return false;

default :

if( c != *s++ )
{
return false;
}
}
}
}

inline bool MatchPatternLess::operator() ( const std::string &s1, const std::string &s2 ) const
{
register const char *c1 = s1.c_str();
register const char *c2 = s2.c_str();

while( *c1 == *c2 && *c1 )
{
c1++; c2++;
}

if( *c1 == '*' || *c2 == '*' )
{
return false;
}

return *c1 < *c2;
}

} // namespace Gaffer

#endif // GAFFER_STRINGALGO_INL
47 changes: 47 additions & 0 deletions include/GafferBindings/StringAlgoBinding.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
//////////////////////////////////////////////////////////////////////////
//
// Copyright (c) 2014, Image Engine Design Inc. All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above
// copyright notice, this list of conditions and the following
// disclaimer.
//
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following
// disclaimer in the documentation and/or other materials provided with
// the distribution.
//
// * Neither the name of John Haddon nor the names of
// any other contributors to this software may be used to endorse or
// promote products derived from this software without specific prior
// written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
// IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
// THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
//////////////////////////////////////////////////////////////////////////

#ifndef GAFFERBINDINGS_STRINGALGOBINDING_H
#define GAFFERBINDINGS_STRINGALGOBINDING_H

namespace GafferBindings
{

void bindStringAlgo();

} // namespace GafferBindings

#endif // GAFFERBINDINGS_STRINGALGOBINDING_H
58 changes: 58 additions & 0 deletions python/GafferTest/StringAlgoTest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
##########################################################################
#
# Copyright (c) 2014, Image Engine Design Inc. All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
#
# * Redistributions of source code must retain the above
# copyright notice, this list of conditions and the following
# disclaimer.
#
# * Redistributions in binary form must reproduce the above
# copyright notice, this list of conditions and the following
# disclaimer in the documentation and/or other materials provided with
# the distribution.
#
# * Neither the name of John Haddon nor the names of
# any other contributors to this software may be used to endorse or
# promote products derived from this software without specific prior
# written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
# IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
##########################################################################

import Gaffer
import GafferTest

class StringAlgoTest( GafferTest.TestCase ) :

def test( self ) :

for s, p, r in [
( "", "", True ),
( "a", "a", True ),
( "a", "*", True ),
( "ab", "a*", True ),
( "cat", "dog", False ),
( "dogfish", "*fish", True ),
( "dogcollar", "*fish", False ),
] :

self.assertEqual( Gaffer.match( s, p ), r )

if __name__ == "__main__":
unittest.main()

3 changes: 2 additions & 1 deletion python/GafferTest/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
##########################################################################
#
# Copyright (c) 2011-2012, John Haddon. All rights reserved.
# Copyright (c) 2011-2013, Image Engine Design Inc. All rights reserved.
# Copyright (c) 2011-2014, Image Engine Design Inc. All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
Expand Down Expand Up @@ -132,6 +132,7 @@ def wrapper( self ) :
from SerialisationTest import SerialisationTest
from SwitchTest import SwitchTest
from MetadataTest import MetadataTest
from StringAlgoTest import StringAlgoTest

if __name__ == "__main__":
import unittest
Expand Down
53 changes: 53 additions & 0 deletions src/GafferBindings/StringAlgoBinding.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
//////////////////////////////////////////////////////////////////////////
//
// Copyright (c) 2014, Image Engine Design Inc. All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above
// copyright notice, this list of conditions and the following
// disclaimer.
//
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following
// disclaimer in the documentation and/or other materials provided with
// the distribution.
//
// * Neither the name of John Haddon nor the names of
// any other contributors to this software may be used to endorse or
// promote products derived from this software without specific prior
// written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
// IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
// THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
//////////////////////////////////////////////////////////////////////////

#include "boost/python.hpp"

#include "Gaffer/StringAlgo.h"

#include "GafferBindings/StringAlgoBinding.h"

using namespace boost::python;

namespace GafferBindings
{

void bindStringAlgo()
{
def( "match", (bool (*)( const char *, const char * ))&Gaffer::match );
}

} // namespace GafferBindings
4 changes: 3 additions & 1 deletion src/GafferModule/GafferModule.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//////////////////////////////////////////////////////////////////////////
//
// Copyright (c) 2011-2012, John Haddon. All rights reserved.
// Copyright (c) 2011-2013, Image Engine Design Inc. All rights reserved.
// Copyright (c) 2011-2014, Image Engine Design Inc. All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
Expand Down Expand Up @@ -83,6 +83,7 @@
#include "GafferBindings/ArrayPlugBinding.h"
#include "GafferBindings/Serialisation.h"
#include "GafferBindings/MetadataBinding.h"
#include "GafferBindings/StringAlgoBinding.h"

using namespace boost::python;
using namespace Gaffer;
Expand Down Expand Up @@ -133,6 +134,7 @@ BOOST_PYTHON_MODULE( _Gaffer )
bindArrayPlug();
bindSerialisation();
bindMetadata();
bindStringAlgo();

NodeClass<Backdrop>();

Expand Down
Loading

0 comments on commit 66e6d71

Please sign in to comment.