diff options
author | Lars-Peter Clausen | 2020-11-14 13:00:00 +0100 |
---|---|---|
committer | Jonathan Cameron | 2020-12-03 19:32:44 +0000 |
commit | e08b60d352dbc3c32d64bdaedbb481fcdc14ff6c (patch) | |
tree | 9913703ede1f1bff85ca4c6d2580695cd7b0d645 /drivers/iio | |
parent | eda20ba1e25e6d8e7fa0c62d1bd685aa5de8e8ff (diff) |
iio: core: Simplify iio_format_list()
iio_format_list() has two branches in a switch statement that are almost
identical. They only differ in the stride that is used to iterate through
the item list.
Consolidate this into a common code path to simplify the code.
Signed-off-by: Lars-Peter Clausen <lars@metafoo.de>
Link: https://lore.kernel.org/r/20201114120000.6533-2-lars@metafoo.de
Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
Diffstat (limited to 'drivers/iio')
-rw-r--r-- | drivers/iio/industrialio-core.c | 45 |
1 files changed, 18 insertions, 27 deletions
diff --git a/drivers/iio/industrialio-core.c b/drivers/iio/industrialio-core.c index 9346f1c788b8..c2e4c267c36b 100644 --- a/drivers/iio/industrialio-core.c +++ b/drivers/iio/industrialio-core.c @@ -715,44 +715,35 @@ static ssize_t iio_format_list(char *buf, const int *vals, int type, int length, const char *prefix, const char *suffix) { ssize_t len; + int stride; int i; - len = scnprintf(buf, PAGE_SIZE, prefix); - switch (type) { case IIO_VAL_INT: - for (i = 0; i < length; i++) { - len += __iio_format_value(buf + len, PAGE_SIZE - len, - type, 1, &vals[i]); - if (len >= PAGE_SIZE) - return -EFBIG; - if (i < length - 1) - len += scnprintf(buf + len, PAGE_SIZE - len, - " "); - else - len += scnprintf(buf + len, PAGE_SIZE - len, - "%s\n", suffix); - if (len >= PAGE_SIZE) - return -EFBIG; - } + stride = 1; break; default: - for (i = 0; i < length / 2; i++) { - len += __iio_format_value(buf + len, PAGE_SIZE - len, - type, 2, &vals[i * 2]); - if (len >= PAGE_SIZE) - return -EFBIG; - if (i < length / 2 - 1) - len += scnprintf(buf + len, PAGE_SIZE - len, - " "); - else - len += scnprintf(buf + len, PAGE_SIZE - len, - "%s\n", suffix); + stride = 2; + break; + } + + len = scnprintf(buf, PAGE_SIZE, prefix); + + for (i = 0; i <= length - stride; i += stride) { + if (i != 0) { + len += scnprintf(buf + len, PAGE_SIZE - len, " "); if (len >= PAGE_SIZE) return -EFBIG; } + + len += __iio_format_value(buf + len, PAGE_SIZE - len, type, + stride, &vals[i]); + if (len >= PAGE_SIZE) + return -EFBIG; } + len += scnprintf(buf + len, PAGE_SIZE - len, "%s\n", suffix); + return len; } |