find.hpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. #pragma once
  2. namespace nall {
  3. auto string::contains(string_view characters) const -> maybe<uint> {
  4. for(uint x : range(size())) {
  5. for(char y : characters) {
  6. if(operator[](x) == y) return x;
  7. }
  8. }
  9. return nothing;
  10. }
  11. template<bool Insensitive, bool Quoted> auto string::_find(int offset, string_view source) const -> maybe<uint> {
  12. if(source.size() == 0) return nothing;
  13. auto p = data();
  14. for(uint n = offset, quoted = 0; n < size();) {
  15. if(Quoted) { if(p[n] == '\"') { quoted ^= 1; n++; continue; } if(quoted) { n++; continue; } }
  16. if(_compare<Insensitive>(p + n, size() - n, source.data(), source.size())) { n++; continue; }
  17. return n - offset;
  18. }
  19. return nothing;
  20. }
  21. auto string::find(string_view source) const -> maybe<uint> { return _find<0, 0>(0, source); }
  22. auto string::ifind(string_view source) const -> maybe<uint> { return _find<1, 0>(0, source); }
  23. auto string::qfind(string_view source) const -> maybe<uint> { return _find<0, 1>(0, source); }
  24. auto string::iqfind(string_view source) const -> maybe<uint> { return _find<1, 1>(0, source); }
  25. auto string::findFrom(int offset, string_view source) const -> maybe<uint> { return _find<0, 0>(offset, source); }
  26. auto string::ifindFrom(int offset, string_view source) const -> maybe<uint> { return _find<1, 0>(offset, source); }
  27. auto string::findNext(int offset, string_view source) const -> maybe<uint> {
  28. if(source.size() == 0) return nothing;
  29. for(int n = offset + 1; n < size(); n++) {
  30. if(memory::compare(data() + n, size() - n, source.data(), source.size()) == 0) return n;
  31. }
  32. return nothing;
  33. }
  34. auto string::ifindNext(int offset, string_view source) const -> maybe<uint> {
  35. if(source.size() == 0) return nothing;
  36. for(int n = offset + 1; n < size(); n++) {
  37. if(memory::icompare(data() + n, size() - n, source.data(), source.size()) == 0) return n;
  38. }
  39. return nothing;
  40. }
  41. auto string::findPrevious(int offset, string_view source) const -> maybe<uint> {
  42. if(source.size() == 0) return nothing;
  43. for(int n = offset - 1; n >= 0; n--) {
  44. if(memory::compare(data() + n, size() - n, source.data(), source.size()) == 0) return n;
  45. }
  46. return nothing;
  47. }
  48. auto string::ifindPrevious(int offset, string_view source) const -> maybe<uint> {
  49. if(source.size() == 0) return nothing;
  50. for(int n = offset - 1; n >= 0; n--) {
  51. if(memory::icompare(data() + n, size() - n, source.data(), source.size()) == 0) return n;
  52. }
  53. return nothing;
  54. }
  55. }