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