On 05.02.2018 21:57, Collin L. Walling wrote:
Moved:
memcmp from bootmap.h to libc.h (renamed from _memcmp)
strlen from sclp.c to libc.h (renamed from _strlen)
Added C standard functions:
isdigit
Added non C-standard function:
itostr
atoui
Signed-off-by: Collin L. Walling <address@hidden>
Acked-by: Christian Borntraeger <address@hidden>
Reviewed-by: Janosch Frank <address@hidden>
---
[...]
+/**
+ * itostr:
+ * @num: an integer (base 10) to be converted.
+ * @str: a pointer to a string to store the conversion.
+ * @len: the length of the passed string.
+ *
+ * Given an integer @num, convert it to a string. The string @str must be
+ * allocated beforehand. The resulting string will be null terminated and
+ * returned. This function only handles numbers between 0 and UINT64_MAX
+ * inclusive.
+ *
+ * Returns: the string @str of the converted integer @num; NULL if @str
+ * is NULL or if there is not enough space allocated.
+ */
+char *itostr(uint64_t num, char *str, size_t len)
Nitpicking: You renamed atoi to atoui, so maybe this should now rather
be uitostr or uitoa now?
+{
+ size_t num_idx = 0;
+ uint64_t tmp = num;
+
+ IPL_assert(num >= 0, "itostr: cannot convert negative values");
(already mentioned by patchew)
+ IPL_assert(str != NULL, "itostr: no space allocated to store string");
+
+ /* Get index to ones place */
+ while ((tmp /= 10) != 0) {
+ num_idx++;
+ }
+
+ /* Check if we have enough space for num and null */
+ IPL_assert(len >= num_idx + 1, "itostr: array too small for conversion");
Should that rather be "len > num_idx + 1" instead?