Skip to content

Commit

Permalink
switch to std::regex
Browse files Browse the repository at this point in the history
  • Loading branch information
Andersbakken committed Mar 1, 2015
1 parent 734079d commit 49fb45d
Show file tree
Hide file tree
Showing 15 changed files with 66 additions and 72 deletions.
14 changes: 7 additions & 7 deletions src/FindFileJob.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ FindFileJob::FindFileJob(const std::shared_ptr<QueryMessage> &query, const std::
{
const String q = query->query();
if (!q.isEmpty()) {
if (query->flags() & QueryMessage::MatchRegexp) {
mRegExp = q;
if (query->flags() & QueryMessage::MatchRegex) {
mRegex = q.ref();
} else {
mPattern = q;
}
Expand All @@ -45,12 +45,12 @@ int FindFileJob::execute()
enum Mode {
All,
FilePath,
RegExp,
Regex,
Pattern,
} mode = All;
String::CaseSensitivity cs = String::CaseSensitive;
if (mRegExp.isValid()) {
mode = RegExp;
if (queryFlags() & QueryMessage::MatchRegex) {
mode = Regex;
} else if (!mPattern.isEmpty()) {
mode = mPattern[0] == '/' ? FilePath : Pattern;
}
Expand Down Expand Up @@ -89,8 +89,8 @@ int FindFileJob::execute()
case All:
ok = true;
break;
case RegExp:
ok = mRegExp.indexIn(out) != -1;
case Regex:
ok = Rct::contains(out, mRegex);
break;
case FilePath:
case Pattern:
Expand Down
4 changes: 2 additions & 2 deletions src/FindFileJob.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ along with RTags. If not, see <http://www.gnu.org/licenses/>. */
#include "RTagsClang.h"
#include "QueryJob.h"
#include "Location.h"
#include <rct/RegExp.h>
#include <regex>

class FindFileJob : public QueryJob
{
Expand All @@ -31,7 +31,7 @@ class FindFileJob : public QueryJob
virtual int execute();
private:
String mPattern;
RegExp mRegExp;
std::regex mRegex;
};

#endif
27 changes: 12 additions & 15 deletions src/Match.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,16 @@ along with RTags. If not, see <http://www.gnu.org/licenses/>. */
#define Match_h

#include <rct/String.h>
#include <rct/RegExp.h>
#include <rct/Log.h>
#include <regex>

class Match
{
public:
enum Flag {
Flag_None = 0x0,
Flag_StringMatch = 0x1,
Flag_RegExp = 0x2,
Flag_Regex = 0x2,
Flag_CaseInsensitive = 0x4
};

Expand All @@ -37,17 +37,13 @@ class Match
inline Match(const String &pattern, unsigned int flags = Flag_StringMatch)
: mFlags(flags)
{
if (flags & Flag_RegExp)
mRegExp = pattern;
if (flags & Flag_Regex)
mRegex = pattern.ref();
mPattern = pattern;
}

unsigned int flags() const { return mFlags; }

inline Match(const RegExp &regExp)
: mRegExp(regExp), mPattern(regExp.pattern()), mFlags(Flag_RegExp)
{}

inline bool match(const String &text) const
{
if (indexIn(text) != -1)
Expand All @@ -62,26 +58,27 @@ class Match
int index = -1;
if (mFlags & Flag_StringMatch)
index = text.indexOf(mPattern, 0, mFlags & Flag_CaseInsensitive ? String::CaseInsensitive : String::CaseSensitive);
if (index == -1 && mFlags & Flag_RegExp)
index = mRegExp.indexIn(text);
if (index == -1 && mFlags & Flag_Regex) {
index = Rct::indexIn(text, mRegex);
}
return index;
}
inline bool isEmpty() const
{
return !mFlags || mPattern.isEmpty();
}

inline RegExp regExp() const
inline std::regex regex() const
{
return mRegExp;
return mRegex;
}

inline String pattern() const
{
return mPattern;
}
private:
RegExp mRegExp;
std::regex mRegex;
String mPattern;
unsigned int mFlags;
};
Expand All @@ -90,8 +87,8 @@ inline Log operator<<(Log log, const Match &match)
{
String ret = "Match(flags: ";
ret += String::number(match.flags(), 16);
if (match.regExp().isValid())
ret += " rx: " + match.regExp().pattern();
if (match.flags() & Match::Flag_Regex)
ret += " rx";
if (!match.pattern().isEmpty())
ret += " pattern: " + match.pattern();
ret += ")";
Expand Down
2 changes: 1 addition & 1 deletion src/Project.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ along with RTags. If not, see <http://www.gnu.org/licenses/>. */
#include <rct/Value.h>
#include <rct/Rct.h>
#include <rct/ReadLocker.h>
#include <rct/RegExp.h>
#include <rct/Thread.h>
#include <rct/DataFile.h>
#include <regex>
#include <memory>

enum {
Expand Down
2 changes: 1 addition & 1 deletion src/Project.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ along with RTags. If not, see <http://www.gnu.org/licenses/>. */
#include <rct/FileSystemWatcher.h>
#include <rct/LinkedList.h>
#include <rct/Path.h>
#include <rct/RegExp.h>
#include <regex>
#include <rct/Timer.h>

class IndexData;
Expand Down
29 changes: 14 additions & 15 deletions src/QueryJob.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ along with RTags. If not, see <http://www.gnu.org/licenses/>. */
#include <rct/EventLoop.h>
#include "Server.h"
#include "CursorInfo.h"
#include <rct/RegExp.h>
#include <regex>
#include "QueryMessage.h"
#include "Project.h"

Expand All @@ -27,19 +27,19 @@ along with RTags. If not, see <http://www.gnu.org/licenses/>. */

QueryJob::QueryJob(const std::shared_ptr<QueryMessage> &query, unsigned int jobFlags, const std::shared_ptr<Project> &proj)
: mAborted(false), mLinesWritten(0), mQueryMessage(query), mJobFlags(jobFlags), mProject(proj), mPathFilters(0),
mPathFiltersRegExp(0), mConnection(0)
mPathFiltersRegex(0), mConnection(0)
{
assert(query);
if (query->flags() & QueryMessage::SilentQuery)
setJobFlag(QuietJob);
const List<String> &pathFilters = query->pathFilters();
if (!pathFilters.isEmpty()) {
if (query->flags() & QueryMessage::MatchRegexp) {
mPathFiltersRegExp = new List<RegExp>();
if (query->flags() & QueryMessage::MatchRegex) {
mPathFiltersRegex = new List<std::regex>();
const int size = pathFilters.size();
mPathFiltersRegExp->reserve(size);
mPathFiltersRegex->reserve(size);
for (int i=0; i<size; ++i) {
mPathFiltersRegExp->append(pathFilters.at(i));
mPathFiltersRegex->append(std::regex(pathFilters.at(i).ref()));
}
} else {
mPathFilters = new List<String>(pathFilters);
Expand All @@ -49,14 +49,14 @@ QueryJob::QueryJob(const std::shared_ptr<QueryMessage> &query, unsigned int jobF

QueryJob::QueryJob(unsigned int jobFlags, const std::shared_ptr<Project> &proj)
: mAborted(false), mLinesWritten(0), mJobFlags(jobFlags), mProject(proj), mPathFilters(0),
mPathFiltersRegExp(0), mConnection(0)
mPathFiltersRegex(0), mConnection(0)
{
}

QueryJob::~QueryJob()
{
delete mPathFilters;
delete mPathFiltersRegExp;
delete mPathFiltersRegex;
}

uint32_t QueryJob::fileFilter() const
Expand Down Expand Up @@ -185,7 +185,7 @@ bool QueryJob::write(const std::shared_ptr<CursorInfo> &ci, unsigned int ciflags

bool QueryJob::filter(const String &value) const
{
if (!mPathFilters && !mPathFiltersRegExp && !(queryFlags() & QueryMessage::FilterSystemIncludes))
if (!mPathFilters && !mPathFiltersRegex && !(queryFlags() & QueryMessage::FilterSystemIncludes))
return true;

const char *val = value.constData();
Expand All @@ -195,23 +195,22 @@ bool QueryJob::filter(const String &value) const
if (queryFlags() & QueryMessage::FilterSystemIncludes && Path::isSystem(val))
return false;

if (!mPathFilters && !mPathFiltersRegExp)
if (!mPathFilters && !mPathFiltersRegex)
return true;

assert(!mPathFilters != !mPathFiltersRegExp);
assert(!mPathFilters != !mPathFiltersRegex);
String copy;
const String &ref = (val != value.constData() ? copy : value);
if (val != value.constData())
copy = val;
if (mPathFilters)
return RTags::startsWith(*mPathFilters, ref);

assert(mPathFiltersRegExp);
assert(mPathFiltersRegex);

const int count = mPathFiltersRegExp->size();
const int count = mPathFiltersRegex->size();
for (int i=0; i<count; ++i) {
error() << "Trying regexp" << mPathFiltersRegExp->at(i).pattern() << mPathFiltersRegExp->at(i).indexIn(ref) << ref;
if (mPathFiltersRegExp->at(i).indexIn(ref) != -1)
if (std::regex_search(ref.constData(), mPathFiltersRegex->at(i)))
return true;
}
return false;
Expand Down
6 changes: 3 additions & 3 deletions src/QueryJob.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ along with RTags. If not, see <http://www.gnu.org/licenses/>. */
#include <rct/String.h>
#include <rct/EventLoop.h>
#include <rct/SignalSlot.h>
#include <rct/RegExp.h>
#include <regex>
#include "RTagsClang.h"
#include "QueryMessage.h"
#include <mutex>
Expand All @@ -45,7 +45,7 @@ class QueryJob
QueryJob(unsigned int jobFlags, const std::shared_ptr<Project> &project);
~QueryJob();

bool hasFilter() const { return mPathFilters || mPathFiltersRegExp; }
bool hasFilter() const { return mPathFilters || mPathFiltersRegex; }
List<String> pathFilters() const { return mPathFilters ? *mPathFilters : List<String>(); }
uint32_t fileFilter() const;
enum WriteFlag {
Expand Down Expand Up @@ -85,7 +85,7 @@ class QueryJob
Signal<std::function<void(const String &)> > mOutput;
std::weak_ptr<Project> mProject;
List<String> *mPathFilters;
List<RegExp> *mPathFiltersRegExp;
List<std::regex> *mPathFiltersRegex;
String mBuffer;
Connection *mConnection;
};
Expand Down
6 changes: 3 additions & 3 deletions src/QueryMessage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ unsigned int QueryMessage::keyFlags(unsigned int queryFlags)
Match QueryMessage::match() const
{
unsigned int flags = Match::Flag_StringMatch;
if (mFlags & MatchRegexp)
flags |= Match::Flag_RegExp;
if (mFlags & MatchRegex)
flags |= Match::Flag_Regex;

return Match(mQuery, flags);
}
Expand All @@ -72,7 +72,7 @@ QueryMessage::Flag QueryMessage::flagFromString(const String &string)
} else if (string == "imenu") {
return IMenu;
} else if (string == "match-regexp") {
return MatchRegexp;
return MatchRegex;
} else if (string == "match-case-insensitive") {
return MatchCaseInsensitive;
} else if (string == "find-virtuals") {
Expand Down
4 changes: 2 additions & 2 deletions src/QueryMessage.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ class QueryMessage : public RTagsMessage
ReverseSort = 0x00000020,
ElispList = 0x00000040,
IMenu = 0x00000080,
MatchRegexp = 0x00000100,
MatchRegex = 0x00000100,
MatchCaseInsensitive = 0x00000200,
FindVirtuals = 0x00000400,
Silent = 0x00000800,
Expand Down Expand Up @@ -147,7 +147,7 @@ class QueryMessage : public RTagsMessage
case RemoveFile:
case UnloadProject:
case Sources:
mFlags |= MatchRegexp;
mFlags |= MatchRegex;
break;
default:
break;
Expand Down
30 changes: 14 additions & 16 deletions src/RClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
#include <rct/QuitMessage.h>
#include <rct/Rct.h>
#include "RTagsClang.h"
#include <rct/RegExp.h>

struct Option {
const RClient::OptionType option;
Expand Down Expand Up @@ -51,7 +50,7 @@ struct Option opts[] = {
{ RClient::None, 0, 0, 0, "Project management:" },
{ RClient::Clear, "clear", 'C', no_argument, "Clear projects." },
{ RClient::Project, "project", 'w', optional_argument, "With arg, select project matching that if unique, otherwise list all projects." },
{ RClient::DeleteProject, "delete-project", 'W', required_argument, "Delete all projects matching regexp." },
{ RClient::DeleteProject, "delete-project", 'W', required_argument, "Delete all projects matching regex." },
{ RClient::UnloadProject, "unload", 'u', required_argument, "Unload project(s) matching argument." },
{ RClient::ReloadProjects, "reload-projects", 'z', no_argument, "Reload projects from projects file." },
{ RClient::JobCount, "jobcount", 'j', optional_argument, "Set or query current job count." },
Expand Down Expand Up @@ -114,7 +113,7 @@ struct Option opts[] = {
{ RClient::ElispList, "elisp-list", 'Y', no_argument, "Output elisp: (list \"one\" \"two\" ...)." },
{ RClient::Diagnostics, "diagnostics", 'G', no_argument, "Receive continual diagnostics from rdm." },
{ RClient::XmlDiagnostics, "xml-diagnostics", 'm', no_argument, "Receive continual XML formatted diagnostics from rdm." },
{ RClient::MatchRegexp, "match-regexp", 'Z', no_argument, "Treat various text patterns as regexps (-P, -i, -V)." },
{ RClient::MatchRegex, "match-regexp", 'Z', no_argument, "Treat various text patterns as regexps (-P, -i, -V)." },
{ RClient::MatchCaseInsensitive, "match-icase", 'I', no_argument, "Match case insensitively" },
{ RClient::AbsolutePath, "absolute-path", 'K', no_argument, "Print files with absolute path." },
{ RClient::SocketFile, "socket-file", 'n', required_argument, "Use this socket file (default ~/.rdm)." },
Expand Down Expand Up @@ -582,8 +581,8 @@ bool RClient::parse(int &argc, char **argv)
case MatchCaseInsensitive:
mQueryFlags |= QueryMessage::MatchCaseInsensitive;
break;
case MatchRegexp:
mQueryFlags |= QueryMessage::MatchRegexp;
case MatchRegex:
mQueryFlags |= QueryMessage::MatchRegex;
break;
case AbsolutePath:
mQueryFlags |= QueryMessage::AbsolutePath;
Expand Down Expand Up @@ -612,14 +611,14 @@ bool RClient::parse(int &argc, char **argv)
mQueryFlags |= QueryMessage::WildcardSymbolNames;
break;
case RangeFilter: {
List<RegExp::Capture> caps;
RegExp rx("^\\([0-9][0-9]*\\)-\\([0-9][0-9]*\\)$");
if (rx.indexIn(optarg, 0, &caps) != 0 || caps.size() != 3) {
std::cmatch caps;
std::regex rx("^([0-9]+)-([0-9]+*)$");
if (!Rct::contains(optarg, rx, &caps) || caps.size() != 3) {
fprintf(stderr, "Can't parse range, must be uint-uint. E.g. 1-123\n");
return false;
} else {
mMinOffset = atoi(caps.at(1).capture.constData());
mMaxOffset = atoi(caps.at(2).capture.constData());
mMinOffset = atoi(caps.str(1).c_str());
mMaxOffset = atoi(caps.str(2).c_str());
if (mMaxOffset <= mMinOffset || mMinOffset < 0) {
fprintf(stderr, "Invalid range (%d-%d), must be uint-uint. E.g. 1-123\n", mMinOffset, mMaxOffset);
return false;
Expand All @@ -631,14 +630,13 @@ bool RClient::parse(int &argc, char **argv)
break;
case PrepareCodeCompleteAt:
case CodeCompleteAt: {
const String arg = optarg;
List<RegExp::Capture> caps;
RegExp rx("^\\(.*\\):\\([0-9][0-9]*\\):\\([0-9][0-9]*\\)");
if (rx.indexIn(arg, 0, &caps) != 0 || caps.size() != 4) {
std::cmatch caps;
std::regex rx("^(.*):([0-9]+):([0-9]+)");
if (!Rct::contains(optarg, rx, &caps) || caps.size() != 4) {
fprintf(stderr, "Can't decode argument for --code-complete-at [%s]\n", optarg);
return false;
}
const Path path = Path::resolved(caps[1].capture, Path::MakeAbsolute);
const Path path = Path::resolved(caps.str(1), Path::MakeAbsolute);
if (!path.isFile()) {
fprintf(stderr, "Can't decode argument for --code-complete-at [%s]\n", optarg);
return false;
Expand All @@ -647,7 +645,7 @@ bool RClient::parse(int &argc, char **argv)
String out;
{
Serializer serializer(out);
serializer << path << atoi(caps[2].capture.constData()) << atoi(caps[3].capture.constData());
serializer << path << atoi(caps.str(2).c_str()) << atoi(caps.str(3).c_str());
}
addQuery(opt->option == CodeCompleteAt ? QueryMessage::CodeCompleteAt : QueryMessage::PrepareCodeCompleteAt, out);
break; }
Expand Down
Loading

0 comments on commit 49fb45d

Please sign in to comment.