Debian Codegraph @

include/ruby/internal/encoding/coderange.h

raw
1 #ifndef RUBY_INTERNAL_ENCODING_CODERANGE_H /*-*-C++-*-vi:se ft=cpp:*/
2 #define RUBY_INTERNAL_ENCODING_CODERANGE_H
3 /**
4 * @file
5 * @author Ruby developers <[email protected]>
6 * @copyright This file is a part of the programming language Ruby.
7 * Permission is hereby granted, to either redistribute and/or
8 * modify this file, provided that the conditions mentioned in the
9 * file COPYING are met. Consult the file for details.
10 * @warning Symbols prefixed with either `RBIMPL` or `rbimpl` are
11 * implementation details. Don't take them as canon. They could
12 * rapidly appear then vanish. The name (path) of this header file
13 * is also an implementation detail. Do not expect it to persist
14 * at the place it is now. Developers are free to move it anywhere
15 * anytime at will.
16 * @note To ruby-core: remember that this header can be possibly
17 * recursively included from extension libraries written in C++.
18 * Do not expect for instance `__VA_ARGS__` is always available.
19 * We assume C99 for ruby itself but we don't assume languages of
20 * extension libraries. They could be written in C++98.
21 * @brief Routines for code ranges.
22 */
23
24 #include "ruby/internal/attr/const.h"
25 #include "ruby/internal/attr/pure.h"
26 #include "ruby/internal/dllexport.h"
27 #include "ruby/internal/fl_type.h"
28 #include "ruby/internal/value.h"
29
30 RBIMPL_SYMBOL_EXPORT_BEGIN()
31
32 /** What rb_enc_str_coderange() returns. */
33 enum ruby_coderange_type {
34
35 /** The object's coderange is unclear yet. */
36 RUBY_ENC_CODERANGE_UNKNOWN = 0,
37
38 /** The object holds 0 to 127 inclusive and nothing else. */
39 RUBY_ENC_CODERANGE_7BIT = ((int)RUBY_FL_USER8),
40
41 /** The object's encoding and contents are consistent each other */
42 RUBY_ENC_CODERANGE_VALID = ((int)RUBY_FL_USER9),
43
44 /** The object holds invalid/malformed/broken character(s). */
45 RUBY_ENC_CODERANGE_BROKEN = ((int)(RUBY_FL_USER8|RUBY_FL_USER9)),
46
47 /** Where the coderange resides. */
48 RUBY_ENC_CODERANGE_MASK = (RUBY_ENC_CODERANGE_7BIT|
49 RUBY_ENC_CODERANGE_VALID|
50 RUBY_ENC_CODERANGE_BROKEN)
51 };
52
53 RBIMPL_ATTR_CONST()
54 /**
55 * @private
56 *
57 * This is an implementation detail of #RB_ENC_CODERANGE_CLEAN_P. People don't
58 * use it directly.
59 *
60 * @param[in] cr An enum ::ruby_coderange_type.
61 * @retval 1 It is.
62 * @retval 0 It isn't.
63 */
64 static inline int
65 rb_enc_coderange_clean_p(int cr)
66 {
67 return (cr ^ (cr >> 1)) & RUBY_ENC_CODERANGE_7BIT;
68 }
69
70 RBIMPL_ATTR_CONST()
71 /**
72 * Queries if a code range is "clean". "Clean" in this context means it is
73 * known and valid.
74 *
75 * @param[in] cr An enum ::ruby_coderange_type.
76 * @retval 1 It is.
77 * @retval 0 It isn't.
78 */
79 static inline bool
80 RB_ENC_CODERANGE_CLEAN_P(enum ruby_coderange_type cr)
81 {
82 return rb_enc_coderange_clean_p(cr);
83 }
84
85 RBIMPL_ATTR_PURE_UNLESS_DEBUG()
86 /**
87 * Queries the (inline) code range of the passed object. The object must be
88 * capable of having inline encoding. Using this macro needs deep
89 * understanding of bit level object binary layout.
90 *
91 * @param[in] obj Target object.
92 * @return An enum ::ruby_coderange_type.
93 */
94 static inline enum ruby_coderange_type
95 RB_ENC_CODERANGE(VALUE obj)
96 {
97 VALUE ret = RB_FL_TEST_RAW(obj, RUBY_ENC_CODERANGE_MASK);
98
99 return RBIMPL_CAST((enum ruby_coderange_type)ret);
100 }
101
102 RBIMPL_ATTR_PURE_UNLESS_DEBUG()
103 /**
104 * Queries the (inline) code range of the passed object is
105 * ::RUBY_ENC_CODERANGE_7BIT. The object must be capable of having inline
106 * encoding. Using this macro needs deep understanding of bit level object
107 * binary layout.
108 *
109 * @param[in] obj Target object.
110 * @retval 1 It is ascii only.
111 * @retval 0 Otherwise (including cases when the range is not known).
112 */
113 static inline bool
114 RB_ENC_CODERANGE_ASCIIONLY(VALUE obj)
115 {
116 return RB_ENC_CODERANGE(obj) == RUBY_ENC_CODERANGE_7BIT;
117 }
118
119 /**
120 * Destructively modifies the passed object so that its (inline) code range is
121 * the passed one. The object must be capable of having inline encoding.
122 * Using this macro needs deep understanding of bit level object binary layout.
123 *
124 * @param[out] obj Target object.
125 * @param[out] cr An enum ::ruby_coderange_type.
126 * @post `obj`'s code range is `cr`.
127 */
128 static inline void
129 RB_ENC_CODERANGE_SET(VALUE obj, enum ruby_coderange_type cr)
130 {
131 RB_FL_UNSET_RAW(obj, RUBY_ENC_CODERANGE_MASK);
132 RB_FL_SET_RAW(obj, cr);
133 }
134
135 /**
136 * Destructively clears the passed object's (inline) code range. The object
137 * must be capable of having inline encoding. Using this macro needs deep
138 * understanding of bit level object binary layout.
139 *
140 * @param[out] obj Target object.
141 * @post `obj`'s code range is ::RUBY_ENC_CODERANGE_UNKNOWN.
142 */
143 static inline void
144 RB_ENC_CODERANGE_CLEAR(VALUE obj)
145 {
146 RB_FL_UNSET_RAW(obj, RUBY_ENC_CODERANGE_MASK);
147 }
148
149 RBIMPL_ATTR_CONST()
150 /* assumed ASCII compatibility */
151 /**
152 * "Mix" two code ranges into one. This is handy for instance when you
153 * concatenate two strings into one. Consider one of then is valid but the
154 * other isn't. The result must be invalid. This macro computes that kind of
155 * mixture.
156 *
157 * @param[in] a An enum ::ruby_coderange_type.
158 * @param[in] b Another enum ::ruby_coderange_type.
159 * @return The `a` "and" `b`.
160 */
161 static inline enum ruby_coderange_type
162 RB_ENC_CODERANGE_AND(enum ruby_coderange_type a, enum ruby_coderange_type b)
163 {
164 if (a == RUBY_ENC_CODERANGE_7BIT) {
165 return b;
166 }
167 else if (a != RUBY_ENC_CODERANGE_VALID) {
168 return RUBY_ENC_CODERANGE_UNKNOWN;
169 }
170 else if (b == RUBY_ENC_CODERANGE_7BIT) {
171 return RUBY_ENC_CODERANGE_VALID;
172 }
173 else {
174 return b;
175 }
176 }
177
178 #define ENC_CODERANGE_MASK RUBY_ENC_CODERANGE_MASK /**< @old{RUBY_ENC_CODERANGE_MASK} */
179 #define ENC_CODERANGE_UNKNOWN RUBY_ENC_CODERANGE_UNKNOWN /**< @old{RUBY_ENC_CODERANGE_UNKNOWN} */
180 #define ENC_CODERANGE_7BIT RUBY_ENC_CODERANGE_7BIT /**< @old{RUBY_ENC_CODERANGE_7BIT} */
181 #define ENC_CODERANGE_VALID RUBY_ENC_CODERANGE_VALID /**< @old{RUBY_ENC_CODERANGE_VALID} */
182 #define ENC_CODERANGE_BROKEN RUBY_ENC_CODERANGE_BROKEN /**< @old{RUBY_ENC_CODERANGE_BROKEN} */
183 #define ENC_CODERANGE_CLEAN_P(cr) RB_ENC_CODERANGE_CLEAN_P(cr) /**< @old{RB_ENC_CODERANGE_CLEAN_P} */
184 #define ENC_CODERANGE(obj) RB_ENC_CODERANGE(obj) /**< @old{RB_ENC_CODERANGE} */
185 #define ENC_CODERANGE_ASCIIONLY(obj) RB_ENC_CODERANGE_ASCIIONLY(obj) /**< @old{RB_ENC_CODERANGE_ASCIIONLY} */
186 #define ENC_CODERANGE_SET(obj,cr) RB_ENC_CODERANGE_SET(obj,cr) /**< @old{RB_ENC_CODERANGE_SET} */
187 #define ENC_CODERANGE_CLEAR(obj) RB_ENC_CODERANGE_CLEAR(obj) /**< @old{RB_ENC_CODERANGE_CLEAR} */
188 #define ENC_CODERANGE_AND(a, b) RB_ENC_CODERANGE_AND(a, b) /**< @old{RB_ENC_CODERANGE_AND} */
189 #define ENCODING_CODERANGE_SET(obj, encindex, cr) RB_ENCODING_CODERANGE_SET(obj, encindex, cr) /**< @old{RB_ENCODING_CODERANGE_SET} */
190
191 /** @cond INTERNAL_MACRO */
192 #define RB_ENC_CODERANGE RB_ENC_CODERANGE
193 #define RB_ENC_CODERANGE_AND RB_ENC_CODERANGE_AND
194 #define RB_ENC_CODERANGE_ASCIIONLY RB_ENC_CODERANGE_ASCIIONLY
195 #define RB_ENC_CODERANGE_CLEAN_P RB_ENC_CODERANGE_CLEAN_P
196 #define RB_ENC_CODERANGE_CLEAR RB_ENC_CODERANGE_CLEAR
197 #define RB_ENC_CODERANGE_SET RB_ENC_CODERANGE_SET
198 /** @endcond */
199
200 RBIMPL_SYMBOL_EXPORT_END()
201
202 #endif /* RUBY_INTERNAL_ENCODING_CODERANGE_H */