string_word.c 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /*
  2. * /lub/string/string_word.c
  3. */
  4. #include <stddef.h>
  5. #include <ctype.h>
  6. #include "private.h"
  7. #include "lub/types.h"
  8. /*--------------------------------------------------------- */
  9. const char *lub_string_nextword(const char *string,
  10. size_t *len, size_t *offset, size_t *quoted)
  11. {
  12. const char *word;
  13. *quoted = 0;
  14. /* Find the start of a word (not including an opening quote) */
  15. while (*string && isspace(*string)) {
  16. string++;
  17. (*offset)++;
  18. }
  19. /* Is this the start of a quoted string ? */
  20. if (*string == '"') {
  21. *quoted = 1;
  22. string++;
  23. }
  24. word = string;
  25. *len = 0;
  26. /* Find the end of the word */
  27. while (*string) {
  28. if (*string == '\\') {
  29. string++;
  30. (*len)++;
  31. if (*string) {
  32. (*len)++;
  33. string++;
  34. }
  35. continue;
  36. }
  37. /* End of word */
  38. if (!*quoted && isspace(*string))
  39. break;
  40. if (*string == '"') {
  41. /* End of a quoted string */
  42. *quoted = 2;
  43. break;
  44. }
  45. (*len)++;
  46. string++;
  47. }
  48. return word;
  49. }
  50. /*--------------------------------------------------------- */
  51. unsigned int lub_string_wordcount(const char *line)
  52. {
  53. const char *word;
  54. unsigned int result = 0;
  55. size_t len = 0, offset = 0;
  56. size_t quoted;
  57. for (word = lub_string_nextword(line, &len, &offset, &quoted);
  58. *word || quoted;
  59. word = lub_string_nextword(word + len, &len, &offset, &quoted)) {
  60. /* account for the terminating quotation mark */
  61. len += quoted ? quoted - 1 : 0;
  62. result++;
  63. }
  64. return result;
  65. }
  66. /*--------------------------------------------------------- */