1
0

string_word.c 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. if (*string == '\\') {
  20. string++;
  21. if (*string)
  22. string++;
  23. }
  24. /* is this the start of a quoted string ? */
  25. if (*string == '"') {
  26. *quoted = 1;
  27. string++;
  28. }
  29. word = string;
  30. *len = 0;
  31. /* find the end of the word */
  32. while (*string) {
  33. if (*string == '\\') {
  34. string++;
  35. (*len)++;
  36. if (*string) {
  37. (*len)++;
  38. string++;
  39. }
  40. continue;
  41. }
  42. /* end of word */
  43. if (!*quoted && isspace(*string))
  44. break;
  45. if (*string == '"') {
  46. /* end of a quoted string */
  47. *quoted = 2;
  48. break;
  49. }
  50. (*len)++;
  51. string++;
  52. }
  53. return word;
  54. }
  55. /*--------------------------------------------------------- */
  56. unsigned int lub_string_wordcount(const char *line)
  57. {
  58. const char *word;
  59. unsigned int result = 0;
  60. size_t len = 0, offset = 0;
  61. size_t quoted;
  62. for (word = lub_string_nextword(line, &len, &offset, &quoted);
  63. *word || quoted;
  64. word = lub_string_nextword(word + len, &len, &offset, &quoted)) {
  65. /* account for the terminating quotation mark */
  66. len += quoted ? quoted - 1 : 0;
  67. result++;
  68. }
  69. return result;
  70. }
  71. /*--------------------------------------------------------- */