Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- size_t search(const std::string& source, const std::string& substring) {
- auto hasher = std::hash<std::string_view>{};
- const auto substringHash = hasher(substring);
- for (size_t index = 0; index < source.size() - substring.size() + 1; ++index) {
- auto view = std::string_view{ source };
- view.remove_prefix(index);
- view.remove_suffix(source.size() - substring.size() - index);
- if (substringHash != hasher(view)) {
- continue;
- }
- bool isSuccess = true;
- for (size_t substringIndex = 0; substringIndex < substring.size(); ++substringIndex) {
- if (source[index + substringIndex] != substring[substringIndex]) {
- isSuccess = false;
- break;
- }
- }
- if (isSuccess) {
- return index;
- }
- }
- return static_cast<size_t>(-1);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement