Skip to content

Commit

Permalink
Cleanup: Use DE_STR, String::take, STL containers, CString, etc.
Browse files Browse the repository at this point in the history
  • Loading branch information
skyjake committed Sep 1, 2019
1 parent 6716e7d commit 8f64466
Show file tree
Hide file tree
Showing 36 changed files with 289 additions and 213 deletions.
19 changes: 12 additions & 7 deletions doomsday/libs/core/include/de/data/block.h
Expand Up @@ -94,12 +94,15 @@ class DE_PUBLIC Block
const Byte *cdata() const;
inline const Byte *constData() const { return cdata(); }

bool empty() const { return size() == 0; }
bool isEmpty() const { return size() == 0; }

Block &append(Byte b);
Block &append(const char *str, int len);
Block &prepend(const Block &);
void remove(size_t pos, size_t len = 1);

operator const iBlock *() const { return &_block; }
operator const iBlock *() const { return &_block; }
inline explicit operator bool() const { return !isEmpty_Block(&_block); }

Block &operator += (const char *nullTerminatedCStr);
Expand All @@ -112,16 +115,18 @@ class DE_PUBLIC Block

Block mid(size_t pos, size_t len = iInvalidSize) const;

Block operator+(const Block &other) const;

/// Appends a block after this one.
Block &operator += (const Block &other);
Block &operator+=(const Block &other);

/// Appends a byte array after this one.
Block &operator += (const IByteArray &byteArray);
Block &operator+=(const IByteArray &byteArray);

/// Copies the contents of another block.
Block &operator = (const Block &other);
Block &operator=(const Block &other);

Block &operator = (const IByteArray &byteArray);
Block &operator=(const IByteArray &byteArray);

Block compressed(int level = -1) const;
Block decompressed() const;
Expand Down Expand Up @@ -193,9 +198,9 @@ class DE_PUBLIC Block
*
* @return Reference to the Writer.
*/
void operator >> (Writer &to) const;
void operator>>(Writer &to) const;

void operator << (Reader &from);
void operator<<(Reader &from);

public:
// Iterators:
Expand Down
35 changes: 24 additions & 11 deletions doomsday/libs/core/include/de/data/cstring.h
Expand Up @@ -44,33 +44,38 @@ class DE_PUBLIC CString
{
if (!_range.end) _range.end = _range.start + strlen(_range.start);
}
operator String() const { return {_range.start, _range.end}; }
String toString() const { return String(_range.start, _range.end); }
operator Rangecc() const
//inline operator String() const { return {_range.start, _range.end}; }
inline String toString() const { return String(_range.start, _range.end); }
inline operator Rangecc() const
{
updateEnd();
return _range;
}
operator iRangecc() const
inline operator iRangecc() const
{
updateEnd();
return iRangecc{begin(), end()};
}
dsize size() const
inline dsize size() const
{
updateEnd();
return _range.size();
}
bool isEmpty() const { return size() == 0; }
bool empty() const { return size() == 0; }
inline bool isEmpty() const { return size() == 0; }
inline bool empty() const { return size() == 0; }
bool contains(char ch) const;
bool endsWith(const CString &suffix, Sensitivity cs = CaseSensitive) const;
dsize indexOf(char ch, size_t from = 0) const;
dsize indexOf(const char *cStr, size_t from = 0) const;
CString substr(size_t start, size_t count = npos) const;
const char *begin() const { return _range.start; }
const char *end() const { updateEnd(); return _range.end; }
int compare(const CString &other, const iStringComparison *sc = &iCaseSensitive) const;
int compare(const char *cStr, const iStringComparison *sc = &iCaseSensitive) const;
inline const char *begin() const { return _range.start; }
inline const char *end() const { updateEnd(); return _range.end; }
int compare(const CString &other, Sensitivity cs = CaseSensitive) const;
int compare(const char *cStr, Sensitivity cs = CaseSensitive) const;
inline bool operator==(const char *cStr) const { return compare(cStr) == 0; }
Char first() const { return *mb_iterator(begin()); }
String lower() const;
String upper() const;

static size_t npos;

Expand All @@ -82,6 +87,14 @@ String operator+(const char *cStr, const CString &str) {
return String(cStr) + str;
}

String operator+(const CString &str, const char *cStr) {
return String(str) + cStr;
}

String operator/(const CString &str, const char *cStr) {
return String(str).concatenatePath(cStr);
}

} // namespace de

#endif // LIBCORE_CSTRING_H
14 changes: 11 additions & 3 deletions doomsday/libs/core/include/de/data/list.h
Expand Up @@ -49,16 +49,19 @@ class List : public std::vector<T>

using Base::begin;
using Base::end;
using Base::push_back;
void pop_front() { removeFirst(); } // slow...
void push_front(const T &v) { prepend(v); } // slow...

// Qt style methods:

int size() const { return int(Base::size()); }
void clear() { Base::clear(); }
bool isEmpty() const { return Base::size() == 0; }
void append(const T &s) { Base::push_back(s); }
void prepend(const T &s) { Base::push_front(s); }
void insert(int pos, const T &value) { Base::insert(Base::begin() + pos, value); }
void append(const T &v) { push_back(v); }
void append(const List &list) { for (const T &v : list) push_back(v); }
void prepend(const T &v) { Base::insert(begin(), v); }
void insert(int pos, const T &value) { Base::insert(begin() + pos, value); }
void insert(const const_iterator &i, const T &value) { Base::insert(i, value); }
const T &operator[](int pos) const { return Base::at(pos); }
T & operator[](int pos) { return Base::operator[](pos); }
Expand All @@ -74,6 +77,11 @@ class List : public std::vector<T>
void removeLast() { Base::erase(Base::begin() + size() - 1); }
void removeAt(int pos) { Base::erase(Base::begin() + pos); }
void removeAll(const T &v) { Base::erase(std::remove(begin(), end(), v), end()); }
void removeOne(const T &v)
{
auto found = std::find(begin(), end(), v);
if (found != end()) Base::erase(found);
}
List & operator=(const List &other) { Base::operator=(other); return *this; }
List & operator=(List &&other) { Base::operator=(other); return *this; }

Expand Down
9 changes: 9 additions & 0 deletions doomsday/libs/core/include/de/data/map.h
Expand Up @@ -44,6 +44,15 @@ class Map : public std::map<Key, Value>
bool contains(const Key &key) const { return Base::find(key) != Base::end(); }
const_iterator constFind(const Key &key) const { return Base::find(key); }

inline Value take(const Key &key)
{
DE_ASSERT(contains(key));
auto found = Base::find(key);
Value v = found->second;
Base::erase(found);
return v;
}

void deleteAll()
{
for (auto i : *this) { delete i->second; }
Expand Down
4 changes: 3 additions & 1 deletion doomsday/libs/core/include/de/data/path.h
Expand Up @@ -121,7 +121,7 @@ class DE_PUBLIC Path : public ISerializable, public LogEntry::Arg::Base

bool operator==(const char *text) const
{
return range.compare(text, &iCaseInsensitive) == 0;
return range.compare(text, CaseInsensitive) == 0;
}

bool operator!=(const char *text) const { return !(*this == text); }
Expand Down Expand Up @@ -256,6 +256,8 @@ class DE_PUBLIC Path : public ISerializable, public LogEntry::Arg::Base
*/
Path operator/(const String &other) const;

Path operator/(const CString &other) const;

Path operator/(char const *otherNullTerminatedUtf8) const;

/**
Expand Down
2 changes: 1 addition & 1 deletion doomsday/libs/core/include/de/data/regexp.h
Expand Up @@ -44,7 +44,7 @@ class DE_PUBLIC RegExpMatch
class DE_PUBLIC RegExp
{
public:
RegExp(const String &expression = {}, String::CaseSensitivity cs = String::CaseSensitive);
RegExp(const String &expression = {}, Sensitivity cs = CaseSensitive);
~RegExp();

bool match(const String &subject, RegExpMatch &match) const;
Expand Down
41 changes: 27 additions & 14 deletions doomsday/libs/core/include/de/data/string.h
Expand Up @@ -33,6 +33,19 @@ template <typename T> struct Range;
class RegExp;
class CString;

enum CaseSensitivity { CaseInsensitive, CaseSensitive };

struct DE_PUBLIC Sensitivity
{
CaseSensitivity cs;

inline Sensitivity(CaseSensitivity cs = CaseSensitive) : cs(cs) {}
inline operator CaseSensitivity() const { return cs; }
inline operator const iStringComparison *() const {
return cs == CaseSensitive ? &iCaseSensitive : &iCaseInsensitive;
}
};

/**
* The String class extends the QString class with Block conversion and
* other convenience methods.
Expand Down Expand Up @@ -78,7 +91,6 @@ class DE_PUBLIC String : public IByteArray
};

typedef List<const IPatternArg *> PatternArgs;
enum CaseSensitivity { CaseInsensitive, CaseSensitive };

public:
using SingleChar = char[2];
Expand Down Expand Up @@ -220,26 +232,25 @@ class DE_PUBLIC String : public IByteArray
bool contains(const char *cStr) const;
int count(char ch) const;

bool beginsWith(const String &s, CaseSensitivity cs = CaseSensitive) const
bool beginsWith(const String &s, Sensitivity cs = CaseSensitive) const
{
return startsWithSc_String(&_str, s, cs == CaseSensitive? &iCaseSensitive : &iCaseInsensitive);
return startsWithSc_String(&_str, s, cs);
}
bool beginsWith(char ch, CaseSensitivity cs = CaseSensitive) const
bool beginsWith(char ch, Sensitivity cs = CaseSensitive) const
{
return beginsWith(SingleChar{ch, 0}, cs);
}
bool beginsWith(const char *cstr, CaseSensitivity cs = CaseSensitive) const
bool beginsWith(const char *cstr, Sensitivity cs = CaseSensitive) const
{
return startsWithSc_String(&_str, cstr, cs == CaseSensitive? &iCaseSensitive : &iCaseInsensitive);
return startsWithSc_String(&_str, cstr, cs);
}
bool endsWith(char ch, CaseSensitivity cs = CaseSensitive) const
bool endsWith(char ch, Sensitivity cs = CaseSensitive) const
{
return endsWith(SingleChar{ch, 0}, cs);
}
bool endsWith(const char *cstr, CaseSensitivity cs = CaseSensitive) const
bool endsWith(const char *cstr, Sensitivity cs = CaseSensitive) const
{
return endsWithSc_String(
&_str, cstr, cs == CaseSensitive ? &iCaseSensitive : &iCaseInsensitive);
return endsWithSc_String(&_str, cstr, cs);
}

inline char operator[](BytePos pos) const { return c_str()[pos.index]; }
Expand Down Expand Up @@ -352,10 +363,10 @@ class DE_PUBLIC String : public IByteArray
String upperFirstChar() const;

/// Extracts the base name from the string (includes extension).
String fileName(Char dirChar = '/') const;
CString fileName(Char dirChar = '/') const;

/// Extracts the base name from the string (does not include extension).
String fileNameWithoutExtension() const;
CString fileNameWithoutExtension() const;

/**
* Extracts the file name extension from a path. A valid extension
Expand All @@ -367,10 +378,10 @@ class DE_PUBLIC String : public IByteArray
* @return The extension, including the period in the beginning.
* An empty string is returned if the string contains no period.
*/
String fileNameExtension() const;
CString fileNameExtension() const;

/// Extracts the path of the string.
String fileNamePath(Char dirChar = '/') const;
CString fileNamePath(Char dirChar = '/') const;

/// Extracts everything but the extension from string.
String fileNameAndPathWithoutExtension(Char dirChar = '/') const;
Expand Down Expand Up @@ -579,6 +590,8 @@ class DE_PUBLIC String : public IByteArray
const_reverse_iterator crend() const { return rend(); }

public:
static String take(iString *str);

/**
* Builds a String out of an array of bytes that contains a UTF-8 string.
*/
Expand Down
2 changes: 1 addition & 1 deletion doomsday/libs/core/include/de/filesys/file.h
Expand Up @@ -280,7 +280,7 @@ class DE_PUBLIC File : public filesys::Node, public IIOStream, public IObject
/**
* Returns the mode of the file.
*/
Flags const &mode() const;
Flags mode() const;

/**
* Changes the mode of the file. For example, using
Expand Down
2 changes: 1 addition & 1 deletion doomsday/libs/core/include/de/filesys/folder.h
Expand Up @@ -233,7 +233,7 @@ class DE_PUBLIC Folder : public File
* @return The removed file object. Ownership of the object is given to
* the caller.
*/
File *remove(String name);
File *remove(const String &name);

File *remove(char const *nameUtf8);

Expand Down
6 changes: 1 addition & 5 deletions doomsday/libs/core/include/de/widgets/animationrule.h
Expand Up @@ -25,8 +25,6 @@
#include "../Animation"
#include "../Time"

#include <QFlags>

namespace de {

/**
Expand Down Expand Up @@ -67,7 +65,7 @@ class DE_PUBLIC AnimationRule : public Rule, DE_OBSERVES(Clock, TimeChange)
RestartWhenTargetChanges = 0x2,
DontAnimateFromZero = 0x4,
};
Q_DECLARE_FLAGS(Behaviors, Behavior)
using Behaviors = Flags;

/**
* Sets the behavior for updating the animation target. The default is Singleshot,
Expand Down Expand Up @@ -116,8 +114,6 @@ class DE_PUBLIC AnimationRule : public Rule, DE_OBSERVES(Clock, TimeChange)
Behaviors _behavior;
};

Q_DECLARE_OPERATORS_FOR_FLAGS(AnimationRule::Behaviors)

} // namespace de

#endif // LIBCORE_ANIMATIONRULE_H
2 changes: 1 addition & 1 deletion doomsday/libs/core/src/core/app.cpp
Expand Up @@ -456,7 +456,7 @@ App::App(const StringList &args)
// line argument. The working directory needs to be changed.
if (d->cmdLine.count() >= 2 && d->cmdLine.at(1).beginsWith("-psn"))
{
DirectoryFeed::changeWorkingDir(d->cmdLine.at(0).fileNamePath() + "/..");
DirectoryFeed::changeWorkingDir(d->cmdLine.at(0).fileNamePath() / "..");
}
}
#endif
Expand Down
7 changes: 7 additions & 0 deletions doomsday/libs/core/src/data/block.cpp
Expand Up @@ -169,6 +169,13 @@ Block Block::mid(size_t pos, size_t len) const
return i;
}

Block Block::operator+(const Block &other) const
{
Block cat(*this);
cat += other;
return cat;
}

Block::Byte *Block::data()
{
return reinterpret_cast<Byte *>(data_Block(&_block));
Expand Down

0 comments on commit 8f64466

Please sign in to comment.