Skip to content

Commit e176189

Browse files
authored
Merge pull request #124 from EmperorYP7/cleanup-new
chore: Util cleanup
2 parents 3332324 + a0337fc commit e176189

15 files changed

+62
-86
lines changed

casbin/util/array_equals.cpp

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,18 +28,13 @@ namespace casbin {
2828

2929
// ArrayEquals determines whether two std::string arrays are identical.
3030
bool ArrayEquals(std::vector<std::string> a, std::vector<std::string> b) {
31-
if (a.size() != b.size()) {
31+
if(a.size() != b.size())
3232
return false;
33-
}
34-
35-
sort(a.begin(), a.end());
36-
sort(b.begin(), b.end());
37-
for (int i = 0 ; i < a.size() ; i++) {
38-
if (a[i] != b[i]) {
39-
return false;
40-
}
41-
}
42-
return true;
33+
34+
std::sort(a.begin(), a.end());
35+
std::sort(b.begin(), b.end());
36+
37+
return (a == b);
4338
}
4439

4540
} // namespace casbin

casbin/util/array_remove_duplicates.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,15 @@ namespace casbin {
2929
// ArrayRemoveDuplicates removes any duplicated elements in a std::string array.
3030
void ArrayRemoveDuplicates(std::vector<std::string> &s) {
3131
std::unordered_map<std::string, bool> found;
32+
found.reserve(s.size());
3233
int j = 0;
33-
for (int i = 0 ; i < s.size() ; i++) {
34-
if (!found[s[i]]) {
35-
found[s[i]] = true;
36-
s[j] = s[i];
37-
j++;
34+
for (const std::string& it : s) {
35+
if (!found[it]) {
36+
found[it] = true;
37+
s[j++] = it;
3838
}
3939
}
40-
s = std::vector<std::string> (s.begin(), s.begin()+j);
40+
s.erase(s.begin() + j, s.end());
4141
}
4242

4343
} // namespace casbin

casbin/util/array_to_string.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@
2424

2525
namespace casbin {
2626

27-
std::string ArrayToString(std::vector<std::string> arr){
27+
std::string ArrayToString(const std::vector<std::string>& arr){
2828
std::string res = arr[0];
29-
for (int i = 0 ; i < arr.size() ; i++)
30-
res += ", " + arr[i];
29+
for (const std::string& it : arr)
30+
res += ", " + it;
3131
return res;
3232
}
3333

casbin/util/ends_with.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@
2424

2525
namespace casbin {
2626

27-
bool EndsWith(std::string base, std::string suffix){
28-
int base_len = int(base.length());
29-
int suffix_len = int(suffix.length());
30-
return base.substr(base_len-suffix_len, suffix_len).compare(suffix) == 0;
27+
bool EndsWith(std::string_view base, std::string_view suffix) {
28+
size_t base_len = base.length();
29+
size_t suffix_len = suffix.length();
30+
return base.substr(base_len - suffix_len, suffix_len).compare(suffix) == 0;
3131
}
3232

3333
} // namespace casbin

casbin/util/escape_assertion.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ std::string EscapeAssertion(std::string s) {
4141
for (std::sregex_iterator k = words_begin; k != words_end; ++k) {
4242
std::smatch match = *k;
4343
std::string match_str = match.str();
44-
int pos = int(match_str.find("."));
45-
if(pos!=-1){
44+
int pos = static_cast<int>(match_str.find("."));
45+
if(pos!=-1) {
4646
std::string new_str = match_str.replace(pos, 1, "_");
4747
s = s.replace(match.position(), match.str().length(), new_str);
4848
}

casbin/util/find_all_occurences.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424

2525
namespace casbin {
2626

27-
std::vector <size_t> FindAllOccurences(std::string data, std::string toSearch){
27+
std::vector<size_t> FindAllOccurences(std::string_view data, std::string_view toSearch){
2828
// Get the first occurrence
2929
size_t pos = data.find(toSearch);
3030

casbin/util/join.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@
2424

2525
namespace casbin {
2626

27-
std::string Join(std::vector<std::string> vos, std::string sep){
27+
std::string Join(const std::vector<std::string>& vos, const std::string& sep){
2828
std::string fs = vos[0];
29-
for (int i = 1 ; i < vos.size() ; i++)
29+
for (size_t i = 1 ; i < vos.size() ; i++)
3030
fs += sep + vos[i];
3131
return fs;
3232
}

casbin/util/join_slice.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,12 @@
2424

2525
namespace casbin {
2626

27-
std::vector<std::string> JoinSlice(std::string a, std::vector<std::string> slice) {
28-
std::vector<std::string> result{a};
29-
for (int i = 0 ; i < slice.size() ; i++)
30-
result.push_back(slice[i]);
27+
std::vector<std::string> JoinSlice(const std::string& a, const std::vector<std::string>& slice) {
28+
std::vector<std::string> result(slice.size() + 1);
29+
size_t i = 0;
30+
result[i] = a;
31+
for(const std::string& it : slice)
32+
result[++i] = it;
3133
return result;
3234
}
3335

casbin/util/pch.h

Lines changed: 0 additions & 29 deletions
This file was deleted.

casbin/util/remove_comments.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,13 @@
2525
namespace casbin {
2626

2727
// RemoveComments removes the comments starting with # in the text.
28-
std::string RemoveComments(std::string s) {
28+
std::string RemoveComments(std::string_view s) {
2929
size_t pos = s.find("#");
30+
3031
if (pos == std::string::npos)
31-
return s;
32-
std::string fin_str = s.substr(0, pos);
32+
return std::string(s);
33+
34+
std::string fin_str(s.substr(0, pos));
3335
return Trim(fin_str);
3436
}
3537

0 commit comments

Comments
 (0)