Lely core libraries 1.9.2
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 unsigned int x = i & -i;
60 // clang-format off
61 unsigned int n = x > UINT32_C(0x00ffffff)
62 ? 24 : (x > UINT16_C(0xffffu)
63 ? 16 : (x > UINT8_C(0xff) ? 8 : 0));
64 // clang-format on
65 return n + ffs_tab[(x >> n) & UINT8_C(0xff)];
66}
67
68#endif
69
70int
71strcasecmp(const char *s1, const char *s2)
72{
73 if (s1 == s2)
74 return 0;
75
76 int result;
77 // clang-format off
78 while ((result = tolower((unsigned char)*s1)
79 - tolower((unsigned char)*s2++)) == 0 && *s1++)
80 // clang-format on
81 ;
82 return result;
83}
84
85int
86strncasecmp(const char *s1, const char *s2, size_t n)
87{
88 if (s1 == s2 || !n)
89 return 0;
90
91 int result;
92 // clang-format off
93 while ((result = tolower((unsigned char)*s1)
94 - tolower((unsigned char)*s2++)) == 0 && --n && *s1++)
95 // clang-format on
96 ;
97 return result;
98}
99
100#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....
int strncasecmp(const char *s1, const char *s2, size_t n)
Compares at most n characters from the the string at s1 to the string at s2, ignoring differences in ...
Definition: strings.c:86
int strcasecmp(const char *s1, const char *s2)
Compares the string at s1 to the string at s2, ignoring differences in case.
Definition: strings.c:71
This header file is part of the C11 and POSIX compatibility library; it includes <strings....
int ffs(int i)
Finds the index of the first (least significant) bit set in i.
Definition: strings.h:58