source: mainline/uspace/lib/posix/source/strings.c@ acdb5bac

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since acdb5bac was acdb5bac, checked in by Jiri Svoboda <jiri@…>, 12 years ago

Replace usage of bzero() with C standard memset().

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