2 This file is part of LilyPond, the GNU music typesetter.
4 Copyright (C) 1997--2015 Han-Wen Nienhuys <hanwen@xs4all.nl>
5 Jan Nieuwenhuizen <janneke@gnu.org>
7 LilyPond is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
12 LilyPond is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with LilyPond. If not, see <http://www.gnu.org/licenses/>.
28 #include "libc-extension.hh"
31 strnlwr (char *start, int n)
36 *p = (char)tolower (*p); /* a macro on some compilers */
42 strnupr (char *start, int n)
47 *p = (char)toupper (*p); /* a macro on some compilers */
54 /** locate a substring. #memmem# finds the first occurrence of
55 #needle# in #haystack#. This is not ANSI-C.
57 The prototype is not in accordance with the Linux Programmer's
58 Manual v1.15, but it is with /usr/include/string.h */
61 _memmem (unsigned char const *haystack, int haystack_len,
62 unsigned char const *needle, int needle_len)
64 unsigned char const *end_haystack = haystack + haystack_len - needle_len + 1;
65 unsigned char const *end_needle = needle + needle_len;
67 /* Ahhh ... Some minimal lowlevel stuff. This *is* nice; Varation
68 is the spice of life */
69 while (haystack < end_haystack)
71 unsigned char const *subneedle = needle;
72 unsigned char const *subhaystack = haystack;
73 while (subneedle < end_needle)
74 if (*subneedle++ != *subhaystack++)
77 /* Completed the needle. Gotcha. */
78 return (unsigned char *) haystack;
86 memmem (void const *haystack, int haystack_len,
87 void const *needle, int needle_len)
89 unsigned char const *haystack_byte_c = (unsigned char const *)haystack;
90 unsigned char const *needle_byte_c = (unsigned char const *)needle;
91 return _memmem (haystack_byte_c, haystack_len, needle_byte_c, needle_len);
97 memrchr (unsigned char const *p, int n, char c)
99 const unsigned char *q = p + n;
103 return (unsigned char *)q;
110 my_swap (T &t1, T &t2, T &tmp)
118 memrev (unsigned char *byte, int length)
120 unsigned char tmp_byte;
121 unsigned char *left = byte;
122 unsigned char *right = byte + length;
125 my_swap (*right--, *left++, tmp_byte);
130 There are some strange problems with round() on early glibcs.
135 return floor (x - 0.5) + 1.0;
138 /* namespace std { */
145 return x && (x == x / 2);
152 snprintf (char *str, size_t n, char const *format, ...)
155 va_start (ap, format);
156 int i = vsprintf (str, format, ap);
157 if (i > 0 && (unsigned) i > n)
166 vsnprintf (char *str, size_t n, char const *format, va_list args)
168 int i = vsprintf (str, format, args);
169 if (i > 0 && (unsigned) i > n)
175 /* } namespace std */