3
0

compare_string_array.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  4. */
  5. #include "libbb.h"
  6. /*
  7. * Return NULL if string is not prefixed with key. Return pointer to the
  8. * first character in string after the prefix key. If key is an empty string,
  9. * return pointer to the beginning of string.
  10. */
  11. char* FAST_FUNC is_prefixed_with(const char *string, const char *key)
  12. {
  13. #if 0 /* Two passes over key - probably slower */
  14. int len = strlen(key);
  15. if (strncmp(string, key, len) == 0)
  16. return string + len;
  17. return NULL;
  18. #else /* Open-coded */
  19. while (*key != '\0') {
  20. if (*key != *string)
  21. return NULL;
  22. key++;
  23. string++;
  24. }
  25. return (char*)string;
  26. #endif
  27. }
  28. /*
  29. * Return NULL if string is not suffixed with key. Return pointer to the
  30. * beginning of prefix key in string. If key is an empty string return pointer
  31. * to the end of string.
  32. */
  33. char* FAST_FUNC is_suffixed_with(const char *string, const char *key)
  34. {
  35. size_t key_len = strlen(key);
  36. ssize_t len_diff = strlen(string) - key_len;
  37. if (len_diff >= 0) {
  38. string += len_diff;
  39. if (strcmp(string, key) == 0) {
  40. return (char*)string;
  41. }
  42. }
  43. return NULL;
  44. }
  45. /* returns the array index of the string */
  46. /* (index of first match is returned, or -1) */
  47. int FAST_FUNC index_in_str_array(const char *const string_array[], const char *key)
  48. {
  49. int i;
  50. for (i = 0; string_array[i] != 0; i++) {
  51. if (strcmp(string_array[i], key) == 0) {
  52. return i;
  53. }
  54. }
  55. return -1;
  56. }
  57. int FAST_FUNC index_in_strings(const char *strings, const char *key)
  58. {
  59. int j, idx = 0;
  60. while (*strings) {
  61. /* Do we see "key\0" at current position in strings? */
  62. for (j = 0; *strings == key[j]; ++j) {
  63. if (*strings++ == '\0') {
  64. //bb_error_msg("found:'%s' i:%u", key, idx);
  65. return idx; /* yes */
  66. }
  67. }
  68. /* No. Move to the start of the next string. */
  69. while (*strings++ != '\0')
  70. continue;
  71. idx++;
  72. }
  73. return -1;
  74. }
  75. /* returns the array index of the string, even if it matches only a beginning */
  76. /* (index of first match is returned, or -1) */
  77. #ifdef UNUSED
  78. int FAST_FUNC index_in_substr_array(const char *const string_array[], const char *key)
  79. {
  80. int i;
  81. if (key[0]) {
  82. for (i = 0; string_array[i] != 0; i++) {
  83. if (is_prefixed_with(string_array[i], key)) {
  84. return i;
  85. }
  86. }
  87. }
  88. return -1;
  89. }
  90. #endif
  91. int FAST_FUNC index_in_substrings(const char *strings, const char *key)
  92. {
  93. int matched_idx = -1;
  94. const int len = strlen(key);
  95. if (len) {
  96. int idx = 0;
  97. while (*strings) {
  98. if (strncmp(strings, key, len) == 0) {
  99. if (strings[len] == '\0')
  100. return idx; /* exact match */
  101. if (matched_idx >= 0)
  102. return -1; /* ambiguous match */
  103. matched_idx = idx;
  104. }
  105. strings += strlen(strings) + 1; /* skip NUL */
  106. idx++;
  107. }
  108. }
  109. return matched_idx;
  110. }
  111. const char* FAST_FUNC nth_string(const char *strings, int n)
  112. {
  113. while (n) {
  114. if (*strings++ == '\0') {
  115. if (*strings == '\0') /* reached end of strings */
  116. break;
  117. n--;
  118. }
  119. }
  120. return strings;
  121. }
  122. #ifdef UNUSED_SO_FAR /* only brctl.c needs it yet */
  123. /* Returns 0 for no, 1 for yes or a negative value on error. */
  124. smallint FAST_FUNC yesno(const char *str)
  125. {
  126. static const char no_yes[] ALIGN1 =
  127. "0\0" "off\0" "no\0"
  128. "1\0" "on\0" "yes\0";
  129. int ret = index_in_substrings(no_yes, str);
  130. return ret / 3;
  131. }
  132. #endif
  133. #if ENABLE_UNIT_TEST
  134. BBUNIT_DEFINE_TEST(is_prefixed_with)
  135. {
  136. BBUNIT_ASSERT_STREQ(" bar", is_prefixed_with("foo bar", "foo"));
  137. BBUNIT_ASSERT_STREQ("bar", is_prefixed_with("foo bar", "foo "));
  138. BBUNIT_ASSERT_STREQ("", is_prefixed_with("foo", "foo"));
  139. BBUNIT_ASSERT_STREQ("foo", is_prefixed_with("foo", ""));
  140. BBUNIT_ASSERT_STREQ("", is_prefixed_with("", ""));
  141. BBUNIT_ASSERT_NULL(is_prefixed_with("foo", "bar foo"));
  142. BBUNIT_ASSERT_NULL(is_prefixed_with("foo foo", "bar"));
  143. BBUNIT_ASSERT_NULL(is_prefixed_with("", "foo"));
  144. BBUNIT_ENDTEST;
  145. }
  146. BBUNIT_DEFINE_TEST(is_suffixed_with)
  147. {
  148. BBUNIT_ASSERT_STREQ("bar", is_suffixed_with("foo bar", "bar"));
  149. BBUNIT_ASSERT_STREQ("foo", is_suffixed_with("foo", "foo"));
  150. BBUNIT_ASSERT_STREQ("", is_suffixed_with("foo", ""));
  151. BBUNIT_ASSERT_STREQ("", is_suffixed_with("", ""));
  152. BBUNIT_ASSERT_STREQ("foo", is_suffixed_with("barfoofoo", "foo"));
  153. BBUNIT_ASSERT_NULL(is_suffixed_with("foo", "bar foo"));
  154. BBUNIT_ASSERT_NULL(is_suffixed_with("foo foo", "bar"));
  155. BBUNIT_ASSERT_NULL(is_suffixed_with("", "foo"));
  156. BBUNIT_ENDTEST;
  157. }
  158. #endif /* ENABLE_UNIT_TEST */