Advertisement
Ilya_Bykonya

Untitled

Oct 14th, 2022
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.89 KB | None | 0 0
  1.  
  2. size_t search(const std::string& source, const std::string& substring) {
  3. auto hasher = std::hash<std::string_view>{};
  4. const auto substringHash = hasher(substring);
  5. for (size_t index = 0; index < source.size() - substring.size() + 1; ++index) {
  6. auto view = std::string_view{ source };
  7. view.remove_prefix(index);
  8. view.remove_suffix(source.size() - substring.size() - index);
  9. if (substringHash != hasher(view)) {
  10. continue;
  11. }
  12.  
  13. bool isSuccess = true;
  14. for (size_t substringIndex = 0; substringIndex < substring.size(); ++substringIndex) {
  15. if (source[index + substringIndex] != substring[substringIndex]) {
  16. isSuccess = false;
  17. break;
  18. }
  19. }
  20.  
  21. if (isSuccess) {
  22. return index;
  23. }
  24. }
  25.  
  26. return static_cast<size_t>(-1);
  27. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement