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

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

Merge mainline changes

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