ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/rxvt-unicode/src/rxvtutil.h
(Generate patch)

Comparing rxvt-unicode/src/rxvtutil.h (file contents):
Revision 1.27 by root, Fri Jun 23 14:31:38 2006 UTC vs.
Revision 1.64 by root, Tue Jun 25 03:35:01 2019 UTC

1#ifndef RXVT_UTIL_H 1#ifndef RXVT_UTIL_H
2#define RXVT_UTIL_H 2#define RXVT_UTIL_H
3 3
4#include <cstdlib> 4#include <stdlib.h>
5#include <cstring> 5#include <string.h>
6 6
7using namespace std; 7#define ECB_NO_THREADS 1
8#include "ecb.h"
8 9
9#define PP_CONCAT_(a, b) a ## b 10#include "estl.h"
10#define PP_CONCAT(a, b) PP_CONCAT_(a, b)
11#define PP_STRINGIFY_(a) #a
12#define PP_STRINGIFY(a) PP_STRINGIFY_(a)
13 11
14// actually, some gcc-3.x versions work, too 12#include "emman.h"
15#define HAVE_GCC_BUILTINS (__GNUC__ >= 4)
16
17#ifndef __attribute__
18# if __GNUC__
19# if (__GNUC__ == 2 && __GNUC_MINOR__ < 5) || (__GNUC__ < 2)
20# define __attribute__(x)
21# endif
22# endif
23# define __attribute__(x)
24#endif
25
26#define NORETURN __attribute__ ((noreturn))
27#define UNUSED __attribute__ ((unused))
28#define CONST __attribute__ ((const))
29 13
30// increases code size unless -fno-enforce-eh-specs 14// increases code size unless -fno-enforce-eh-specs
31#if __GNUC__ 15#if __GNUC__
32# define NOTHROW 16# define NOTHROW
33# define THROW(x) 17# define THROW(x)
34#else 18#else
35# define NOTHROW throw() 19# define NOTHROW throw()
36# define THROW(x) throw x 20# define THROW(x) throw x
37#endif 21#endif
38 22
39extern class byteorder {
40 static unsigned int e; // at least 32 bits
41public:
42 byteorder ();
43
44 static bool big_endian () { return e == 0x11223344; };
45 static bool network () { return e == 0x11223344; };
46 static bool little_endian () { return e == 0x44332211; };
47 static bool vax () { return e == 0x44332211; };
48} byteorder;
49
50// various utility functions 23// various utility functions
51template<typename T, typename U> static inline T min (T a, U b) { return a < (T)b ? a : (T)b; }
52template<typename T, typename U> static inline void min_it (T &a, U b) { a = a < (T)b ? a : (T)b; } 24template<typename T, typename U> static inline void min_it (T &a, U b) { a = a < (T)b ? a : (T)b; }
53template<typename T, typename U> static inline T max (T a, U b) { return a > (T)b ? a : (T)b; }
54template<typename T, typename U> static inline void max_it (T &a, U b) { a = a > (T)b ? a : (T)b; } 25template<typename T, typename U> static inline void max_it (T &a, U b) { a = a > (T)b ? a : (T)b; }
55 26
56template<typename T, typename U, typename V> static inline T clamp (T v, U a, V b) { return v < (T)a ? a : v >(T)b ? b : v; } 27template<typename T, typename U, typename V> static inline T clamp (T v, U a, V b) { return v < (T)a ? a : v >(T)b ? b : v; }
57template<typename T, typename U, typename V> static inline void clamp_it (T &v, U a, V b) { v = v < (T)a ? a : v >(T)b ? b : v; } 28template<typename T, typename U, typename V> static inline void clamp_it (T &v, U a, V b) { v = v < (T)a ? a : v >(T)b ? b : v; }
58 29
59template<typename T, typename U> static inline void swap (T& a, U& b) { T t=a; a=(T)b; b=(U)t; }
60
61template<typename T> static inline T squared_diff (T a, T b) { return (a-b)*(a-b); }
62
63// linear interpolation 30// linear interpolation
64template<typename T, typename U, typename P> 31template<typename T, typename U, typename P>
65static inline 32static inline T
66T lerp (T a, U b, P p) 33lerp (T a, U b, P p)
67{ 34{
68 return (long(a) * long(100 - p) + long(b) * long(p) + 50) / 100; 35 return (long(a) * long(100 - p) + long(b) * long(p) + 50) / 100;
69} 36}
70 37
71// some bit functions, xft fuck me plenty 38// return a very temporary (and never deallocated) buffer. keep small.
72#if HAVE_GCC_BUILTINS 39void *rxvt_temp_buf (int len);
73static inline int ctz (unsigned int x) CONST { return __builtin_ctz (x); } 40
74static inline int popcount (unsigned int x) CONST { return __builtin_popcount (x); } 41template<typename T>
75#else 42static inline T *
76// count trailing zero bits and count # of one bits 43rxvt_temp_buf (int len)
77int ctz (unsigned int x) CONST; 44{
78int popcount (unsigned int x) CONST; 45 return (T *)rxvt_temp_buf (len * sizeof (T));
79#endif 46}
80 47
81// in range including end 48// in range including end
82#define IN_RANGE_INC(val,beg,end) \ 49#define IN_RANGE_INC(val,beg,end) \
83 ((unsigned int)(val) - (unsigned int)(beg) <= (unsigned int)(end) - (unsigned int)(beg)) 50 ((unsigned int)(val) - (unsigned int)(beg) <= (unsigned int)(end) - (unsigned int)(beg))
84 51
85// in range excluding end 52// in range excluding end
86#define IN_RANGE_EXC(val,beg,end) \ 53#define IN_RANGE_EXC(val,beg,end) \
87 ((unsigned int)(val) - (unsigned int)(beg) < (unsigned int)(end) - (unsigned int)(beg)) 54 ((unsigned int)(val) - (unsigned int)(beg) < (unsigned int)(end) - (unsigned int)(beg))
88 55
56// for m >= -n, ensure remainder lies between 0..n-1
57#define MOD(m,n) (((m) + (n)) % (n))
58
89// makes dynamically allocated objects zero-initialised 59// makes dynamically allocated objects zero-initialised
90struct zero_initialized { 60struct zero_initialized
61{
91 void *operator new (size_t s); 62 void *operator new (size_t s);
92 void operator delete (void *p, size_t s); 63 void operator delete (void *p, size_t s);
93}; 64};
94 65
95/* simplevec taken (and heavily modified), from: 66struct stringvec : simplevec<char *>
96 * 67{
97 * MICO --- a free CORBA implementation 68 ~stringvec ()
98 * Copyright (C) 1997-98 Kay Roemer & Arno Puder
99 */
100template<class T>
101struct simplevec {
102 typedef T* iterator;
103 typedef const T* const_iterator;
104 typedef unsigned long size_type;
105
106private:
107 size_type _last, _size;
108 T *_buf;
109
110public:
111 const_iterator begin () const
112 { 69 {
113 return &_buf[0]; 70 for (char **c = begin (); c != end (); c++)
71 free (*c);
114 } 72 }
115 iterator begin ()
116 {
117 return &_buf[0];
118 }
119 const_iterator end () const
120 {
121 return &_buf[_last];
122 }
123 iterator end ()
124 {
125 return &_buf[_last];
126 }
127 size_type capacity () const
128 {
129 return _size;
130 }
131 size_type size () const
132 {
133 return _last;
134 }
135
136private:
137 static T *alloc (size_type n)
138 {
139 return (T *)::operator new ((size_t) (n * sizeof (T)));
140 }
141 static void dealloc (T *buf)
142 {
143 if (buf)
144 ::operator delete (buf);
145 }
146
147 void reserve (iterator where, size_type n)
148 {
149 if (_last + n <= _size) {
150 memmove (where+n, where, (end ()-where)*sizeof (T));
151 } else {
152 size_type sz = _last+n;
153 sz = (_size == 0) ? max (sz, 5) : max (sz, 2*_size);
154 T *nbuf = alloc (sz);
155 if (_buf) {
156 memcpy (nbuf, begin (), (where-begin ())*sizeof (T));
157 memcpy (nbuf + (where-begin ()) + n, where,
158 (end ()-where)*sizeof (T));
159 dealloc (_buf);
160 }
161 _buf = nbuf;
162 _size = sz;
163 }
164 }
165
166public:
167 void reserve (size_type sz)
168 {
169 if (_size < sz) {
170 sz = (_size == 0) ? max (sz, 5) : max (sz, 2*_size);
171 T *nbuf = alloc (sz);
172 if (_buf) {
173 memcpy (nbuf, begin (), size ()*sizeof (T));
174 dealloc (_buf);
175 }
176 _buf = nbuf;
177 _size = sz;
178 }
179 }
180 simplevec ()
181 : _last(0), _size(0), _buf(0)
182 {
183 }
184 simplevec (size_type n, const T& t = T ())
185 : _last(0), _size(0), _buf(0)
186 {
187 insert (begin (), n, t);
188 }
189 simplevec (const_iterator first, const_iterator last)
190 : _last(0), _size(0), _buf(0)
191 {
192 insert (begin (), first, last);
193 }
194 simplevec (const simplevec<T> &v)
195 : _last(0), _size(0), _buf(0)
196 {
197 reserve (v._last);
198 memcpy (_buf, v.begin (), v.size ()*sizeof (T));
199 _last = v._last;
200 }
201 simplevec<T> &operator= (const simplevec<T> &v)
202 {
203 if (this != &v) {
204 _last = 0;
205 reserve (v._last);
206 memcpy (_buf, v.begin (), v.size ()*sizeof (T));
207 _last = v._last;
208 }
209 return *this;
210 }
211 ~simplevec ()
212 {
213 dealloc (_buf);
214 }
215 const T &front () const
216 {
217 //ministl_assert (size () > 0);
218 return _buf[0];
219 }
220 T &front ()
221 {
222 //ministl_assert (size () > 0);
223 return _buf[0];
224 }
225 const T &back () const
226 {
227 //ministl_assert (size () > 0);
228 return _buf[_last-1];
229 }
230 T &back ()
231 {
232 //ministl_assert (size () > 0);
233 return _buf[_last-1];
234 }
235 bool empty () const
236 {
237 return _last == 0;
238 }
239 void clear ()
240 {
241 _last = 0;
242 }
243 void push_back (const T &t)
244 {
245 reserve (_last+1);
246 *end () = t;
247 ++_last;
248 }
249 void push_back (T &t)
250 {
251 reserve (_last+1);
252 *end () = t;
253 ++_last;
254 }
255 void pop_back ()
256 {
257 //ministl_assert (size () > 0);
258 --_last;
259 }
260 const T &operator[] (size_type idx) const
261 {
262 //ministl_assert (idx < size ());
263 return _buf[idx];
264 }
265 T &operator[] (size_type idx)
266 {
267 //ministl_assert (idx < size ());
268 return _buf[idx];
269 }
270 iterator insert (iterator pos, const T &t)
271 {
272 //ministl_assert (pos <= end ());
273 long at = pos - begin ();
274 reserve (pos, 1);
275 pos = begin ()+at;
276 *pos = t;
277 ++_last;
278 return pos;
279 }
280 iterator insert (iterator pos, const_iterator first, const_iterator last)
281 {
282 //ministl_assert (pos <= end ());
283 long n = last - first;
284 long at = pos - begin ();
285 if (n > 0) {
286 reserve (pos, n);
287 pos = begin ()+at;
288 memcpy (pos, first, (last-first)*sizeof (T));
289 _last += n;
290 }
291 return pos;
292 }
293 iterator insert (iterator pos, size_type n, const T &t)
294 {
295 //ministl_assert (pos <= end ());
296 long at = pos - begin ();
297 if (n > 0) {
298 reserve (pos, n);
299 pos = begin ()+at;
300 for (int i = 0; i < n; ++i)
301 pos[i] = t;
302 _last += n;
303 }
304 return pos;
305 }
306 void erase (iterator first, iterator last)
307 {
308 if (last != first) {
309 memmove (first, last, (end () - last) * sizeof (T));
310 _last -= last - first;
311 }
312 }
313 void erase (iterator pos)
314 {
315 if (pos != end ()) {
316 memmove (pos, pos+1, (end () - (pos+1)) * sizeof (T));
317 --_last;
318 }
319 }
320 void swap (simplevec<T> &t)
321 {
322 ::swap(_last, t._last);
323 ::swap(_size, t._size);
324 ::swap(_buf, t._buf);
325 }
326}; 73};
327
328template<class T>
329bool operator== (const simplevec<T> &v1, const simplevec<T> &v2)
330{
331 if (v1.size () != v2.size ())
332 return false;
333 return !v1.size () || !memcmp (&v1[0], &v2[0], v1.size ()*sizeof (T));
334}
335
336template<class T>
337bool operator< (const simplevec<T> &v1, const simplevec<T> &v2)
338{
339 unsigned long minlast = min (v1.size (), v2.size ());
340 for (unsigned long i = 0; i < minlast; ++i) {
341 if (v1[i] < v2[i])
342 return true;
343 if (v2[i] < v1[i])
344 return false;
345 }
346 return v1.size () < v2.size ();
347}
348
349
350template<typename T>
351struct vector : simplevec<T>
352{ };
353 74
354#if 0 75#if 0
355template<typename T> 76template<typename T>
356struct rxvt_vec : simplevec<void *> { 77struct rxvt_vec : simplevec<void *>
78{
357 typedef T *iterator; 79 typedef T *iterator;
358 80
359 void push_back (T d) { simplevec<void *>::push_back ((void *)d); } 81 void push_back (T d) { simplevec<void *>::push_back ((void *)d); }
360 T pop_back () { return (T*)simplevec<void *>::pop_back (); } 82 T pop_back () { return (T*)simplevec<void *>::pop_back (); }
361 void erase (int i) { erase (begin () + i); } 83 void erase (int i) { erase (begin () + i); }
365 T &operator [] (int i) { return * (T *) (& ((* (simplevec<void *> *)this)[i])); } 87 T &operator [] (int i) { return * (T *) (& ((* (simplevec<void *> *)this)[i])); }
366 const T &operator [] (int i) const { return * (const T *) (& ((* (const simplevec<void *> *)this)[i])); } 88 const T &operator [] (int i) const { return * (const T *) (& ((* (const simplevec<void *> *)this)[i])); }
367}; 89};
368#endif 90#endif
369 91
370template <typename I, typename T> 92typedef estl::scoped_array<char> auto_str;
371I find (I first, I last, const T& value)
372{
373 while (first != last && *first != value)
374 ++first;
375
376 return first;
377}
378
379template<typename T>
380struct auto_ptr {
381 T *p;
382
383 auto_ptr () : p (0) { }
384 auto_ptr (T *a) : p (a) { }
385
386 auto_ptr (auto_ptr<T> &a)
387 {
388 p = a.p;
389 a.p = 0;
390 }
391
392 template<typename A>
393 auto_ptr (auto_ptr<A> &a)
394 {
395 p = a.p;
396 a.p = 0;
397 }
398
399 ~auto_ptr ()
400 {
401 delete p;
402 }
403
404 // void because it makes sense in our context
405 void operator = (T *a)
406 {
407 delete p;
408 p = a;
409 }
410
411 void operator = (auto_ptr &a)
412 {
413 *this = a.p;
414 a.p = 0;
415 }
416
417 template<typename A>
418 void operator = (auto_ptr<A> &a)
419 {
420 *this = a.p;
421 a.p = 0;
422 }
423
424 operator T * () const { return p; }
425
426 T *operator -> () const { return p; }
427 T &operator * () const { return *p; }
428
429 T *get ()
430 {
431 T *r = p;
432 p = 0;
433 return r;
434 }
435};
436
437typedef auto_ptr<char> auto_str;
438
439struct stringvec : simplevec<char *>
440{
441 ~stringvec ()
442 {
443 for (char **c = begin (); c != end (); c++)
444 free (*c);
445 }
446};
447
448// return a very temporary (and never deallocated) buffer. keep small.
449void *rxvt_temp_buf (int len);
450
451template<typename T>
452static inline T *
453rxvt_temp_buf (int len)
454{
455 return (T *)rxvt_temp_buf (len * sizeof (T));
456}
457 93
458#endif 94#endif
459 95

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines