threads / patch / 24514

patch, 2 partssimplify digit_in_number

Subject: [PATCH 2/2] simplify digit_in_number

## tl;dr

3 messages between Jul 25, 2010 and Jul 25, 2010. Diffs are folded; open one to read it.

replies: 2people: 2as markdown or json

Ralf Thielow· Jul 25, 2010, 17:36 UTC · lore

[PATCH 1/2] log-tree: simplify digit_in_number

---
 log-tree.c |   10 ++++------
 1 files changed, 4 insertions(+), 6 deletions(-)
Show changes to log-tree.c +4 −6
diff --git a/log-tree.c b/log-tree.c
index b46ed3b..95a00a4 100644
--- a/log-tree.c
+++ b/log-tree.c
@@ -254,12 +254,10 @@ static void append_signoff(struct strbuf *sb, const char *signoff)
 
 static unsigned int digits_in_number(unsigned int number)
 {
-	unsigned int i = 10, result = 1;
-	while (i <= number) {
-		i *= 10;
-		result++;
-	}
-	return result;
+	int digits = 0;
+	while (number /= 10) 
+		digits++;
+	return digits++;
 }
 
 void get_patch_filename(struct commit *commit, int nr, const char *suffix,
-- 
1.7.0.4
Ralf Thielow· Jul 25, 2010, 17:36 UTC · re: Ralf Thielow · lore
---
 log-tree.c |    4 ++--
 1 files changed, 2 insertions(+), 2 deletions(-)
Show changes to log-tree.c +2 −2
diff --git a/log-tree.c b/log-tree.c
index 95a00a4..c2af716 100644
--- a/log-tree.c
+++ b/log-tree.c
@@ -254,10 +254,10 @@ static void append_signoff(struct strbuf *sb, const char *signoff)
 
 static unsigned int digits_in_number(unsigned int number)
 {
-	int digits = 0;
+	int digits = 1;
 	while (number /= 10) 
 		digits++;
-	return digits++;
+	return digits;
 }
 
 void get_patch_filename(struct commit *commit, int nr, const char *suffix,
-- 
1.7.0.4
Jonathan Nieder· Jul 25, 2010, 17:46 UTC · re: Ralf Thielow · lore

Re: [PATCH 1/2] log-tree: simplify digit_in_number

Ralf Thielow wrote:
Show 15 quoted lines
> +++ b/log-tree.c
> @@ -254,12 +254,10 @@ static void append_signoff(struct strbuf *sb, const char *signoff)
>  
>  static unsigned int digits_in_number(unsigned int number)
>  {
> -	unsigned int i = 10, result = 1;
> -	while (i <= number) {
> -		i *= 10;
> -		result++;
> -	}
> -	return result;
> +	int digits = 0;
> +	while (number /= 10) 
> +		digits++;
> +	return digits++;
I think you mean "return ++digits;".
But other questions come to mind first:
 - What is the motivation?  How did this come up and what does your change
   improve?
 - Why rewrite this in one patch, only to rewrite it again?

Hope that helps, Jonathan

← back to recent threads