[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"
|
---|
| 46 |
|
---|
[4f4b4e7] | 47 | /**
|
---|
[fc3680e] | 48 | * Find first set bit (beginning with the least significant bit).
|
---|
[4f4b4e7] | 49 | *
|
---|
[fc3680e] | 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.
|
---|
[4f4b4e7] | 52 | */
|
---|
[ae1c11b] | 53 | int posix_ffs(int i)
|
---|
| 54 | {
|
---|
[8ecef91] | 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;
|
---|
[ae1c11b] | 83 | }
|
---|
| 84 |
|
---|
[4f4b4e7] | 85 | /**
|
---|
[fc3680e] | 86 | * Compare two strings (case-insensitive).
|
---|
[4f4b4e7] | 87 | *
|
---|
[fc3680e] | 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.
|
---|
[4f4b4e7] | 92 | */
|
---|
[ae1c11b] | 93 | int posix_strcasecmp(const char *s1, const char *s2)
|
---|
| 94 | {
|
---|
[6128390] | 95 | return posix_strncasecmp(s1, s2, STR_NO_LIMIT);
|
---|
[ae1c11b] | 96 | }
|
---|
| 97 |
|
---|
[4f4b4e7] | 98 | /**
|
---|
[fc3680e] | 99 | * Compare part of two strings (case-insensitive).
|
---|
[4f4b4e7] | 100 | *
|
---|
[fc3680e] | 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.
|
---|
[4f4b4e7] | 106 | */
|
---|
[ae1c11b] | 107 | int posix_strncasecmp(const char *s1, const char *s2, size_t n)
|
---|
| 108 | {
|
---|
[6128390] | 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;
|
---|
[ae1c11b] | 121 | }
|
---|
| 122 |
|
---|
[4f4b4e7] | 123 | /**
|
---|
[fc3680e] | 124 | * Compare two memory areas.
|
---|
[4f4b4e7] | 125 | *
|
---|
[fc3680e] | 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.
|
---|
[4f4b4e7] | 131 | */
|
---|
[ae1c11b] | 132 | int posix_bcmp(const void *mem1, const void *mem2, size_t n)
|
---|
| 133 | {
|
---|
[8ecef91] | 134 | return bcmp(mem1, mem2, n);
|
---|
[ae1c11b] | 135 | }
|
---|
| 136 |
|
---|
[4f4b4e7] | 137 | /**
|
---|
[fc3680e] | 138 | * Copy bytes in memory with overlapping areas.
|
---|
[4f4b4e7] | 139 | *
|
---|
[fc3680e] | 140 | * @param src Source area.
|
---|
| 141 | * @param dest Destination area.
|
---|
| 142 | * @param n Number of bytes to copy.
|
---|
[4f4b4e7] | 143 | */
|
---|
[fc3680e] | 144 | void posix_bcopy(const void *src, void *dest, size_t n)
|
---|
[ae1c11b] | 145 | {
|
---|
[8ecef91] | 146 | /* Note that memmove has different order of arguments. */
|
---|
[fc3680e] | 147 | memmove(dest, src, n);
|
---|
[ae1c11b] | 148 | }
|
---|
| 149 |
|
---|
[4f4b4e7] | 150 | /**
|
---|
[fc3680e] | 151 | * Reset bytes in memory area to zero.
|
---|
[4f4b4e7] | 152 | *
|
---|
[fc3680e] | 153 | * @param mem Memory area to be zeroed.
|
---|
| 154 | * @param n Number of bytes to reset.
|
---|
[4f4b4e7] | 155 | */
|
---|
[ae1c11b] | 156 | void posix_bzero(void *mem, size_t n)
|
---|
| 157 | {
|
---|
[8ecef91] | 158 | bzero(mem, n);
|
---|
[ae1c11b] | 159 | }
|
---|
| 160 |
|
---|
[4f4b4e7] | 161 | /**
|
---|
[fc3680e] | 162 | * Scan string for a first occurence of a character.
|
---|
[4f4b4e7] | 163 | *
|
---|
[fc3680e] | 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.
|
---|
[4f4b4e7] | 168 | */
|
---|
[ae1c11b] | 169 | char *posix_index(const char *s, int c)
|
---|
| 170 | {
|
---|
| 171 | return posix_strchr(s, c);
|
---|
| 172 | }
|
---|
| 173 |
|
---|
[4f4b4e7] | 174 | /**
|
---|
[fc3680e] | 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.
|
---|
[4f4b4e7] | 181 | */
|
---|
[ae1c11b] | 182 | char *posix_rindex(const char *s, int c)
|
---|
| 183 | {
|
---|
| 184 | return posix_strrchr(s, c);
|
---|
| 185 | }
|
---|
| 186 |
|
---|
| 187 | /** @}
|
---|
| 188 | */
|
---|