2 libc-extension.cc -- implement some string.h extensions
4 source file of the flowerlib
6 (c) 1997 Han-Wen Nienhuys <hanwen@stack.nl>
11 #include "libc-extension.hh"
14 compensate for lacking libc functions.
17 strnlwr( char* start_l
,int n
)
19 char * p
= start_l
+ n
;
20 while ( --p
>= start_l
) {
21 *p
= tolower( *p
); /* a macro on some compilers */
27 strnupr( char* start_l
, int n
)
29 char * p
= start_l
+ n
;
30 while ( --p
>= start_l
) {
31 *p
= toupper( *p
); /* a macro on some compilers */
38 /** locate a substring. #memmem# finds the first occurrence of
39 #needle# in #haystack#
43 memmem(const Byte
* haystack
, int haystack_len
,
44 const Byte
*needle
,int needle_len
)
46 const Byte
* end_haystack
= haystack
+ haystack_len
- needle_len
;
47 const Byte
* end_needle
= needle
+ needle_len
;
49 /* Ahhh ... Some minimal lowlevel stuff. This *is* nice; Varation
50 is the spice of life */
51 while (haystack
< end_haystack
) {
52 const Byte
*subneedle_l
= needle
;
53 const Byte
*subhaystack_l
= haystack
;
54 while (subneedle_l
< end_needle
) {
55 if (*subneedle_l
++ != *subhaystack_l
++)
56 goto next
; // yeah. I should be prosecuted.
59 // completed the needle. Gotcha.
60 return (char*) haystack
;
69 memrchr(const Byte
* p
, int n
, char c
)
82 my_swap(T
&t1
, T
&t2
, T
&tmp
)
90 strrev( Byte
* byte_l
, int length_i
)
94 Byte
* left_l
= byte_l
;
95 Byte
* right_l
= byte_l
+ length_i
;
97 while ( right_l
> left_l
) {
98 my_swap(*right_l
-- , *left_l
++ , tmp_byte
);
103 #ifndef HAVE_SNPRINTF
104 int snprintf ( char *str
, size_t,
105 const char *format
, ... )
108 va_start(ap
, format
);
109 int i
= vsprintf(str
, format
, ap
);