Skip to content

Commit

Permalink
Change the interface of PosListProviderInterface::GetPosList.
Browse files Browse the repository at this point in the history
#codehealth

PiperOrigin-RevId: 585548213
  • Loading branch information
hiroyuki-komatsu committed Nov 27, 2023
1 parent 8ddd19d commit 4b0836a
Show file tree
Hide file tree
Showing 11 changed files with 28 additions and 27 deletions.
1 change: 1 addition & 0 deletions src/data_manager/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,7 @@ mozc_cc_library(
"//base:logging",
"//base/container:serialized_string_array",
"//dictionary:user_pos_interface",
"@com_google_absl//absl/strings",
],
)

Expand Down
13 changes: 9 additions & 4 deletions src/data_manager/pos_list_provider.cc
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
#include "base/embedded_file.h"
#include "base/logging.h"

#include "absl/strings/string_view.h"

namespace mozc {
namespace {

Expand All @@ -49,13 +51,16 @@ namespace {

} // namespace

void PosListProvider::GetPosList(std::vector<std::string> *pos_list) const {
std::vector<std::string> PosListProvider::GetPosList() const {

SerializedStringArray array;
CHECK(array.Init(LoadEmbeddedFile(kPosArray)));
pos_list->resize(array.size());
for (size_t i = 0; i < array.size(); ++i) {
(*pos_list)[i].assign(array[i].begin(), array[i].end());

std::vector<std::string> pos_list(array.size());
for (absl::string_view pos : array) {
pos_list.push_back({pos.data(), pos.size()});
}
return pos_list;
}

} // namespace mozc
2 changes: 1 addition & 1 deletion src/data_manager/pos_list_provider.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ namespace mozc {

class PosListProvider : public PosListProviderInterface {
public:
void GetPosList(std::vector<std::string> *pos_list) const override;
std::vector<std::string> GetPosList() const override;
};

} // namespace mozc
Expand Down
4 changes: 1 addition & 3 deletions src/dictionary/user_dictionary.cc
Original file line number Diff line number Diff line change
Expand Up @@ -584,9 +584,7 @@ bool UserDictionary::Load(
}

std::vector<std::string> UserDictionary::GetPosList() const {
std::vector<std::string> pos_list;
user_pos_->GetPosList(&pos_list);
return pos_list;
return user_pos_->GetPosList();
}

void UserDictionary::SetUserDictionaryName(const absl::string_view filename) {
Expand Down
4 changes: 3 additions & 1 deletion src/dictionary/user_dictionary_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,9 @@ class UserPosMock : public UserPosInterface {
}
}

void GetPosList(std::vector<std::string> *pos_list) const override {}
std::vector<std::string> GetPosList() const override {
return {};
}

bool GetPosIds(absl::string_view pos, uint16_t *id) const override {
return false;
Expand Down
7 changes: 4 additions & 3 deletions src/dictionary/user_pos.cc
Original file line number Diff line number Diff line change
Expand Up @@ -57,16 +57,17 @@ UserPos::UserPos(absl::string_view token_array_data,
string_array_.Set(string_array_data);
}

void UserPos::GetPosList(std::vector<std::string> *pos_list) const {
pos_list->clear();
std::vector<std::string> UserPos::GetPosList() const {
std::vector<std::string> pos_list;
absl::flat_hash_set<uint16_t> seen;
for (auto iter = begin(); iter != end(); ++iter) {
if (!seen.insert(iter.pos_index()).second) {
continue;
}
const absl::string_view pos = string_array_[iter.pos_index()];
pos_list->emplace_back(pos.data(), pos.size());
pos_list.emplace_back(pos.data(), pos.size());
}
return pos_list;
}

bool UserPos::IsValidPos(absl::string_view pos) const {
Expand Down
2 changes: 1 addition & 1 deletion src/dictionary/user_pos.h
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ class UserPos : public UserPosInterface {
UserPos &operator=(const UserPos &) = delete;

// Implementation of UserPosInterface.
void GetPosList(std::vector<std::string> *pos_list) const override;
std::vector<std::string> GetPosList() const override;
bool IsValidPos(absl::string_view pos) const override;
bool GetPosIds(absl::string_view pos, uint16_t *id) const override;
bool GetTokens(absl::string_view key, absl::string_view value,
Expand Down
4 changes: 2 additions & 2 deletions src/dictionary/user_pos_interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ class PosListProviderInterface {
public:
virtual ~PosListProviderInterface() = default;

// Sets possible list of POS which Mozc can handle.
virtual void GetPosList(std::vector<std::string> *pos_list) const = 0;
// Gets possible list of POS which Mozc can handle.
virtual std::vector<std::string> GetPosList() const = 0;
};

// Interface of the helper class used by POS.
Expand Down
9 changes: 3 additions & 6 deletions src/dictionary/user_pos_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,7 @@ class UserPosTest : public ::testing::Test {
};

TEST_F(UserPosTest, UserPosBasicTest) {
std::vector<std::string> pos_list;
user_pos_->GetPosList(&pos_list);
const std::vector<std::string> pos_list = user_pos_->GetPosList();
EXPECT_FALSE(pos_list.empty());
// test contains
EXPECT_TRUE(std::find(pos_list.begin(), pos_list.end(), "名詞サ変") !=
Expand All @@ -86,8 +85,7 @@ TEST_F(UserPosTest, UserPosBasicTest) {
}

TEST_F(UserPosTest, UserPosGetTokensTest) {
std::vector<std::string> pos_list;
user_pos_->GetPosList(&pos_list);
const std::vector<std::string> pos_list = user_pos_->GetPosList();

std::vector<UserPos::Token> tokens;
EXPECT_FALSE(user_pos_->GetTokens("", "test", pos_list[0], &tokens));
Expand Down Expand Up @@ -136,8 +134,7 @@ TEST_F(UserPosTest, UserPosGetTokensWithAttributesTest) {
}

TEST_F(UserPosTest, UserPosGetTokensWithLocaleTest) {
std::vector<std::string> pos_list;
user_pos_->GetPosList(&pos_list);
const std::vector<std::string> pos_list = user_pos_->GetPosList();

std::vector<UserPos::Token> tokens, tokens_ja, tokens_en;
EXPECT_TRUE(user_pos_->GetTokens("あか", "", "形容詞", "", &tokens));
Expand Down
6 changes: 2 additions & 4 deletions src/gui/dictionary_tool/dictionary_tool.cc
Original file line number Diff line number Diff line change
Expand Up @@ -414,8 +414,7 @@ DictionaryTool::DictionaryTool(QWidget *parent)
GetTableHeight(dic_content_));

// Get a list of POS and set a custom delagate that holds the list.
std::vector<std::string> tmp_pos_vec;
pos_list_provider_->GetPosList(&tmp_pos_vec);
const std::vector<std::string> tmp_pos_vec = pos_list_provider_->GetPosList();
QStringList pos_list;
for (size_t i = 0; i < tmp_pos_vec.size(); ++i) {
pos_list.append(QUtf8(tmp_pos_vec[i]));
Expand Down Expand Up @@ -1318,8 +1317,7 @@ void DictionaryTool::OnContextMenuRequestedForContent(const QPoint &pos) {

menu->addSeparator();
QMenu *change_category_to = menu->addMenu(tr("Change category to"));
std::vector<std::string> pos_list;
pos_list_provider_->GetPosList(&pos_list);
const std::vector<std::string> pos_list = pos_list_provider_->GetPosList();
std::vector<QAction *> change_pos_actions(pos_list.size());
for (size_t i = 0; i < pos_list.size(); ++i) {
change_pos_actions[i] = change_category_to->addAction(QUtf8(pos_list[i]));
Expand Down
3 changes: 1 addition & 2 deletions src/gui/word_register_dialog/word_register_dialog.cc
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,7 @@ WordRegisterDialog::WordRegisterDialog()
#endif // !ENABLE_CLOUD_SYNC

// Initialize ComboBox
std::vector<std::string> pos_set;
pos_list_provider_->GetPosList(&pos_set);
const std::vector<std::string> pos_set = pos_list_provider_->GetPosList();
CHECK(!pos_set.empty());

for (const std::string &pos : pos_set) {
Expand Down

0 comments on commit 4b0836a

Please sign in to comment.