Last active
April 28, 2021 05:54
-
-
Save tosone/df05c57353cd7960df9306dd267d0205 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <vector> | |
#include <iostream> | |
#include <algorithm> | |
#include <locale> | |
#include <sstream> | |
using namespace std; | |
int catchEmailBegin(string str, int place) | |
{ | |
int res = -1; | |
for (int i = place - 1; i > 0; i--) | |
{ | |
if (!(isalpha(str[i]) || isdigit(str[i]))) | |
{ | |
res = i + 1; | |
break; | |
} | |
} | |
return res; | |
} | |
int catchEmailEnd(string str, int place) | |
{ | |
int res = -1; | |
int domain_length = 0; | |
bool find_dot = false; | |
for (size_t i = place + 1; i < str.length(); i++) | |
{ | |
if (str[i] == '.') | |
{ | |
find_dot = true; | |
domain_length = -1; | |
} | |
if (!(isalpha(str[i]) || isdigit(str[i]) || str[i] == '.')) | |
{ | |
// cout << "domain_length: " << domain_length << endl; | |
if (domain_length >= 2 && domain_length <= 4) | |
{ | |
res = i; | |
} | |
break; | |
} | |
else | |
{ | |
if (find_dot) | |
{ | |
domain_length++; | |
} | |
} | |
} | |
if (res == -1 && find_dot && domain_length >= 2 && domain_length <= 4) | |
{ | |
res = str.length(); | |
} | |
return res; | |
} | |
vector<string> extractEmail(string str) | |
{ | |
std::string delim = "@"; | |
int start = 0; | |
vector<string> res; | |
int begin = 0, end = 0, place = 0; | |
for (;;) | |
{ | |
place = str.find(delim, start); | |
if (place == string::npos) | |
{ | |
break; | |
} | |
begin = catchEmailBegin(str, place); | |
if (begin == -1) | |
{ | |
goto next; | |
} | |
end = catchEmailEnd(str, place); | |
if (end == -1) | |
{ | |
goto next; | |
} | |
next: | |
// cout << begin << " " << end << endl; | |
if (begin != -1 && end != -1) | |
{ | |
// cout << begin << " " << end << str.substr(begin, end - begin) << endl; | |
res.push_back(str.substr(begin, end - begin)); | |
} | |
start = place + delim.length(); | |
} | |
return res; | |
} | |
template <typename T> | |
string array_show(vector<T> vec) | |
{ | |
stringstream ss; | |
ss << "["; | |
for (auto it = vec.begin(); it != vec.end(); ++it) | |
{ | |
if (it != vec.begin()) | |
{ | |
ss << ", " << *it; | |
} | |
else | |
{ | |
ss << *it; | |
} | |
} | |
ss << "]"; | |
return ss.str(); | |
} | |
int main(int argc, char const *argv[]) | |
{ | |
string input = "hello [email protected] [email protected]"; | |
cout << "res: " << array_show(extractEmail(input)) << endl; | |
return EXIT_SUCCESS; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CCFiles := $(wildcard *.cc) | |
.PHONY: cc | |
cc: $(CCFiles) | |
.PHONY: $(CCFiles) | |
$(CCFiles): | |
@echo Running `echo $@ | cut -d. -f1`: | |
@echo | |
@$(CXX) -std=c++17 -lm -Os -g3 -fsanitize=address,signed-integer-overflow \ | |
-o `echo $@ | cut -d. -f1`.test $@ | |
@./`echo $@ | cut -d. -f1`.test | |
@$(RM) -rf `echo $@ | cut -d. -f1`.test *.dSYM | |
@echo |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment