]> git.donarmstrong.com Git - lilypond.git/blob - flower/libc-extension.cc
release: 0.0.38
[lilypond.git] / flower / libc-extension.cc
1 /*
2   libc-extension.cc -- implement some string.h extensions
3
4   source file of the flowerlib
5
6   (c) 1997 Han-Wen Nienhuys <hanwen@stack.nl>
7 */
8
9 #include <string.h>
10 #include <ctype.h>
11 #include "libc-extension.hh"
12
13 /*
14   compensate for lacking libc functions.
15  */
16  char* 
17 strnlwr( char* start_l ,int n)
18 {
19     char * p = start_l + n;
20     while ( --p >= start_l) {
21         *p = tolower( *p );    /* a macro on some compilers */
22     }
23     return start_l;
24 }
25
26  char* 
27 strnupr( char* start_l, int n)
28 {
29     char * p = start_l + n;
30     while ( --p >= start_l) {
31         *p = toupper( *p );    /* a macro on some compilers */
32     }
33     return start_l;
34 }
35
36 /** locate a substring. #memmem# finds the first occurrence of
37   #needle# in #haystack#
38   */
39
40   char *
41 memmem(const Byte * haystack, const Byte *needle,
42        int haystack_len, int needle_len)
43 {
44     const Byte * end_haystack = haystack + haystack_len - needle_len;
45     const Byte * end_needle = needle + needle_len ;
46
47     /* Ahhh ... Some minimal lowlevel stuff. This *is* nice; Varation
48       is the spice of life */
49     while (haystack < end_haystack) {
50         const Byte *subneedle_l = needle;
51         const Byte *subhaystack_l = haystack;
52         while (subneedle_l < end_needle) {
53             if (*subneedle_l++ != *subhaystack_l++)
54                 goto next;      // yeah. I should be prosecuted.
55         }
56         
57         // completed the needle. Gotcha.
58         return (char*) haystack;
59     next:
60         haystack++;
61     }
62     return 0;
63 }
64
65 Byte *
66 memrchr(const Byte * p, int n, char c)
67 {
68     const    Byte * q = p+n;
69     while (q > p) {
70         if (*--q == c)
71             return (Byte*)q;
72     }
73     return 0;
74 }
75
76
77 template<class T>
78 inline void
79 my_swap(T &t1, T &t2, T &tmp)
80 {
81     tmp = t1;
82     t1 = t2;
83     t2 = tmp;
84 }
85
86 Byte*
87 strrev( Byte* byte_l, int length_i )
88 {
89   Byte tmp_byte;
90   
91   Byte* left_l = byte_l;
92   Byte* right_l = byte_l + length_i;
93
94   while ( right_l > left_l ) {
95       my_swap(*right_l-- , *left_l++ , tmp_byte);
96   }
97   return byte_l;
98 }