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

Last change on this file was 9b8be79, checked in by Jiří Zárevúcky <jiri.zarevucky@…>, 7 years ago

libposix: Change header organization and remove passthrough headers

Posix headers now function like an overlay. The system include directories
are searched after posix directories. The headers don't need to be patched
for export now. libposix files now include headers using bracket notation
instead of quoted notation.

  • Property mode set to 100644
File size: 4.5 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#include "internal/common.h"
37#include <strings.h>
38
39#include <string.h>
40#include <ctype.h>
41
42#include <mem.h>
43#include <str.h>
44
45/**
46 * Find first set bit (beginning with the least significant bit).
47 *
48 * @param i Integer in which to look for the first set bit.
49 * @return Index of first set bit. Bits are numbered starting at one.
50 */
51int ffs(int i)
52{
53 if (i == 0) {
54 return 0;
55 }
56
57 int result = 0;
58
59 // XXX: assumes at most 32-bit int
60 if (!(i & 0xFFFF)) {
61 result |= 16;
62 i >>= 16;
63 }
64 if (!(i & 0xFF)) {
65 result |= 8;
66 i >>= 8;
67 }
68 if (!(i & 0xF)) {
69 result |= 4;
70 i >>= 4;
71 }
72 if (!(i & 0x3)) {
73 result |= 2;
74 i >>= 2;
75 }
76 if (!(i & 0x1)) {
77 result |= 1;
78 }
79
80 return result + 1;
81}
82
83/**
84 * Compare two strings (case-insensitive).
85 *
86 * @param s1 First string to be compared.
87 * @param s2 Second string to be compared.
88 * @return Difference of the first pair of inequal characters,
89 * or 0 if strings have the same content.
90 */
91int strcasecmp(const char *s1, const char *s2)
92{
93 return strncasecmp(s1, s2, STR_NO_LIMIT);
94}
95
96/**
97 * Compare part of two strings (case-insensitive).
98 *
99 * @param s1 First string to be compared.
100 * @param s2 Second string to be compared.
101 * @param n Maximum number of characters to be compared.
102 * @return Difference of the first pair of inequal characters,
103 * or 0 if strings have the same content.
104 */
105int strncasecmp(const char *s1, const char *s2, size_t n)
106{
107 for (size_t i = 0; i < n; ++i) {
108 int cmp = tolower(s1[i]) - tolower(s2[i]);
109 if (cmp != 0) {
110 return cmp;
111 }
112
113 if (s1[i] == 0) {
114 return 0;
115 }
116 }
117
118 return 0;
119}
120
121/**
122 * Compare two memory areas.
123 *
124 * @param mem1 Pointer to the first area to compare.
125 * @param mem2 Pointer to the second area to compare.
126 * @param n Common size of both areas.
127 * @return If n is 0, return zero. If the areas match, return
128 * zero. Otherwise return non-zero.
129 */
130int bcmp(const void *mem1, const void *mem2, size_t n)
131{
132 return memcmp(mem1, mem2, n);
133}
134
135/**
136 * Copy bytes in memory with overlapping areas.
137 *
138 * @param src Source area.
139 * @param dest Destination area.
140 * @param n Number of bytes to copy.
141 */
142void bcopy(const void *src, void *dest, size_t n)
143{
144 /* Note that memmove has different order of arguments. */
145 memmove(dest, src, n);
146}
147
148/**
149 * Reset bytes in memory area to zero.
150 *
151 * @param mem Memory area to be zeroed.
152 * @param n Number of bytes to reset.
153 */
154void bzero(void *mem, size_t n)
155{
156 memset(mem, 0, n);
157}
158
159/**
160 * Scan string for a first occurence of a character.
161 *
162 * @param s String in which to look for the character.
163 * @param c Character to look for.
164 * @return Pointer to the specified character on success,
165 * NULL pointer otherwise.
166 */
167char *index(const char *s, int c)
168{
169 return strchr(s, c);
170}
171
172/**
173 * Scan string for a last occurence of a character.
174 *
175 * @param s String in which to look for the character.
176 * @param c Character to look for.
177 * @return Pointer to the specified character on success,
178 * NULL pointer otherwise.
179 */
180char *rindex(const char *s, int c)
181{
182 return strrchr(s, c);
183}
184
185/** @}
186 */
Note: See TracBrowser for help on using the repository browser.