source: trunk/kStuff/kHlp/Bare/kHlpBareStr.c@ 3574

Last change on this file since 3574 was 3574, checked in by bird, 18 years ago

Generic kHlpString.h implementation.

  • Property svn:keywords set to Id
File size: 3.5 KB
Line 
1/* $Id: kHlpBareStr.c 3574 2007-09-02 19:30:58Z bird $ */
2/** @file
3 * kHlpBare - String Manipulation.
4 */
5
6/*
7 * Copyright (c) 2006-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
32
33KSIZE kHlpStrNLen(const char *psz, KSIZE cchMax)
34{
35 const char * const pszStart = psz;
36 while (*psz && cchMax--)
37 psz++;
38 return psz - pszStart;
39}
40
41
42int kHlpMemICompAscii(const void *pv1, const void *pv2, KSIZE cb)
43{
44 const KU8 *pb1 = (const KU8 *)pv1;
45 const KU8 *pb2 = (const KU8 *)pv2;
46 while (cb-- > 0)
47 {
48 if (*pb1 != *pb2)
49 {
50 const KU8 u1 = *pb1 >= 'a' && *pb1 <= 'z' ? *pb1 - 'a' : *pb1;
51 const KU8 u2 = *pb2 >= 'a' && *pb2 <= 'z' ? *pb2 - 'a' : *pb2;
52 if (u1 != u2)
53 return (int)*pb1 - (int)*pb2;
54 }
55 pb1++;
56 pb2++;
57 }
58 return 0;
59}
60
61
62int kHlpStrICompAscii(const char *pv1, const char *pv2)
63{
64 const KU8 *pb1 = (const KU8 *)pv1;
65 const KU8 *pb2 = (const KU8 *)pv2;
66 for (;;)
67 {
68 if (*pb1 != *pb2)
69 {
70 const KU8 u1 = *pb1 >= 'a' && *pb1 <= 'z' ? *pb1 - 'a' : *pb1;
71 const KU8 u2 = *pb2 >= 'a' && *pb2 <= 'z' ? *pb2 - 'a' : *pb2;
72 if (u1 != u2)
73 return (int)*pb1 - (int)*pb2;
74 }
75 if (!*pb1)
76 break;
77 pb1++;
78 pb2++;
79 }
80 return 0;
81}
82
83
84#ifdef kHlpStrChr_needed
85char *kHlpStrChr(const char *psz, int ch)
86{
87 while (*psz)
88 {
89 if (*psz == ch)
90 return (char *)psz;
91 psz++;
92 }
93 return NULL;
94}
95#endif
96
97
98#ifdef kHlpMemChr_needed
99void *kHlpMemChr(const void *pv, int ch, KSIZE cb)
100{
101 const KU8 *pb = (const KU8 *)pv;
102 const KU8 b = (KU8)ch;
103 while (cb-- > 0)
104 {
105 if (*pb == b)
106 return (void *)pb;
107 pb++;
108 }
109 return NULL;
110}
111#endif
112
113
114#ifdef kHlpMemMove_needed
115void *kHlpMemMove(void *pv1, const void *pv2, KSIZE cb)
116{
117 KU8 *pbDst = (KU8 *)pv1;
118 const KU8 *pbSrc = (const KU8 *)pv2;
119 while (cb-- > 0)
120 {
121 const KU8 b = *pbSrc++;
122 *pbDst++ = b;
123 }
124 return pv1;
125}
126#endif
127
128
129#ifdef kHlpStrNComp_needed
130int kHlpStrNComp(const char *psz1, const char *psz2, KSIZE cch)
131{
132 while (cch-- > 0)
133 {
134 if (*psz1 != *psz2)
135 return (int)*psz1 - (int)*psz2;
136 if (!*psz1)
137 break;
138 psz1++;
139 psz2++;
140 }
141 return 0;
142}
143#endif
144
Note: See TracBrowser for help on using the repository browser.