Subversion Repositories freemyipod

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
71 theseven 1
/*
2
FUNCTION
3
        <<strlen>>---character string length
4
 
5
INDEX
6
        strlen
7
 
8
ANSI_SYNOPSIS
9
        #include <string.h>
10
        size_t strlen(const char *<[str]>);
11
 
12
TRAD_SYNOPSIS
13
        #include <string.h>
14
        size_t strlen(<[str]>)
15
        char *<[src]>;
16
 
17
DESCRIPTION
18
        The <<strlen>> function works out the length of the string
19
        starting at <<*<[str]>>> by counting chararacters until it
20
        reaches a <<NULL>> character.
21
 
22
RETURNS
23
        <<strlen>> returns the character count.
24
 
25
PORTABILITY
26
<<strlen>> is ANSI C.
27
 
28
<<strlen>> requires no supporting OS subroutines.
29
 
30
QUICKREF
31
        strlen ansi pure
32
*/
33
 
34
#include "global.h"
113 theseven 35
#include "include/_ansi.h"
36
#include "include/string.h"
71 theseven 37
#include <limits.h>
38
 
39
#define LBLOCKSIZE   (sizeof (long))
40
#define UNALIGNED(X) ((long)X & (LBLOCKSIZE - 1))
41
 
42
#if LONG_MAX == 2147483647L
43
#define DETECTNULL(X) (((X) - 0x01010101) & ~(X) & 0x80808080)
44
#else
45
#if LONG_MAX == 9223372036854775807L
46
/* Nonzero if X (a long int) contains a NULL byte. */
47
#define DETECTNULL(X) (((X) - 0x0101010101010101) & ~(X) & 0x8080808080808080)
48
#else
49
#error long int is not a 32bit or 64bit type.
50
#endif
51
#endif
52
 
53
#ifndef DETECTNULL
54
#error long int is not a 32bit or 64bit byte
55
#endif
56
 
57
size_t
58
_DEFUN (strlen, (str),
59
        _CONST char *str) ICODE_ATTR;
60
 
61
size_t
62
_DEFUN (strlen, (str),
63
        _CONST char *str)
64
{
65
#if defined(PREFER_SIZE_OVER_SPEED) || defined(__OPTIMIZE_SIZE__)
66
  _CONST char *start = str;
67
 
68
  while (*str)
69
    str++;
70
 
71
  return str - start;
72
#else
73
  _CONST char *start = str;
74
  unsigned long *aligned_addr;
75
 
76
  if (!UNALIGNED (str))
77
    {
78
      /* If the string is word-aligned, we can check for the presence of 
79
         a null in each word-sized block.  */
80
      aligned_addr = (unsigned long*)str;
81
      while (!DETECTNULL (*aligned_addr))
82
        aligned_addr++;
83
 
84
      /* Once a null is detected, we check each byte in that block for a
85
         precise position of the null.  */
86
      str = (char*)aligned_addr;
87
    }
88
 
89
  while (*str)
90
    str++;
91
  return str - start;
92
#endif /* not PREFER_SIZE_OVER_SPEED */
93
}