On Thursday 18 March 2010 12:27, Bernhard Reutner-Fischer wrote:
> untested
>
> Signed-off-by: Bernhard Reutner-Fischer <[email protected]>
> ---
> include/libbb.h | 1 +
> libbb/compare_string_array.c | 25 +++++++++++++++++++++----
> networking/udhcp/files.c | 14 +++++---------
> 3 files changed, 27 insertions(+), 13 deletions(-)
>
> diff --git a/include/libbb.h b/include/libbb.h
> index 9d99b0d..c24c4c1 100644
> --- a/include/libbb.h
> +++ b/include/libbb.h
> @@ -1190,6 +1190,7 @@ int index_in_strings(const char *strings, const char
> *key) FAST_FUNC;
> int index_in_substr_array(const char *const string_array[], const char *key)
> FAST_FUNC;
> int index_in_substrings(const char *strings, const char *key) FAST_FUNC;
> const char *nth_string(const char *strings, int n) FAST_FUNC;
> +smallint yesno(const char *str) FAST_FUNC;
>
> extern void print_login_issue(const char *issue_file, const char *tty)
> FAST_FUNC;
> extern void print_login_prompt(void) FAST_FUNC;
> diff --git a/libbb/compare_string_array.c b/libbb/compare_string_array.c
> index 43c59e8..e382cda 100644
> --- a/libbb/compare_string_array.c
> +++ b/libbb/compare_string_array.c
> @@ -53,19 +53,26 @@ int FAST_FUNC index_in_substr_array(const char *const
> string_array[], const char
>
> int FAST_FUNC index_in_substrings(const char *strings, const char *key)
> {
> - int len = strlen(key);
> + const size_t len = strlen(key) /*key ? strlen(key) : 0*/;
> + int idx, best_len, best_idx = -1;
>
> if (len) {
> - int idx = 0;
> + idx = best_len = 0;
> while (*strings) {
> if (strncmp(strings, key, len) == 0) {
> - return idx;
> + size_t string_len = strlen(strings);
> + if (string_len > best_len) {
> + best_len = string_len;
> + best_idx = idx;
> + } else if (string_len == best_len) {
> + return -1; /* Ambiguous match */
> + }
> }
> strings += strlen(strings) + 1; /* skip NUL */
This prefers longer matches: "foo" = "foobarbaz" preferred
over "foo" = "foobar". Don't see why it should be this way.
More logical is to return -1, since the match is ambiguous in this case.
You calculate strlen(strings) twice.
> +/* Returns 0 for no, 1 for yes or a negative value on error. */
> +smallint FAST_FUNC yesno(const char *str)
> +{
> + static const char yes_no[] ALIGN1 =
> + "0\0" "off\0" "no\0"
> + "1\0" "on\0" "yes\0";
> + int ret = str ? index_in_substrings(yes_no, str) : -1;
Why do you want to allow NULL?
> + return (ret == -1) ? -1 : (ret > 2);
ret / 3 is shorter. (Is -1 / 3 always == -1 in C?)
> --- a/networking/udhcp/files.c
> +++ b/networking/udhcp/files.c
> @@ -63,16 +63,12 @@ static int FAST_FUNC read_u32(const char *line, void *arg)
> static int FAST_FUNC read_yn(const char *line, void *arg)
This function is unused, and is now commented out.
Thus yesno() is not needed yet, since there is only one user - brctl.
How about this patch?
function old new delta
index_in_substrings 67 93 +26
--
vda
diff -ad -urpN busybox.5/libbb/compare_string_array.c
busybox.6/libbb/compare_string_array.c
--- busybox.5/libbb/compare_string_array.c 2010-03-28 20:06:04.000000000
+0200
+++ busybox.6/libbb/compare_string_array.c 2010-04-02 08:05:14.000000000
+0200
@@ -53,19 +53,24 @@ int FAST_FUNC index_in_substr_array(cons
int FAST_FUNC index_in_substrings(const char *strings, const char *key)
{
- int len = strlen(key);
+ int matched_idx = -1;
+ const int len = strlen(key);
if (len) {
int idx = 0;
while (*strings) {
if (strncmp(strings, key, len) == 0) {
- return idx;
+ if (strings[len] == '\0')
+ return idx; /* Exact match */
+ if (matched_idx >= 0)
+ return -1; /* Ambiguous match */
+ matched_idx = idx;
}
strings += strlen(strings) + 1; /* skip NUL */
idx++;
}
}
- return -1;
+ return matched_idx;
}
const char* FAST_FUNC nth_string(const char *strings, int n)
@@ -76,3 +81,15 @@ const char* FAST_FUNC nth_string(const c
}
return strings;
}
+
+#ifdef UNUSED_SO_FAR
+/* Returns 0 for no, 1 for yes or a negative value on error. */
+smallint FAST_FUNC yesno(const char *str)
+{
+ static const char yes_no[] ALIGN1 =
+ "0\0" "off\0" "no\0"
+ "1\0" "on\0" "yes\0";
+ int ret = index_in_substrings(yes_no, str);
+ return ret / 3;
+}
+#endif
_______________________________________________
busybox mailing list
[email protected]
http://lists.busybox.net/mailman/listinfo/busybox