Skip to content

Commit

Permalink
libcore: Continuing removal of Qt dependencies
Browse files Browse the repository at this point in the history
Added C++ wrapper for iRegExp.
  • Loading branch information
skyjake committed Sep 1, 2019
1 parent 7b1e43d commit 6716e7d
Show file tree
Hide file tree
Showing 20 changed files with 252 additions and 93 deletions.
5 changes: 5 additions & 0 deletions doomsday/libs/core/include/de/data/cstring.h
Expand Up @@ -51,6 +51,11 @@ class DE_PUBLIC CString
updateEnd();
return _range;
}
operator iRangecc() const
{
updateEnd();
return iRangecc{begin(), end()};
}
dsize size() const
{
updateEnd();
Expand Down
1 change: 1 addition & 0 deletions doomsday/libs/core/include/de/data/date.h
Expand Up @@ -45,6 +45,7 @@ class DE_PUBLIC Date : public LogEntry::Arg::Base
int year() const;
int month() const;
int dayOfMonth() const;
int dayOfYear() const;
int hours() const;
int minutes() const;
int seconds() const;
Expand Down
1 change: 1 addition & 0 deletions doomsday/libs/core/include/de/data/path.h
Expand Up @@ -84,6 +84,7 @@ class DE_PUBLIC Path : public ISerializable, public LogEntry::Arg::Base
// String toString() const;

Rangecc toRange() const { return range; }
String toString() const { return String(range); }

/**
* Determines the length of the segment in characters.
Expand Down
18 changes: 14 additions & 4 deletions doomsday/libs/core/include/de/data/regexp.h
Expand Up @@ -24,22 +24,32 @@

namespace de {

class RegExpMatch
class DE_PUBLIC RegExpMatch
{
public:
iRegExpMatch match;

RegExpMatch();

private:
iRegExpMatch _match;
const char *begin() const;
const char *end() const;

void clear();
String captured(int index = 0) const;
};

/**
* Perl-compatible regular expression.
*/
class RegExp
class DE_PUBLIC RegExp
{
public:
RegExp(const String &expression = {}, String::CaseSensitivity cs = String::CaseSensitive);
~RegExp();

bool match(const String &subject, RegExpMatch &match) const;
bool hasMatch(const String &subject) const;
bool exactMatch(const String &subject) const;

private:
iRegExp *_d;
Expand Down
11 changes: 8 additions & 3 deletions doomsday/libs/core/include/de/data/string.h
Expand Up @@ -199,6 +199,7 @@ class DE_PUBLIC String : public IByteArray

inline operator const char *() const { return c_str(); }
inline operator const iString *() const { return &_str; }
inline operator iString *() { return &_str; }
inline operator std::string() const { return toStdString(); }

inline std::string toStdString() const
Expand Down Expand Up @@ -280,6 +281,7 @@ class DE_PUBLIC String : public IByteArray
void insert(BytePos pos, const String &str);
String &replace(Char before, Char after);
String &replace(const CString &before, const CString &after);
String &replace(const RegExp &before, const CString &after);

/**
* Does a path concatenation on this string and the argument. Note that if
Expand Down Expand Up @@ -391,7 +393,10 @@ class DE_PUBLIC String : public IByteArray
inline bool operator>=(const char *cstr) const { return compare(cstr) >= 0; }

inline int compare(const char *cstr) const { return cmp_String(&_str, cstr); }
inline int compare(const String &s) const { return cmpString_String(&_str, &s._str); }
inline int compare(const String &s, CaseSensitivity cs = CaseSensitive) const {
return cmpStringSc_String(
&_str, &s._str, cs == CaseSensitive ? &iCaseSensitive : &iCaseInsensitive);
}

/**
* Compare two strings (case sensitive).
Expand Down Expand Up @@ -690,8 +695,8 @@ inline String operator+(Char ch, const String &s) {
return String(1, ch) + s;
}

inline String operator+(const char *cStr, const String &s) {
return String(cStr) + s;
inline String operator+(const char *left, const String &right) {
return String(left) + right;
}

inline const char *operator+(const char *cStr, const String::BytePos &offset) {
Expand Down
7 changes: 7 additions & 0 deletions doomsday/libs/core/include/de/data/time.h
Expand Up @@ -25,6 +25,7 @@
#include "../ISerializable"

#include <chrono>
#include <c_plus/time.h>

namespace de {

Expand Down Expand Up @@ -169,8 +170,12 @@ class DE_PUBLIC Time : public ISerializable

Time(Time &&moved);

Time(int year, int month, int day, int hour, int minute, int second);

Time(const TimePoint &tp);

Time(iTime time);

/**
* Construct a time relative to the shared high performance timer.
*
Expand Down Expand Up @@ -274,6 +279,8 @@ class DE_PUBLIC Time : public ISerializable
*/
String asText(Format format = ISOFormat) const;

String asText(const char *format) const;

/**
* Parses a text string into a Time.
*
Expand Down
3 changes: 1 addition & 2 deletions doomsday/libs/core/include/de/filesys/feed.h
Expand Up @@ -21,8 +21,7 @@
#define LIBCORE_FEED_H

#include "../libcore.h"

#include <list>
#include "../List"

namespace de {

Expand Down
5 changes: 0 additions & 5 deletions doomsday/libs/core/include/de/scriptsys/catchstatement.h
Expand Up @@ -24,8 +24,6 @@
#include "../ArrayExpression"
#include "../Compound"

#include <QFlags>

namespace de {

/**
Expand All @@ -41,7 +39,6 @@ class CatchStatement : public Statement
/// The final catch compound in a sequence of catch compounds.
FinalCompound = 0x1
};
Q_DECLARE_FLAGS(Flags, Flag)

/// Flags.
Flags flags;
Expand Down Expand Up @@ -83,8 +80,6 @@ class CatchStatement : public Statement
Compound _compound;
};

Q_DECLARE_OPERATORS_FOR_FLAGS(CatchStatement::Flags)

} // namespace de

#endif /* LIBCORE_CATCHSTATEMENT_H */
5 changes: 5 additions & 0 deletions doomsday/libs/core/src/data/date.cpp
Expand Up @@ -62,6 +62,11 @@ int Date::dayOfMonth() const
return d->date.day;
}

int Date::dayOfYear() const
{
return d->date.dayOfYear;
}

int Date::hours() const
{
return d->date.hour;
Expand Down
85 changes: 85 additions & 0 deletions doomsday/libs/core/src/data/regexp.cpp
@@ -0,0 +1,85 @@
/** @file regexp.cpp
*
* @authors Copyright (c) 2018 Jaakko Keränen <jaakko.keranen@iki.fi>
*
* @par License
* LGPL: http://www.gnu.org/licenses/lgpl.html
*
* <small>This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 3 of the License, or (at your
* option) any later version. This program is distributed in the hope that it
* will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
* General Public License for more details. You should have received a copy of
* the GNU Lesser General Public License along with this program; if not, see:
* http://www.gnu.org/licenses</small>
*/

#include "de/RegExp"

#include <c_plus/object.h>

namespace de {

RegExpMatch::RegExpMatch()
{
clear();
}

const char *RegExpMatch::begin() const
{
return match.subject + match.range.start;
}

const char *RegExpMatch::end() const
{
return match.subject + match.range.end;
}

void RegExpMatch::clear()
{
zap(match);
}

String RegExpMatch::captured(int index) const
{
iString *cap = captured_RegExpMatch(&match, index);
String str(cap);
delete_String(cap);
return str;
}

RegExp::RegExp(const String &expression, String::CaseSensitivity cs)
{
_d = new_RegExp(expression, cs == String::CaseSensitive? caseSensitive_RegExpOption
: caseInsensitive_RegExpOption);
}

RegExp::~RegExp()
{
iRelease(_d);
}

bool RegExp::exactMatch(const String &subject) const
{
iRegExpMatch match;
if (matchString_RegExp(_d, subject, &match))
{
return match.range.start == 0 && match.range.end == subject.sizei();
}
return false;
}

bool RegExp::match(const String &subject, RegExpMatch &match) const
{
return matchString_RegExp(_d, subject, &match.match);
}

bool RegExp::hasMatch(const String &subject) const
{
iRegExpMatch match;
return matchString_RegExp(_d, subject, &match);
}

} // namespace de
27 changes: 22 additions & 5 deletions doomsday/libs/core/src/data/string.cpp
Expand Up @@ -294,18 +294,35 @@ String &String::replace(Char before, Char after)
String &String::replace(const CString &before, const CString &after)
{
const String oldTerm{before};
const iRangecc newTerm{after.begin(), after.end()};
const iRangecc newTerm = after;
iRangecc remaining(*this);
String result;
dsize found;
while ((found = CString(remaining).indexOf(oldTerm)) != npos)
{
const iRangecc prefix{remaining.start, remaining.start + found};
appendRange_String(&result._str, &prefix);
appendRange_String(&result._str, &newTerm);
appendRange_String(result, &prefix);
appendRange_String(result, &newTerm);
remaining.start += found + before.size();
}
appendRange_String(&result._str, &remaining);
appendRange_String(result, &remaining);
return *this = result;
}

String &String::replace(const RegExp &before, const CString &after)
{
const iRangecc newTerm = after;
iRangecc remaining(*this);
String result;
RegExpMatch found;
while (before.match(*this, found))
{
const iRangecc prefix{remaining.start, found.begin()};
appendRange_String(result, &prefix);
appendRange_String(result, &newTerm);
remaining.start = found.end();
}
appendRange_String(result, &remaining);
return *this = result;
}

Expand Down Expand Up @@ -526,7 +543,7 @@ bool String::containsWord(const String &word) const
{
return false;
}
return RegExp(stringf("\\b%s\\b", word.c_str())).match(*this).hasMatch();
return RegExp(stringf("\\b%s\\b", word.c_str())).hasMatch(*this);
}

dint String::compareWithCase(const String &other) const
Expand Down
7 changes: 3 additions & 4 deletions doomsday/libs/core/src/data/stringpool.cpp
Expand Up @@ -72,10 +72,10 @@ class CaselessString : public ISerializable
return _str;
}
bool operator < (CaselessString const &other) const {
return _str.compare(other, String::CaseInsensitive) < 0;
return _str.compareWithoutCase(other) < 0;
}
bool operator == (CaselessString const &other) const {
return _str.compare(other, String::CaseInsensitive) == 0;
return _str.compareWithoutCase(other) == 0;
}
InternalId id() const {
return _id;
Expand Down Expand Up @@ -531,8 +531,7 @@ void StringPool::print() const
duint count = 0;
forAll([this, &count] (Id id)
{
QByteArray strUtf8 = string(id).toUtf8();
fprintf(stderr, "%*u %5u %s\n", padding, count++, id, strUtf8.constData());
fprintf(stderr, "%*u %5u %s\n", padding, count++, id, string(id).c_str());
return LoopContinue;
});
fprintf(stderr, " There is %u %s in the pool.\n", duint( size() ),
Expand Down

0 comments on commit 6716e7d

Please sign in to comment.