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
RevLine 
[ae1c11b]1/*
2 * Copyright (c) 2011 Jiri Zarevucky
[4f4b4e7]3 * Copyright (c) 2011 Petr Koupy
[ae1c11b]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 */
[fc3680e]33/** @file Additional string manipulation.
[ae1c11b]34 */
35
[491e1ee]36#define LIBPOSIX_INTERNAL
[ae1c11b]37
[9b1503e]38#include "internal/common.h"
[a3da2b2]39#include "posix/strings.h"
[a6d908c1]40
[a3da2b2]41#include "posix/string.h"
42#include "posix/ctype.h"
[ae1c11b]43
[8ecef91]44#include "libc/mem.h"
45
[4f4b4e7]46/**
[fc3680e]47 * Find first set bit (beginning with the least significant bit).
[4f4b4e7]48 *
[fc3680e]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.
[4f4b4e7]51 */
[ae1c11b]52int posix_ffs(int i)
53{
[8ecef91]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;
[ae1c11b]82}
83
[4f4b4e7]84/**
[fc3680e]85 * Compare two strings (case-insensitive).
[4f4b4e7]86 *
[fc3680e]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.
[4f4b4e7]91 */
[ae1c11b]92int posix_strcasecmp(const char *s1, const char *s2)
93{
[6128390]94 return posix_strncasecmp(s1, s2, STR_NO_LIMIT);
[ae1c11b]95}
96
[4f4b4e7]97/**
[fc3680e]98 * Compare part of two strings (case-insensitive).
[4f4b4e7]99 *
[fc3680e]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.
[4f4b4e7]105 */
[ae1c11b]106int posix_strncasecmp(const char *s1, const char *s2, size_t n)
107{
[6128390]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;
[ae1c11b]120}
121
[4f4b4e7]122/**
[fc3680e]123 * Compare two memory areas.
[4f4b4e7]124 *
[fc3680e]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.
[4f4b4e7]130 */
[ae1c11b]131int posix_bcmp(const void *mem1, const void *mem2, size_t n)
132{
[44ecf89]133 return memcmp(mem1, mem2, n);
[ae1c11b]134}
135
[4f4b4e7]136/**
[fc3680e]137 * Copy bytes in memory with overlapping areas.
[4f4b4e7]138 *
[fc3680e]139 * @param src Source area.
140 * @param dest Destination area.
141 * @param n Number of bytes to copy.
[4f4b4e7]142 */
[fc3680e]143void posix_bcopy(const void *src, void *dest, size_t n)
[ae1c11b]144{
[8ecef91]145 /* Note that memmove has different order of arguments. */
[fc3680e]146 memmove(dest, src, n);
[ae1c11b]147}
148
[4f4b4e7]149/**
[fc3680e]150 * Reset bytes in memory area to zero.
[4f4b4e7]151 *
[fc3680e]152 * @param mem Memory area to be zeroed.
153 * @param n Number of bytes to reset.
[4f4b4e7]154 */
[ae1c11b]155void posix_bzero(void *mem, size_t n)
156{
[acdb5bac]157 memset(mem, 0, n);
[ae1c11b]158}
159
[4f4b4e7]160/**
[fc3680e]161 * Scan string for a first occurence of a character.
[4f4b4e7]162 *
[fc3680e]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.
[4f4b4e7]167 */
[ae1c11b]168char *posix_index(const char *s, int c)
169{
170 return posix_strchr(s, c);
171}
172
[4f4b4e7]173/**
[fc3680e]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.
[4f4b4e7]180 */
[ae1c11b]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.