summaryrefslogtreecommitdiff
path: root/firmware/libc/strlen.c
diff options
context:
space:
mode:
authorThomas Martitz <kugel@rockbox.org>2010-05-06 21:04:40 +0000
committerThomas Martitz <kugel@rockbox.org>2010-05-06 21:04:40 +0000
commit50a6ca39ad4ed01922aa4f755f0ca579788226cf (patch)
treec7881b015b220558167310345b162324c96be15a /firmware/libc/strlen.c
parentadb506df14aded06ed6e9ebf8540e6fd383ffd6a (diff)
downloadrockbox-50a6ca39ad4ed01922aa4f755f0ca579788226cf.tar.gz
rockbox-50a6ca39ad4ed01922aa4f755f0ca579788226cf.zip
Move c/h files implementing/defining standard library stuff into a new libc directory, also standard'ify some parts of the code base (almost entirely #include fixes).
This is to a) to cleanup firmware/common and firmware/include a bit, but also b) for Rockbox as an application which should use the host system's c library and headers, separating makes it easy to exclude our files from the build. git-svn-id: svn://svn.rockbox.org/rockbox/trunk@25850 a1c6a512-1295-4272-9138-f99709370657
Diffstat (limited to 'firmware/libc/strlen.c')
-rw-r--r--firmware/libc/strlen.c93
1 files changed, 93 insertions, 0 deletions
diff --git a/firmware/libc/strlen.c b/firmware/libc/strlen.c
new file mode 100644
index 0000000000..4d33eafce6
--- /dev/null
+++ b/firmware/libc/strlen.c
@@ -0,0 +1,93 @@
1/*
2FUNCTION
3 <<strlen>>---character string length
4
5INDEX
6 strlen
7
8ANSI_SYNOPSIS
9 #include <string.h>
10 size_t strlen(const char *<[str]>);
11
12TRAD_SYNOPSIS
13 #include <string.h>
14 size_t strlen(<[str]>)
15 char *<[src]>;
16
17DESCRIPTION
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
22RETURNS
23 <<strlen>> returns the character count.
24
25PORTABILITY
26<<strlen>> is ANSI C.
27
28<<strlen>> requires no supporting OS subroutines.
29
30QUICKREF
31 strlen ansi pure
32*/
33
34#include "config.h"
35#include <_ansi.h>
36#include <string.h>
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
57size_t
58_DEFUN (strlen, (str),
59 _CONST char *str) ICODE_ATTR;
60
61size_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}