Skip to content

Commit e0c1d2f

Browse files
committed
Update some algo's for better Performance
1 parent b180618 commit e0c1d2f

File tree

7 files changed

+157
-200
lines changed

7 files changed

+157
-200
lines changed

Resources/resmblfun.tcc

Lines changed: 40 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -4,78 +4,76 @@
44

55
/// @c Author Usama Azad.
66

7-
bool usa::string::is_upper()
7+
bool usa::string::is_upper() const
88
{
9-
for (int i = 0; i < this->size(); i++)
10-
if (this->at(i) >= 97 && this->at(i) <= 122)
9+
for (const char &ch : *this)
10+
if(ch & 0x20)
1111
return false;
1212
return true;
1313
}
1414

15-
bool usa::string::is_lower()
15+
bool usa::string::is_lower() const
1616
{
17-
for (int i = 0; i < this->size(); i++)
18-
if (this->at(i) >= 65 && this->at(i) <= 90)
17+
for (const char& ch : *this)
18+
if (!(ch & 0x20))
1919
return false;
2020
return true;
2121
}
2222

23-
bool usa::string::is_alpha()
23+
bool usa::string::is_alpha() const
2424
{
25-
if (this->is_contain_space() || this->is_contain() || this->is_None())
25+
if (this->is_contain_space() || this->is_contain_any(special_chars) || this->is_None())
2626
return false;
2727
for (int i = 0; i < this->size(); i++)
2828
if (this->at(i) >= '0' && this->at(i) <= '9')
2929
return false;
30-
return true;
30+
return true;
3131
}
3232

33-
bool usa::string::is_alnum()
33+
bool usa::string::is_alnum() const
3434
{
35-
if (this->is_contain_space() || this->is_contain() || this->is_None())
36-
return false;
37-
return true;
35+
return !(this->is_contain_space() || this->is_contain_any(special_chars) || this->is_None());
3836
}
3937

40-
bool usa::string::is_numeric()
38+
bool usa::string::is_numeric() const
4139
{
42-
if (this->is_contain_space() || this->is_contain() || this->is_None())
40+
if (this->is_contain_space() || this->is_contain_any(special_chars) || this->is_None())
4341
return false;
4442
for (int i = 0; i < this->size(); i++)
4543
if (this->at(i) >= 'a' && this->at(i) <= 'z' || this->at(i) >= 'A' && this->at(i) <= 'Z')
4644
return false;
4745
return true;
4846
}
4947

50-
bool usa::string::is_space()
48+
bool usa::string::is_space() const
5149
{
5250
if (this->is_None())
5351
return false;
5452
for (int i = 0; i < this->size(); i++)
55-
if (this->at(i) != ' ')
53+
if (this->at(i) != 32)
5654
return false;
5755
return true;
5856
}
5957

60-
bool usa::string::is_contain_space()
58+
bool usa::string::is_contain_space() const
6159
{
6260
if (!this->is_None())
6361
for (int i = 0; i < this->size(); i++)
64-
if (this->at(i) == ' ')
62+
if (this->at(i) == 32)
6563
return true;
6664
return false;
6765
}
6866

69-
bool usa::string::is_contain(const char* type)
67+
bool usa::string::is_contain_any(const char *str) const
7068
{
71-
for (char c : *this)
72-
for (char s : std::string(type))
69+
for (const char &s : std::string(str))
70+
for (const char &c : *this)
7371
if (c == s)
7472
return true;
7573
return false;
7674
}
7775

78-
bool usa::string::startswith(std::string str, bool match_case)
76+
bool usa::string::startswith(const std::string& str, bool match_case)
7977
{
8078
std::string cmp1 = (match_case) ? (*this) : Lower_Case(*this);
8179
std::string cmp2 = (match_case) ? str : Lower_Case(str);
@@ -85,7 +83,7 @@ bool usa::string::startswith(std::string str, bool match_case)
8583
return true;
8684
}
8785

88-
bool usa::string::endswith(std::string str, bool match_case)
86+
bool usa::string::endswith(const std::string& str, bool match_case)
8987
{
9088
std::string cmp1 = (match_case) ? (*this) : Lower_Case(*this);
9189
std::string cmp2 = (match_case) ? str : Lower_Case(str);
@@ -96,16 +94,30 @@ bool usa::string::endswith(std::string str, bool match_case)
9694
return true;
9795
}
9896

99-
int usa::string::count(char ch)
97+
int usa::string::count(const char &ch) const
10098
{
10199
int cnt = 0;
102-
for (int i = 0; i < this->size(); i++)
103-
if (this->at(i) == ch)
100+
for (const char &c : *this)
101+
if (c == ch)
102+
cnt++;
103+
return cnt;
104+
}
105+
106+
int usa::string::countWord(string word, bool match_case) const
107+
{
108+
string match = *this;
109+
if (!match_case){
110+
word.toLowerCase();
111+
match.toLowerCase();
112+
}
113+
int cnt = 0;
114+
for (const std::string &w : match.split(32))
115+
if (w == word)
104116
cnt++;
105117
return cnt;
106118
}
107119

108-
int usa::string::find_str(std::string str, int pos, bool match_case)
120+
int usa::string::find_str(const std::string &str, int pos, bool match_case)
109121
{
110122
std::string cmp1 = (match_case) ? (*this) : Lower_Case(*this);
111123
std::string cmp2 = (match_case) ? str : Lower_Case(str);

0 commit comments

Comments
 (0)