diff options
Diffstat (limited to 'src/lib')
-rw-r--r-- | src/lib/string.C | 22 | ||||
-rw-r--r-- | src/lib/string_utils.C | 59 |
2 files changed, 58 insertions, 23 deletions
diff --git a/src/lib/string.C b/src/lib/string.C index 004345372..43e46f6cc 100644 --- a/src/lib/string.C +++ b/src/lib/string.C @@ -78,28 +78,6 @@ extern "C" char* strcpy(char* d, const char* s) } while(1); } -extern "C" char* strncpy(char* d, const char* s, size_t l) -{ - char* d1 = d; - size_t len = 0; - - do - { - if (len++ >= l) break; - *d1 = *s; - if (*s == '\0') break; - d1++; s++; - } while(1); - - // pad the remainder - while( len < l ) - { - d1[len++] = '\0'; - } - - return d; -} - extern "C" int strcmp(const char* a, const char* b) { while((*a != '\0') && (*b != '\0')) diff --git a/src/lib/string_utils.C b/src/lib/string_utils.C index 79a8bf709..27d9228fb 100644 --- a/src/lib/string_utils.C +++ b/src/lib/string_utils.C @@ -120,4 +120,61 @@ extern "C" int memcmp(const void *p1, const void *p2, size_t len) } return 0; -}
\ No newline at end of file +} + +extern "C" int strncmp(const char* a, const char* b, size_t l) +{ + if(l==0) + { + return 0; + } + + const char* begin=a; + while( (*a != '\0') && (*b != '\0') + && (static_cast<size_t>(a-begin)+1<l) ) + { + if (*a == *b) + { + a++; b++; + } + else + { + return (*a > *b) ? 1 : -1; + } + } + if (*a == *b) + { + return 0; + } + if (*a == '\0') + { + return -1; + } + else + { + return 1; + } +} + +extern "C" char* strncpy(char* d, const char* s, size_t l) +{ + char* d1 = d; + size_t len = 0; + + do + { + if (len++ >= l) break; + *d1 = *s; + if (*s == '\0') break; + d1++; s++; + } while(1); + + // pad the remainder + while( len < l ) + { + d1[len++] = '\0'; + } + + return d; +} + |