Skip to content

Commit 1640617

Browse files
committed
add task 12.15
1 parent 05ad201 commit 1640617

File tree

5 files changed

+86
-1
lines changed

5 files changed

+86
-1
lines changed

week-15/CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
set(DIRS task-12-14)
1+
set(DIRS task-12-14 task-12-15)
22
foreach(DIR ${DIRS})
33
add_subdirectory(${DIR})
44
endforeach()

week-15/task-12-15/CMakeLists.txt

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
include_directories(include)
2+
3+
add_subdirectory(test)

week-15/task-12-15/include/emails.hpp

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#pragma once
2+
3+
#include <algorithm>
4+
#include <ranges>
5+
#include <regex>
6+
#include <string>
7+
#include <vector>
8+
9+
std::vector<std::string> emails(const std::string& str) {
10+
const std::regex pattern(R"(\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{1,}\b)");
11+
std::vector<std::string> emails;
12+
std::ranges::for_each(std::sregex_iterator(std::cbegin(str), std::cend(str), pattern),
13+
std::sregex_iterator(),
14+
[&emails](const auto& matches) { emails.push_back(matches[0]); });
15+
return emails;
16+
}
+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
cpp_test(test-12-15.cpp)
+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
#include "emails.hpp"
2+
#include "gtest/gtest.h"
3+
4+
TEST(EmailExtractionTest, EmptyInput) { EXPECT_EQ(emails(""), std::vector<std::string>({})); }
5+
6+
TEST(EmailExtractionTest, NoEmailsInText) {
7+
const std::string text = R"(
8+
This is just regular text without any emails.
9+
Some symbols: @hello, test@, @test.com
10+
Numbers: 12345, random!text
11+
)";
12+
EXPECT_EQ(emails(text), std::vector<std::string>({}));
13+
}
14+
15+
TEST(EmailExtractionTest, ValidEmails) {
16+
const std::string text = R"(
17+
18+
19+
20+
)";
21+
EXPECT_EQ(emails(text),
22+
std::vector<std::string>({"[email protected]", "[email protected]",
23+
24+
25+
}
26+
27+
TEST(EmailExtractionTest, InvalidEmails) {
28+
const std::string text = R"(
29+
Invalid formats:
30+
plaintext
31+
@missing.start
32+
missing@at
33+
double@@sign.com
34+
invalid@chars_here.com
35+
missing@tld.
36+
)";
37+
EXPECT_EQ(emails(text), std::vector<std::string>({}));
38+
}
39+
40+
TEST(EmailExtractionTest, MixedContent) {
41+
const std::string text = R"(
42+
43+
Invalid: bad@email, @wrong.com
44+
Almost valid: almost@valid. but with space
45+
Trash: !@#$%^&*
46+
Valid but tricky: "[email protected]" (should extract without quotes)
47+
)";
48+
EXPECT_EQ(emails(text), std::vector<std::string>(
49+
50+
}
51+
52+
TEST(EmailExtractionTest, EdgeCases) {
53+
const std::string text = R"(
54+
55+
Long domain: [email protected]
56+
Special chars: [email protected]
57+
58+
59+
60+
)";
61+
EXPECT_EQ(emails(text),
62+
std::vector<std::string>({"[email protected]", "[email protected]",
63+
64+
65+
}

0 commit comments

Comments
 (0)