Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

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>

authored by

Lars-Peter Clausen and committed by
Jonathan Cameron
e08b60d3 eda20ba1

+18 -27
+18 -27
drivers/iio/industrialio-core.c
··· 715 715 const char *prefix, const char *suffix) 716 716 { 717 717 ssize_t len; 718 + int stride; 718 719 int i; 719 - 720 - len = scnprintf(buf, PAGE_SIZE, prefix); 721 720 722 721 switch (type) { 723 722 case IIO_VAL_INT: 724 - for (i = 0; i < length; i++) { 725 - len += __iio_format_value(buf + len, PAGE_SIZE - len, 726 - type, 1, &vals[i]); 727 - if (len >= PAGE_SIZE) 728 - return -EFBIG; 729 - if (i < length - 1) 730 - len += scnprintf(buf + len, PAGE_SIZE - len, 731 - " "); 732 - else 733 - len += scnprintf(buf + len, PAGE_SIZE - len, 734 - "%s\n", suffix); 735 - if (len >= PAGE_SIZE) 736 - return -EFBIG; 737 - } 723 + stride = 1; 738 724 break; 739 725 default: 740 - for (i = 0; i < length / 2; i++) { 741 - len += __iio_format_value(buf + len, PAGE_SIZE - len, 742 - type, 2, &vals[i * 2]); 743 - if (len >= PAGE_SIZE) 744 - return -EFBIG; 745 - if (i < length / 2 - 1) 746 - len += scnprintf(buf + len, PAGE_SIZE - len, 747 - " "); 748 - else 749 - len += scnprintf(buf + len, PAGE_SIZE - len, 750 - "%s\n", suffix); 726 + stride = 2; 727 + break; 728 + } 729 + 730 + len = scnprintf(buf, PAGE_SIZE, prefix); 731 + 732 + for (i = 0; i <= length - stride; i += stride) { 733 + if (i != 0) { 734 + len += scnprintf(buf + len, PAGE_SIZE - len, " "); 751 735 if (len >= PAGE_SIZE) 752 736 return -EFBIG; 753 737 } 738 + 739 + len += __iio_format_value(buf + len, PAGE_SIZE - len, type, 740 + stride, &vals[i]); 741 + if (len >= PAGE_SIZE) 742 + return -EFBIG; 754 743 } 744 + 745 + len += scnprintf(buf + len, PAGE_SIZE - len, "%s\n", suffix); 755 746 756 747 return len; 757 748 }