stringManipulation.cpp 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. #include "stringManipulation.h"
  2. #include <cctype>
  3. namespace pika
  4. {
  5. //todo probably should also add a /0 at the end + TAKE CARE OF SIZE!!!!
  6. void removeCharacters(char *dest, const char *source, const char *charsToRemove, size_t destSize)
  7. {
  8. int write = 0;
  9. for (int read = 0; source[read] != 0; read++)
  10. {
  11. if (findChar(charsToRemove, source[read]))
  12. {
  13. }
  14. else
  15. {
  16. dest[write] = source[read];
  17. write++;
  18. if (write >= destSize) { break; }
  19. }
  20. }
  21. }
  22. void toLower(char *dest, const char *source, size_t size)
  23. {
  24. for (int i = 0; i < size; i++)
  25. {
  26. if (dest[i] == 0) { break; }
  27. dest[i] = std::tolower(source[i]);
  28. }
  29. }
  30. void toUpper(char *dest, const char *source, size_t size)
  31. {
  32. for (int i = 0; i < size; i++)
  33. {
  34. if (dest[i] == 0) { break; }
  35. dest[i] = std::toupper(source[i]);
  36. }
  37. }
  38. bool findChar(const char *source, char c)
  39. {
  40. int i = 0;
  41. while (source[i] != 0)
  42. {
  43. if (source[i] == c)
  44. {
  45. return true;
  46. }
  47. i++;
  48. }
  49. return false;
  50. }
  51. size_t strlcpy(char *dst, const char *src, size_t size)
  52. {
  53. for (size_t i = 0; i < size-1; i++)
  54. {
  55. dst[i] = src[i];
  56. if (src[i] == '\0') { return i; }
  57. }
  58. dst[size - 1] = '\0';
  59. return size - 1;
  60. }
  61. size_t strlcpy(char *dst, const std::string &src, size_t size)
  62. {
  63. return strlcpy(dst, src.c_str(), size);
  64. }
  65. std::vector<std::string> split(const char *source, char c)
  66. {
  67. std::string s = "";
  68. std::vector<std::string> ret;
  69. for (int i = 0; source[i] != 0; i++)
  70. {
  71. if (source[i] == c)
  72. {
  73. if (s != "")
  74. {
  75. ret.push_back(s);
  76. }
  77. s = "";
  78. }
  79. else
  80. {
  81. s += source[i];
  82. }
  83. }
  84. if (s != "")
  85. {
  86. ret.push_back(s);
  87. }
  88. return ret;
  89. }
  90. }