strfuncs.h 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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 0 == strncmp(str, prefix, strlen(prefix));
  17. }
  18. PRINTF_FUNC(3, 4)
  19. static inline void str_appendf(char *str, size_t alloc_size,
  20. const char *format, ...)
  21. {
  22. va_list ap;
  23. va_start(ap, format);
  24. (void)vsnprintf(str + strlen(str), alloc_size - strlen(str), format, ap);
  25. va_end(ap);
  26. }
  27. static inline void str_vappendf(char *str, size_t alloc_size,
  28. const char *format, va_list ap)
  29. {
  30. (void) vsnprintf(str + strlen(str), alloc_size - strlen(str), format, ap);
  31. }
  32. static inline void str_rstrip_char(char *str, char ch)
  33. {
  34. if (0 != strlen(str) &&
  35. str[strlen(str) - 1] == ch) {
  36. str[strlen(str) - 1] = '\0';
  37. }
  38. }
  39. #endif /* _GPSD_STRFUNCS_H_ */