Sha256: 4e6204af37268a773fda189ee9301eb68ea36f57e0a81a0f98ae94027ca8da29

Contents?: true

Size: 1.01 KB

Versions: 17

Compression:

Stored size: 1.01 KB

Contents

# frozen_string_literal: true

class ThinkingSphinx::Wildcard
  DEFAULT_TOKEN = /\p{Word}+/

  def self.call(query, pattern = DEFAULT_TOKEN)
    new(query, pattern).call
  end

  def initialize(query, pattern = DEFAULT_TOKEN)
    @query   = query || ''
    @pattern = pattern.is_a?(Regexp) ? pattern : DEFAULT_TOKEN
  end

  def call
    query.gsub(extended_pattern) do
      pre, proper, post = $`, $&, $'
      # E.g. "@foo", "/2", "~3", but not as part of a token pattern
      is_operator = pre.match(%r{@$}) ||
                    pre.match(%r{([^\\]+|\A)[~/]\Z}) ||
                    pre.match(%r{(\W|^)@\([^\)]*$})
      # E.g. "foo bar", with quotes
      is_quote    = proper[/^".*"$/]
      has_star    = post[/\*$/] || pre[/^\*/]
      if is_operator || is_quote || has_star
        proper
      else
        "*#{proper}*"
      end
    end
  end

  private

  attr_reader :query, :pattern

  def extended_pattern
    Regexp.new(
      "(\"#{pattern}(.*?#{pattern})?\"|(?![!-])#{pattern})".encode('UTF-8')
    )
  end
end

Version data entries

17 entries across 17 versions & 1 rubygems

Version Path
thinking-sphinx-5.6.0 lib/thinking_sphinx/wildcard.rb
thinking-sphinx-5.5.1 lib/thinking_sphinx/wildcard.rb
thinking-sphinx-5.5.0 lib/thinking_sphinx/wildcard.rb
thinking-sphinx-5.4.0 lib/thinking_sphinx/wildcard.rb
thinking-sphinx-5.3.0 lib/thinking_sphinx/wildcard.rb
thinking-sphinx-5.2.1 lib/thinking_sphinx/wildcard.rb
thinking-sphinx-5.2.0 lib/thinking_sphinx/wildcard.rb
thinking-sphinx-5.1.0 lib/thinking_sphinx/wildcard.rb
thinking-sphinx-5.0.0 lib/thinking_sphinx/wildcard.rb
thinking-sphinx-4.4.1 lib/thinking_sphinx/wildcard.rb
thinking-sphinx-4.4.0 lib/thinking_sphinx/wildcard.rb
thinking-sphinx-4.3.2 lib/thinking_sphinx/wildcard.rb
thinking-sphinx-4.3.1 lib/thinking_sphinx/wildcard.rb
thinking-sphinx-4.3.0 lib/thinking_sphinx/wildcard.rb
thinking-sphinx-4.2.0 lib/thinking_sphinx/wildcard.rb
thinking-sphinx-4.1.0 lib/thinking_sphinx/wildcard.rb
thinking-sphinx-4.0.0 lib/thinking_sphinx/wildcard.rb