Skip to content

Commit

Permalink
Do a cpplint and clang-tidy pass
Browse files Browse the repository at this point in the history
This only touches files in src/ but not its subdirectories.
  • Loading branch information
lukhnos committed Feb 16, 2024
1 parent fcc0aa1 commit 8928faa
Show file tree
Hide file tree
Showing 10 changed files with 120 additions and 63 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@

```bash
sudo apt install \
pkg-config fcitx5 libfcitx5core-dev libfcitx5config-dev libfcitx5utils-dev fcitx5-modules-dev\
pkg-config fcitx5 libfcitx5core-dev libfcitx5config-dev libfcitx5utils-dev fcitx5-modules-dev \
cmake extra-cmake-modules gettext libfmt-dev libicu-dev libjson-c-dev
```

Expand Down
2 changes: 1 addition & 1 deletion src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ project(mcbopomofo VERSION 2.5.2)
find_package(PkgConfig REQUIRED)
find_package(Fcitx5Core REQUIRED)
find_package(Fcitx5Utils REQUIRED)
#find_package(Fcitx5Module REQUIRED COMPONENTS Notifications)
find_package(Fcitx5Module REQUIRED COMPONENTS Notifications)
find_package(fmt REQUIRED)
find_package(Gettext REQUIRED)
find_package(ICU COMPONENTS uc i18n REQUIRED)
Expand Down
23 changes: 23 additions & 0 deletions src/DictionaryService.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,26 @@
// Copyright (c) 2024 and onwards The McBopomofo Authors.
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.

#include "DictionaryService.h"

#include <fcitx-utils/i18n.h>
Expand Down
32 changes: 28 additions & 4 deletions src/DictionaryService.h
Original file line number Diff line number Diff line change
@@ -1,10 +1,34 @@

#ifndef FCITX5_MCBOPOMOFO_DICTIONARYSERVICE_H
#define FCITX5_MCBOPOMOFO_DICTIONARYSERVICE_H
// Copyright (c) 2024 and onwards The McBopomofo Authors.
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.

#ifndef SRC_DICTIONARYSERVICE_H_
#define SRC_DICTIONARYSERVICE_H_

#include <functional>
#include <memory>
#include <string>
#include <vector>

#include "InputState.h"

Expand All @@ -18,7 +42,7 @@ using StateCallback =
*/
class DictionaryService {
public:
virtual ~DictionaryService(){};
virtual ~DictionaryService() = default;

virtual std::string name() const = 0;
virtual void lookup(std::string phrase, InputState* state,
Expand Down Expand Up @@ -58,4 +82,4 @@ class DictionaryServices {

} // namespace McBopomofo

#endif // FCITX5_MCBOPOMOFO_DICTIONARYSERVICE_H
#endif // SRC_DICTIONARYSERVICE_H_
34 changes: 18 additions & 16 deletions src/InputMacro.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ std::string FormatTimeZone(icu::TimeZone::EDisplayType type);
int GetCurrentYear();
std::string GetGanzhi(int year);
std::string GetChineseZodiac(int year);
std::string ConvertWeekdayUnit(std::string original);
void AddMacro(std::unordered_map<std::string, std::unique_ptr<InputMacro>>& m,
std::unique_ptr<InputMacro> p);
} // namespace

class InputMacroDate : public InputMacro {
Expand Down Expand Up @@ -353,13 +356,6 @@ class InputMacroNextYearJapanese : public InputMacroYear {
: InputMacroYear("MACRO@NEXT_YEAR_JAPANESE", "japanese", 1, "Gy") {}
};

std::string convertWeekdayUnit(std::string& original) {
// s/星期/禮拜/
std::string src = "星期";
std::string dst = "禮拜";
return original.replace(original.find(src), src.length(), dst);
}

class InputMacroWeekdayTodayShort : public InputMacroDayOfTheWeek {
public:
InputMacroWeekdayTodayShort()
Expand All @@ -378,7 +374,7 @@ class InputMacroWeekdayToday2 : public InputMacroDayOfTheWeek {
: InputMacroDayOfTheWeek("MACRO@DATE_TODAY2_WEEKDAY", "", 0, "EEEE") {}
[[nodiscard]] std::string replacement() const override {
std::string original(InputMacroDayOfTheWeek::replacement());
return convertWeekdayUnit(original);
return ConvertWeekdayUnit(original);
}
};

Expand Down Expand Up @@ -410,7 +406,7 @@ class InputMacroWeekdayYesterday2 : public InputMacroDayOfTheWeek {
"EEEE") {}
[[nodiscard]] std::string replacement() const override {
std::string original(InputMacroDayOfTheWeek::replacement());
return convertWeekdayUnit(original);
return ConvertWeekdayUnit(original);
}
};

Expand Down Expand Up @@ -440,7 +436,7 @@ class InputMacroWeekdayTomorrow2 : public InputMacroDayOfTheWeek {
: InputMacroDayOfTheWeek("MACRO@DATE_TOMORROW2_WEEKDAY", "", 1, "EEEE") {}
[[nodiscard]] std::string replacement() const override {
std::string original(InputMacroDayOfTheWeek::replacement());
return convertWeekdayUnit(original);
return ConvertWeekdayUnit(original);
}
};

Expand Down Expand Up @@ -512,12 +508,6 @@ class InputMacroNextYearChineseZodiac : public InputMacroZodiac {
: InputMacroZodiac("MACRO@NEXT_YEAR_CHINESE_ZODIAC", 1) {}
};

static void AddMacro(
std::unordered_map<std::string, std::unique_ptr<InputMacro>>& m,
std::unique_ptr<InputMacro> p) {
m.insert({p->name(), std::move(p)});
}

InputMacroController::InputMacroController() {
AddMacro(macros_, std::make_unique<InputMacroDateTodayShort>());
AddMacro(macros_, std::make_unique<InputMacroDateTodayMedium>());
Expand Down Expand Up @@ -702,6 +692,18 @@ std::string GetChineseZodiac(int year) {
return gan[ganIndex] + zhi[zhiIndex] + "";
}

std::string ConvertWeekdayUnit(std::string original) {
// s/星期/禮拜/
std::string src = "星期";
std::string dst = "禮拜";
return original.replace(original.find(src), src.length(), dst);
}

void AddMacro(std::unordered_map<std::string, std::unique_ptr<InputMacro>>& m,
std::unique_ptr<InputMacro> p) {
m.insert({p->name(), std::move(p)});
}

} // namespace

} // namespace McBopomofo
2 changes: 1 addition & 1 deletion src/InputMode.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,4 @@ enum class InputMode { McBopomofo, PlainBopomofo };

}

#endif // SRC_INPUTMODE_H_
#endif // SRC_INPUTMODE_H_
2 changes: 1 addition & 1 deletion src/InputState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,4 @@ InputStates::SelectingDateMacro::SelectingDateMacro(
}
}

} // namespace McBopomofo
} // namespace McBopomofo
13 changes: 8 additions & 5 deletions src/Key.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ struct Key {
const bool shiftPressed;
const bool ctrlPressed;

Key(char c = 0, KeyName n = KeyName::UNKNOWN, bool isShift = false,
bool isCtrl = false)
explicit Key(char c = 0, KeyName n = KeyName::UNKNOWN, bool isShift = false,
bool isCtrl = false)
: ascii(c), name(n), shiftPressed(isShift), ctrlPressed(isCtrl) {}

static Key asciiKey(char c, bool shiftPressed = false,
Expand All @@ -65,15 +65,18 @@ struct Key {
static Key namedKey(KeyName name, bool shiftPressed = false,
bool ctrlPressed = false) {
return Key(0, name, shiftPressed, ctrlPressed);
};
}

// Regardless of the shift state.
bool isCursorKeys() {
[[nodiscard]] bool isCursorKeys() const {
return name == KeyName::LEFT || name == KeyName::RIGHT ||
name == KeyName::HOME || name == KeyName::END;
}

// Regardless of the shift state.
bool isDeleteKeys() { return ascii == BACKSPACE || ascii == DELETE; }
[[nodiscard]] bool isDeleteKeys() const {
return ascii == BACKSPACE || ascii == DELETE;
}
};

} // namespace McBopomofo
Expand Down
8 changes: 5 additions & 3 deletions src/KeyHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ constexpr char kHalfWidthPunctuationKeyPrefix[] = "_half_punctuation_";
constexpr char kLetterPrefix[] = "_letter_";
constexpr size_t kMinValidMarkingReadingCount = 2;
constexpr size_t kMaxValidMarkingReadingCount = 6;
constexpr size_t kMaxChineseNumberConversionDigits = 20;

constexpr int kUserOverrideModelCapacity = 500;
constexpr double kObservedOverrideHalfLife = 5400.0; // 1.5 hr.
Expand Down Expand Up @@ -121,7 +122,7 @@ bool KeyHandler::handle(Key key, McBopomofo::InputState* state,
return true;
}

auto chineseNumber = dynamic_cast<InputStates::ChineseNumber*>(state);
auto* chineseNumber = dynamic_cast<InputStates::ChineseNumber*>(state);
if (chineseNumber != nullptr) {
return handleChineseNumber(key, chineseNumber, stateCallback,
errorCallback);
Expand Down Expand Up @@ -953,7 +954,7 @@ bool KeyHandler::handleChineseNumber(
return true;
}
if (key.ascii >= '0' && key.ascii <= '9') {
if (state->number.length() > 20) {
if (state->number.length() > kMaxChineseNumberConversionDigits) {
errorCallback();
return true;
}
Expand All @@ -966,7 +967,8 @@ bool KeyHandler::handleChineseNumber(
errorCallback();
return true;
}
if (state->number.empty() || state->number.length() > 20) {
if (state->number.empty() ||
state->number.length() > kMaxChineseNumberConversionDigits) {
errorCallback();
return true;
}
Expand Down
Loading

0 comments on commit 8928faa

Please sign in to comment.