1 // Copyright (c) 1994, 1997 James Clark
2 // See the file COPYING for copying permission.
3 #pragma ident "%Z%%M% %I% %E% SMI"
4
5 #ifndef CharsetInfo_INCLUDED
6 #define CharsetInfo_INCLUDED 1
7 #ifdef __GNUG__
8 #pragma interface
9 #endif
10
11 #include <limits.h>
12 #include "UnivCharsetDesc.h"
13 #include "Boolean.h"
14 #include "types.h"
15 #include "StringC.h"
16 #include "ISet.h"
17 #include "CharMap.h"
18
19 #ifdef SP_NAMESPACE
20 namespace SP_NAMESPACE {
21 #endif
22
23 class SP_API CharsetInfo {
24 public:
25 CharsetInfo();
26 CharsetInfo(const UnivCharsetDesc &);
27 void set(const UnivCharsetDesc &);
28 // Use only for characters guaranteed to me in the C basic execution
29 // character set and which have been verified to be in this
30 // character set.
31 Char execToDesc(char) const;
32 StringC execToDesc(const char *s) const;
33 Boolean descToUniv(WideChar from, UnivChar &to) const;
34 Boolean descToUniv(WideChar from, UnivChar &to, WideChar &alsoMax) const;
35 // Return 0 for no matches, 1 for 1, 2 for more than 1
36 // to gets the first character; toSet gets all the characters
37 // if there's more than 1.
38 unsigned univToDesc(UnivChar from, WideChar &to, ISet<WideChar> &toSet)
39 const;
40 unsigned univToDesc(UnivChar from, WideChar &to, ISet<WideChar> &toSet,
41 WideChar &count)
42 const;
43 void getDescSet(ISet<Char> &) const;
44 int digitWeight(Char) const;
45 int hexDigitWeight(Char) const;
46 const UnivCharsetDesc &desc() const;
47 private:
48 void init();
49 UnivCharsetDesc desc_;
50 CharMap<Unsigned32> inverse_;
51 Char execToDesc_[UCHAR_MAX + 1];
52 };
53
54 inline
univToDesc(UnivChar from,WideChar & to,ISet<WideChar> & toSet)55 unsigned CharsetInfo::univToDesc(UnivChar from, WideChar &to,
56 ISet<WideChar> &toSet)
57 const
58 {
59 if (from <= Char(-1)) {
60 Unsigned32 n = inverse_[from];
61 if (n == Unsigned32(-1))
62 return 0;
63 if (n != Unsigned32(-2)) {
64 to = ((n + from) & ((Unsigned32(1) << 31) - 1));
65 return 1;
66 }
67 }
68 return desc_.univToDesc(from, to, toSet);
69 }
70
71 inline
univToDesc(UnivChar from,WideChar & to,ISet<WideChar> & toSet,WideChar & count)72 unsigned CharsetInfo::univToDesc(UnivChar from, WideChar &to,
73 ISet<WideChar> &toSet, WideChar &count)
74 const
75 {
76 if (from <= Char(-1)) {
77 Char fromMax;
78 Unsigned32 n = inverse_.getRange(from, fromMax);
79 if (n == Unsigned32(-1)) {
80 count = (fromMax - from) + 1;
81 return 0;
82 }
83 if (n != Unsigned32(-2)) {
84 to = ((n + from) & ((Unsigned32(1) << 31) - 1));
85 count = (fromMax - from) + 1;
86 return 1;
87 }
88 }
89 return desc_.univToDesc(from, to, toSet, count);
90 }
91
92 inline
descToUniv(UnivChar from,WideChar & to)93 Boolean CharsetInfo::descToUniv(UnivChar from, WideChar &to) const
94 {
95 return desc_.descToUniv(from, to);
96 }
97
98 inline
execToDesc(char c)99 Char CharsetInfo::execToDesc(char c) const
100 {
101 return execToDesc_[(unsigned char)c];
102 }
103
104 inline
descToUniv(WideChar from,UnivChar & to,WideChar & alsoMax)105 Boolean CharsetInfo::descToUniv(WideChar from, UnivChar &to,
106 WideChar &alsoMax) const
107 {
108 return desc_.descToUniv(from, to, alsoMax);
109 }
110
111 inline
desc()112 const UnivCharsetDesc &CharsetInfo::desc() const
113 {
114 return desc_;
115 }
116
117 #ifdef SP_NAMESPACE
118 }
119 #endif
120
121 #endif /* not CharsetInfo_INCLUDED */
122