Skip to content

Commit

Permalink
feat(InputMacro): implement weekday macros
Browse files Browse the repository at this point in the history
  • Loading branch information
xatier committed Dec 18, 2023
1 parent 79b8ee0 commit f3106cf
Show file tree
Hide file tree
Showing 2 changed files with 134 additions and 0 deletions.
12 changes: 12 additions & 0 deletions data/data.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16769,6 +16769,10 @@ _punctuation_~ ~ 0.0
ㄇㄧㄥˊ-ㄊㄧㄢ MACRO@DATE_TOMORROW_MEDIUM_ROC -8
ㄇㄧㄥˊ-ㄊㄧㄢ MACRO@DATE_TOMORROW_MEDIUM_CHINESE -8
ㄇㄧㄥˊ-ㄊㄧㄢ MACRO@DATE_TOMORROW_MEDIUM_JAPANESE -8
ㄇㄧㄥˊ-ㄊㄧㄢ MACRO@DATE_TOMORROW_WEEKDAY_SHORT -8
ㄇㄧㄥˊ-ㄊㄧㄢ MACRO@DATE_TOMORROW_WEEKDAY -8
ㄇㄧㄥˊ-ㄊㄧㄢ MACRO@DATE_TOMORROW2_WEEKDAY -8
ㄇㄧㄥˊ-ㄊㄧㄢ MACRO@DATE_TOMORROW_WEEKDAY_JAPANESE -8
ㄇㄧㄥˊ-ㄊㄧㄢ-ㄉㄜ˙ 明天的 -5.34470065
ㄇㄧㄥˊ-ㄊㄧㄢ-ㄕㄤˋ-ㄨˇ-ㄅㄚ-ㄕˊ 明天上午八時 -6.69148813
ㄇㄧㄥˊ-ㄊㄧㄢ-ㄖˋ-ㄑㄧˊ 明天日期 -8
Expand Down Expand Up @@ -77413,6 +77417,10 @@ _punctuation_~ ~ 0.0
ㄐㄧㄣ-ㄊㄧㄢ MACRO@DATE_TODAY_MEDIUM_ROC -8
ㄐㄧㄣ-ㄊㄧㄢ MACRO@DATE_TODAY_MEDIUM_CHINESE -8
ㄐㄧㄣ-ㄊㄧㄢ MACRO@DATE_TODAY_MEDIUM_JAPANESE -8
ㄐㄧㄣ-ㄊㄧㄢ MACRO@DATE_TODAY_WEEKDAY_SHORT -8
ㄐㄧㄣ-ㄊㄧㄢ MACRO@DATE_TODAY_WEEKDAY -8
ㄐㄧㄣ-ㄊㄧㄢ MACRO@DATE_TODAY2_WEEKDAY -8
ㄐㄧㄣ-ㄊㄧㄢ MACRO@DATE_TODAY_WEEKDAY_JAPANESE -8
ㄐㄧㄣ-ㄊㄧㄢ-ㄉㄜ˙ 今天的 -4.34181496
ㄐㄧㄣ-ㄊㄧㄢ-ㄋㄥˊ 今天能 -6.82182190
ㄐㄧㄣ-ㄊㄧㄢ-ㄏㄨㄟˋ 今天會 -5.67569387
Expand Down Expand Up @@ -125791,6 +125799,10 @@ _punctuation_~ ~ 0.0
ㄗㄨㄛˊ-ㄊㄧㄢ MACRO@DATE_YESTERDAY_MEDIUM_ROC -8
ㄗㄨㄛˊ-ㄊㄧㄢ MACRO@DATE_YESTERDAY_MEDIUM_CHINESE -8
ㄗㄨㄛˊ-ㄊㄧㄢ MACRO@DATE_YESTERDAY_MEDIUM_JAPANESE -8
ㄗㄨㄛˊ-ㄊㄧㄢ MACRO@DATE_YESTERDAY_WEEKDAY_SHORT -8
ㄗㄨㄛˊ-ㄊㄧㄢ MACRO@DATE_YESTERDAY_WEEKDAY -8
ㄗㄨㄛˊ-ㄊㄧㄢ MACRO@DATE_YESTERDAY2_WEEKDAY -8
ㄗㄨㄛˊ-ㄊㄧㄢ MACRO@DATE_YESTERDAY_WEEKDAY_JAPANESE -8
ㄗㄨㄛˊ-ㄊㄧㄢ-ㄉㄜ˙ 昨天的 -5.38248921
ㄗㄨㄛˊ-ㄊㄧㄢ-ㄒㄧㄚˋ-ㄨˇ 昨天下午 -5.36628012
ㄗㄨㄛˊ-ㄊㄧㄢ-ㄓㄨㄥ-ㄨˇ 昨天中午 -6.20091272
Expand Down
122 changes: 122 additions & 0 deletions src/InputMacro.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,25 @@ class InputMacroYear : public InputMacro {
icu::UnicodeString pattern_;
};

class InputMacroDayOfTheWeek : public InputMacro {
public:
InputMacroDayOfTheWeek(std::string macroName, std::string calendar, int offset, icu::UnicodeString pattern)
: name_(macroName),
calendarName_(calendar),
dayOffset_(offset),
pattern_(pattern) {}
std::string name() const override { return name_; }
std::string replacement() const override {
return formatWithPattern(calendarName_, /*yearOffset*/ 0, dayOffset_, pattern_);
}

private:
std::string name_;
std::string calendarName_;
int dayOffset_;
icu::UnicodeString pattern_;
};

class InputMacroDateTime : public InputMacro {
public:
InputMacroDateTime(std::string macroName, icu::DateFormat::EStyle style)
Expand Down Expand Up @@ -297,6 +316,97 @@ 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()
: InputMacroDayOfTheWeek("MACRO@DATE_TODAY_WEEKDAY_SHORT", "", 0, "E") {}
};

class InputMacroWeekdayToday : public InputMacroDayOfTheWeek {
public:
InputMacroWeekdayToday()
: InputMacroDayOfTheWeek("MACRO@DATE_TODAY_WEEKDAY", "", 0, "EEEE") {}
};

class InputMacroWeekdayToday2 : public InputMacroDayOfTheWeek {
public:
InputMacroWeekdayToday2()
: InputMacroDayOfTheWeek("MACRO@DATE_TODAY2_WEEKDAY", "", 0, "EEEE") {}
std::string replacement () const override {
std::string original(InputMacroDayOfTheWeek::replacement());
return convertWeekdayUnit(original);
}
};

class InputMacroWeekdayTodayJapanese : public InputMacroDayOfTheWeek {
public:
InputMacroWeekdayTodayJapanese()
: InputMacroDayOfTheWeek("MACRO@DATE_TODAY_WEEKDAY_JAPANESE", "japanese", 0, "EEEE") {}
};

class InputMacroWeekdayYesterdayShort : public InputMacroDayOfTheWeek {
public:
InputMacroWeekdayYesterdayShort()
: InputMacroDayOfTheWeek("MACRO@DATE_YESTERDAY_WEEKDAY_SHORT", "", -1, "E") {}
};

class InputMacroWeekdayYesterday : public InputMacroDayOfTheWeek {
public:
InputMacroWeekdayYesterday()
: InputMacroDayOfTheWeek("MACRO@DATE_YESTERDAY_WEEKDAY", "", -1, "EEEE") {}
};

class InputMacroWeekdayYesterday2 : public InputMacroDayOfTheWeek {
public:
InputMacroWeekdayYesterday2()
: InputMacroDayOfTheWeek("MACRO@DATE_YESTERDAY2_WEEKDAY", "", -1, "EEEE") {}
std::string replacement () const override {
std::string original(InputMacroDayOfTheWeek::replacement());
return convertWeekdayUnit(original);
}
};

class InputMacroWeekdayYesterdayJapanese : public InputMacroDayOfTheWeek {
public:
InputMacroWeekdayYesterdayJapanese()
: InputMacroDayOfTheWeek("MACRO@DATE_YESTERDAY_WEEKDAY_JAPANESE", "japanese", -1, "EEEE") {}
};

class InputMacroWeekdayTomorrowShort : public InputMacroDayOfTheWeek {
public:
InputMacroWeekdayTomorrowShort()
: InputMacroDayOfTheWeek("MACRO@DATE_TOMORROW_WEEKDAY_SHORT", "", -1, "E") {}
};

class InputMacroWeekdayTomorrow : public InputMacroDayOfTheWeek {
public:
InputMacroWeekdayTomorrow()
: InputMacroDayOfTheWeek("MACRO@DATE_TOMORROW_WEEKDAY", "", -1, "EEEE") {}
};

class InputMacroWeekdayTomorrow2 : public InputMacroDayOfTheWeek {
public:
InputMacroWeekdayTomorrow2()
: InputMacroDayOfTheWeek("MACRO@DATE_TOMORROW2_WEEKDAY", "", -1, "EEEE") {}
std::string replacement () const override {
std::string original(InputMacroDayOfTheWeek::replacement());
return convertWeekdayUnit(original);
}
};

class InputMacroWeekdayTomorrowJapanese : public InputMacroDayOfTheWeek {
public:
InputMacroWeekdayTomorrowJapanese()
: InputMacroDayOfTheWeek("MACRO@DATE_TOMORROW_WEEKDAY_JAPANESE", "japanese", -1, "EEEE") {}
};

class InputMacroDateTimeNowShort : public InputMacroDateTime {
public:
InputMacroDateTimeNowShort()
Expand Down Expand Up @@ -387,6 +497,18 @@ InputMacroController::InputMacroController() {
AddMacro(macros_, std::make_unique<InputMacroNextYearPlainWithEra>());
AddMacro(macros_, std::make_unique<InputMacroNextYearRoc>());
AddMacro(macros_, std::make_unique<InputMacroNextYearJapanese>());
AddMacro(macros_, std::make_unique<InputMacroWeekdayTodayShort>());
AddMacro(macros_, std::make_unique<InputMacroWeekdayToday>());
AddMacro(macros_, std::make_unique<InputMacroWeekdayToday2>());
AddMacro(macros_, std::make_unique<InputMacroWeekdayTodayJapanese>());
AddMacro(macros_, std::make_unique<InputMacroWeekdayYesterdayShort>());
AddMacro(macros_, std::make_unique<InputMacroWeekdayYesterday>());
AddMacro(macros_, std::make_unique<InputMacroWeekdayYesterday2>());
AddMacro(macros_, std::make_unique<InputMacroWeekdayYesterdayJapanese>());
AddMacro(macros_, std::make_unique<InputMacroWeekdayTomorrowShort>());
AddMacro(macros_, std::make_unique<InputMacroWeekdayTomorrow>());
AddMacro(macros_, std::make_unique<InputMacroWeekdayTomorrow2>());
AddMacro(macros_, std::make_unique<InputMacroWeekdayTomorrowJapanese>());
AddMacro(macros_, std::make_unique<InputMacroDateYesterdayShort>());
AddMacro(macros_, std::make_unique<InputMacroDateYesterdayMedium>());
AddMacro(macros_, std::make_unique<InputMacroDateYesterdayMediumRoc>());
Expand Down

0 comments on commit f3106cf

Please sign in to comment.