[1/8] btrfs-progs: cleanup and comment parse_range
diff mbox

Message ID 4d4646c4a4938b58e4c5bd0ab57361b0261d202b.1445011409.git.dsterba@suse.com
State New
Headers show

Commit Message

David Sterba Oct. 16, 2015, 4:17 p.m. UTC
Simplify a check and unindent some code.

Signed-off-by: David Sterba <dsterba@suse.com>
---
 cmds-balance.c | 63 ++++++++++++++++++++++++++++++++++------------------------
 1 file changed, 37 insertions(+), 26 deletions(-)

Patch
diff mbox

diff --git a/cmds-balance.c b/cmds-balance.c
index 9af218bbfa51..798b533aa7d6 100644
--- a/cmds-balance.c
+++ b/cmds-balance.c
@@ -88,43 +88,54 @@  static int parse_u64(const char *str, u64 *result)
 	return 0;
 }
 
+/*
+ * Parse range that's missing some part that can be implicit:
+ * a..b	- exact range, a can be equal to b
+ * a..	- implicitly unbounded maximum (end == (u64)-1)
+ * ..b	- implicitly starting at 0
+ * a	- invalid; unclear semantics, use parse_u64 instead
+ *
+ * Returned values are u64, value validation and interpretation should be done
+ * by the caller.
+ */
 static int parse_range(const char *range, u64 *start, u64 *end)
 {
 	char *dots;
+	const char *rest;
+	int skipped = 0;
 
 	dots = strstr(range, "..");
-	if (dots) {
-		const char *rest = dots + 2;
-		int skipped = 0;
-
-		*dots = 0;
+	if (!dots)
+		return 1;
 
-		if (!*rest) {
-			*end = (u64)-1;
-			skipped++;
-		} else {
-			if (parse_u64(rest, end))
-				return 1;
-		}
-		if (dots == range) {
-			*start = 0;
-			skipped++;
-		} else {
-			if (parse_u64(range, start))
-				return 1;
-		}
+	rest = dots + 2;
+	*dots = 0;
 
-		if (*start >= *end) {
-			fprintf(stderr, "Range %llu..%llu doesn't make "
-				"sense\n", (unsigned long long)*start,
-				(unsigned long long)*end);
+	if (!*rest) {
+		*end = (u64)-1;
+		skipped++;
+	} else {
+		if (parse_u64(rest, end))
 			return 1;
-		}
+	}
+	if (dots == range) {
+		*start = 0;
+		skipped++;
+	} else {
+		if (parse_u64(range, start))
+			return 1;
+	}
 
-		if (skipped <= 1)
-			return 0;
+	if (*start >= *end) {
+		fprintf(stderr, "Range %llu..%llu doesn't make "
+			"sense\n", (unsigned long long)*start,
+			(unsigned long long)*end);
+		return 1;
 	}
 
+	if (skipped <= 1)
+		return 0;
+
 	return 1;
 }