Patchwork [2/2] strtosz(): Use suffix macros in switch() statement

login
register
mail settings
Submitter Jes Sorensen
Date Jan. 17, 2011, 5:12 p.m.
Message ID <1295284345-24524-3-git-send-email-Jes.Sorensen@redhat.com>
Download mbox | patch
Permalink /patch/79197/
State New
Headers show

Comments

Jes Sorensen - Jan. 17, 2011, 5:12 p.m.
From: Jes Sorensen <Jes.Sorensen@redhat.com>

Signed-off-by: Jes Sorensen <Jes.Sorensen@redhat.com>
---
 cutils.c |   10 +++++-----
 1 files changed, 5 insertions(+), 5 deletions(-)
Markus Armbruster - Jan. 18, 2011, 9:20 a.m.
Jes.Sorensen@redhat.com writes:

> From: Jes Sorensen <Jes.Sorensen@redhat.com>
>
> Signed-off-by: Jes Sorensen <Jes.Sorensen@redhat.com>
> ---
>  cutils.c |   10 +++++-----
>  1 files changed, 5 insertions(+), 5 deletions(-)
>
> diff --git a/cutils.c b/cutils.c
> index 328738c..f2c8bbd 100644
> --- a/cutils.c
> +++ b/cutils.c
> @@ -324,26 +324,26 @@ ssize_t strtosz_suffix(const char *nptr, char **end, const char default_suffix)
>          }
>      }
>      switch (toupper(d)) {
> -    case 'B':
> +    case STRTOSZ_DEFSUFFIX_B:
>          mul = 1;
>          if (mul_required) {
>              goto fail;
>          }
>          break;
> -    case 'K':
> +    case STRTOSZ_DEFSUFFIX_KB:
>          mul = 1 << 10;
>          break;
>      case 0:
>          if (mul_required) {
>              goto fail;
>          }
> -    case 'M':
> +    case STRTOSZ_DEFSUFFIX_MB:
>          mul = 1ULL << 20;
>          break;
> -    case 'G':
> +    case STRTOSZ_DEFSUFFIX_GB:
>          mul = 1ULL << 30;
>          break;
> -    case 'T':
> +    case STRTOSZ_DEFSUFFIX_TB:
>          mul = 1ULL << 40;
>          break;
>      default:

And this improves what?  Certainly not clarity.

In my opinion, the STRTOSZ_DEFSUFFIX_TB are useless chaff.  Chacun à son
goût.
Jes Sorensen - Jan. 18, 2011, 9:22 a.m.
On 01/18/11 10:20, Markus Armbruster wrote:
>> diff --git a/cutils.c b/cutils.c
>> index 328738c..f2c8bbd 100644
>> --- a/cutils.c
>> +++ b/cutils.c
>> @@ -324,26 +324,26 @@ ssize_t strtosz_suffix(const char *nptr, char **end, const char default_suffix)
>>          }
>>      }
>>      switch (toupper(d)) {
>> -    case 'B':
>> +    case STRTOSZ_DEFSUFFIX_B:
>>          mul = 1;
>>          if (mul_required) {
>>              goto fail;
>>          }
>>          break;
>> -    case 'K':
>> +    case STRTOSZ_DEFSUFFIX_KB:
>>          mul = 1 << 10;
>>          break;
>>      case 0:
>>          if (mul_required) {
>>              goto fail;
>>          }
>> -    case 'M':
>> +    case STRTOSZ_DEFSUFFIX_MB:
>>          mul = 1ULL << 20;
>>          break;
>> -    case 'G':
>> +    case STRTOSZ_DEFSUFFIX_GB:
>>          mul = 1ULL << 30;
>>          break;
>> -    case 'T':
>> +    case STRTOSZ_DEFSUFFIX_TB:
>>          mul = 1ULL << 40;
>>          break;
>>      default:
> 
> And this improves what?  Certainly not clarity.
> 
> In my opinion, the STRTOSZ_DEFSUFFIX_TB are useless chaff.  Chacun à son
> goût.

It cuts out lines of code, which is good, and using the macros means the
user is less likely to make a type and use a wrong character.

It's a taste issue though, I agree!

Cheers,
Jes
Alex Williamson - Jan. 18, 2011, 3:24 p.m.
On Tue, 2011-01-18 at 10:20 +0100, Markus Armbruster wrote:
> Jes.Sorensen@redhat.com writes:
> 
> > From: Jes Sorensen <Jes.Sorensen@redhat.com>
> >
> > Signed-off-by: Jes Sorensen <Jes.Sorensen@redhat.com>
> > ---
> >  cutils.c |   10 +++++-----
> >  1 files changed, 5 insertions(+), 5 deletions(-)
> >
> > diff --git a/cutils.c b/cutils.c
> > index 328738c..f2c8bbd 100644
> > --- a/cutils.c
> > +++ b/cutils.c
> > @@ -324,26 +324,26 @@ ssize_t strtosz_suffix(const char *nptr, char **end, const char default_suffix)
> >          }
> >      }
> >      switch (toupper(d)) {
> > -    case 'B':
> > +    case STRTOSZ_DEFSUFFIX_B:
> >          mul = 1;
> >          if (mul_required) {
> >              goto fail;
> >          }
> >          break;
> > -    case 'K':
> > +    case STRTOSZ_DEFSUFFIX_KB:
> >          mul = 1 << 10;
> >          break;
> >      case 0:
> >          if (mul_required) {
> >              goto fail;
> >          }
> > -    case 'M':
> > +    case STRTOSZ_DEFSUFFIX_MB:
> >          mul = 1ULL << 20;
> >          break;
> > -    case 'G':
> > +    case STRTOSZ_DEFSUFFIX_GB:
> >          mul = 1ULL << 30;
> >          break;
> > -    case 'T':
> > +    case STRTOSZ_DEFSUFFIX_TB:
> >          mul = 1ULL << 40;
> >          break;
> >      default:
> 
> And this improves what?  Certainly not clarity.
> 
> In my opinion, the STRTOSZ_DEFSUFFIX_TB are useless chaff.  Chacun à son
> goût.

<shrug> I prefer it.  Better than having 'B'/'K'/'M'/'G'/'T' sprinkled
throughout the code.  Easier to search for, easier to update, more
consistent.

Alex
Anthony Liguori - Jan. 18, 2011, 4:50 p.m.
On 01/18/2011 03:20 AM, Markus Armbruster wrote:
> Jes.Sorensen@redhat.com writes:
>
>    
>> From: Jes Sorensen<Jes.Sorensen@redhat.com>
>>
>> Signed-off-by: Jes Sorensen<Jes.Sorensen@redhat.com>
>> ---
>>   cutils.c |   10 +++++-----
>>   1 files changed, 5 insertions(+), 5 deletions(-)
>>
>> diff --git a/cutils.c b/cutils.c
>> index 328738c..f2c8bbd 100644
>> --- a/cutils.c
>> +++ b/cutils.c
>> @@ -324,26 +324,26 @@ ssize_t strtosz_suffix(const char *nptr, char **end, const char default_suffix)
>>           }
>>       }
>>       switch (toupper(d)) {
>> -    case 'B':
>> +    case STRTOSZ_DEFSUFFIX_B:
>>           mul = 1;
>>           if (mul_required) {
>>               goto fail;
>>           }
>>           break;
>> -    case 'K':
>> +    case STRTOSZ_DEFSUFFIX_KB:
>>           mul = 1<<  10;
>>           break;
>>       case 0:
>>           if (mul_required) {
>>               goto fail;
>>           }
>> -    case 'M':
>> +    case STRTOSZ_DEFSUFFIX_MB:
>>           mul = 1ULL<<  20;
>>           break;
>> -    case 'G':
>> +    case STRTOSZ_DEFSUFFIX_GB:
>>           mul = 1ULL<<  30;
>>           break;
>> -    case 'T':
>> +    case STRTOSZ_DEFSUFFIX_TB:
>>           mul = 1ULL<<  40;
>>           break;
>>       default:
>>      
> And this improves what?  Certainly not clarity.
>
> In my opinion, the STRTOSZ_DEFSUFFIX_TB are useless chaff.  Chacun à son
> goût.
>    

Yeah, I have to agree.  I'm not of the literals are evil camp.

BTW, a useful change would be to accept both upper and lower case letters.

Regards,

Anthony Liguori
Jes Sorensen - Jan. 18, 2011, 4:52 p.m.
On 01/18/11 17:50, Anthony Liguori wrote:
> On 01/18/2011 03:20 AM, Markus Armbruster wrote:
>> Jes.Sorensen@redhat.com writes:
>>
>>   
>>> From: Jes Sorensen<Jes.Sorensen@redhat.com>
>>>
>>> Signed-off-by: Jes Sorensen<Jes.Sorensen@redhat.com>
>>> ---
>>>   cutils.c |   10 +++++-----
>>>   1 files changed, 5 insertions(+), 5 deletions(-)
>>>
>>> diff --git a/cutils.c b/cutils.c
>>> index 328738c..f2c8bbd 100644
>>> --- a/cutils.c
>>> +++ b/cutils.c
>>> @@ -324,26 +324,26 @@ ssize_t strtosz_suffix(const char *nptr, char
>>> **end, const char default_suffix)
>>>           }
>>>       }
>>>       switch (toupper(d)) {

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

>>>      
>> And this improves what?  Certainly not clarity.
>>
>> In my opinion, the STRTOSZ_DEFSUFFIX_TB are useless chaff.  Chacun à son
>> goût.
>>    
> 
> Yeah, I have to agree.  I'm not of the literals are evil camp.
> 
> BTW, a useful change would be to accept both upper and lower case letters.

It already supports both, it's handle in the toupper() call.

Cheers,
Jes
Eric Blake - Jan. 18, 2011, 4:53 p.m.
On 01/18/2011 09:50 AM, Anthony Liguori wrote:
>>> @@ -324,26 +324,26 @@ ssize_t strtosz_suffix(const char *nptr, char
>>> **end, const char default_suffix)
>>>           }
>>>       }
>>>       switch (toupper(d)) {
> BTW, a useful change would be to accept both upper and lower case letters.

And it does, via the toupper() added earlier in the series (and which
has separately been pointed out that using qemu_toupper() might be nicer).
Anthony Liguori - Jan. 18, 2011, 8:30 p.m.
On 01/18/2011 10:53 AM, Eric Blake wrote:
> On 01/18/2011 09:50 AM, Anthony Liguori wrote:
>    
>>>> @@ -324,26 +324,26 @@ ssize_t strtosz_suffix(const char *nptr, char
>>>> **end, const char default_suffix)
>>>>            }
>>>>        }
>>>>        switch (toupper(d)) {
>>>>          
>> BTW, a useful change would be to accept both upper and lower case letters.
>>      
> And it does, via the toupper() added earlier in the series (and which
> has separately been pointed out that using qemu_toupper() might be nicer).
>    

Ok.  Just taking the different case labels would be nicer IMHO.

Regards,

Anthony Liguori
Jes Sorensen - Jan. 18, 2011, 8:36 p.m.
On 01/18/11 21:30, Anthony Liguori wrote:
> On 01/18/2011 10:53 AM, Eric Blake wrote:
>> On 01/18/2011 09:50 AM, Anthony Liguori wrote:
>>   
>>>>> @@ -324,26 +324,26 @@ ssize_t strtosz_suffix(const char *nptr, char
>>>>> **end, const char default_suffix)
>>>>>            }
>>>>>        }
>>>>>        switch (toupper(d)) {
>>>>>          
>>> BTW, a useful change would be to accept both upper and lower case
>>> letters.
>>>      
>> And it does, via the toupper() added earlier in the series (and which
>> has separately been pointed out that using qemu_toupper() might be
>> nicer).
>>    
> 
> Ok.  Just taking the different case labels would be nicer IMHO.

The old code did that, but I was suggested to do it this way, which I
think is cleaner too. Fewer lines of code are easier to read.

Cheers,
Jes
Anthony Liguori - Jan. 18, 2011, 8:39 p.m.
On 01/18/2011 02:36 PM, Jes Sorensen wrote:
> On 01/18/11 21:30, Anthony Liguori wrote:
>    
>> On 01/18/2011 10:53 AM, Eric Blake wrote:
>>      
>>> On 01/18/2011 09:50 AM, Anthony Liguori wrote:
>>>
>>>        
>>>>>> @@ -324,26 +324,26 @@ ssize_t strtosz_suffix(const char *nptr, char
>>>>>> **end, const char default_suffix)
>>>>>>             }
>>>>>>         }
>>>>>>         switch (toupper(d)) {
>>>>>>
>>>>>>              
>>>> BTW, a useful change would be to accept both upper and lower case
>>>> letters.
>>>>
>>>>          
>>> And it does, via the toupper() added earlier in the series (and which
>>> has separately been pointed out that using qemu_toupper() might be
>>> nicer).
>>>
>>>        
>> Ok.  Just taking the different case labels would be nicer IMHO.
>>      
> The old code did that, but I was suggested to do it this way, which I
> think is cleaner too. Fewer lines of code are easier to read.
>    

toupper() is based on locale so it's not consistent.

Regards,

Anthony Liguori

> Cheers,
> Jes
>
Jes Sorensen - Jan. 19, 2011, 10:03 a.m.
On 01/18/11 21:39, Anthony Liguori wrote:
> On 01/18/2011 02:36 PM, Jes Sorensen wrote:
>> On 01/18/11 21:30, Anthony Liguori wrote:  
>>> On 01/18/2011 10:53 AM, Eric Blake wrote:       
>>>> And it does, via the toupper() added earlier in the series (and which
>>>> has separately been pointed out that using qemu_toupper() might be
>>>> nicer).
>>>>        
>>> Ok.  Just taking the different case labels would be nicer IMHO.
>>>      
>> The old code did that, but I was suggested to do it this way, which I
>> think is cleaner too. Fewer lines of code are easier to read.
>>    
> toupper() is based on locale so it's not consistent.

If you can show me an actually used locale where the toupper() on
k/m/g/t doesn't result in K/M/G/T then I guess there's a case. Otherwise
I don't really see this being a real point.

I think we are hitting the point where it's about who's taste is better,
and not about the actual code in this discussion.

One point in favor of the patch is this:

Without the patch:
jes@red-feather qemu]$ size cutils.o
   text	   data	    bss	    dec	    hex	filename
   4212	      0	      0	   4212	   1074	cutils.o
With patch:
[jes@red-feather qemu]$ size cutils.o
   text	   data	    bss	    dec	    hex	filename
   4196	      0	      0	   4196	   1064	cutils.o

IMHO it makes the code easier to read, but beyond that there isn't much.
If people are strongly against it, I'll just drop the patch. It's not
worth our time arguing over this level of detail. Otherwise I'd like to
see it applied.

Cheers,
Jes

Patch

diff --git a/cutils.c b/cutils.c
index 328738c..f2c8bbd 100644
--- a/cutils.c
+++ b/cutils.c
@@ -324,26 +324,26 @@  ssize_t strtosz_suffix(const char *nptr, char **end, const char default_suffix)
         }
     }
     switch (toupper(d)) {
-    case 'B':
+    case STRTOSZ_DEFSUFFIX_B:
         mul = 1;
         if (mul_required) {
             goto fail;
         }
         break;
-    case 'K':
+    case STRTOSZ_DEFSUFFIX_KB:
         mul = 1 << 10;
         break;
     case 0:
         if (mul_required) {
             goto fail;
         }
-    case 'M':
+    case STRTOSZ_DEFSUFFIX_MB:
         mul = 1ULL << 20;
         break;
-    case 'G':
+    case STRTOSZ_DEFSUFFIX_GB:
         mul = 1ULL << 30;
         break;
-    case 'T':
+    case STRTOSZ_DEFSUFFIX_TB:
         mul = 1ULL << 40;
         break;
     default: