File: find.hpp

package info (click to toggle)
ares 126-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 32,600 kB
  • sloc: cpp: 356,508; ansic: 20,394; makefile: 16; sh: 2
file content (65 lines) | stat: -rw-r--r-- 2,515 bytes parent folder | download | duplicates (3)
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#pragma once

namespace nall {

inline auto string::contains(string_view characters) const -> maybe<u32> {
  for(u32 x : range(size())) {
    for(char y : characters) {
      if(operator[](x) == y) return x;
    }
  }
  return nothing;
}

template<bool Insensitive, bool Quoted> inline auto string::_find(s32 offset, string_view source) const -> maybe<u32> {
  if(source.size() == 0) return nothing;
  auto p = data();
  for(u32 n = offset, quoted = 0; n < size();) {
    if(Quoted) { if(p[n] == '\"') { quoted ^= 1; n++; continue; } if(quoted) { n++; continue; } }
    if(_compare<Insensitive>(p + n, size() - n, source.data(), source.size())) { n++; continue; }
    return n - offset;
  }
  return nothing;
}

inline auto string::find(string_view source) const -> maybe<u32> { return _find<0, 0>(0, source); }
inline auto string::ifind(string_view source) const -> maybe<u32> { return _find<1, 0>(0, source); }
inline auto string::qfind(string_view source) const -> maybe<u32> { return _find<0, 1>(0, source); }
inline auto string::iqfind(string_view source) const -> maybe<u32> { return _find<1, 1>(0, source); }

inline auto string::findFrom(s32 offset, string_view source) const -> maybe<u32> { return _find<0, 0>(offset, source); }
inline auto string::ifindFrom(s32 offset, string_view source) const -> maybe<u32> { return _find<1, 0>(offset, source); }

inline auto string::findNext(s32 offset, string_view source) const -> maybe<u32> {
  if(source.size() == 0) return nothing;
  for(s32 n = offset + 1; n < size(); n++) {
    if(memory::compare(data() + n, size() - n, source.data(), source.size()) == 0) return n;
  }
  return nothing;
}

inline auto string::ifindNext(s32 offset, string_view source) const -> maybe<u32> {
  if(source.size() == 0) return nothing;
  for(s32 n = offset + 1; n < size(); n++) {
    if(memory::icompare(data() + n, size() - n, source.data(), source.size()) == 0) return n;
  }
  return nothing;
}

inline auto string::findPrevious(s32 offset, string_view source) const -> maybe<u32> {
  if(source.size() == 0) return nothing;
  for(s32 n = offset - 1; n >= 0; n--) {
    if(memory::compare(data() + n, size() - n, source.data(), source.size()) == 0) return n;
  }
  return nothing;
}

inline auto string::ifindPrevious(s32 offset, string_view source) const -> maybe<u32> {
  if(source.size() == 0) return nothing;
  for(s32 n = offset - 1; n >= 0; n--) {
    if(memory::icompare(data() + n, size() - n, source.data(), source.size()) == 0) return n;
  }
  return nothing;
}

}