Skip to content

Commit

Permalink
use clang-format to lint code (#665)
Browse files Browse the repository at this point in the history
* lint

* add CI job
  • Loading branch information
eagleoflqj authored Jun 24, 2023
1 parent bd3c7c7 commit ab586ca
Show file tree
Hide file tree
Showing 185 changed files with 4,550 additions and 6,974 deletions.
2 changes: 2 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
BasedOnStyle: Chromium
SortIncludes: false
13 changes: 13 additions & 0 deletions .github/workflows/commit-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,24 @@ on:
pull_request:

jobs:
lint:
runs-on: ubuntu-latest
steps:
- name: Checkout last commit
uses: actions/checkout@v3
- name: Install clang-format
run: sudo apt install -y clang-format
- name: Lint
run: find src -name '*.cc' -o -name '*.h' | xargs clang-format -Werror --dry-run || { echo Please lint your code by '"'"find src -name '*.cc' -o -name '*.h' | xargs clang-format -i"'"'.; false; }

linux:
needs: lint
uses: ./.github/workflows/linux-build.yml

macos:
needs: lint
uses: ./.github/workflows/macos-build.yml

windows:
needs: lint
uses: ./.github/workflows/windows-build.yml
28 changes: 11 additions & 17 deletions src/rime/algo/algebra.cc
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@ void Script::Merge(const string& s,
auto e = std::find(m.begin(), m.end(), x);
if (e == m.end()) {
m.push_back(y);
}
else {
} else {
SpellingProperties& zz(e->properties);
if (yy.type < zz.type)
zz.type = yy.type;
Expand All @@ -53,19 +52,18 @@ void Script::Dump(const string& file_name) const {
for (const value_type& v : *this) {
bool first = true;
for (const Spelling& s : v.second) {
out << (first ? v.first : "") << '\t'
<< s.str << '\t'
<< "-ac?!"[s.properties.type] << '\t'
<< s.properties.credibility << '\t'
<< s.properties.tips << std::endl;
out << (first ? v.first : "") << '\t' << s.str << '\t'
<< "-ac?!"[s.properties.type] << '\t' << s.properties.credibility
<< '\t' << s.properties.tips << std::endl;
first = false;
}
}
out.close();
}

bool Projection::Load(an<ConfigList> settings) {
if (!settings) return false;
if (!settings)
return false;
calculation_.clear();
Calculus calc;
bool success = true;
Expand All @@ -76,12 +74,11 @@ bool Projection::Load(an<ConfigList> settings) {
success = false;
break;
}
const string &formula(v->str());
const string& formula(v->str());
an<Calculation> x;
try {
x.reset(calc.Parse(formula));
}
catch (boost::regex_error& e) {
} catch (boost::regex_error& e) {
LOG(ERROR) << "Error parsing formula '" << formula << "': " << e.what();
}
if (!x) {
Expand All @@ -107,8 +104,7 @@ bool Projection::Apply(string* value) {
try {
if (x->Apply(&s))
modified = true;
}
catch (std::runtime_error& e) {
} catch (std::runtime_error& e) {
LOG(ERROR) << "Error applying calculation: " << e.what();
return false;
}
Expand All @@ -132,8 +128,7 @@ bool Projection::Apply(Script* value) {
bool applied = false;
try {
applied = x->Apply(&s);
}
catch (std::runtime_error& e) {
} catch (std::runtime_error& e) {
LOG(ERROR) << "Error applying calculation: " << e.what();
return false;
}
Expand All @@ -145,8 +140,7 @@ bool Projection::Apply(Script* value) {
if (x->addition() && !s.str.empty()) {
temp.Merge(s.str, s.properties, v.second);
}
}
else {
} else {
temp.Merge(v.first, SpellingProperties(), v.second);
}
}
Expand Down
1 change: 1 addition & 0 deletions src/rime/algo/algebra.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class Projection {
RIME_API bool Apply(string* value);
// {z, y, x} -> {a, b, c, d}
RIME_API bool Apply(Script* value);

protected:
vector<of<Calculation>> calculation_;
};
Expand Down
13 changes: 5 additions & 8 deletions src/rime/algo/calculus.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@

namespace rime {

const double kAbbreviationPenalty = -0.6931471805599453; // log(0.5)
const double kFuzzySpellingPenalty = -0.6931471805599453; // log(0.5)
const double kAbbreviationPenalty = -0.6931471805599453; // log(0.5)
const double kFuzzySpellingPenalty = -0.6931471805599453; // log(0.5)

Calculus::Calculus() {
Register("xlit", &Transliteration::Parse);
Expand All @@ -23,8 +23,7 @@ Calculus::Calculus() {
Register("abbrev", &Abbreviation::Parse);
}

void Calculus::Register(const string& token,
Calculation::Factory* factory) {
void Calculus::Register(const string& token, Calculation::Factory* factory) {
factories_[token] = factory;
}

Expand Down Expand Up @@ -55,8 +54,7 @@ Calculation* Transliteration::Parse(const vector<string>& args) {
const char* pr = right.c_str();
uint32_t cl, cr;
map<uint32_t, uint32_t> char_map;
while ((cl = utf8::unchecked::next(pl)),
(cr = utf8::unchecked::next(pr)),
while ((cl = utf8::unchecked::next(pl)), (cr = utf8::unchecked::next(pr)),
cl && cr) {
char_map[cl] = cr;
}
Expand Down Expand Up @@ -113,8 +111,7 @@ Calculation* Transformation::Parse(const vector<string>& args) {
bool Transformation::Apply(Spelling* spelling) {
if (!spelling || spelling->str.empty())
return false;
string result = boost::regex_replace(spelling->str,
pattern_, replacement_);
string result = boost::regex_replace(spelling->str, pattern_, replacement_);
if (result == spelling->str)
return false;
spelling->str.swap(result);
Expand Down
2 changes: 1 addition & 1 deletion src/rime/algo/calculus.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ namespace rime {

class Calculation {
public:
using Factory = Calculation* (const vector<string>& args);
using Factory = Calculation*(const vector<string>& args);

Calculation() = default;
virtual ~Calculation() = default;
Expand Down
8 changes: 4 additions & 4 deletions src/rime/algo/dynamics.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ inline double formula_d(double d, double t, double da, double ta) {
inline double formula_p(double s, double u, double t, double d) {
const double kM = 1 / (1 - exp(-0.005));
double m = s - (s - u) * pow((1 - exp(-t / 10000)), 10);
return (d < 20) ? m + (0.5 - m) * (d / kM) :
m + (1 - m) * (pow(4, (d / kM)) - 1) / 3;
return (d < 20) ? m + (0.5 - m) * (d / kM)
: m + (1 - m) * (pow(4, (d / kM)) - 1) / 3;
}

}
}
} // namespace algo
} // namespace rime
55 changes: 21 additions & 34 deletions src/rime/algo/encoder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,12 @@ string RawCode::ToString() const {
return strings::join(*this, " ");
}

void RawCode::FromString(const string &code_str) {
*dynamic_cast<vector<string> *>(this) =
strings::split(code_str, " ");
void RawCode::FromString(const string& code_str) {
*dynamic_cast<vector<string>*>(this) = strings::split(code_str, " ");
}

TableEncoder::TableEncoder(PhraseCollector* collector)
: Encoder(collector), loaded_(false), max_phrase_length_(0) {
}
: Encoder(collector), loaded_(false), max_phrase_length_(0) {}

/*
# sample encoder configuration (from cangjie5.dict.yaml)
Expand All @@ -50,7 +48,8 @@ bool TableEncoder::LoadSettings(Config* config) {
exclude_patterns_.clear();
tail_anchor_.clear();

if (!config) return false;
if (!config)
return false;

if (auto rules = config->GetList("encoder/rules")) {
for (auto it = rules->begin(); it != rules->end(); ++it) {
Expand All @@ -72,10 +71,8 @@ bool TableEncoder::LoadSettings(Config* config) {
if (max_phrase_length_ < length) {
max_phrase_length_ = length;
}
}
else if (auto range = As<ConfigList>(rule->Get("length_in_range"))) {
if (range->size() != 2 ||
!range->GetValueAt(0) ||
} else if (auto range = As<ConfigList>(rule->Get("length_in_range"))) {
if (range->size() != 2 || !range->GetValueAt(0) ||
!range->GetValueAt(1) ||
!range->GetValueAt(0)->GetInt(&r.min_word_length) ||
!range->GetValueAt(1)->GetInt(&r.max_word_length) ||
Expand Down Expand Up @@ -113,7 +110,7 @@ bool TableEncoder::ParseFormula(const string& formula,
LOG(ERROR) << "bad formula: '%s'" << formula;
return false;
}
for (auto it = formula.cbegin(), end = formula.cend(); it != end; ) {
for (auto it = formula.cbegin(), end = formula.cend(); it != end;) {
CodeCoords c;
if (*it < 'A' || *it > 'Z') {
LOG(ERROR) << "invalid character index in formula: '%s'" << formula;
Expand Down Expand Up @@ -161,17 +158,16 @@ bool TableEncoder::Encode(const RawCode& code, string* result) {
if (c.char_index < 0) {
continue; // 'abc def' ~ 'Xa'
}
if (current.char_index < 0 &&
c.char_index < encoded.char_index) {
if (current.char_index < 0 && c.char_index < encoded.char_index) {
continue; // 'abc def' ~ '(AaBa)Ya'
// 'abc def' ~ '(AaBa)Aa' is OK
}
int start_index = 0;
if (c.char_index == encoded.char_index) {
start_index = encoded.code_index + 1;
}
c.code_index = CalculateCodeIndex(code[c.char_index], c.code_index,
start_index);
c.code_index =
CalculateCodeIndex(code[c.char_index], c.code_index, start_index);
if (c.code_index >= static_cast<int>(code[c.char_index].length())) {
continue; // 'abc def' ~ 'Ad'
}
Expand Down Expand Up @@ -207,10 +203,9 @@ bool TableEncoder::Encode(const RawCode& code, string* result) {
// beyond `start` is used to locate the encoding character at index -1.
// returns string index in `code` for the character at virtual `index`.
// may return a negative number if `index` does not exist in `code`.
int TableEncoder::CalculateCodeIndex(const string& code, int index,
int start) {
DLOG(INFO) << "code = " << code
<< ", index = " << index << ", start = " << start;
int TableEncoder::CalculateCodeIndex(const string& code, int index, int start) {
DLOG(INFO) << "code = " << code << ", index = " << index
<< ", start = " << start;
// tail_anchor = '|'
const int n = static_cast<int>(code.length());
int k = 0;
Expand All @@ -224,24 +219,20 @@ int TableEncoder::CalculateCodeIndex(const string& code, int index,
k = static_cast<int>(tail) - 1;
}
while (++index < 0) {
while (--k >= 0 &&
tail_anchor_.find(code[k]) != string::npos) {
while (--k >= 0 && tail_anchor_.find(code[k]) != string::npos) {
}
}
}
else {
} else {
// 'ab|cd|ef|g' ~ '(AaAb)Ac' -> 'abc'; index = 2
while (index-- > 0) {
while (++k < n &&
tail_anchor_.find(code[k]) != string::npos) {
while (++k < n && tail_anchor_.find(code[k]) != string::npos) {
}
}
}
return k;
}

bool TableEncoder::EncodePhrase(const string& phrase,
const string& value) {
bool TableEncoder::EncodePhrase(const string& phrase, const string& value) {
size_t phrase_length = utf8::unchecked::distance(
phrase.c_str(), phrase.c_str() + phrase.length());
if (static_cast<int>(phrase_length) > max_phrase_length_)
Expand All @@ -267,8 +258,7 @@ bool TableEncoder::DfsEncode(const string& phrase,
<< "[" << code->ToString() << "] -> [" << encoded << "]";
collector_->CreateEntry(phrase, encoded, value);
return true;
}
else {
} else {
DLOG(WARNING) << "failed to encode '" << phrase << "': "
<< "[" << code->ToString() << "]";
return false;
Expand Down Expand Up @@ -298,12 +288,9 @@ bool TableEncoder::DfsEncode(const string& phrase,
return ret;
}

ScriptEncoder::ScriptEncoder(PhraseCollector* collector)
: Encoder(collector) {
}
ScriptEncoder::ScriptEncoder(PhraseCollector* collector) : Encoder(collector) {}

bool ScriptEncoder::EncodePhrase(const string& phrase,
const string& value) {
bool ScriptEncoder::EncodePhrase(const string& phrase, const string& value) {
size_t phrase_length = utf8::unchecked::distance(
phrase.c_str(), phrase.c_str() + phrase.length());
if (static_cast<int>(phrase_length) > kMaxPhraseLength)
Expand Down
26 changes: 14 additions & 12 deletions src/rime/algo/encoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ namespace rime {
class RawCode : public vector<string> {
public:
RIME_API string ToString() const;
RIME_API void FromString(const string &code_str);
RIME_API void FromString(const string& code_str);
};

class PhraseCollector {
Expand All @@ -28,8 +28,7 @@ class PhraseCollector {
const string& code_str,
const string& value) = 0;
// return a list of alternative code for the given word
virtual bool TranslateWord(const string& word,
vector<string>* code) = 0;
virtual bool TranslateWord(const string& word, vector<string>* code) = 0;
};

class Config;
Expand All @@ -39,12 +38,9 @@ class Encoder {
Encoder(PhraseCollector* collector) : collector_(collector) {}
virtual ~Encoder() = default;

virtual bool LoadSettings(Config* config) {
return false;
}
virtual bool LoadSettings(Config* config) { return false; }

virtual bool EncodePhrase(const string& phrase,
const string& value) = 0;
virtual bool EncodePhrase(const string& phrase, const string& value) = 0;

void set_collector(PhraseCollector* collector) { collector_ = collector; }

Expand Down Expand Up @@ -87,8 +83,11 @@ class TableEncoder : public Encoder {
protected:
bool ParseFormula(const string& formula, TableEncodingRule* rule);
int CalculateCodeIndex(const string& code, int index, int start);
bool DfsEncode(const string& phrase, const string& value,
size_t start_pos, RawCode* code, int* limit);
bool DfsEncode(const string& phrase,
const string& value,
size_t start_pos,
RawCode* code,
int* limit);

bool loaded_;
// settings
Expand All @@ -107,8 +106,11 @@ class ScriptEncoder : public Encoder {
bool EncodePhrase(const string& phrase, const string& value);

private:
bool DfsEncode(const string& phrase, const string& value,
size_t start_pos, RawCode* code, int* limit);
bool DfsEncode(const string& phrase,
const string& value,
size_t start_pos,
RawCode* code,
int* limit);
};

} // namespace rime
Expand Down
Loading

0 comments on commit ab586ca

Please sign in to comment.