Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(#77): Make query parser more c++-friendly #87

Merged
merged 1 commit into from
Jan 18, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 10 additions & 22 deletions zeal/zealsearchquery.cpp
Original file line number Diff line number Diff line change
@@ -1,37 +1,26 @@
#include <QString>
#include <QStringList>

#include "zealsearchquery.h"

// Creates a search query from a string
//
// Examples:
// "android:setTypeFa" #=> docsetFilter = "android", coreQuery = "setTypeFa"
// "noprefix" #=> docsetFilter = "", coreQuery = "noprefix"
// ":find" #=> docsetFilter = "", coreQuery = ":find"
ZealSearchQuery::ZealSearchQuery(const QString &rawQuery)
{
this->coreQuery = rawQuery;
this->docsetFilter = "";

if(rawQuery.indexOf(ZealSearchQuery::DOCSET_FILTER_SEPARATOR) >= 1) {
QStringList partitioned = rawQuery.split(ZealSearchQuery::DOCSET_FILTER_SEPARATOR);
QString prefix = partitioned[0];
this->coreQuery = partitioned[1];
this->docsetFilter = prefix.trimmed();
const int sepAt = rawQuery.indexOf(DOCSET_FILTER_SEPARATOR);
const int next = sepAt + 1;
if (sepAt >= 1
&& (next >= rawQuery.size()
|| rawQuery.at(next) != DOCSET_FILTER_SEPARATOR)) {
this->docsetFilter = rawQuery.leftRef(sepAt).toString().trimmed();
this->coreQuery = rawQuery.midRef(next).toString().trimmed();
} else {
this->docsetFilter = "";
this->coreQuery = rawQuery.trimmed();
}

this->coreQuery = this->coreQuery.trimmed();
}


// Returns the docset filter for the given query.
QString ZealSearchQuery::getDocsetFilter()
{
return this->docsetFilter;
}

// Returns the core query, sanitized for use in SQL queries
QString ZealSearchQuery::getSanitizedQuery()
{
QString q = getCoreQuery();
Expand All @@ -42,7 +31,6 @@ QString ZealSearchQuery::getSanitizedQuery()
return q;
}

// Returns the query with any docset prefixes removed.
QString ZealSearchQuery::getCoreQuery()
{
return this->coreQuery;
Expand Down
26 changes: 24 additions & 2 deletions zeal/zealsearchquery.h
Original file line number Diff line number Diff line change
@@ -1,15 +1,37 @@
#ifndef ZEALSEARCHQUERY_H
#define ZEALSEARCHQUERY_H

class QString;

/**
* @short The search query model.
*/
class ZealSearchQuery
{
public:
ZealSearchQuery(const QString& coreQuery);

static const char DOCSET_FILTER_SEPARATOR = ':';

/// Creates a search query from a string. Single separator will be
/// used to contstruct docset filter, but separator repeated twice
/// will be left inside coreQuery part since double semicolon is
/// used inside qualified symbol names in popular programming
/// languages (c++, ruby, perl, etc.).
///
/// Examples:
/// "android:setTypeFa" #=> docsetFilter = "android", coreQuery = "setTypeFa"
/// "noprefix" #=> docsetFilter = "", coreQuery = "noprefix"
/// ":find" #=> docsetFilter = "", coreQuery = ":find"
/// "std::string" #=> docsetFilter = "", coreQuery = "std::string"
/// "c++:std::string" #=> docsetFilter = "c++", coreQuery = "std::string"
ZealSearchQuery(const QString& coreQuery);

/// Returns the docset filter for the given query.
QString getDocsetFilter();

/// Returns the core query, sanitized for use in SQL queries.
QString getSanitizedQuery();

/// Returns the query with any docset prefixes removed.
QString getCoreQuery();

private:
Expand Down