convert.hpp 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. #pragma once
  2. namespace nall {
  3. auto string::downcase() -> string& {
  4. char* p = get();
  5. for(uint n = 0; n < size(); n++) {
  6. if(p[n] >= 'A' && p[n] <= 'Z') p[n] += 0x20;
  7. }
  8. return *this;
  9. }
  10. auto string::qdowncase() -> string& {
  11. char* p = get();
  12. for(uint n = 0, quoted = 0; n < size(); n++) {
  13. if(p[n] == '\"') quoted ^= 1;
  14. if(!quoted && p[n] >= 'A' && p[n] <= 'Z') p[n] += 0x20;
  15. }
  16. return *this;
  17. }
  18. auto string::upcase() -> string& {
  19. char* p = get();
  20. for(uint n = 0; n < size(); n++) {
  21. if(p[n] >= 'a' && p[n] <= 'z') p[n] -= 0x20;
  22. }
  23. return *this;
  24. }
  25. auto string::qupcase() -> string& {
  26. char* p = get();
  27. for(uint n = 0, quoted = 0; n < size(); n++) {
  28. if(p[n] == '\"') quoted ^= 1;
  29. if(!quoted && p[n] >= 'a' && p[n] <= 'z') p[n] -= 0x20;
  30. }
  31. return *this;
  32. }
  33. auto string::transform(string_view from, string_view to) -> string& {
  34. if(from.size() != to.size() || from.size() == 0) return *this; //patterns must be the same length
  35. char* p = get();
  36. for(uint n = 0; n < size(); n++) {
  37. for(uint s = 0; s < from.size(); s++) {
  38. if(p[n] == from[s]) {
  39. p[n] = to[s];
  40. break;
  41. }
  42. }
  43. }
  44. return *this;
  45. }
  46. }