git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH v3] http: Add Accept-Language header if possible

From
Yi, EungJun <semtlenori@gmail.com>
Date
Jul 13, 2014, 09:51 UTC
Message-ID
<CAFT+Tg8pJrdoD4M1uy6A=PPJaSrq_QcRUQdxdrA-T5sJiZ7j1Q@mail.gmail.com>
In-Reply-To
<CAPig+cRwJhyZ=R_HGs9JKUFLuJxoS=abrD9+HuoWDiA5oJrtjw@mail.gmail.com>
2014-07-13 13:26 GMT+09:00 Eric Sunshine <sunshine@sunshineco.com>:
Show 21 quoted lines
>> +       /* Decide the precision for q-factor on number of preferred languages. */
>> +       if (num_langs + 1 > 100) { /* +1 is for '*' */
>> +               q_precision = 0.001;
>> +               q_format = "; q=%.3f";
>> +       } else if (num_langs + 1 > 10) { /* +1 is for '*' */
>> +               q_precision = 0.01;
>> +               q_format = "; q=%.2f";
>> +       }
>
> It might make sense to have a final 'else' here which sets these
> variables for the 0.1 case so that the reader of the code doesn't have
> to refer back to the top of the function to figure out what is going
> on.
>
>     } else {
>         q_precision = 0.1;
>         q_format = "; q=%.1f";
>     }
>
> Better yet, would it be possible to compute these values rather than
> having to set them manually via a cascading if-chain?
I think it is possible like this:
    num_langs += 1; /* for '*' */
    decimal_places = 1 + (num_langs > 10) + (num_langs > 100);
    snprintf(q_format, sizeof(q_format), "; q=%%.%df", decimal_places);
    for (q_precision = 1.0; decimal_places-- > 0;) q_precision /= 10;
Does this one look better than before? I'm not sure which one is better.

ps. The last line can be simpler by using pow() but I'm not sure it is okay to include math.h.

Previous: Eric SunshineNext: Junio C Hamano
Message 7 of 9 in “http: Add Accept-Language header if possible”
  1. http: Add Accept-Language header if possibleYi EungJun, Jul 11, 2014
  2. Jeff KingJul 11, 2014
  3. Eric SunshineJul 11, 2014
  4. Yi, EungJunJul 12, 2014
  5. Peter KreftingJul 12, 2014
  6. Eric SunshineJul 13, 2014
  7. Yi, EungJunJul 13, 2014
  8. Junio C HamanoJul 13, 2014
  9. Yi, EungJunJul 14, 2014

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.