source: mainline/uspace/lib/posix/source/strings.c@ 1d6dd2a

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

Remove unnecessary includes from <stdio.h>.

  • Property mode set to 100644
File size: 4.7 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
[fdf97f6]37#define __POSIX_DEF__(x) posix_##x
[ae1c11b]38
[9b1503e]39#include "internal/common.h"
[a3da2b2]40#include "posix/strings.h"
[a6d908c1]41
[a3da2b2]42#include "posix/string.h"
43#include "posix/ctype.h"
[ae1c11b]44
[8ecef91]45#include "libc/mem.h"
[1d6dd2a]46#include "libc/str.h"
[8ecef91]47
[4f4b4e7]48/**
[fc3680e]49 * Find first set bit (beginning with the least significant bit).
[4f4b4e7]50 *
[fc3680e]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.
[4f4b4e7]53 */
[ae1c11b]54int posix_ffs(int i)
55{
[8ecef91]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;
[ae1c11b]84}
85
[4f4b4e7]86/**
[fc3680e]87 * Compare two strings (case-insensitive).
[4f4b4e7]88 *
[fc3680e]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.
[4f4b4e7]93 */
[ae1c11b]94int posix_strcasecmp(const char *s1, const char *s2)
95{
[6128390]96 return posix_strncasecmp(s1, s2, STR_NO_LIMIT);
[ae1c11b]97}
98
[4f4b4e7]99/**
[fc3680e]100 * Compare part of two strings (case-insensitive).
[4f4b4e7]101 *
[fc3680e]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.
[4f4b4e7]107 */
[ae1c11b]108int posix_strncasecmp(const char *s1, const char *s2, size_t n)
109{
[6128390]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;
[ae1c11b]122}
123
[4f4b4e7]124/**
[fc3680e]125 * Compare two memory areas.
[4f4b4e7]126 *
[fc3680e]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.
[4f4b4e7]132 */
[ae1c11b]133int posix_bcmp(const void *mem1, const void *mem2, size_t n)
134{
[44ecf89]135 return memcmp(mem1, mem2, n);
[ae1c11b]136}
137
[4f4b4e7]138/**
[fc3680e]139 * Copy bytes in memory with overlapping areas.
[4f4b4e7]140 *
[fc3680e]141 * @param src Source area.
142 * @param dest Destination area.
143 * @param n Number of bytes to copy.
[4f4b4e7]144 */
[fc3680e]145void posix_bcopy(const void *src, void *dest, size_t n)
[ae1c11b]146{
[8ecef91]147 /* Note that memmove has different order of arguments. */
[fc3680e]148 memmove(dest, src, n);
[ae1c11b]149}
150
[4f4b4e7]151/**
[fc3680e]152 * Reset bytes in memory area to zero.
[4f4b4e7]153 *
[fc3680e]154 * @param mem Memory area to be zeroed.
155 * @param n Number of bytes to reset.
[4f4b4e7]156 */
[ae1c11b]157void posix_bzero(void *mem, size_t n)
158{
[acdb5bac]159 memset(mem, 0, n);
[ae1c11b]160}
161
[4f4b4e7]162/**
[fc3680e]163 * Scan string for a first occurence of a character.
[4f4b4e7]164 *
[fc3680e]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.
[4f4b4e7]169 */
[ae1c11b]170char *posix_index(const char *s, int c)
171{
172 return posix_strchr(s, c);
173}
174
[4f4b4e7]175/**
[fc3680e]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.
[4f4b4e7]182 */
[ae1c11b]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.