-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnaive_string_match.h
More file actions
43 lines (39 loc) · 1.11 KB
/
Copy pathnaive_string_match.h
File metadata and controls
43 lines (39 loc) · 1.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#ifndef CPP_ALGORITHM_NAIVE_STRING_MATCH_H
#define CPP_ALGORITHM_NAIVE_STRING_MATCH_H
#include <string>
#include <vector>
namespace NaiveStringMatch
{
/**
* \brief Find all occurrences of a pattern in a text.
* \param text input text
* \param pattern find pattern
* \return position of pattern in text
*/
std::vector<int> NaiveStringMatcher(
const std::string& text,
const std::string& pattern);
}
// ----------------------------------------------------------------------------
inline std::vector<int> NaiveStringMatch::NaiveStringMatcher(
const std::string& text,
const std::string& pattern)
{
std::vector<int> position;
for (int i = 0; i < static_cast<int>(text.size()) - static_cast<int>(pattern.size()) + 1; ++i)
{
for (int j = 0; j < static_cast<int>(pattern.size()); ++j)
{
if (text[i + j] != pattern[j])
{
break;
}
if (j == static_cast<int>(pattern.size()) - 1)
{
position.push_back(i);
}
}
}
return position;
}
#endif