trim.hpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. #pragma once
  2. namespace nall {
  3. inline auto string::trim(string_view lhs, string_view rhs, long limit) -> string& {
  4. trimRight(rhs, limit);
  5. trimLeft(lhs, limit);
  6. return *this;
  7. }
  8. inline auto string::trimLeft(string_view lhs, long limit) -> string& {
  9. if(lhs.size() == 0) return *this;
  10. long matches = 0;
  11. while(matches < limit) {
  12. int offset = lhs.size() * matches;
  13. int length = (int)size() - offset;
  14. if(length < (int)lhs.size()) break;
  15. if(memory::compare(data() + offset, lhs.data(), lhs.size()) != 0) break;
  16. matches++;
  17. }
  18. if(matches) remove(0, lhs.size() * matches);
  19. return *this;
  20. }
  21. inline auto string::trimRight(string_view rhs, long limit) -> string& {
  22. if(rhs.size() == 0) return *this;
  23. long matches = 0;
  24. while(matches < limit) {
  25. int offset = (int)size() - rhs.size() * (matches + 1);
  26. int length = (int)size() - offset;
  27. if(offset < 0 || length < (int)rhs.size()) break;
  28. if(memory::compare(data() + offset, rhs.data(), rhs.size()) != 0) break;
  29. matches++;
  30. }
  31. if(matches) resize(size() - rhs.size() * matches);
  32. return *this;
  33. }
  34. inline auto string::itrim(string_view lhs, string_view rhs, long limit) -> string& {
  35. itrimRight(rhs, limit);
  36. itrimLeft(lhs, limit);
  37. return *this;
  38. }
  39. inline auto string::itrimLeft(string_view lhs, long limit) -> string& {
  40. if(lhs.size() == 0) return *this;
  41. long matches = 0;
  42. while(matches < limit) {
  43. int offset = lhs.size() * matches;
  44. int length = (int)size() - offset;
  45. if(length < (int)lhs.size()) break;
  46. if(memory::icompare(data() + offset, lhs.data(), lhs.size()) != 0) break;
  47. matches++;
  48. }
  49. if(matches) remove(0, lhs.size() * matches);
  50. return *this;
  51. }
  52. inline auto string::itrimRight(string_view rhs, long limit) -> string& {
  53. if(rhs.size() == 0) return *this;
  54. long matches = 0;
  55. while(matches < limit) {
  56. int offset = (int)size() - rhs.size() * (matches + 1);
  57. int length = (int)size() - offset;
  58. if(offset < 0 || length < (int)rhs.size()) break;
  59. if(memory::icompare(data() + offset, rhs.data(), rhs.size()) != 0) break;
  60. matches++;
  61. }
  62. if(matches) resize(size() - rhs.size() * matches);
  63. return *this;
  64. }
  65. inline auto string::strip() -> string& {
  66. stripRight();
  67. stripLeft();
  68. return *this;
  69. }
  70. inline auto string::stripLeft() -> string& {
  71. uint length = 0;
  72. while(length < size()) {
  73. char input = operator[](length);
  74. if(input != ' ' && input != '\t' && input != '\r' && input != '\n') break;
  75. length++;
  76. }
  77. if(length) remove(0, length);
  78. return *this;
  79. }
  80. inline auto string::stripRight() -> string& {
  81. uint length = 0;
  82. while(length < size()) {
  83. bool matched = false;
  84. char input = operator[](size() - length - 1);
  85. if(input != ' ' && input != '\t' && input != '\r' && input != '\n') break;
  86. length++;
  87. }
  88. if(length) resize(size() - length);
  89. return *this;
  90. }
  91. }