trim.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*
  2. * Copyright (C) 2001-2003 FhG Fokus
  3. *
  4. * This file is part of Kamailio, a free SIP server.
  5. *
  6. * Kamailio is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version
  10. *
  11. * Kamailio is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #ifndef TRIM_H
  21. #define TRIM_H
  22. #include "str.h"
  23. /*!
  24. * This switch-case statement is used in
  25. * trim_leading and trim_trailing. You can
  26. * define characters that should be skipped
  27. * here.
  28. */
  29. #define TRIM_SWITCH(c) switch(c) { \
  30. case ' ': \
  31. case '\t': \
  32. case '\r': \
  33. case '\n': \
  34. break; \
  35. \
  36. default: \
  37. return; \
  38. }
  39. /*
  40. * Remove any leading whitechars, like spaces,
  41. * horizontal tabs, carriage returns and line
  42. * feeds
  43. *
  44. * WARNING: String descriptor structure will be
  45. * modified ! Make a copy otherwise you
  46. * might be unable to free _s->s for
  47. * example !
  48. *
  49. */
  50. static inline void trim_leading(str* _s)
  51. {
  52. for(; _s->len > 0; _s->len--, _s->s++) {
  53. TRIM_SWITCH(*(_s->s));
  54. }
  55. }
  56. /*
  57. * Remove any trailing white char, like spaces,
  58. * horizontal tabs, carriage returns and line feeds
  59. *
  60. * WARNING: String descriptor structure will be
  61. * modified ! Make a copy otherwise you
  62. * might be unable to free _s->s for
  63. * example !
  64. */
  65. static inline void trim_trailing(str* _s)
  66. {
  67. for(; _s->len > 0; _s->len--) {
  68. TRIM_SWITCH(_s->s[_s->len - 1]);
  69. }
  70. }
  71. /*
  72. * Do trim_leading and trim_trailing
  73. *
  74. * WARNING: String structure will be modified !
  75. * Make a copy otherwise you might be
  76. * unable to free _s->s for example !
  77. */
  78. static inline void trim(str* _s)
  79. {
  80. trim_leading(_s);
  81. trim_trailing(_s);
  82. }
  83. /*
  84. * right and left space trimming
  85. *
  86. * WARNING: String structure will be modified !
  87. * Make a copy otherwise you might be
  88. * unable to free _s_->s for example !
  89. */
  90. #define trim_spaces_lr(_s_) \
  91. do{ \
  92. for(;(_s_).s[(_s_).len-1]==' ';(_s_).s[--(_s_).len]=0); \
  93. for(;(_s_).s[0]==' ';(_s_).s=(_s_).s+1,(_s_).len--); \
  94. \
  95. } while(0);
  96. #endif /* TRIM_H */