source: mainline/uspace/lib/posix/src/strings.c@ df2e5514

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since df2e5514 was df2e5514, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 7 years ago

Rename uspace/lib/posix/source to src, for consistency with other libraries.

  • Property mode set to 100644
File size: 4.7 KB
Line 
1/*
2 * Copyright (c) 2011 Jiri Zarevucky
3 * Copyright (c) 2011 Petr Koupy
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * - Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * - Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * - The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30/** @addtogroup libposix
31 * @{
32 */
33/** @file Additional string manipulation.
34 */
35
36#define LIBPOSIX_INTERNAL
37#define __POSIX_DEF__(x) posix_##x
38
39#include "internal/common.h"
40#include "posix/strings.h"
41
42#include "posix/string.h"
43#include "posix/ctype.h"
44
45#include "libc/mem.h"
46#include "libc/str.h"
47
48/**
49 * Find first set bit (beginning with the least significant bit).
50 *
51 * @param i Integer in which to look for the first set bit.
52 * @return Index of first set bit. Bits are numbered starting at one.
53 */
54int posix_ffs(int i)
55{
56 if (i == 0) {
57 return 0;
58 }
59
60 int result = 0;
61
62 // XXX: assumes at most 32-bit int
63 if (!(i & 0xFFFF)) {
64 result |= 16;
65 i >>= 16;
66 }
67 if (!(i & 0xFF)) {
68 result |= 8;
69 i >>= 8;
70 }
71 if (!(i & 0xF)) {
72 result |= 4;
73 i >>= 4;
74 }
75 if (!(i & 0x3)) {
76 result |= 2;
77 i >>= 2;
78 }
79 if (!(i & 0x1)) {
80 result |= 1;
81 }
82
83 return result + 1;
84}
85
86/**
87 * Compare two strings (case-insensitive).
88 *
89 * @param s1 First string to be compared.
90 * @param s2 Second string to be compared.
91 * @return Difference of the first pair of inequal characters,
92 * or 0 if strings have the same content.
93 */
94int posix_strcasecmp(const char *s1, const char *s2)
95{
96 return posix_strncasecmp(s1, s2, STR_NO_LIMIT);
97}
98
99/**
100 * Compare part of two strings (case-insensitive).
101 *
102 * @param s1 First string to be compared.
103 * @param s2 Second string to be compared.
104 * @param n Maximum number of characters to be compared.
105 * @return Difference of the first pair of inequal characters,
106 * or 0 if strings have the same content.
107 */
108int posix_strncasecmp(const char *s1, const char *s2, size_t n)
109{
110 for (size_t i = 0; i < n; ++i) {
111 int cmp = tolower(s1[i]) - tolower(s2[i]);
112 if (cmp != 0) {
113 return cmp;
114 }
115
116 if (s1[i] == 0) {
117 return 0;
118 }
119 }
120
121 return 0;
122}
123
124/**
125 * Compare two memory areas.
126 *
127 * @param mem1 Pointer to the first area to compare.
128 * @param mem2 Pointer to the second area to compare.
129 * @param n Common size of both areas.
130 * @return If n is 0, return zero. If the areas match, return
131 * zero. Otherwise return non-zero.
132 */
133int posix_bcmp(const void *mem1, const void *mem2, size_t n)
134{
135 return memcmp(mem1, mem2, n);
136}
137
138/**
139 * Copy bytes in memory with overlapping areas.
140 *
141 * @param src Source area.
142 * @param dest Destination area.
143 * @param n Number of bytes to copy.
144 */
145void posix_bcopy(const void *src, void *dest, size_t n)
146{
147 /* Note that memmove has different order of arguments. */
148 memmove(dest, src, n);
149}
150
151/**
152 * Reset bytes in memory area to zero.
153 *
154 * @param mem Memory area to be zeroed.
155 * @param n Number of bytes to reset.
156 */
157void posix_bzero(void *mem, size_t n)
158{
159 memset(mem, 0, n);
160}
161
162/**
163 * Scan string for a first occurence of a character.
164 *
165 * @param s String in which to look for the character.
166 * @param c Character to look for.
167 * @return Pointer to the specified character on success,
168 * NULL pointer otherwise.
169 */
170char *posix_index(const char *s, int c)
171{
172 return posix_strchr(s, c);
173}
174
175/**
176 * Scan string for a last occurence of a character.
177 *
178 * @param s String in which to look for the character.
179 * @param c Character to look for.
180 * @return Pointer to the specified character on success,
181 * NULL pointer otherwise.
182 */
183char *posix_rindex(const char *s, int c)
184{
185 return posix_strrchr(s, c);
186}
187
188/** @}
189 */
Note: See TracBrowser for help on using the repository browser.