summaryrefslogtreecommitdiff
path: root/firmware/common/strlen.c
diff options
context:
space:
mode:
authorBjörn Stenberg <bjorn@haxx.se>2002-05-30 21:03:17 +0000
committerBjörn Stenberg <bjorn@haxx.se>2002-05-30 21:03:17 +0000
commitb69338f9f8ae2ae957949a0f4af9749ebb4adc3f (patch)
treec29019589ce21007d1ff99dd74d3115f19dbd3f4 /firmware/common/strlen.c
parent80361f88818ad1ce2ebc00a10f7aa8de1605b3a5 (diff)
downloadrockbox-b69338f9f8ae2ae957949a0f4af9749ebb4adc3f.tar.gz
rockbox-b69338f9f8ae2ae957949a0f4af9749ebb4adc3f.zip
Added code from libc instead of requiring newlib
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@835 a1c6a512-1295-4272-9138-f99709370657
Diffstat (limited to 'firmware/common/strlen.c')
-rw-r--r--firmware/common/strlen.c88
1 files changed, 88 insertions, 0 deletions
diff --git a/firmware/common/strlen.c b/firmware/common/strlen.c
new file mode 100644
index 0000000000..4249e14c78
--- /dev/null
+++ b/firmware/common/strlen.c
@@ -0,0 +1,88 @@
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 <_ansi.h>
35#include <string.h>
36#include <limits.h>
37
38#define LBLOCKSIZE (sizeof (long))
39#define UNALIGNED(X) ((long)X & (LBLOCKSIZE - 1))
40
41#if LONG_MAX == 2147483647L
42#define DETECTNULL(X) (((X) - 0x01010101) & ~(X) & 0x80808080)
43#else
44#if LONG_MAX == 9223372036854775807L
45/* Nonzero if X (a long int) contains a NULL byte. */
46#define DETECTNULL(X) (((X) - 0x0101010101010101) & ~(X) & 0x8080808080808080)
47#else
48#error long int is not a 32bit or 64bit type.
49#endif
50#endif
51
52#ifndef DETECTNULL
53#error long int is not a 32bit or 64bit byte
54#endif
55
56size_t
57_DEFUN (strlen, (str),
58 _CONST char *str)
59{
60#if defined(PREFER_SIZE_OVER_SPEED) || defined(__OPTIMIZE_SIZE__)
61 _CONST char *start = str;
62
63 while (*str)
64 str++;
65
66 return str - start;
67#else
68 _CONST char *start = str;
69 unsigned long *aligned_addr;
70
71 if (!UNALIGNED (str))
72 {
73 /* If the string is word-aligned, we can check for the presence of
74 a null in each word-sized block. */
75 aligned_addr = (unsigned long*)str;
76 while (!DETECTNULL (*aligned_addr))
77 aligned_addr++;
78
79 /* Once a null is detected, we check each byte in that block for a
80 precise position of the null. */
81 str = (char*)aligned_addr;
82 }
83
84 while (*str)
85 str++;
86 return str - start;
87#endif /* not PREFER_SIZE_OVER_SPEED */
88}