Skip to content

Commit e870893

Browse files
committed
add ChessEngine::tokenize method
The `ChessEngine::tokenize` method that will replace the existing `QStringRef` based tokenizer methods. The method returns a pair with the token in the first element of the pair and the rest of the string in the second element of the pair.
1 parent 480e275 commit e870893

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

projects/lib/src/chessengine.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,18 @@
2626

2727
int ChessEngine::s_count = 0;
2828

29+
std::pair<QStringView, QStringView> ChessEngine::tokenize(QStringView sv)
30+
{
31+
if (sv.isEmpty())
32+
return std::make_pair(QStringView(), QStringView());
33+
34+
auto index = sv.indexOf(QChar(' '));
35+
if (index == -1)
36+
return std::make_pair(sv, QStringView());
37+
38+
return std::make_pair(sv.mid(0, index), sv.mid(index, sv.size() - 1).trimmed());
39+
}
40+
2941
QStringRef ChessEngine::nextToken(const QStringRef& previous, bool untilEnd)
3042
{
3143
const QString* str = previous.string();

projects/lib/src/chessengine.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,20 @@ class LIB_EXPORT ChessEngine : public ChessPlayer
131131
virtual void kill();
132132

133133
protected:
134+
/*!
135+
* Reads the first whitespace-delimited token from a string
136+
* and returns a pair with the token in the first element
137+
* and the rest of the string in the second element of the pair.
138+
*
139+
* \note Leading and trailing whitespace need to be removed from
140+
* \a sv before calling this function.
141+
*
142+
* If \a sv doesn't contain any whitespace, the whole string
143+
* is returned in the first element of the pair.
144+
*
145+
* If \a sv is empty, a pair of empty QStringView is returned.
146+
*/
147+
static std::pair<QStringView, QStringView> tokenize(QStringView sv);
134148
/*!
135149
* Reads the first whitespace-delimited token from a string
136150
* and returns a QStringRef reference to the token.

0 commit comments

Comments
 (0)