1/*- 2 * Copyright (c) 2014 Antti Kantee. All Rights Reserved. 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 1. Redistributions of source code must retain the above copyright 8 * notice, this list of conditions and the following disclaimer. 9 * 2. Redistributions in binary form must reproduce the above copyright 10 * notice, this list of conditions and the following disclaimer in the 11 * documentation and/or other materials provided with the distribution. 12 * 13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS 14 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 15 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 16 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 17 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 19 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 20 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 21 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 23 * SUCH DAMAGE. 24 */ 25 26/* 27 * The assumption is that these won't be used very often, 28 * only for the very low-level routines. 29 * 30 * Some code from public domain implementations. 31 */ 32 33#include <bmk-core/null.h> 34#include <bmk-core/string.h> 35 36unsigned long 37bmk_strlen(const char *str) 38{ 39 unsigned long rv = 0; 40 41 while (*str++) 42 rv++; 43 return rv; 44} 45 46int 47bmk_strcmp(const char *a, const char *b) 48{ 49 50 while (*a && *a++ == *b++) { 51 continue; 52 } 53 if (*a) { 54 a--; 55 b--; 56 } 57 return *a - *b; 58} 59 60int 61bmk_strncmp(const char *a, const char *b, unsigned long n) 62{ 63 unsigned char u1, u2; 64 65 while (n-- > 0) { 66 u1 = (unsigned char)*a++; 67 u2 = (unsigned char)*b++; 68 if (u1 != u2) 69 return u1 - u2; 70 if (u1 == '\0') 71 return 0; 72 } 73 return 0; 74} 75 76char * 77bmk_strcpy(char *d, const char *s) 78{ 79 char *orig = d; 80 81 while ((*d++ = *s++) != '\0') 82 continue; 83 return orig; 84} 85 86char * 87bmk_strncpy(char *d, const char *s, unsigned long n) 88{ 89 char *orig = d; 90 91 while (n && (*d++ = *s++)) 92 n--; 93 while (n--) 94 *d++ = '\0'; 95 return orig; 96} 97 98void * 99bmk_memset(void *b, int c, unsigned long n) 100{ 101 unsigned char *v = b; 102 103 while (n--) 104 *v++ = (unsigned char)c; 105 106 return b; 107} 108 109void * 110bmk_memcpy(void *d, const void *src, unsigned long n) 111{ 112 unsigned char *dp; 113 const unsigned char *sp; 114 115 dp = d; 116 sp = src; 117 118 while (n--) 119 *dp++ = *sp++; 120 121 return d; 122} 123 124void * 125bmk_memchr(const void *d, int c, unsigned long n) 126{ 127 const unsigned char *p = d; 128 129 while (n--) { 130 if (*p == (unsigned char)c) 131 return (void *)(unsigned long)p; 132 p++; 133 } 134 return NULL; 135} 136 137void * 138bmk_memrchr(const void *d, int c, unsigned long n) 139{ 140 const unsigned char *p = d; 141 142 while (n--) { 143 if (*p == (unsigned char)c) 144 return (void *)(unsigned long)p; 145 p--; 146 } 147 return NULL; 148} 149