Skip to content

Commit 2d55c7b

Browse files
committed
ipv4: Optimize ip4addr_aton()
If <ctype.h> is not used, then lwip_in_range() is available. Use it to optimize the hexadecimal number case. Here, the digit case is already handled. Check only for alphabet ranges and combine it with the case selection. This eliminates a couple of dead branches.
1 parent 477a892 commit 2d55c7b

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

src/core/ipv4/ip4_addr.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,8 +181,21 @@ ip4addr_aton(const char *cp, ip4_addr_t *addr)
181181
break;
182182
val = (val * base) + (u32_t)(c - '0');
183183
c = *++cp;
184+
#if LWIP_NO_CTYPE_H
185+
} else if (base == 16) {
186+
u32_t a;
187+
if (lwip_in_range(c, 'a', 'f')) {
188+
a = 'a';
189+
} else if (lwip_in_range(c, 'A', 'F')) {
190+
a = 'A';
191+
} else {
192+
break;
193+
}
194+
val = (val << 4) | (u32_t)(c - a + 10);
195+
#else /* LWIP_NO_CTYPE_H */
184196
} else if (base == 16 && lwip_isxdigit(c)) {
185197
val = (val << 4) | (u32_t)(c + 10 - (lwip_islower(c) ? 'a' : 'A'));
198+
#endif /* LWIP_NO_CTYPE_H */
186199
c = *++cp;
187200
} else {
188201
break;

0 commit comments

Comments
 (0)