Hello,
I suggest de-factorising the encode_digit function which is used in two different contexts with two different range of inputs.
diff --git a/lib/punycode.c b/lib/punycode.c
index b2ebfd2..98b68c4 100644
--- a/lib/punycode.c
+++ b/lib/punycode.c
@@ -96,18 +96,28 @@ decode_digit (punycode_uint cp)
cp - 97 < 26 ? cp - 97 : base;
}
-/* encode_digit(d,flag) returns the basic code point whose value */
-/* (when used for representing integers) is d, which needs to be in */
-/* the range 0 to base-1. The lowercase form is used unless flag is */
-/* nonzero, in which case the uppercase form is used. The behavior */
-/* is undefined if flag is nonzero and digit d has no uppercase form. */
+/* encode_digit(d) returns the basic code point whose value (when */
+/* used for representing integers) is d, which needs to be in the */
+/* range 0 to base-1. The output always uses lowercase letters. */
static char
-encode_digit (punycode_uint d, int flag)
+encode_digit (punycode_uint d)
{
- return d + 22 + 75 * (d < 26) - ((flag != 0) << 5);
+ return d + 22 + 75 * (d < 26);
+ /* 0..25 map to ASCII a..z */
+ /* 26..35 map to ASCII 0..9 */
+}
+
+/* encode_digit_as_letter(d,flag) returns the basic code point whose */
+/* value (when used for representing integers) is d, which has to be */
+/* in the range 0 to tmax-1. The lowercase form is used unless flag */
+/* is nonzero, in which case the uppercase form is used. */
+
+static char
+encode_digit_as_letter (punycode_uint d, int flag)
+{
+ return d + 97 - ((flag != 0) << 5);
/* 0..25 map to ASCII a..z or A..Z */
- /* 26..35 map to ASCII 0..9 */
}
/* flagged(bcp) tests whether a basic code point is flagged */
@@ -287,11 +297,11 @@ punycode_encode (size_t input_length,
k >= bias + tmax ? tmax : k - bias;
if (q < t)
break;
- output[out++] = encode_digit (t + (q - t) % (base - t), 0);
+ output[out++] = encode_digit (t + (q - t) % (base - t));
q = (q - t) / (base - t);
}
- output[out++] = encode_digit (q, case_flags && case_flags[j]);
+ output[out++] = encode_digit_as_letter (q, case_flags && case_fla
bias = adapt (delta, h + 1, h == b);
delta = 0;
++h;