diff --git a/hal/armv7a/string.c b/hal/armv7a/string.c index 8fc36ae1d..43b962e49 100644 --- a/hal/armv7a/string.c +++ b/hal/armv7a/string.c @@ -176,9 +176,14 @@ unsigned long hal_i2s(const char *prefix, char *s, unsigned long i, unsigned cha m = hal_strlen(prefix); hal_memcpy(s, prefix, m); - for (k = m, l = (unsigned long)-1; l; i /= b, l /= b) { - if (!zero && !i) + if ((zero == 0) && (i == 0)) { + s[m++] = '0'; + } + + for (k = m, l = (unsigned long)-1; l != 0; i /= b, l /= b) { + if ((zero == 0) && (i == 0)) { break; + } s[k++] = digits[i % b]; } diff --git a/hal/armv7m/string.c b/hal/armv7m/string.c index 8ad10d8d8..ad3d8cd86 100644 --- a/hal/armv7m/string.c +++ b/hal/armv7m/string.c @@ -177,9 +177,14 @@ unsigned long hal_i2s(const char *prefix, char *s, unsigned long i, unsigned cha m = hal_strlen(prefix); hal_memcpy(s, prefix, m); - for (k = m, l = (unsigned long)-1; l; i /= b, l /= b) { - if (!zero && !i) + if ((zero == 0) && (i == 0)) { + s[m++] = '0'; + } + + for (k = m, l = (unsigned long)-1; l != 0; i /= b, l /= b) { + if ((zero == 0) && (i == 0)) { break; + } s[k++] = digits[i % b]; } diff --git a/hal/armv8m/string.c b/hal/armv8m/string.c index 4e62b74c3..4340ed993 100644 --- a/hal/armv8m/string.c +++ b/hal/armv8m/string.c @@ -178,8 +178,12 @@ unsigned long hal_i2s(const char *prefix, char *s, unsigned long i, unsigned cha m = hal_strlen(prefix); hal_memcpy(s, prefix, m); - for (k = m, l = (unsigned long)-1; l; i /= b, l /= b) { - if (!zero && !i) { + if ((zero == 0) && (i == 0)) { + s[m++] = '0'; + } + + for (k = m, l = (unsigned long)-1; l != 0; i /= b, l /= b) { + if ((zero == 0) && (i == 0)) { break; } s[k++] = digits[i % b]; diff --git a/hal/ia32/string.c b/hal/ia32/string.c index 8e73c85ab..0fc61efe6 100644 --- a/hal/ia32/string.c +++ b/hal/ia32/string.c @@ -40,13 +40,15 @@ void hal_memcpy(void *dst, const void *src, size_t l) int hal_memcmp(const void *ptr1, const void *ptr2, size_t num) { - int i; + size_t i; for (i = 0; i < num; ++i) { - if (((const u8 *)ptr1)[i] < ((const u8 *)ptr2)[i]) + if (((const u8 *)ptr1)[i] < ((const u8 *)ptr2)[i]) { return -1; - else if (((const u8 *)ptr1)[i] > ((const u8 *)ptr2)[i]) + } + else if (((const u8 *)ptr1)[i] > ((const u8 *)ptr2)[i]) { return 1; + } } return 0; @@ -85,10 +87,11 @@ void hal_memset(void *dst, int v, size_t l) size_t hal_strlen(const char *s) { - unsigned int k; + size_t k; - for (k = 0; *s; s++, k++) + for (k = 0; *s; s++, k++) { ; + } return k; } @@ -99,18 +102,21 @@ int hal_strcmp(const char *s1, const char *s2) const unsigned char *us1 = (const unsigned char *)s1; const unsigned char *us2 = (const unsigned char *)s2; const unsigned char *p; - unsigned int k; + size_t k; for (p = us1, k = 0; *p; p++, k++) { - if (*p < *(us2 + k)) + if (*p < *(us2 + k)) { return -1; - else if (*p > *(us2 + k)) + } + else if (*p > *(us2 + k)) { return 1; + } } - if (*p != *(us2 + k)) + if (*p != *(us2 + k)) { return -1; + } return 0; } @@ -120,13 +126,15 @@ int hal_strncmp(const char *s1, const char *s2, size_t count) { const unsigned char *us1 = (const unsigned char *)s1; const unsigned char *us2 = (const unsigned char *)s2; - unsigned int k; + size_t k; - for (k = 0; k < count && *us1 && *us2 && (*us1 == *us2); ++k, ++us1, ++us2) + for (k = 0; (k < count) && (*us1 != 0) && (*us2 != 0) && (*us1 == *us2); ++k, ++us1, ++us2) { ; + } - if (k == count || (!*us1 && !*us2)) + if ((k == count) || ((*us1 == 0) && (*us2 == 0))) { return 0; + } return (*us1 < *us2) ? -k - 1 : k + 1; } @@ -134,7 +142,7 @@ int hal_strncmp(const char *s1, const char *s2, size_t count) char *hal_strcpy(char *dest, const char *src) { - int i = 0; + size_t i = 0; do { dest[i] = src[i]; @@ -146,15 +154,16 @@ char *hal_strcpy(char *dest, const char *src) char *hal_strncpy(char *dest, const char *src, size_t n) { - int i = 0; + size_t i = 0; - if (n == 0) + if (n == 0) { return dest; + } do { dest[i] = src[i]; i++; - } while (i < n && src[i - 1] != '\0'); + } while ((i < n) && (src[i - 1] != '\0')); return dest; } @@ -169,9 +178,14 @@ unsigned long hal_i2s(const char *prefix, char *s, unsigned long i, unsigned cha m = hal_strlen(prefix); hal_memcpy(s, prefix, m); - for (k = m, l = (unsigned long)-1; l; i /= b, l /= b) { - if (!zero && !i) + if ((zero == 0) && (i == 0)) { + s[m++] = '0'; + } + + for (k = m, l = (unsigned long)-1; l != 0; i /= b, l /= b) { + if ((zero == 0) && (i == 0)) { break; + } s[k++] = digits[i % b]; } diff --git a/hal/riscv64/string.c b/hal/riscv64/string.c index e58020548..4a8dea5a0 100644 --- a/hal/riscv64/string.c +++ b/hal/riscv64/string.c @@ -19,13 +19,15 @@ int hal_memcmp(const void *ptr1, const void *ptr2, size_t num) { - int i; + size_t i; for (i = 0; i < num; ++i) { - if (((const u8 *)ptr1)[i] < ((const u8 *)ptr2)[i]) + if (((const u8 *)ptr1)[i] < ((const u8 *)ptr2)[i]) { return -1; - else if (((const u8 *)ptr1)[i] > ((const u8 *)ptr2)[i]) + } + else if (((const u8 *)ptr1)[i] > ((const u8 *)ptr2)[i]) { return 1; + } } return 0; @@ -36,8 +38,9 @@ size_t hal_strlen(const char *s) { size_t k; - for (k = 0; *s; s++, k++) + for (k = 0; *s; s++, k++) { ; + } return k; } @@ -48,18 +51,21 @@ int hal_strcmp(const char *s1, const char *s2) const unsigned char *us1 = (const unsigned char *)s1; const unsigned char *us2 = (const unsigned char *)s2; const unsigned char *p; - unsigned int k; + size_t k; for (p = us1, k = 0; *p; p++, k++) { - if (*p < *(us2 + k)) + if (*p < *(us2 + k)) { return -1; - else if (*p > *(us2 + k)) + } + else if (*p > *(us2 + k)) { return 1; + } } - if (*p != *(us2 + k)) + if (*p != *(us2 + k)) { return -1; + } return 0; } @@ -71,10 +77,13 @@ int hal_strncmp(const char *s1, const char *s2, size_t count) const unsigned char *us2 = (const unsigned char *)s2; size_t k; - for (k = 0; k < count && *us1 && *us2 && (*us1 == *us2); ++k, ++us1, ++us2); + for (k = 0; (k < count) && (*us1 != 0) && (*us2 != 0) && (*us1 == *us2); ++k, ++us1, ++us2) { + ; + } - if (k == count || (!*us1 && !*us2)) + if ((k == count) || ((*us1 == 0) && (*us2 == 0))) { return 0; + } return (*us1 < *us2) ? -k - 1 : k + 1; } @@ -82,7 +91,7 @@ int hal_strncmp(const char *s1, const char *s2, size_t count) char *hal_strcpy(char *dest, const char *src) { - int i = 0; + size_t i = 0; do { dest[i] = src[i]; @@ -94,15 +103,16 @@ char *hal_strcpy(char *dest, const char *src) char *hal_strncpy(char *dest, const char *src, size_t n) { - int i = 0; + size_t i = 0; - if (n == 0) + if (n == 0) { return dest; + } do { dest[i] = src[i]; i++; - } while (i < n && src[i - 1] != '\0'); + } while ((i < n) && (src[i - 1] != '\0')); return dest; } @@ -117,9 +127,14 @@ unsigned long hal_i2s(const char *prefix, char *s, unsigned long i, unsigned cha m = hal_strlen(prefix); hal_memcpy(s, prefix, m); - for (k = m, l = (unsigned long)-1; l; i /= b, l /= b) { - if (!zero && !i) + if ((zero == 0) && (i == 0)) { + s[m++] = '0'; + } + + for (k = m, l = (unsigned long)-1; l != 0; i /= b, l /= b) { + if ((zero == 0) && (i == 0)) { break; + } s[k++] = digits[i % b]; } diff --git a/hal/sparcv8leon3/string.c b/hal/sparcv8leon3/string.c index 4c608ac47..5048fcf9b 100644 --- a/hal/sparcv8leon3/string.c +++ b/hal/sparcv8leon3/string.c @@ -282,14 +282,19 @@ unsigned long hal_i2s(const char *prefix, char *s, unsigned long i, unsigned cha { static const char digits[] = "0123456789abcdef"; char c; - unsigned int l, k, m; + unsigned long l, k, m; m = hal_strlen(prefix); hal_memcpy(s, prefix, m); - for (k = m, l = (unsigned int)-1; l; i /= b, l /= b) { - if (!zero && !i) + if ((zero == 0) && (i == 0)) { + s[m++] = '0'; + } + + for (k = m, l = (unsigned long)-1; l != 0; i /= b, l /= b) { + if ((zero == 0) && (i == 0)) { break; + } s[k++] = digits[i % b]; }