Lely core libraries 2.3.4
strings.c
Go to the documentation of this file.
1
23#include "libc.h"
24#define LELY_LIBC_STRINGS_INLINE extern inline
25#include <lely/libc/strings.h>
26
27#if !LELY_HAVE_STRINGS_H
28
29#include <ctype.h>
30
31#if !(defined(__GNUC__) || __has_builtin(__builtin_ffs))
32
33#include <stdint.h>
34
35// clang-format off
36static const int ffs_tab[] = {
37 0, 1, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4,
38 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
39 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
40 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
41 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
42 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
43 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
44 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
45 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
46 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
47 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
48 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
49 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
50 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
51 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
52 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8
53};
54// clang-format on
55
56int
57ffs(int i)
58{
59 // cppcheck-suppress oppositeExpression
60 unsigned int x = i & -i;
61 // clang-format off
62 unsigned int n = x > UINT32_C(0x00ffffff)
63 ? 24 : (x > UINT16_C(0xffffu)
64 ? 16 : (x > UINT8_C(0xff) ? 8 : 0));
65 // clang-format on
66 return n + ffs_tab[(x >> n) & UINT8_C(0xff)];
67}
68
69#endif
70
71int
72strcasecmp(const char *s1, const char *s2)
73{
74 if (s1 == s2)
75 return 0;
76
77 int result;
78 // clang-format off
79 while ((result = tolower((unsigned char)*s1)
80 - tolower((unsigned char)*s2++)) == 0 && *s1++)
81 // clang-format on
82 ;
83 return result;
84}
85
86int
87strncasecmp(const char *s1, const char *s2, size_t n)
88{
89 if (s1 == s2 || !n)
90 return 0;
91
92 int result;
93 // clang-format off
94 while ((result = tolower((unsigned char)*s1)
95 - tolower((unsigned char)*s2++)) == 0 && --n && *s1++)
96 // clang-format on
97 ;
98 return result;
99}
100
101#endif // !LELY_HAVE_STRINGS_H
This is the internal header file of the C11 and POSIX compatibility library.
This header file is part of the C11 and POSIX compatibility library; it includes <stdint....
This header file is part of the C11 and POSIX compatibility library; it includes <strings....