Skip to content

Commit

Permalink
Merge pull request #124 from EmperorYP7/cleanup-new
Browse files Browse the repository at this point in the history
chore: Util cleanup
  • Loading branch information
hsluoyz authored Aug 5, 2021
2 parents 3332324 + a0337fc commit e176189
Show file tree
Hide file tree
Showing 15 changed files with 62 additions and 86 deletions.
17 changes: 6 additions & 11 deletions casbin/util/array_equals.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,13 @@ namespace casbin {

// ArrayEquals determines whether two std::string arrays are identical.
bool ArrayEquals(std::vector<std::string> a, std::vector<std::string> b) {
if (a.size() != b.size()) {
if(a.size() != b.size())
return false;
}

sort(a.begin(), a.end());
sort(b.begin(), b.end());
for (int i = 0 ; i < a.size() ; i++) {
if (a[i] != b[i]) {
return false;
}
}
return true;

std::sort(a.begin(), a.end());
std::sort(b.begin(), b.end());

return (a == b);
}

} // namespace casbin
Expand Down
12 changes: 6 additions & 6 deletions casbin/util/array_remove_duplicates.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,15 @@ namespace casbin {
// ArrayRemoveDuplicates removes any duplicated elements in a std::string array.
void ArrayRemoveDuplicates(std::vector<std::string> &s) {
std::unordered_map<std::string, bool> found;
found.reserve(s.size());
int j = 0;
for (int i = 0 ; i < s.size() ; i++) {
if (!found[s[i]]) {
found[s[i]] = true;
s[j] = s[i];
j++;
for (const std::string& it : s) {
if (!found[it]) {
found[it] = true;
s[j++] = it;
}
}
s = std::vector<std::string> (s.begin(), s.begin()+j);
s.erase(s.begin() + j, s.end());
}

} // namespace casbin
Expand Down
6 changes: 3 additions & 3 deletions casbin/util/array_to_string.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@

namespace casbin {

std::string ArrayToString(std::vector<std::string> arr){
std::string ArrayToString(const std::vector<std::string>& arr){
std::string res = arr[0];
for (int i = 0 ; i < arr.size() ; i++)
res += ", " + arr[i];
for (const std::string& it : arr)
res += ", " + it;
return res;
}

Expand Down
8 changes: 4 additions & 4 deletions casbin/util/ends_with.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@

namespace casbin {

bool EndsWith(std::string base, std::string suffix){
int base_len = int(base.length());
int suffix_len = int(suffix.length());
return base.substr(base_len-suffix_len, suffix_len).compare(suffix) == 0;
bool EndsWith(std::string_view base, std::string_view suffix) {
size_t base_len = base.length();
size_t suffix_len = suffix.length();
return base.substr(base_len - suffix_len, suffix_len).compare(suffix) == 0;
}

} // namespace casbin
Expand Down
4 changes: 2 additions & 2 deletions casbin/util/escape_assertion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ std::string EscapeAssertion(std::string s) {
for (std::sregex_iterator k = words_begin; k != words_end; ++k) {
std::smatch match = *k;
std::string match_str = match.str();
int pos = int(match_str.find("."));
if(pos!=-1){
int pos = static_cast<int>(match_str.find("."));
if(pos!=-1) {
std::string new_str = match_str.replace(pos, 1, "_");
s = s.replace(match.position(), match.str().length(), new_str);
}
Expand Down
2 changes: 1 addition & 1 deletion casbin/util/find_all_occurences.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

namespace casbin {

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

Expand Down
4 changes: 2 additions & 2 deletions casbin/util/join.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@

namespace casbin {

std::string Join(std::vector<std::string> vos, std::string sep){
std::string Join(const std::vector<std::string>& vos, const std::string& sep){
std::string fs = vos[0];
for (int i = 1 ; i < vos.size() ; i++)
for (size_t i = 1 ; i < vos.size() ; i++)
fs += sep + vos[i];
return fs;
}
Expand Down
10 changes: 6 additions & 4 deletions casbin/util/join_slice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,12 @@

namespace casbin {

std::vector<std::string> JoinSlice(std::string a, std::vector<std::string> slice) {
std::vector<std::string> result{a};
for (int i = 0 ; i < slice.size() ; i++)
result.push_back(slice[i]);
std::vector<std::string> JoinSlice(const std::string& a, const std::vector<std::string>& slice) {
std::vector<std::string> result(slice.size() + 1);
size_t i = 0;
result[i] = a;
for(const std::string& it : slice)
result[++i] = it;
return result;
}

Expand Down
29 changes: 0 additions & 29 deletions casbin/util/pch.h

This file was deleted.

8 changes: 5 additions & 3 deletions casbin/util/remove_comments.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,13 @@
namespace casbin {

// RemoveComments removes the comments starting with # in the text.
std::string RemoveComments(std::string s) {
std::string RemoveComments(std::string_view s) {
size_t pos = s.find("#");

if (pos == std::string::npos)
return s;
std::string fin_str = s.substr(0, pos);
return std::string(s);

std::string fin_str(s.substr(0, pos));
return Trim(fin_str);
}

Expand Down
16 changes: 10 additions & 6 deletions casbin/util/set_subtract.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,20 @@
namespace casbin {

// SetSubtract returns the elements in `a` that aren't in `b`.
std::vector<std::string> SetSubtract(std::vector<std::string> a, std::vector<std::string> b) {
std::vector<std::string> SetSubtract(const std::vector<std::string>& a, const std::vector<std::string>& b) {
std::unordered_map<std::string, bool> mb;
mb.reserve(b.size());

for (int i = 0 ; i < b.size() ; i++)
mb[b[i]] = true;
for (const std::string& it : b)
mb[it] = true;

std::vector<std::string> diff;
for (int i = 0 ; i < a.size() ; i++)
if (!mb[a[i]])
diff.push_back(a[i]);
diff.reserve(a.size());

for (const std::string& it : a)
if (!mb[it])
diff.push_back(it);

return diff;
}

Expand Down
14 changes: 9 additions & 5 deletions casbin/util/split.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,25 @@

namespace casbin {

std::vector<std::string> Split(std::string str, std::string del, int limit){
std::vector<std::string> Split(std::string str, std::string del, int limit) {
std::vector<std::string> tokens;

if(limit<=0)
if(limit <= 0)
limit = LARGE;

tokens.reserve((limit == LARGE) ? 100000 : limit);

for (int i = 1; i < limit ; i++) {
size_t pos = str.find(del);
if (pos != std::string::npos) {
tokens.push_back(str.substr(0, pos));
tokens.emplace_back(str.substr(0, pos));
str = str.substr(pos + del.length());
} else
}
else
break;
}
tokens.push_back(str);

tokens.emplace_back(str);

return tokens;
}
Expand Down
2 changes: 0 additions & 2 deletions casbin/util/ticker.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@
* limitations under the License.
*/

#include "pch.h"

#ifndef TICKER_H
#define TICKER_H

Expand Down
14 changes: 7 additions & 7 deletions casbin/util/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ bool ArrayEquals(std::vector<std::string> a, std::vector<std::string> b);
// ArrayRemoveDuplicates removes any duplicated elements in a std::string array.
void ArrayRemoveDuplicates(std::vector<std::string>& s);

std::string ArrayToString(std::vector<std::string> arr);
std::string ArrayToString(const std::vector<std::string>& arr);

bool EndsWith(std::string base, std::string suffix);
bool EndsWith(std::string_view base, std::string_view suffix);

/**
* escapeAssertion escapes the dots in the assertion, because the expression evaluation doesn't support such variable names.
Expand All @@ -40,20 +40,20 @@ bool EndsWith(std::string base, std::string suffix);
*/
std::string EscapeAssertion(std::string s);

std::vector<size_t> FindAllOccurences(std::string data, std::string toSearch);
std::vector<size_t> FindAllOccurences(std::string_view data, std::string_view toSearch);

template<typename Base, typename T>
bool IsInstanceOf(const T*);

std::vector<std::string> JoinSlice(std::string a, std::vector<std::string> slice);
std::vector<std::string> JoinSlice(const std::string& a, const std::vector<std::string>& slice);

std::string Join(std::vector<std::string> vos, std::string sep = " ");
std::string Join(const std::vector<std::string>& vos, const std::string& sep = " ");

// RemoveComments removes the comments starting with # in the text.
std::string RemoveComments(std::string s);
std::string RemoveComments(std::string_view s);

// SetSubtract returns the elements in `a` that aren't in `b`.
std::vector<std::string> SetSubtract(std::vector<std::string> a, std::vector<std::string> b);
std::vector<std::string> SetSubtract(const std::vector<std::string>& a, const std::vector<std::string>& b);

std::vector<std::string> Split(std::string str, std::string del, int limit = 0);

Expand Down
2 changes: 1 addition & 1 deletion tests/management_api_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ TEST(TestManagementAPI, TestModifyGroupingPolicyAPI) {
e.UpdateGroupingPolicy({"eve", "data3_admin"}, {"eve", "admin"});
e.UpdateGroupingPolicy({"data3_admin", "data4_admin"}, {"admin", "data4_admin"});

// ASSERT_TRUE(ArrayEquals({"admin"}, e.GetUsersForRole("data4_admin")));
// ASSERT_TRUE(casbin::ArrayEquals({"admin"}, e.GetUsersForRole("data4_admin")));
ASSERT_TRUE(casbin::ArrayEquals({"eve"}, e.GetUsersForRole("admin")));

ASSERT_TRUE(casbin::ArrayEquals({"admin"}, e.GetRolesForUser("eve")));
Expand Down

0 comments on commit e176189

Please sign in to comment.