3
0

compare_string_array.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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 idx = 0;
  60. while (*strings) {
  61. if (strcmp(strings, key) == 0) {
  62. return idx;
  63. }
  64. strings += strlen(strings) + 1; /* skip NUL */
  65. idx++;
  66. }
  67. return -1;
  68. }
  69. /* returns the array index of the string, even if it matches only a beginning */
  70. /* (index of first match is returned, or -1) */
  71. #ifdef UNUSED
  72. int FAST_FUNC index_in_substr_array(const char *const string_array[], const char *key)
  73. {
  74. int i;
  75. if (key[0]) {
  76. for (i = 0; string_array[i] != 0; i++) {
  77. if (is_prefixed_with(string_array[i], key)) {
  78. return i;
  79. }
  80. }
  81. }
  82. return -1;
  83. }
  84. #endif
  85. int FAST_FUNC index_in_substrings(const char *strings, const char *key)
  86. {
  87. int matched_idx = -1;
  88. const int len = strlen(key);
  89. if (len) {
  90. int idx = 0;
  91. while (*strings) {
  92. if (strncmp(strings, key, len) == 0) {
  93. if (strings[len] == '\0')
  94. return idx; /* exact match */
  95. if (matched_idx >= 0)
  96. return -1; /* ambiguous match */
  97. matched_idx = idx;
  98. }
  99. strings += strlen(strings) + 1; /* skip NUL */
  100. idx++;
  101. }
  102. }
  103. return matched_idx;
  104. }
  105. const char* FAST_FUNC nth_string(const char *strings, int n)
  106. {
  107. while (n) {
  108. n--;
  109. strings += strlen(strings) + 1;
  110. }
  111. return strings;
  112. }
  113. #ifdef UNUSED_SO_FAR /* only brctl.c needs it yet */
  114. /* Returns 0 for no, 1 for yes or a negative value on error. */
  115. smallint FAST_FUNC yesno(const char *str)
  116. {
  117. static const char no_yes[] ALIGN1 =
  118. "0\0" "off\0" "no\0"
  119. "1\0" "on\0" "yes\0";
  120. int ret = index_in_substrings(no_yes, str);
  121. return ret / 3;
  122. }
  123. #endif
  124. #if ENABLE_UNIT_TEST
  125. BBUNIT_DEFINE_TEST(is_prefixed_with)
  126. {
  127. BBUNIT_ASSERT_STREQ(" bar", is_prefixed_with("foo bar", "foo"));
  128. BBUNIT_ASSERT_STREQ("bar", is_prefixed_with("foo bar", "foo "));
  129. BBUNIT_ASSERT_STREQ("", is_prefixed_with("foo", "foo"));
  130. BBUNIT_ASSERT_STREQ("foo", is_prefixed_with("foo", ""));
  131. BBUNIT_ASSERT_STREQ("", is_prefixed_with("", ""));
  132. BBUNIT_ASSERT_NULL(is_prefixed_with("foo", "bar foo"));
  133. BBUNIT_ASSERT_NULL(is_prefixed_with("foo foo", "bar"));
  134. BBUNIT_ASSERT_NULL(is_prefixed_with("", "foo"));
  135. BBUNIT_ENDTEST;
  136. }
  137. BBUNIT_DEFINE_TEST(is_suffixed_with)
  138. {
  139. BBUNIT_ASSERT_STREQ("bar", is_suffixed_with("foo bar", "bar"));
  140. BBUNIT_ASSERT_STREQ("foo", is_suffixed_with("foo", "foo"));
  141. BBUNIT_ASSERT_STREQ("", is_suffixed_with("foo", ""));
  142. BBUNIT_ASSERT_STREQ("", is_suffixed_with("", ""));
  143. BBUNIT_ASSERT_STREQ("foo", is_suffixed_with("barfoofoo", "foo"));
  144. BBUNIT_ASSERT_NULL(is_suffixed_with("foo", "bar foo"));
  145. BBUNIT_ASSERT_NULL(is_suffixed_with("foo foo", "bar"));
  146. BBUNIT_ASSERT_NULL(is_suffixed_with("", "foo"));
  147. BBUNIT_ENDTEST;
  148. }
  149. #endif /* ENABLE_UNIT_TEST */