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 * strchr( const char * s, |
---|
129 | int c ) |
---|
130 | { |
---|
131 | while(*s && *s != (char) c) |
---|
132 | s++; |
---|
133 | |
---|
134 | if(*s == (char) c) |
---|
135 | return (char*)s; |
---|
136 | |
---|
137 | return NULL; |
---|
138 | } |
---|
139 | |
---|
140 | /////////////////////////////// |
---|
141 | char * strrchr( const char * t, |
---|
142 | int c ) |
---|
143 | { |
---|
144 | register char ch; |
---|
145 | register const char * l =0; |
---|
146 | |
---|
147 | ch = c; |
---|
148 | for (;;) { |
---|
149 | if (*t == ch) l=t; if (!*t) return (char*)l; ++t; |
---|
150 | } |
---|
151 | return (char*)l; |
---|
152 | } |
---|
153 | |
---|
154 | ///////////////////////////////////// |
---|
155 | inline uint8_t to_lower( uint8_t c ) |
---|
156 | { |
---|
157 | if (c >= 'A' && c <= 'Z') return (c | 0x20); |
---|
158 | else return c; |
---|
159 | } |
---|
160 | |
---|
161 | |
---|
162 | ///////////////////////////////////// |
---|
163 | inline uint8_t to_upper( uint8_t c ) |
---|
164 | { |
---|
165 | if (c >= 'a' && c <= 'z') return (c & ~(0x20)); |
---|
166 | else return c; |
---|
167 | } |
---|
168 | |
---|
169 | |
---|