Embedded Artistry libc
C Standard Library Support for Bare-metal Systems
strlen.c File Reference
#include <limits.h>
#include <string.h>
Include dependency graph for strlen.c:

Go to the source code of this file.

Macros

#define LONGPTR_MASK   (sizeof(long) - 1)
 
#define testbyte(x)
 

Functions

size_t strlen (const char *str)
 Returns the length of the given null-terminated byte string. More...
 

Macro Definition Documentation

◆ LONGPTR_MASK

#define LONGPTR_MASK   (sizeof(long) - 1)

Definition at line 64 of file strlen.c.

◆ testbyte

#define testbyte (   x)
Value:
do \
{ \
if(p[x] == '\0') \
return ((uintptr_t)p - (uintptr_t)str + x); \
} while(0)

Definition at line 70 of file strlen.c.

Function Documentation

◆ strlen()

size_t strlen ( const char *  str)

Returns the length of the given null-terminated byte string.

Returns the length of the given null-terminated byte string, that is, the number of characters in a character array whose first element is pointed to by str up to and not including the first null character.

The behavior is undefined if str is not a pointer to a null-terminated byte string.

Parameters
strpointer to the null-terminated byte string to be examined
Returns
The length of the null-terminated byte string str.

Definition at line 77 of file strlen.c.

78 {
79  const char* p;
80  const unsigned long* lp;
81 
82  /* Skip the first few bytes until we have an aligned p */
83  for(p = str; (uintptr_t)p & LONGPTR_MASK; p++)
84  {
85  if(*p == '\0')
86  {
87  return ((uintptr_t)p - (uintptr_t)str);
88  }
89  }
90 
91  /* Scan the rest of the string using word sized operation */
92  // Cast to void to prevent alignment warning
93  for(lp = (const unsigned long*)(const void*)p;; lp++)
94  {
95  if((*lp - mask01) & mask80)
96  {
97  p = (const char*)(lp);
98  testbyte(0);
99  testbyte(1);
100  testbyte(2);
101  testbyte(3);
102 #if(LONG_BIT >= 64)
103  testbyte(4);
104  testbyte(5);
105  testbyte(6);
106  testbyte(7);
107 #endif
108  }
109  }
110 
111  /* NOTREACHED */
112  // return (0);
113 }
#define LONGPTR_MASK
Definition: strlen.c:64
#define testbyte(x)
Definition: strlen.c:70

References LONGPTR_MASK, and testbyte.

Referenced by __strchrnul(), strcat(), strcpy(), strdup(), strerror_r(), strncat(), strnstr(), strrchr(), and strxfrm().

Here is the caller graph for this function: