File: trim.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 (102 lines) | stat: -rw-r--r-- 2,850 bytes parent folder | download | duplicates (4)
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#pragma once

namespace nall {

inline auto string::trim(string_view lhs, string_view rhs, long limit) -> string& {
  trimRight(rhs, limit);
  trimLeft(lhs, limit);
  return *this;
}

inline auto string::trimLeft(string_view lhs, long limit) -> string& {
  if(lhs.size() == 0) return *this;
  long matches = 0;
  while(matches < limit) {
    s32 offset = lhs.size() * matches;
    s32 length = (s32)size() - offset;
    if(length < (s32)lhs.size()) break;
    if(memory::compare(data() + offset, lhs.data(), lhs.size()) != 0) break;
    matches++;
  }
  if(matches) remove(0, lhs.size() * matches);
  return *this;
}

inline auto string::trimRight(string_view rhs, long limit) -> string& {
  if(rhs.size() == 0) return *this;
  long matches = 0;
  while(matches < limit) {
    s32 offset = (s32)size() - rhs.size() * (matches + 1);
    s32 length = (s32)size() - offset;
    if(offset < 0 || length < (s32)rhs.size()) break;
    if(memory::compare(data() + offset, rhs.data(), rhs.size()) != 0) break;
    matches++;
  }
  if(matches) resize(size() - rhs.size() * matches);
  return *this;
}

inline auto string::itrim(string_view lhs, string_view rhs, long limit) -> string& {
  itrimRight(rhs, limit);
  itrimLeft(lhs, limit);
  return *this;
}

inline auto string::itrimLeft(string_view lhs, long limit) -> string& {
  if(lhs.size() == 0) return *this;
  long matches = 0;
  while(matches < limit) {
    s32 offset = lhs.size() * matches;
    s32 length = (s32)size() - offset;
    if(length < (s32)lhs.size()) break;
    if(memory::icompare(data() + offset, lhs.data(), lhs.size()) != 0) break;
    matches++;
  }
  if(matches) remove(0, lhs.size() * matches);
  return *this;
}

inline auto string::itrimRight(string_view rhs, long limit) -> string& {
  if(rhs.size() == 0) return *this;
  long matches = 0;
  while(matches < limit) {
    s32 offset = (s32)size() - rhs.size() * (matches + 1);
    s32 length = (s32)size() - offset;
    if(offset < 0 || length < (s32)rhs.size()) break;
    if(memory::icompare(data() + offset, rhs.data(), rhs.size()) != 0) break;
    matches++;
  }
  if(matches) resize(size() - rhs.size() * matches);
  return *this;
}

inline auto string::strip() -> string& {
  stripRight();
  stripLeft();
  return *this;
}

inline auto string::stripLeft() -> string& {
  u32 length = 0;
  while(length < size()) {
    char input = operator[](length);
    if(input != ' ' && input != '\t' && input != '\r' && input != '\n') break;
    length++;
  }
  if(length) remove(0, length);
  return *this;
}

inline auto string::stripRight() -> string& {
  u32 length = 0;
  while(length < size()) {
    bool matched = false;
    char input = operator[](size() - length - 1);
    if(input != ' ' && input != '\t' && input != '\r' && input != '\n') break;
    length++;
  }
  if(length) resize(size() - length);
  return *this;
}

}