| 1 | /*
|
|---|
| 2 | * string.c - string helper function implementation.
|
|---|
| 3 | *
|
|---|
| 4 | * Authors Ghassan Almaless (2007,2009,2010,2011,2012)
|
|---|
| 5 | * Alain Greiner (2017)
|
|---|
| 6 | *
|
|---|
| 7 | * Copyright (c) UPMC Sorbonne Universites
|
|---|
| 8 | *
|
|---|
| 9 | * This file is part of ALMOS-MKH.
|
|---|
| 10 | *
|
|---|
| 11 | * ALMOS-MKH is free software; you can redistribute it and/or modify it
|
|---|
| 12 | * under the terms of the GNU General Public License as published by
|
|---|
| 13 | * the Free Software Foundation; version 2.0 of the License.
|
|---|
| 14 | *
|
|---|
| 15 | * ALMOS-MKH is distributed in the hope that it will be useful, but
|
|---|
| 16 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
|---|
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|---|
| 18 | * General Public License for more details.
|
|---|
| 19 | *
|
|---|
| 20 | * You should have received a copy of the GNU General Public License
|
|---|
| 21 | * along with ALMOS-MKH; if not, write to the Free Software Foundation,
|
|---|
| 22 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|---|
| 23 | */
|
|---|
| 24 |
|
|---|
| 25 | #include <hal_types.h>
|
|---|
| 26 | #include <ctype.h>
|
|---|
| 27 | #include <string.h>
|
|---|
| 28 |
|
|---|
| 29 | ///////////////////////////////////
|
|---|
| 30 | uint32_t strlen( const char * str )
|
|---|
| 31 | {
|
|---|
| 32 | register const char *ptr = str;
|
|---|
| 33 |
|
|---|
| 34 | while (*ptr) ptr++;
|
|---|
| 35 |
|
|---|
| 36 | return (ptr - str);
|
|---|
| 37 | }
|
|---|
| 38 |
|
|---|
| 39 | ///////////////////////////////////
|
|---|
| 40 | uint32_t strnlen( const char * str,
|
|---|
| 41 | uint32_t maxlen )
|
|---|
| 42 | {
|
|---|
| 43 | register const char *ptr = str;
|
|---|
| 44 |
|
|---|
| 45 | while (*ptr && (maxlen-- > 0)) ptr++;
|
|---|
| 46 |
|
|---|
| 47 | return (ptr - str);
|
|---|
| 48 | }
|
|---|
| 49 |
|
|---|
| 50 | ////////////////////////////
|
|---|
| 51 | int strcmp( const char * s1,
|
|---|
| 52 | const char * s2 )
|
|---|
| 53 | {
|
|---|
| 54 |
|
|---|
| 55 | while (*s1 && *s1 == *s2)
|
|---|
| 56 | {
|
|---|
| 57 | s1 ++;
|
|---|
| 58 | s2 ++;
|
|---|
| 59 | }
|
|---|
| 60 |
|
|---|
| 61 | return (*s1 - *s2);
|
|---|
| 62 | }
|
|---|
| 63 |
|
|---|
| 64 | /////////////////////////////
|
|---|
| 65 | int strncmp( const char * s1,
|
|---|
| 66 | const char * s2,
|
|---|
| 67 | uint32_t n )
|
|---|
| 68 | {
|
|---|
| 69 | if (n == 0)
|
|---|
| 70 | return s1 - s2; // XXX ???
|
|---|
| 71 |
|
|---|
| 72 | while (*s1 && (*s1 == *s2) && (n > 1))
|
|---|
| 73 | {
|
|---|
| 74 | s1 ++;
|
|---|
| 75 | s2 ++;
|
|---|
| 76 | n--;
|
|---|
| 77 | }
|
|---|
| 78 |
|
|---|
| 79 | return (*s1 - *s2);
|
|---|
| 80 | }
|
|---|
| 81 |
|
|---|
| 82 | //////////////////////////////////
|
|---|
| 83 | int strcasecmp( const char * str1,
|
|---|
| 84 | const char * str2 )
|
|---|
| 85 | {
|
|---|
| 86 | char c1;
|
|---|
| 87 | char c2;
|
|---|
| 88 |
|
|---|
| 89 | do{
|
|---|
| 90 | c1 = toupper(*++str1);
|
|---|
| 91 | c2 = toupper(*++str2);
|
|---|
| 92 | }while(c1 && c1 == c2);
|
|---|
| 93 |
|
|---|
| 94 | return (c1 - c2);
|
|---|
| 95 | }
|
|---|
| 96 |
|
|---|
| 97 | ///////////////////////////
|
|---|
| 98 | char * strcpy (char * dest,
|
|---|
| 99 | char * src )
|
|---|
| 100 | {
|
|---|
| 101 | char *src_ptr = src;
|
|---|
| 102 | char *dst_ptr = dest;
|
|---|
| 103 |
|
|---|
| 104 | while(*src_ptr)
|
|---|
| 105 | *(dst_ptr++) = *(src_ptr++);
|
|---|
| 106 |
|
|---|
| 107 | *dst_ptr = 0;
|
|---|
| 108 | return dest;
|
|---|
| 109 | }
|
|---|
| 110 |
|
|---|
| 111 | ///////////////////////////////
|
|---|
| 112 | char * strncpy( char * dest,
|
|---|
| 113 | char * src,
|
|---|
| 114 | uint32_t n )
|
|---|
| 115 | {
|
|---|
| 116 | uint32_t i;
|
|---|
| 117 |
|
|---|
| 118 | for (i = 0; (i < n) && (src[i] != '\0') ; i++)
|
|---|
| 119 | dest[i] = src[i];
|
|---|
| 120 |
|
|---|
| 121 | for (; i < n; i++)
|
|---|
| 122 | dest[i] = '\0';
|
|---|
| 123 |
|
|---|
| 124 | return dest;
|
|---|
| 125 | }
|
|---|
| 126 |
|
|---|
| 127 | ////////////////////////////////
|
|---|
| 128 | char * strstr( char * s,
|
|---|
| 129 | const char * find)
|
|---|
| 130 | {
|
|---|
| 131 | char c, sc;
|
|---|
| 132 | size_t len;
|
|---|
| 133 |
|
|---|
| 134 | if ((c = *find++) != 0) {
|
|---|
| 135 | len = strlen(find);
|
|---|
| 136 | do {
|
|---|
| 137 | do {
|
|---|
| 138 | if ((sc = *s++) == 0)
|
|---|
| 139 | return NULL;
|
|---|
| 140 | } while (sc != c);
|
|---|
| 141 | } while (strncmp(s, find, len) != 0);
|
|---|
| 142 | s--;
|
|---|
| 143 | }
|
|---|
| 144 | return s;
|
|---|
| 145 | }
|
|---|
| 146 |
|
|---|
| 147 | //////////////////////////////
|
|---|
| 148 | char * strchr( const char * s,
|
|---|
| 149 | int c )
|
|---|
| 150 | {
|
|---|
| 151 | while(*s && *s != (char) c)
|
|---|
| 152 | s++;
|
|---|
| 153 |
|
|---|
| 154 | if(*s == (char) c)
|
|---|
| 155 | return (char*)s;
|
|---|
| 156 |
|
|---|
| 157 | return NULL;
|
|---|
| 158 | }
|
|---|
| 159 |
|
|---|
| 160 | ///////////////////////////////
|
|---|
| 161 | char * strrchr( const char * t,
|
|---|
| 162 | int c )
|
|---|
| 163 | {
|
|---|
| 164 | register char ch;
|
|---|
| 165 | register const char * l =0;
|
|---|
| 166 |
|
|---|
| 167 | ch = c;
|
|---|
| 168 | for (;;) {
|
|---|
| 169 | if (*t == ch)
|
|---|
| 170 | l=t;
|
|---|
| 171 | if (!*t)
|
|---|
| 172 | return (char*)l;
|
|---|
| 173 | ++t;
|
|---|
| 174 | }
|
|---|
| 175 | return (char*)l;
|
|---|
| 176 | }
|
|---|
| 177 |
|
|---|
| 178 | /////////////////////////////////////
|
|---|
| 179 | inline uint8_t to_lower( uint8_t c )
|
|---|
| 180 | {
|
|---|
| 181 | if (c >= 'A' && c <= 'Z') return (c | 0x20);
|
|---|
| 182 | else return c;
|
|---|
| 183 | }
|
|---|
| 184 |
|
|---|
| 185 |
|
|---|
| 186 | /////////////////////////////////////
|
|---|
| 187 | inline uint8_t to_upper( uint8_t c )
|
|---|
| 188 | {
|
|---|
| 189 | if (c >= 'a' && c <= 'z') return (c & ~(0x20));
|
|---|
| 190 | else return c;
|
|---|
| 191 | }
|
|---|
| 192 |
|
|---|
| 193 |
|
|---|