From 06f65f709c12f21be9adb6bce8455ac1ec0ffb08 Mon Sep 17 00:00:00 2001 From: David Kuo Date: Mon, 10 Apr 2017 11:05:51 +0800 Subject: [PATCH] Fix #204 Replace all duplicated spaces (included tabs) into single space. Move `testBopomofo` method from UserphraseModel into BopomofoUtil for testing. --- src/model/UserphraseModel.cpp | 15 +++----------- src/model/UserphraseModel.h | 3 +-- src/util/BopomofoUtil.hpp | 39 +++++++++++++++++++++++++++++++++++ test/testBopomofoUtil.cpp | 38 ++++++++++++++++++++++++++++++++++ 4 files changed, 81 insertions(+), 14 deletions(-) create mode 100644 src/util/BopomofoUtil.hpp create mode 100644 test/testBopomofoUtil.cpp diff --git a/src/model/UserphraseModel.cpp b/src/model/UserphraseModel.cpp index 995abeb..6ea5671 100644 --- a/src/model/UserphraseModel.cpp +++ b/src/model/UserphraseModel.cpp @@ -191,22 +191,13 @@ void UserphraseModel::exportUserphrase(std::shared_ptr expor emit exportCompleted(result, exporter.get()->getPath(), exported); } -QString UserphraseModel::checkBopomofo(const QString &bopomofo) const -{ - QString replaceBopomofo = bopomofo; - replaceBopomofo.replace(QString::fromUtf8("一"),QString::fromUtf8("ㄧ")); - replaceBopomofo.replace(QString::fromUtf8("丫"),QString::fromUtf8("ㄚ")); - - return replaceBopomofo; -} - void UserphraseModel::add(const QString &phrase, const QString &bopomofo) { - QString replaceBopomofo = checkBopomofo(bopomofo); + QString normalizedBopomofo = BopomofoUtil::normalize(bopomofo); auto ret = chewing_userphrase_add( ctx_.get(), phrase.toUtf8().constData(), - replaceBopomofo.toUtf8().constData()); + normalizedBopomofo.toUtf8().constData()); addresult_ = ret; @@ -214,7 +205,7 @@ void UserphraseModel::add(const QString &phrase, const QString &bopomofo) emit beginResetModel(); userphrase_.insert(Userphrase{ phrase, - bopomofo + normalizedBopomofo }); emit endResetModel(); emit addNewPhraseCompleted(userphrase_[userphrase_.size()-1]); diff --git a/src/model/UserphraseModel.h b/src/model/UserphraseModel.h index 92140bf..837b15c 100644 --- a/src/model/UserphraseModel.h +++ b/src/model/UserphraseModel.h @@ -30,6 +30,7 @@ #include "UserphraseExporter.h" #include "UserphraseImporter.h" #include "UserphraseSet.h" +#include "BopomofoUtil.hpp" class UserphraseModel final: public QAbstractListModel { Q_OBJECT @@ -71,8 +72,6 @@ public slots: void undo(); private: - QString checkBopomofo(const QString &bopomofo) const; - std::unique_ptr ctx_; UserphraseSet userphrase_; std::vector removerecord_; diff --git a/src/util/BopomofoUtil.hpp b/src/util/BopomofoUtil.hpp new file mode 100644 index 0000000..a336766 --- /dev/null +++ b/src/util/BopomofoUtil.hpp @@ -0,0 +1,39 @@ +/* + * chewing-editor: Chewing userphrase editor + * Copyright (C) 2014 Chewing Development Team + + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 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 General Public License for more details. + + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#pragma once + +#include +#include + +namespace BopomofoUtil { + static QString normalize(const QString &bopomofo) + { + QString normalized = bopomofo; + + // Issue #106: replace ambiguity chars + normalized.replace(QString::fromUtf8("一"), QString::fromUtf8("ㄧ")); + normalized.replace(QString::fromUtf8("丫"), QString::fromUtf8("ㄚ")); + + // Issue #204: reduce spaces into one space + normalized.replace(QRegExp("\\s+"), " "); + + return normalized; + } +}; diff --git a/test/testBopomofoUtil.cpp b/test/testBopomofoUtil.cpp new file mode 100644 index 0000000..0ffa0ad --- /dev/null +++ b/test/testBopomofoUtil.cpp @@ -0,0 +1,38 @@ +/* + * chewing-editor: Chewing userphrase editor + * Copyright (C) 2014 Chewing Development Team + + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 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 General Public License for more details. + + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#include "gtest/gtest.h" + +#include "BopomofoUtil.hpp" + +#if (defined _MSC_VER) && (_MSC_VER >= 1600) +# pragma execution_character_set("utf-8") +#endif + +class TestBopomofoUtil : public ::testing::Test { +protected: + TestBopomofoUtil() = default; + virtual ~TestBopomofoUtil() = default; +}; + +TEST_F(TestBopomofoUtil, ReduceDuplicatedSpacesInsideBopomofo) +{ + ASSERT_EQ(0, QString::compare("ㄘㄜˋ ㄕˋ", BopomofoUtil::normalize("ㄘㄜˋ ㄕˋ"))); + ASSERT_EQ(0, QString::compare("ㄘㄜˋ ㄕˋ", BopomofoUtil::normalize("ㄘㄜˋ ㄕˋ"))); +}