clang-format
This commit is contained in:
@@ -45,32 +45,28 @@
|
||||
* memory.
|
||||
*/
|
||||
|
||||
void
|
||||
bzero(void *vblock, size_t len)
|
||||
{
|
||||
char *block = vblock;
|
||||
size_t i;
|
||||
void bzero(void *vblock, size_t len) {
|
||||
char *block = vblock;
|
||||
size_t i;
|
||||
|
||||
/*
|
||||
* For performance, optimize the common case where the pointer
|
||||
* and the length are word-aligned, and write word-at-a-time
|
||||
* instead of byte-at-a-time. Otherwise, write bytes.
|
||||
*
|
||||
* The alignment logic here should be portable. We rely on the
|
||||
* compiler to be reasonably intelligent about optimizing the
|
||||
* divides and moduli out. Fortunately, it is.
|
||||
*/
|
||||
/*
|
||||
* For performance, optimize the common case where the pointer
|
||||
* and the length are word-aligned, and write word-at-a-time
|
||||
* instead of byte-at-a-time. Otherwise, write bytes.
|
||||
*
|
||||
* The alignment logic here should be portable. We rely on the
|
||||
* compiler to be reasonably intelligent about optimizing the
|
||||
* divides and moduli out. Fortunately, it is.
|
||||
*/
|
||||
|
||||
if ((uintptr_t)block % sizeof(long) == 0 &&
|
||||
len % sizeof(long) == 0) {
|
||||
long *lb = (long *)block;
|
||||
for (i=0; i<len/sizeof(long); i++) {
|
||||
lb[i] = 0;
|
||||
}
|
||||
}
|
||||
else {
|
||||
for (i=0; i<len; i++) {
|
||||
block[i] = 0;
|
||||
}
|
||||
}
|
||||
if ((uintptr_t)block % sizeof(long) == 0 && len % sizeof(long) == 0) {
|
||||
long *lb = (long *)block;
|
||||
for (i = 0; i < len / sizeof(long); i++) {
|
||||
lb[i] = 0;
|
||||
}
|
||||
} else {
|
||||
for (i = 0; i < len; i++) {
|
||||
block[i] = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -44,43 +44,39 @@
|
||||
* C standard function - copy a block of memory.
|
||||
*/
|
||||
|
||||
void *
|
||||
memcpy(void *dst, const void *src, size_t len)
|
||||
{
|
||||
size_t i;
|
||||
void *memcpy(void *dst, const void *src, size_t len) {
|
||||
size_t i;
|
||||
|
||||
/*
|
||||
* memcpy does not support overlapping buffers, so always do it
|
||||
* forwards. (Don't change this without adjusting memmove.)
|
||||
*
|
||||
* For speedy copying, optimize the common case where both pointers
|
||||
* and the length are word-aligned, and copy word-at-a-time instead
|
||||
* of byte-at-a-time. Otherwise, copy by bytes.
|
||||
*
|
||||
* The alignment logic below should be portable. We rely on
|
||||
* the compiler to be reasonably intelligent about optimizing
|
||||
* the divides and modulos out. Fortunately, it is.
|
||||
*/
|
||||
/*
|
||||
* memcpy does not support overlapping buffers, so always do it
|
||||
* forwards. (Don't change this without adjusting memmove.)
|
||||
*
|
||||
* For speedy copying, optimize the common case where both pointers
|
||||
* and the length are word-aligned, and copy word-at-a-time instead
|
||||
* of byte-at-a-time. Otherwise, copy by bytes.
|
||||
*
|
||||
* The alignment logic below should be portable. We rely on
|
||||
* the compiler to be reasonably intelligent about optimizing
|
||||
* the divides and modulos out. Fortunately, it is.
|
||||
*/
|
||||
|
||||
if ((uintptr_t)dst % sizeof(long) == 0 &&
|
||||
(uintptr_t)src % sizeof(long) == 0 &&
|
||||
len % sizeof(long) == 0) {
|
||||
if ((uintptr_t)dst % sizeof(long) == 0 &&
|
||||
(uintptr_t)src % sizeof(long) == 0 && len % sizeof(long) == 0) {
|
||||
|
||||
long *d = dst;
|
||||
const long *s = src;
|
||||
long *d = dst;
|
||||
const long *s = src;
|
||||
|
||||
for (i=0; i<len/sizeof(long); i++) {
|
||||
d[i] = s[i];
|
||||
}
|
||||
}
|
||||
else {
|
||||
char *d = dst;
|
||||
const char *s = src;
|
||||
for (i = 0; i < len / sizeof(long); i++) {
|
||||
d[i] = s[i];
|
||||
}
|
||||
} else {
|
||||
char *d = dst;
|
||||
const char *s = src;
|
||||
|
||||
for (i=0; i<len; i++) {
|
||||
d[i] = s[i];
|
||||
}
|
||||
}
|
||||
for (i = 0; i < len; i++) {
|
||||
d[i] = s[i];
|
||||
}
|
||||
}
|
||||
|
||||
return dst;
|
||||
return dst;
|
||||
}
|
||||
|
||||
@@ -45,73 +45,69 @@
|
||||
* regions correctly.
|
||||
*/
|
||||
|
||||
void *
|
||||
memmove(void *dst, const void *src, size_t len)
|
||||
{
|
||||
size_t i;
|
||||
void *memmove(void *dst, const void *src, size_t len) {
|
||||
size_t i;
|
||||
|
||||
/*
|
||||
* If the buffers don't overlap, it doesn't matter what direction
|
||||
* we copy in. If they do, it does, so just assume they always do.
|
||||
* We don't concern ourselves with the possibility that the region
|
||||
* to copy might roll over across the top of memory, because it's
|
||||
* not going to happen.
|
||||
*
|
||||
* If the destination is above the source, we have to copy
|
||||
* back to front to avoid overwriting the data we want to
|
||||
* copy.
|
||||
*
|
||||
* dest: dddddddd
|
||||
* src: ssssssss ^
|
||||
* | ^ |___|
|
||||
* |___|
|
||||
*
|
||||
* If the destination is below the source, we have to copy
|
||||
* front to back.
|
||||
*
|
||||
* dest: dddddddd
|
||||
* src: ^ ssssssss
|
||||
* |___| ^ |
|
||||
* |___|
|
||||
*/
|
||||
/*
|
||||
* If the buffers don't overlap, it doesn't matter what direction
|
||||
* we copy in. If they do, it does, so just assume they always do.
|
||||
* We don't concern ourselves with the possibility that the region
|
||||
* to copy might roll over across the top of memory, because it's
|
||||
* not going to happen.
|
||||
*
|
||||
* If the destination is above the source, we have to copy
|
||||
* back to front to avoid overwriting the data we want to
|
||||
* copy.
|
||||
*
|
||||
* dest: dddddddd
|
||||
* src: ssssssss ^
|
||||
* | ^ |___|
|
||||
* |___|
|
||||
*
|
||||
* If the destination is below the source, we have to copy
|
||||
* front to back.
|
||||
*
|
||||
* dest: dddddddd
|
||||
* src: ^ ssssssss
|
||||
* |___| ^ |
|
||||
* |___|
|
||||
*/
|
||||
|
||||
if ((uintptr_t)dst < (uintptr_t)src) {
|
||||
/*
|
||||
* As author/maintainer of libc, take advantage of the
|
||||
* fact that we know memcpy copies forwards.
|
||||
*/
|
||||
return memcpy(dst, src, len);
|
||||
}
|
||||
if ((uintptr_t)dst < (uintptr_t)src) {
|
||||
/*
|
||||
* As author/maintainer of libc, take advantage of the
|
||||
* fact that we know memcpy copies forwards.
|
||||
*/
|
||||
return memcpy(dst, src, len);
|
||||
}
|
||||
|
||||
/*
|
||||
* Copy by words in the common case. Look in memcpy.c for more
|
||||
* information.
|
||||
*/
|
||||
/*
|
||||
* Copy by words in the common case. Look in memcpy.c for more
|
||||
* information.
|
||||
*/
|
||||
|
||||
if ((uintptr_t)dst % sizeof(long) == 0 &&
|
||||
(uintptr_t)src % sizeof(long) == 0 &&
|
||||
len % sizeof(long) == 0) {
|
||||
if ((uintptr_t)dst % sizeof(long) == 0 &&
|
||||
(uintptr_t)src % sizeof(long) == 0 && len % sizeof(long) == 0) {
|
||||
|
||||
long *d = dst;
|
||||
const long *s = src;
|
||||
long *d = dst;
|
||||
const long *s = src;
|
||||
|
||||
/*
|
||||
* The reason we copy index i-1 and test i>0 is that
|
||||
* i is unsigned -- so testing i>=0 doesn't work.
|
||||
*/
|
||||
/*
|
||||
* The reason we copy index i-1 and test i>0 is that
|
||||
* i is unsigned -- so testing i>=0 doesn't work.
|
||||
*/
|
||||
|
||||
for (i=len/sizeof(long); i>0; i--) {
|
||||
d[i-1] = s[i-1];
|
||||
}
|
||||
}
|
||||
else {
|
||||
char *d = dst;
|
||||
const char *s = src;
|
||||
for (i = len / sizeof(long); i > 0; i--) {
|
||||
d[i - 1] = s[i - 1];
|
||||
}
|
||||
} else {
|
||||
char *d = dst;
|
||||
const char *s = src;
|
||||
|
||||
for (i=len; i>0; i--) {
|
||||
d[i-1] = s[i-1];
|
||||
}
|
||||
}
|
||||
for (i = len; i > 0; i--) {
|
||||
d[i - 1] = s[i - 1];
|
||||
}
|
||||
}
|
||||
|
||||
return dst;
|
||||
return dst;
|
||||
}
|
||||
|
||||
@@ -38,15 +38,13 @@
|
||||
* C standard function - initialize a block of memory
|
||||
*/
|
||||
|
||||
void *
|
||||
memset(void *ptr, int ch, size_t len)
|
||||
{
|
||||
char *p = ptr;
|
||||
size_t i;
|
||||
void *memset(void *ptr, int ch, size_t len) {
|
||||
char *p = ptr;
|
||||
size_t i;
|
||||
|
||||
for (i=0; i<len; i++) {
|
||||
p[i] = ch;
|
||||
}
|
||||
for (i = 0; i < len; i++) {
|
||||
p[i] = ch;
|
||||
}
|
||||
|
||||
return ptr;
|
||||
return ptr;
|
||||
}
|
||||
|
||||
@@ -43,12 +43,10 @@
|
||||
* Standard C string function: append one string to another.
|
||||
*/
|
||||
|
||||
char *
|
||||
strcat(char *dest, const char *src)
|
||||
{
|
||||
size_t offset;
|
||||
char *strcat(char *dest, const char *src) {
|
||||
size_t offset;
|
||||
|
||||
offset = strlen(dest);
|
||||
strcpy(dest+offset, src);
|
||||
return dest;
|
||||
offset = strlen(dest);
|
||||
strcpy(dest + offset, src);
|
||||
return dest;
|
||||
}
|
||||
|
||||
@@ -43,26 +43,24 @@
|
||||
* C standard string function: find leftmost instance of a character
|
||||
* in a string.
|
||||
*/
|
||||
char *
|
||||
strchr(const char *s, int ch_arg)
|
||||
{
|
||||
/* avoid sign-extension problems */
|
||||
const char ch = ch_arg;
|
||||
char *strchr(const char *s, int ch_arg) {
|
||||
/* avoid sign-extension problems */
|
||||
const char ch = ch_arg;
|
||||
|
||||
/* scan from left to right */
|
||||
while (*s) {
|
||||
/* if we hit it, return it */
|
||||
if (*s == ch) {
|
||||
return (char *)s;
|
||||
}
|
||||
s++;
|
||||
}
|
||||
/* scan from left to right */
|
||||
while (*s) {
|
||||
/* if we hit it, return it */
|
||||
if (*s == ch) {
|
||||
return (char *)s;
|
||||
}
|
||||
s++;
|
||||
}
|
||||
|
||||
/* if we were looking for the 0, return that */
|
||||
if (*s == ch) {
|
||||
return (char *)s;
|
||||
}
|
||||
/* if we were looking for the 0, return that */
|
||||
if (*s == ch) {
|
||||
return (char *)s;
|
||||
}
|
||||
|
||||
/* didn't find it */
|
||||
return NULL;
|
||||
/* didn't find it */
|
||||
return NULL;
|
||||
}
|
||||
|
||||
@@ -44,47 +44,44 @@
|
||||
* sort order.
|
||||
*/
|
||||
|
||||
int
|
||||
strcmp(const char *a, const char *b)
|
||||
{
|
||||
size_t i;
|
||||
int strcmp(const char *a, const char *b) {
|
||||
size_t i;
|
||||
|
||||
/*
|
||||
* Walk down both strings until either they're different
|
||||
* or we hit the end of A.
|
||||
*
|
||||
* If A and B strings are not the same length, when the
|
||||
* shorter one ends, the two will be different, and we'll
|
||||
* stop before running off the end of either.
|
||||
*
|
||||
* If they *are* the same length, it's sufficient to check
|
||||
* that we haven't run off the end of A, because that's the
|
||||
* same as checking to make sure we haven't run off the end of
|
||||
* B.
|
||||
*/
|
||||
/*
|
||||
* Walk down both strings until either they're different
|
||||
* or we hit the end of A.
|
||||
*
|
||||
* If A and B strings are not the same length, when the
|
||||
* shorter one ends, the two will be different, and we'll
|
||||
* stop before running off the end of either.
|
||||
*
|
||||
* If they *are* the same length, it's sufficient to check
|
||||
* that we haven't run off the end of A, because that's the
|
||||
* same as checking to make sure we haven't run off the end of
|
||||
* B.
|
||||
*/
|
||||
|
||||
for (i=0; a[i]!=0 && a[i]==b[i]; i++) {
|
||||
/* nothing */
|
||||
}
|
||||
for (i = 0; a[i] != 0 && a[i] == b[i]; i++) {
|
||||
/* nothing */
|
||||
}
|
||||
|
||||
/*
|
||||
* If A is greater than B, return 1. If A is less than B,
|
||||
* return -1. If they're the same, return 0. Since we have
|
||||
* stopped at the first character of difference (or the end of
|
||||
* both strings) checking the character under I accomplishes
|
||||
* this.
|
||||
*
|
||||
* Note that strcmp does not handle accented characters,
|
||||
* internationalization, or locale sort order; strcoll() does
|
||||
* that.
|
||||
*
|
||||
* The rules say we compare order in terms of *unsigned* char.
|
||||
*/
|
||||
if ((unsigned char)a[i] > (unsigned char)b[i]) {
|
||||
return 1;
|
||||
}
|
||||
else if (a[i] == b[i]) {
|
||||
return 0;
|
||||
}
|
||||
return -1;
|
||||
/*
|
||||
* If A is greater than B, return 1. If A is less than B,
|
||||
* return -1. If they're the same, return 0. Since we have
|
||||
* stopped at the first character of difference (or the end of
|
||||
* both strings) checking the character under I accomplishes
|
||||
* this.
|
||||
*
|
||||
* Note that strcmp does not handle accented characters,
|
||||
* internationalization, or locale sort order; strcoll() does
|
||||
* that.
|
||||
*
|
||||
* The rules say we compare order in terms of *unsigned* char.
|
||||
*/
|
||||
if ((unsigned char)a[i] > (unsigned char)b[i]) {
|
||||
return 1;
|
||||
} else if (a[i] == b[i]) {
|
||||
return 0;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
@@ -42,22 +42,20 @@
|
||||
/*
|
||||
* Standard C string function: copy one string to another.
|
||||
*/
|
||||
char *
|
||||
strcpy(char *dest, const char *src)
|
||||
{
|
||||
size_t i;
|
||||
char *strcpy(char *dest, const char *src) {
|
||||
size_t i;
|
||||
|
||||
/*
|
||||
* Copy characters until we hit the null terminator.
|
||||
*/
|
||||
for (i=0; src[i]; i++) {
|
||||
dest[i] = src[i];
|
||||
}
|
||||
/*
|
||||
* Copy characters until we hit the null terminator.
|
||||
*/
|
||||
for (i = 0; src[i]; i++) {
|
||||
dest[i] = src[i];
|
||||
}
|
||||
|
||||
/*
|
||||
* Add null terminator to result.
|
||||
*/
|
||||
dest[i] = 0;
|
||||
/*
|
||||
* Add null terminator to result.
|
||||
*/
|
||||
dest[i] = 0;
|
||||
|
||||
return dest;
|
||||
return dest;
|
||||
}
|
||||
|
||||
@@ -43,13 +43,11 @@
|
||||
* C standard string function: get length of a string
|
||||
*/
|
||||
|
||||
size_t
|
||||
strlen(const char *str)
|
||||
{
|
||||
size_t ret = 0;
|
||||
size_t strlen(const char *str) {
|
||||
size_t ret = 0;
|
||||
|
||||
while (str[ret]) {
|
||||
ret++;
|
||||
}
|
||||
return ret;
|
||||
while (str[ret]) {
|
||||
ret++;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
@@ -43,27 +43,25 @@
|
||||
* C standard string function: find rightmost instance of a character
|
||||
* in a string.
|
||||
*/
|
||||
char *
|
||||
strrchr(const char *s, int ch_arg)
|
||||
{
|
||||
/* avoid sign-extension problems */
|
||||
const char ch = ch_arg;
|
||||
char *strrchr(const char *s, int ch_arg) {
|
||||
/* avoid sign-extension problems */
|
||||
const char ch = ch_arg;
|
||||
|
||||
/* start one past the last character INCLUDING NULL TERMINATOR */
|
||||
size_t i = strlen(s)+1;
|
||||
/* start one past the last character INCLUDING NULL TERMINATOR */
|
||||
size_t i = strlen(s) + 1;
|
||||
|
||||
/* go from right to left; stop at 0 */
|
||||
while (i > 0) {
|
||||
/* go from right to left; stop at 0 */
|
||||
while (i > 0) {
|
||||
|
||||
/* decrement first */
|
||||
i--;
|
||||
/* decrement first */
|
||||
i--;
|
||||
|
||||
/* now check the character we're over */
|
||||
if (s[i] == ch) {
|
||||
return (char *)(s+i);
|
||||
}
|
||||
}
|
||||
/* now check the character we're over */
|
||||
if (s[i] == ch) {
|
||||
return (char *)(s + i);
|
||||
}
|
||||
}
|
||||
|
||||
/* didn't find it */
|
||||
return NULL;
|
||||
/* didn't find it */
|
||||
return NULL;
|
||||
}
|
||||
|
||||
@@ -46,51 +46,48 @@
|
||||
* The "context" argument should point to a "char *" that is preserved
|
||||
* between calls to strtok_r that wish to operate on same string.
|
||||
*/
|
||||
char *
|
||||
strtok_r(char *string, const char *seps, char **context)
|
||||
{
|
||||
char *head; /* start of word */
|
||||
char *tail; /* end of word */
|
||||
char *strtok_r(char *string, const char *seps, char **context) {
|
||||
char *head; /* start of word */
|
||||
char *tail; /* end of word */
|
||||
|
||||
/* If we're starting up, initialize context */
|
||||
if (string) {
|
||||
*context = string;
|
||||
}
|
||||
/* If we're starting up, initialize context */
|
||||
if (string) {
|
||||
*context = string;
|
||||
}
|
||||
|
||||
/* Get potential start of this next word */
|
||||
head = *context;
|
||||
if (head == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
/* Get potential start of this next word */
|
||||
head = *context;
|
||||
if (head == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* Skip any leading separators */
|
||||
while (*head && strchr(seps, *head)) {
|
||||
head++;
|
||||
}
|
||||
/* Skip any leading separators */
|
||||
while (*head && strchr(seps, *head)) {
|
||||
head++;
|
||||
}
|
||||
|
||||
/* Did we hit the end? */
|
||||
if (*head == 0) {
|
||||
/* Nothing left */
|
||||
*context = NULL;
|
||||
return NULL;
|
||||
}
|
||||
/* Did we hit the end? */
|
||||
if (*head == 0) {
|
||||
/* Nothing left */
|
||||
*context = NULL;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* skip over word */
|
||||
tail = head;
|
||||
while (*tail && !strchr(seps, *tail)) {
|
||||
tail++;
|
||||
}
|
||||
/* skip over word */
|
||||
tail = head;
|
||||
while (*tail && !strchr(seps, *tail)) {
|
||||
tail++;
|
||||
}
|
||||
|
||||
/* Save head for next time in context */
|
||||
if (*tail == 0) {
|
||||
*context = NULL;
|
||||
}
|
||||
else {
|
||||
*tail = 0;
|
||||
tail++;
|
||||
*context = tail;
|
||||
}
|
||||
/* Save head for next time in context */
|
||||
if (*tail == 0) {
|
||||
*context = NULL;
|
||||
} else {
|
||||
*tail = 0;
|
||||
tail++;
|
||||
*context = tail;
|
||||
}
|
||||
|
||||
/* Return current word */
|
||||
return head;
|
||||
/* Return current word */
|
||||
return head;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user