strfuncs.h 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. /*
  2. * strfuncs.h - string functions
  3. *
  4. * This software is distributed under a BSD-style license. See the
  5. * file "COPYING" in the toop-level directory of the distribution for details.
  6. */
  7. #ifndef _GPSD_STRFUNCS_H_
  8. #define _GPSD_STRFUNCS_H_
  9. #include <stdarg.h>
  10. #include <stdbool.h>
  11. #include <stdio.h>
  12. #include <string.h>
  13. #include "compiler.h"
  14. static inline bool str_starts_with(const char *str, const char *prefix)
  15. {
  16. return strncmp(str, prefix, strlen(prefix)) == 0;
  17. }
  18. PRINTF_FUNC(3, 4)
  19. static inline void str_appendf(char *str, size_t alloc_size, const char *format, ...)
  20. {
  21. va_list ap;
  22. va_start(ap, format);
  23. (void) vsnprintf(str + strlen(str), alloc_size - strlen(str), format, ap);
  24. va_end(ap);
  25. }
  26. static inline void str_vappendf(char *str, size_t alloc_size, const char *format, va_list ap)
  27. {
  28. (void) vsnprintf(str + strlen(str), alloc_size - strlen(str), format, ap);
  29. }
  30. static inline void str_rstrip_char(char *str, char ch)
  31. {
  32. if (strlen(str) != 0 && str[strlen(str) - 1] == ch) {
  33. str[strlen(str) - 1] = '\0';
  34. }
  35. }
  36. #endif /* _GPSD_STRFUNCS_H_ */