1 /*
2  *
3  *  C++ Portable Types Library (PTypes)
4  *  Version 2.1.1  Released 27-Jun-2007
5  *
6  *  Copyright (C) 2001-2007 Hovik Melikyan
7  *
8  *  http://www.melikyan.com/ptypes/
9  *
10  */
11 
12 #include "ptypes.h"
13 
14 
15 PTYPES_BEGIN
16 
17 
hexchar(uchar c)18 static char hexchar(uchar c)
19 {
20     if (c < 10)
21         return char(c + '0');
22     else
23         return char(c - 10 + 'a');
24 }
25 
26 
isprintable(uchar c)27 inline bool isprintable(uchar c)
28 {
29     return ((c >= ' ') && (c < 127));
30 }
31 
32 
showmember(uchar c)33 static string showmember(uchar c)
34 {
35     if ((c == '-') || (c == '~'))
36         return string('~') + string(c);
37     else if (isprintable(c))
38         return c;
39     else
40     {
41         string ret = "~  ";
42         ret[1] = hexchar(uchar(c >> 4));
43         ret[2] = hexchar(uchar(c & 0x0f));
44         return ret;
45     }
46 }
47 
48 
asstring(const cset & s)49 string ptdecl asstring(const cset& s)
50 {
51     string ret;
52     int l = -1, r = -1;
53     for(int i = 0; i <= _csetbits; i++)
54     {
55         if (i < _csetbits && uchar(i) & s)
56         {
57             if (l == -1)
58                 l = i;
59             else
60                 r = i;
61         }
62         else if (l != -1)
63         {
64             concat(ret, showmember(uchar(l)));
65             if (r != -1) {
66                 if (r > l + 1)
67                     concat(ret, '-');
68                 concat(ret, showmember(uchar(r)));
69             }
70             l = -1;
71             r = -1;
72         }
73     }
74     return ret;
75 }
76 
77 
78 PTYPES_END
79