1 | /* $Id: kHlpCRTString.cpp 3574 2007-09-02 19:30:58Z bird $ */
|
---|
2 | /** @file
|
---|
3 | * kHlpString - String And Memory Routines, CRT based implementation.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (c) 2007 knut st. osmundsen <bird-src-spam@anduin.net>
|
---|
8 | *
|
---|
9 | * This file is part of kStuff.
|
---|
10 | *
|
---|
11 | * kStuff is free software; you can redistribute it and/or
|
---|
12 | * modify it under the terms of the GNU Lesser General Public
|
---|
13 | * License as published by the Free Software Foundation; either
|
---|
14 | * version 2.1 of the License, or (at your option) any later version.
|
---|
15 | *
|
---|
16 | * kStuff is distributed in the hope that it will be useful,
|
---|
17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
19 | * Lesser General Public License for more details.
|
---|
20 | *
|
---|
21 | * You should have received a copy of the GNU Lesser General Public
|
---|
22 | * License along with kStuff; if not, write to the Free Software
|
---|
23 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
---|
24 | *
|
---|
25 | */
|
---|
26 |
|
---|
27 | /*******************************************************************************
|
---|
28 | * Header Files *
|
---|
29 | *******************************************************************************/
|
---|
30 | #include <k/kHlpString.h>
|
---|
31 | #include <string.h>
|
---|
32 |
|
---|
33 |
|
---|
34 | #ifndef kHlpMemChr
|
---|
35 | void *kHlpMemChr(const void *pv, int ch, KSIZE cb)
|
---|
36 | {
|
---|
37 | return memchr(pv, ch, cb);
|
---|
38 | }
|
---|
39 | #endif
|
---|
40 |
|
---|
41 |
|
---|
42 | #ifndef kHlpMemComp
|
---|
43 | int kHlpMemComp(const void *pv1, const void *pv2, KSIZE cb)
|
---|
44 | {
|
---|
45 | return memcmp(pv1, pv2, cb);
|
---|
46 | }
|
---|
47 | #endif
|
---|
48 |
|
---|
49 |
|
---|
50 | #ifndef kHlpMemCopy
|
---|
51 | void *kHlpMemCopy(void *pv1, const void *pv2, KSIZE cb)
|
---|
52 | {
|
---|
53 | return memcpy(pv1, pv2, cb);
|
---|
54 | }
|
---|
55 | #endif
|
---|
56 |
|
---|
57 |
|
---|
58 | #ifndef kHlpMemMove
|
---|
59 | void *kHlpMemMove(void *pv1, const void *pv2, KSIZE cb)
|
---|
60 | {
|
---|
61 | return memmove(pv1, pv2, cb);
|
---|
62 | }
|
---|
63 | #endif
|
---|
64 |
|
---|
65 |
|
---|
66 | #ifndef kHlpMemSet
|
---|
67 | void *kHlpMemSet(void *pv1, int ch, KSIZE cb)
|
---|
68 | {
|
---|
69 | return memset(pv1, ch, cb);
|
---|
70 | }
|
---|
71 | #endif
|
---|
72 |
|
---|
73 |
|
---|
74 | #ifndef kHlpStrCat
|
---|
75 | char *kHlpStrCat(char *psz1, const char *psz2)
|
---|
76 | {
|
---|
77 | return strcat(psz1, psz2);
|
---|
78 | }
|
---|
79 | #endif
|
---|
80 |
|
---|
81 |
|
---|
82 | #ifndef kHlpStrNCat
|
---|
83 | char *kHlpStrNCat(char *psz1, const char *psz2, KSIZE cb)
|
---|
84 | {
|
---|
85 | return strncat(psz1, psz2, cb);
|
---|
86 | }
|
---|
87 | #endif
|
---|
88 |
|
---|
89 |
|
---|
90 | #ifndef kHlpStrChr
|
---|
91 | char *kHlpStrChr(const char *psz, int ch)
|
---|
92 | {
|
---|
93 | return strchr(psz, ch);
|
---|
94 | }
|
---|
95 | #endif
|
---|
96 |
|
---|
97 |
|
---|
98 | #ifndef kHlpStrRChr
|
---|
99 | char *kHlpStrRChr(const char *psz, int ch)
|
---|
100 | {
|
---|
101 | return strrchr(psz, ch);
|
---|
102 | }
|
---|
103 | #endif
|
---|
104 |
|
---|
105 |
|
---|
106 | #ifndef kHlpStrComp
|
---|
107 | int kHlpStrComp(const char *psz1, const char *psz2)
|
---|
108 | {
|
---|
109 | return strcmp(psz1, psz2);
|
---|
110 | }
|
---|
111 | #endif
|
---|
112 |
|
---|
113 |
|
---|
114 | #ifndef kHlpStrNComp
|
---|
115 | int kHlpStrNComp(const char *psz1, const char *psz2, KSIZE cch)
|
---|
116 | {
|
---|
117 | return strncmp(psz1, psz2, cch);
|
---|
118 | }
|
---|
119 | #endif
|
---|
120 |
|
---|
121 |
|
---|
122 | #ifndef kHlpStrCopy
|
---|
123 | char *kHlpStrCopy(char *psz1, const char *psz2)
|
---|
124 | {
|
---|
125 | return strcpy(psz1, psz2);
|
---|
126 | }
|
---|
127 | #endif
|
---|
128 |
|
---|
129 |
|
---|
130 | #ifndef kHlpStrLen
|
---|
131 | KSIZE kHlpStrLen(const char *psz1)
|
---|
132 | {
|
---|
133 | return strlen(psz1);
|
---|
134 | }
|
---|
135 | #endif
|
---|
136 |
|
---|
137 |
|
---|
138 | KSIZE kHlpStrNLen(const char *psz, KSIZE cchMax)
|
---|
139 | {
|
---|
140 | const char * const pszStart = psz;
|
---|
141 | while (*psz && cchMax--)
|
---|
142 | psz++;
|
---|
143 | return psz - pszStart;
|
---|
144 | }
|
---|
145 |
|
---|
146 |
|
---|
147 | int kHlpMemICompAscii(const void *pv1, const void *pv2, KSIZE cb)
|
---|
148 | {
|
---|
149 | const KU8 *pb1 = (const KU8 *)pv1;
|
---|
150 | const KU8 *pb2 = (const KU8 *)pv2;
|
---|
151 | while (cb-- > 0)
|
---|
152 | {
|
---|
153 | if (*pb1 != *pb2)
|
---|
154 | {
|
---|
155 | const KU8 u1 = *pb1 >= 'a' && *pb1 <= 'z' ? *pb1 - 'a' : *pb1;
|
---|
156 | const KU8 u2 = *pb2 >= 'a' && *pb2 <= 'z' ? *pb2 - 'a' : *pb2;
|
---|
157 | if (u1 != u2)
|
---|
158 | return (int)*pb1 - (int)*pb2;
|
---|
159 | }
|
---|
160 | pb1++;
|
---|
161 | pb2++;
|
---|
162 | }
|
---|
163 | return 0;
|
---|
164 | }
|
---|
165 |
|
---|
166 |
|
---|
167 | int kLdrHlpStrIComp(const char *pv1, const char *pv2)
|
---|
168 | {
|
---|
169 | const KU8 *pb1 = (const KU8 *)pv1;
|
---|
170 | const KU8 *pb2 = (const KU8 *)pv2;
|
---|
171 | for (;;)
|
---|
172 | {
|
---|
173 | if (*pb1 != *pb2)
|
---|
174 | {
|
---|
175 | const KU8 u1 = *pb1 >= 'a' && *pb1 <= 'z' ? *pb1 - 'a' : *pb1;
|
---|
176 | const KU8 u2 = *pb2 >= 'a' && *pb2 <= 'z' ? *pb2 - 'a' : *pb2;
|
---|
177 | if (u1 != u2)
|
---|
178 | return (int)*pb1 - (int)*pb2;
|
---|
179 | }
|
---|
180 | if (!*pb1)
|
---|
181 | break;
|
---|
182 | pb1++;
|
---|
183 | pb2++;
|
---|
184 | }
|
---|
185 | return 0;
|
---|
186 | }
|
---|
187 |
|
---|
188 |
|
---|
189 |
|
---|