String.cpp 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. #include <cstdio>
  2. #include "utils/String.h"
  3. String::String() : String("") {
  4. }
  5. String::String(const char* str) : usedCapacity(1) {
  6. *this += str;
  7. }
  8. String& String::operator+=(const char* str) {
  9. usedCapacity--;
  10. size_t start = usedCapacity;
  11. while(usedCapacity + 1 < capacity && str[usedCapacity - start] != '\0') {
  12. path[usedCapacity] = str[usedCapacity - start];
  13. usedCapacity++;
  14. }
  15. path[usedCapacity++] = '\0';
  16. return *this;
  17. }
  18. String& String::operator+=(char c) {
  19. if(usedCapacity + 1 < capacity) {
  20. path[usedCapacity - 1] = c;
  21. path[usedCapacity] = '\0';
  22. usedCapacity++;
  23. }
  24. return *this;
  25. }
  26. String& String::operator+=(unsigned int i) {
  27. usedCapacity += snprintf(path + usedCapacity - 1, capacity - usedCapacity, "%u", i);
  28. return *this;
  29. }
  30. String& String::operator+=(double d) {
  31. usedCapacity += snprintf(path + usedCapacity - 1, capacity - usedCapacity, (d == (long) d) ? "%lg.0" : "%lg", d);
  32. return *this;
  33. }
  34. String String::operator+(const char* str) const {
  35. String s(this->path);
  36. s += str;
  37. return s;
  38. }
  39. String::operator const char*() const {
  40. return path;
  41. }