SplitString.cpp 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. #include "common/utils/SplitString.h"
  2. SplitString::SplitString(const char* str) : entries(0) {
  3. for(uint i = 0; str[i] != '\0'; i++) {
  4. if(str[i] == '"') {
  5. if(i >= 1 && str[i - 1] != ' ') {
  6. return;
  7. }
  8. data.append('"');
  9. i++;
  10. while(str[i] != '"') {
  11. if(str[i] == '\0') {
  12. return;
  13. }
  14. data.append(str[i++]);
  15. }
  16. if(str[i + 1] != '\0' && str[i + 1] != ' ') {
  17. return;
  18. }
  19. data.append('\0');
  20. continue;
  21. }
  22. data.append(str[i] == ' ' ? '\0' : str[i]);
  23. }
  24. uint last = 0;
  25. for(uint i = 0; i < data.getLength() + 1; i++) {
  26. if(data[i] != '\0' && data[i] != '"') {
  27. continue;
  28. }
  29. if(i - last > 0 || (i >= 1 && data[i - 1] == '"')) {
  30. starts[entries++] = last;
  31. }
  32. last = i + 1;
  33. }
  34. }
  35. uint SplitString::getLength() const {
  36. return entries;
  37. }
  38. const char* SplitString::operator[](uint index) const {
  39. return data + starts[index];
  40. }