Skip to content

Commit 06f65f7

Browse files
committed
Fix #204
Replace all duplicated spaces (included tabs) into single space. Move `testBopomofo` method from UserphraseModel into BopomofoUtil for testing.
1 parent 1679266 commit 06f65f7

4 files changed

Lines changed: 81 additions & 14 deletions

File tree

src/model/UserphraseModel.cpp

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -191,30 +191,21 @@ void UserphraseModel::exportUserphrase(std::shared_ptr<UserphraseExporter> expor
191191
emit exportCompleted(result, exporter.get()->getPath(), exported);
192192
}
193193

194-
QString UserphraseModel::checkBopomofo(const QString &bopomofo) const
195-
{
196-
QString replaceBopomofo = bopomofo;
197-
replaceBopomofo.replace(QString::fromUtf8(""),QString::fromUtf8(""));
198-
replaceBopomofo.replace(QString::fromUtf8(""),QString::fromUtf8(""));
199-
200-
return replaceBopomofo;
201-
}
202-
203194
void UserphraseModel::add(const QString &phrase, const QString &bopomofo)
204195
{
205-
QString replaceBopomofo = checkBopomofo(bopomofo);
196+
QString normalizedBopomofo = BopomofoUtil::normalize(bopomofo);
206197
auto ret = chewing_userphrase_add(
207198
ctx_.get(),
208199
phrase.toUtf8().constData(),
209-
replaceBopomofo.toUtf8().constData());
200+
normalizedBopomofo.toUtf8().constData());
210201

211202
addresult_ = ret;
212203

213204
if (ret > 0) {
214205
emit beginResetModel();
215206
userphrase_.insert(Userphrase{
216207
phrase,
217-
bopomofo
208+
normalizedBopomofo
218209
});
219210
emit endResetModel();
220211
emit addNewPhraseCompleted(userphrase_[userphrase_.size()-1]);

src/model/UserphraseModel.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include "UserphraseExporter.h"
3131
#include "UserphraseImporter.h"
3232
#include "UserphraseSet.h"
33+
#include "BopomofoUtil.hpp"
3334

3435
class UserphraseModel final: public QAbstractListModel {
3536
Q_OBJECT
@@ -71,8 +72,6 @@ public slots:
7172
void undo();
7273

7374
private:
74-
QString checkBopomofo(const QString &bopomofo) const;
75-
7675
std::unique_ptr<ChewingContext, void (*)(ChewingContext*)> ctx_;
7776
UserphraseSet userphrase_;
7877
std::vector<Userphrase> removerecord_;

src/util/BopomofoUtil.hpp

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* chewing-editor: Chewing userphrase editor
3+
* Copyright (C) 2014 Chewing Development Team
4+
5+
* This program is free software; you can redistribute it and/or modify
6+
* it under the terms of the GNU General Public License as published by
7+
* the Free Software Foundation; either version 2 of the License, or
8+
* (at your option) any later version.
9+
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU General Public License for more details.
14+
15+
* You should have received a copy of the GNU General Public License along
16+
* with this program; if not, write to the Free Software Foundation, Inc.,
17+
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*/
19+
20+
#pragma once
21+
22+
#include <QString>
23+
#include <QRegExp>
24+
25+
namespace BopomofoUtil {
26+
static QString normalize(const QString &bopomofo)
27+
{
28+
QString normalized = bopomofo;
29+
30+
// Issue #106: replace ambiguity chars
31+
normalized.replace(QString::fromUtf8(""), QString::fromUtf8(""));
32+
normalized.replace(QString::fromUtf8(""), QString::fromUtf8(""));
33+
34+
// Issue #204: reduce spaces into one space
35+
normalized.replace(QRegExp("\\s+"), " ");
36+
37+
return normalized;
38+
}
39+
};

test/testBopomofoUtil.cpp

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* chewing-editor: Chewing userphrase editor
3+
* Copyright (C) 2014 Chewing Development Team
4+
5+
* This program is free software; you can redistribute it and/or modify
6+
* it under the terms of the GNU General Public License as published by
7+
* the Free Software Foundation; either version 2 of the License, or
8+
* (at your option) any later version.
9+
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU General Public License for more details.
14+
15+
* You should have received a copy of the GNU General Public License along
16+
* with this program; if not, write to the Free Software Foundation, Inc.,
17+
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*/
19+
20+
#include "gtest/gtest.h"
21+
22+
#include "BopomofoUtil.hpp"
23+
24+
#if (defined _MSC_VER) && (_MSC_VER >= 1600)
25+
# pragma execution_character_set("utf-8")
26+
#endif
27+
28+
class TestBopomofoUtil : public ::testing::Test {
29+
protected:
30+
TestBopomofoUtil() = default;
31+
virtual ~TestBopomofoUtil() = default;
32+
};
33+
34+
TEST_F(TestBopomofoUtil, ReduceDuplicatedSpacesInsideBopomofo)
35+
{
36+
ASSERT_EQ(0, QString::compare("ㄘㄜˋ ㄕˋ", BopomofoUtil::normalize("ㄘㄜˋ ㄕˋ")));
37+
ASSERT_EQ(0, QString::compare("ㄘㄜˋ ㄕˋ", BopomofoUtil::normalize("ㄘㄜˋ ㄕˋ")));
38+
}

0 commit comments

Comments
 (0)