Debian Codegraph @

include/ruby/internal/encoding/encoding.h

raw
1 #ifndef RUBY_INTERNAL_ENCODING_ENCODING_H /*-*-C++-*-vi:se ft=cpp:*/
2 #define RUBY_INTERNAL_ENCODING_ENCODING_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 Defines ::rb_encoding
22 */
23
24 #include "ruby/oniguruma.h"
25 #include "ruby/internal/attr/const.h"
26 #include "ruby/internal/attr/deprecated.h"
27 #include "ruby/internal/attr/noalias.h"
28 #include "ruby/internal/attr/pure.h"
29 #include "ruby/internal/attr/returns_nonnull.h"
30 #include "ruby/internal/dllexport.h"
31 #include "ruby/internal/value.h"
32 #include "ruby/internal/core/rbasic.h"
33 #include "ruby/internal/fl_type.h"
34
35 RBIMPL_SYMBOL_EXPORT_BEGIN()
36
37 /**
38 * `Encoding` class.
39 *
40 * @ingroup object
41 */
42 RUBY_EXTERN VALUE rb_cEncoding;
43
44 /**
45 * @private
46 *
47 * Bit constants used when embedding encodings into ::RBasic::flags. Extension
48 * libraries must not bother such things.
49 */
50 enum ruby_encoding_consts {
51
52 /** Max possible number of embeddable encodings. */
53 RUBY_ENCODING_INLINE_MAX = 127,
54
55 /** Where inline encodings reside. */
56 RUBY_ENCODING_SHIFT = (RUBY_FL_USHIFT+10),
57
58 /** Bits we use to store inline encodings. */
59 RUBY_ENCODING_MASK = (RUBY_ENCODING_INLINE_MAX<<RUBY_ENCODING_SHIFT
60 /* RUBY_FL_USER10..RUBY_FL_USER16 */),
61
62 /** Max possible length of an encoding name. */
63 RUBY_ENCODING_MAXNAMELEN = 42
64 };
65
66 #define ENCODING_INLINE_MAX RUBY_ENCODING_INLINE_MAX /**< @old{RUBY_ENCODING_INLINE_MAX} */
67 #define ENCODING_SHIFT RUBY_ENCODING_SHIFT /**< @old{RUBY_ENCODING_SHIFT} */
68 #define ENCODING_MASK RUBY_ENCODING_MASK /**< @old{RUBY_ENCODING_MASK} */
69
70 /**
71 * Destructively assigns the passed encoding to the passed object. The object
72 * must be capable of having inline encoding. Using this macro needs deep
73 * understanding of bit level object binary layout.
74 *
75 * @param[out] obj Target object to modify.
76 * @param[in] ecindex Encoding in encindex format.
77 * @post `obj`'s encoding is `encindex`.
78 */
79 static inline void
80 RB_ENCODING_SET_INLINED(VALUE obj, int encindex)
81 {
82 VALUE f = /* upcast */ encindex;
83
84 f <<= RUBY_ENCODING_SHIFT;
85 RB_FL_UNSET_RAW(obj, RUBY_ENCODING_MASK);
86 RB_FL_SET_RAW(obj, f);
87 }
88
89 /**
90 * Queries the encoding of the passed object. The encoding must be smaller
91 * than ::RUBY_ENCODING_INLINE_MAX, which means you have some assumption on the
92 * return value. This means the API is for internal use only.
93 *
94 * @param[in] obj Target object.
95 * @return `obj`'s encoding index.
96 */
97 static inline int
98 RB_ENCODING_GET_INLINED(VALUE obj)
99 {
100 VALUE ret = RB_FL_TEST_RAW(obj, RUBY_ENCODING_MASK) >> RUBY_ENCODING_SHIFT;
101
102 return RBIMPL_CAST((int)ret);
103 }
104
105 #define ENCODING_SET_INLINED(obj,i) RB_ENCODING_SET_INLINED(obj,i) /**< @old{RB_ENCODING_SET_INLINED} */
106 #define ENCODING_SET(obj,i) RB_ENCODING_SET(obj,i) /**< @old{RB_ENCODING_SET} */
107 #define ENCODING_GET_INLINED(obj) RB_ENCODING_GET_INLINED(obj) /**< @old{RB_ENCODING_GET_INLINED} */
108 #define ENCODING_GET(obj) RB_ENCODING_GET(obj) /**< @old{RB_ENCODING_GET} */
109 #define ENCODING_IS_ASCII8BIT(obj) RB_ENCODING_IS_ASCII8BIT(obj) /**< @old{RB_ENCODING_IS_ASCII8BIT} */
110 #define ENCODING_MAXNAMELEN RUBY_ENCODING_MAXNAMELEN /**< @old{RUBY_ENCODING_MAXNAMELEN} */
111
112 /**
113 * The type of encoding. Our design here is we take Oniguruma/Onigmo's
114 * multilingualisation schema as our base data structure.
115 */
116 typedef const OnigEncodingType rb_encoding;
117
118 RBIMPL_ATTR_NOALIAS()
119 /**
120 * Converts a character option to its encoding. It only supports a very
121 * limited set of Japanese encodings due to its Japanese origin. Ruby still
122 * has this in-core for backwards compatibility. But new codes must not bother
123 * such concept like one-character encoding option. Consider deprecated in
124 * practice.
125 *
126 * @param[in] c One of `['n', 'e', 's', 'u', 'i', 'x', 'm']`.
127 * @param[out] option Return buffer.
128 * @param[out] kcode Return buffer.
129 * @retval 1 `c` understood properly.
130 * @retval 0 `c` is not understood.
131 * @post `option` is a ::OnigOptionType.
132 * @post `kcode` is an enum `ruby_preserved_encindex`.
133 *
134 * @internal
135 *
136 * `kcode` is opaque because `ruby_preserved_encindex` is not visible from
137 * extension libraries. But who cares?
138 */
139 int rb_char_to_option_kcode(int c, int *option, int *kcode);
140
141 /**
142 * Creates a new "dummy" encoding. Roughly speaking, an encoding is dummy when
143 * it is stateful. Notable example of dummy encoding are those defined in
144 * ISO/IEC 2022
145 *
146 * @param[in] name Name of the creating encoding.
147 * @exception rb_eArgError Duplicated or malformed `name`.
148 * @return New dummy encoding's index.
149 * @post Encoding named `name` is created, whose index is the return
150 * value.
151 */
152 int rb_define_dummy_encoding(const char *name);
153
154 RBIMPL_ATTR_PURE()
155 /**
156 * Queries if the passed encoding is dummy.
157 *
158 * @param[in] enc Encoding in question.
159 * @retval 1 It is.
160 * @retval 0 It isn't.
161 */
162 int rb_enc_dummy_p(rb_encoding *enc);
163
164 RBIMPL_ATTR_PURE()
165 /**
166 * Queries the index of the encoding. An encoding's index is a Ruby-local
167 * concept. It is a (sequential) number assigned to each encoding.
168 *
169 * @param[in] enc Encoding in question.
170 * @return Its index.
171 * @note You can pass null pointers to this function. It is equivalent
172 * to rb_usascii_encindex() then.
173 */
174 int rb_enc_to_index(rb_encoding *enc);
175
176 /**
177 * Queries the index of the encoding of the passed object, if any.
178 *
179 * @param[in] obj Object in question.
180 * @retval -1 `obj` is incapable of having an encoding.
181 * @retval otherwise `obj`'s encoding's index.
182 */
183 int rb_enc_get_index(VALUE obj);
184
185 /**
186 * @alias{rb_enc_get_index}
187 *
188 * @internal
189 *
190 * Implementation wise this is not a verbatim alias of rb_enc_get_index(). But
191 * the API is consistent. Don't bother.
192 */
193 static inline int
194 RB_ENCODING_GET(VALUE obj)
195 {
196 int encindex = RB_ENCODING_GET_INLINED(obj);
197
198 if (encindex == RUBY_ENCODING_INLINE_MAX) {
199 return rb_enc_get_index(obj);
200 }
201 else {
202 return encindex;
203 }
204 }
205
206 /**
207 * Destructively assigns an encoding (via its index) to an object.
208 *
209 * @param[out] obj Object in question.
210 * @param[in] encindex An encoding index.
211 * @exception rb_eFrozenError `obj` is frozen.
212 * @exception rb_eArgError `obj` is incapable of having an encoding.
213 * @exception rb_eEncodingError `encindex` is out of bounds.
214 * @exception rb_eLoadError Failed to load the encoding.
215 */
216 void rb_enc_set_index(VALUE obj, int encindex);
217
218 /** @alias{rb_enc_set_index} */
219 static inline void
220 RB_ENCODING_SET(VALUE obj, int encindex)
221 {
222 rb_enc_set_index(obj, encindex);
223 }
224
225 /**
226 * This is #RB_ENCODING_SET + RB_ENC_CODERANGE_SET combo. The object must be
227 * capable of having inline encoding. Using this macro needs deep
228 * understanding of bit level object binary layout.
229 *
230 * @param[out] obj Target object.
231 * @param[in] encindex Encoding in encindex format.
232 * @param[in] cr An enum ::ruby_coderange_type.
233 * @post `obj`'s encoding is `encindex`.
234 * @post `obj`'s code range is `cr`.
235 */
236 static inline void
237 RB_ENCODING_CODERANGE_SET(VALUE obj, int encindex, enum ruby_coderange_type cr)
238 {
239 RB_ENCODING_SET(obj, encindex);
240 RB_ENC_CODERANGE_SET(obj, cr);
241 }
242
243 RBIMPL_ATTR_PURE()
244 /**
245 * Queries if the passed object can have its encoding.
246 *
247 * @param[in] obj Object in question.
248 * @retval 1 It can.
249 * @retval 0 It cannot.
250 */
251 int rb_enc_capable(VALUE obj);
252
253 /**
254 * Queries the index of the encoding.
255 *
256 * @param[in] name Name of the encoding to find.
257 * @exception rb_eArgError No such encoding named `name`.
258 * @retval -1 `name` exists, but unable to load.
259 * @retval otherwise Index of encoding named `name`.
260 */
261 int rb_enc_find_index(const char *name);
262
263 /**
264 * Registers an "alias" name. In the wild, an encoding can be called using
265 * multiple names. For instance an encoding known as `"CP932"` is also called
266 * `"SJIS"` on occasions. This API registers such relationships.
267 *
268 * @param[in] alias New name.
269 * @param[in] orig Old name.
270 * @exception rb_eArgError `alias` is duplicated or malformed.
271 * @retval -1 Failed to load `orig`.
272 * @retval otherwise The index of `orig` and `alias`.
273 * @post `alias` is a synonym of `orig`. They refer to the identical
274 * encoding.
275 */
276 int rb_enc_alias(const char *alias, const char *orig);
277
278 /**
279 * Obtains a encoding index from a wider range of objects (than
280 * rb_enc_find_index()).
281 *
282 * @param[in] obj An ::rb_cEncoding, or its name in ::rb_cString.
283 * @retval -1 `obj` is unexpected type/contents.
284 * @retval otherwise Index corresponding to `obj`.
285 */
286 int rb_to_encoding_index(VALUE obj);
287
288 /**
289 * Identical to rb_find_encoding(), except it raises an exception instead of
290 * returning NULL.
291 *
292 * @param[in] obj An ::rb_cEncoding, or its name in ::rb_cString.
293 * @exception rb_eTypeError `obj` is neither ::rb_cEncoding nor ::rb_cString.
294 * @exception rb_eArgError `obj` is an unknown encoding name.
295 * @return Encoding of `obj`.
296 */
297 rb_encoding *rb_to_encoding(VALUE obj);
298
299 /**
300 * Identical to rb_to_encoding_index(), except the return type.
301 *
302 * @param[in] obj An ::rb_cEncoding, or its name in ::rb_cString.
303 * @exception rb_eTypeError `obj` is neither ::rb_cEncoding nor ::rb_cString.
304 * @retval NULL No such encoding.
305 * @return otherwise Encoding of `obj`.
306 */
307 rb_encoding *rb_find_encoding(VALUE obj);
308
309 /**
310 * Identical to rb_enc_get_index(), except the return type.
311 *
312 * @param[in] obj Object in question.
313 * @retval NULL Obj is incapable of having an encoding.
314 * @retval otherwise `obj`'s encoding.
315 */
316 rb_encoding *rb_enc_get(VALUE obj);
317
318 /**
319 * Look for the "common" encoding between the two. One character can or cannot
320 * be expressed depending on an encoding. This function finds the super-set of
321 * encodings that satisfy contents of both arguments. If that is impossible
322 * returns NULL.
323 *
324 * @param[in] str1 An object.
325 * @param[in] str2 Another object.
326 * @retval NULL No encoding can satisfy both at once.
327 * @retval otherwise Common encoding between the two.
328 * @note Arguments can be non-string, e.g. Regexp.
329 */
330 rb_encoding *rb_enc_compatible(VALUE str1, VALUE str2);
331
332 /**
333 * Identical to rb_enc_compatible(), except it raises an exception instead of
334 * returning NULL.
335 *
336 * @param[in] str1 An object.
337 * @param[in] str2 Another object.
338 * @exception rb_eEncCompatError No encoding can satisfy both.
339 * @return Common encoding between the two.
340 * @note Arguments can be non-string, e.g. Regexp.
341 */
342 rb_encoding *rb_enc_check(VALUE str1,VALUE str2);
343
344 /**
345 * Identical to rb_enc_set_index(), except it additionally does contents fix-up
346 * depending on the passed object. It for instance changes the byte length of
347 * terminating `U+0000` according to the passed encoding.
348 *
349 * @param[out] obj Object in question.
350 * @param[in] encindex An encoding index.
351 * @exception rb_eFrozenError `obj` is frozen.
352 * @exception rb_eArgError `obj` is incapable of having an encoding.
353 * @exception rb_eEncodingError `encindex` is out of bounds.
354 * @exception rb_eLoadError Failed to load the encoding.
355 * @return The passed `obj`.
356 * @post `obj`'s contents might be fixed according to `encindex`.
357 */
358 VALUE rb_enc_associate_index(VALUE obj, int encindex);
359
360 /**
361 * Identical to rb_enc_associate_index(), except it takes an encoding itself
362 * instead of its index.
363 *
364 * @param[out] obj Object in question.
365 * @param[in] enc An encoding.
366 * @exception rb_eFrozenError `obj` is frozen.
367 * @exception rb_eArgError `obj` is incapable of having an encoding.
368 * @return The passed `obj`.
369 * @post `obj`'s contents might be fixed according to `enc`.
370 */
371 VALUE rb_enc_associate(VALUE obj, rb_encoding *enc);
372
373 /**
374 * Destructively copies the encoding of the latter object to that of former
375 * one. It can also be seen as a routine identical to
376 * rb_enc_associate_index(), except it takes an object's encoding instead of an
377 * encoding's index.
378 *
379 * @param[out] dst Object to modify.
380 * @param[in] src Object to reference.
381 * @exception rb_eFrozenError `dst` is frozen.
382 * @exception rb_eArgError `dst` is incapable of having an encoding.
383 * @exception rb_eEncodingError `src` is incapable of having an encoding.
384 * @post `dst`'s encoding is that of `src`'s.
385 */
386 void rb_enc_copy(VALUE dst, VALUE src);
387
388
389 /**
390 * Identical to rb_find_encoding(), except it takes an encoding index instead
391 * of a Ruby object.
392 *
393 * @param[in] idx An encoding index.
394 * @retval NULL No such encoding.
395 * @retval otherwise An encoding whose index is `idx`.
396 */
397 rb_encoding *rb_enc_from_index(int idx);
398
399 /**
400 * Identical to rb_find_encoding(), except it takes a C's string instead of
401 * Ruby's.
402 *
403 * @param[in] name Name of the encoding to query.
404 * @retval NULL No such encoding.
405 * @retval otherwise An encoding whose index is `idx`.
406 */
407 rb_encoding *rb_enc_find(const char *name);
408
409 /**
410 * Queries the (canonical) name of the passed encoding.
411 *
412 * @param[in] enc An encoding.
413 * @return Its name.
414 */
415 static inline const char *
416 rb_enc_name(rb_encoding *enc)
417 {
418 return enc->name;
419 }
420
421 /**
422 * Queries the minimum number of bytes that the passed encoding needs to
423 * represent a character. For ASCII and compatible encodings this is typically
424 * 1. There are however encodings whose minimum is not 1; they are
425 * historically called wide characters.
426 *
427 * @param[in] enc An encoding.
428 * @return Its least possible number of bytes except 0.
429 */
430 static inline int
431 rb_enc_mbminlen(rb_encoding *enc)
432 {
433 return enc->min_enc_len;
434 }
435
436 /**
437 * Queries the maximum number of bytes that the passed encoding needs to
438 * represent a character. Fixed-width encodings have the same value for this
439 * one and #rb_enc_mbminlen. However there are variable-width encodings.
440 * UTF-8, for instance, takes from 1 up to 6 bytes.
441 *
442 * @param[in] enc An encoding.
443 * @return Its maximum possible number of bytes of a character.
444 */
445 static inline int
446 rb_enc_mbmaxlen(rb_encoding *enc)
447 {
448 return enc->max_enc_len;
449 }
450
451 /**
452 * Queries the number of bytes of the character at the passed pointer.
453 *
454 * @param[in] p Pointer to a character's first byte.
455 * @param[in] e End of the string that has `p`.
456 * @param[in] enc Encoding of the string.
457 * @return If the character at `p` does not end until `e`, number of bytes
458 * between `p` and `e`. Otherwise the number of bytes that the
459 * character at `p` is encoded.
460 *
461 * @internal
462 *
463 * Strictly speaking there are chances when `p` points to a middle byte of a
464 * wide character. This function returns "the number of bytes from `p` to
465 * nearest of either `e` or the next character boundary", if you go strict.
466 */
467 int rb_enc_mbclen(const char *p, const char *e, rb_encoding *enc);
468
469 /**
470 * Identical to rb_enc_mbclen() unless the character at `p` overruns `e`. That
471 * can happen for instance when you read from a socket and its partial read
472 * cuts a wide character in-between. In those situations this function
473 * "estimates" theoretical length of the character in question. Typically it
474 * tends to be possible to know how many bytes a character needs before
475 * actually reaching its end; for instance UTF-8 encodes a character's length
476 * in the first byte of it. This function returns that info.
477 *
478 * @note This implies that the string is not broken.
479 *
480 * @param[in] p Pointer to the character's first byte.
481 * @param[in] e End of the string that has `p`.
482 * @param[in] enc Encoding of the string.
483 * @return Number of bytes of character at `p`, measured or estimated.
484 */
485 int rb_enc_fast_mbclen(const char *p, const char *e, rb_encoding *enc);
486
487 /**
488 * Queries the number of bytes of the character at the passed pointer. This
489 * function returns 3 different types of information:
490 *
491 * ```CXX
492 * auto n = rb_enc_precise_mbclen(p, q, r);
493 *
494 * if (ONIGENC_MBCLEN_CHARFOUND_P(n)) {
495 * // Character found. Normal return.
496 * auto found_length = ONIGENC_MBCLEN_CHARFOUND_LEN(n);
497 * }
498 * else if (ONIGENC_MBCLEN_NEEDMORE_P(n)) {
499 * // Character overruns past `q`; needs more.
500 * auto requested_length = ONIGENC_MBCLEN_NEEDMORE_LEN(n);
501 * }
502 * else {
503 * // `p` is broken.
504 * assert(ONIGENC_MBCLEN_INVALID_P(n));
505 * }
506 * ```
507 *
508 * @param[in] p Pointer to the character's first byte.
509 * @param[in] e End of the string that has `p`.
510 * @param[in] enc Encoding of the string.
511 * @return Encoded read/needed number of bytes (see above).
512 */
513 int rb_enc_precise_mbclen(const char *p, const char *e, rb_encoding *enc);
514
515 #define MBCLEN_CHARFOUND_P(ret) ONIGENC_MBCLEN_CHARFOUND_P(ret) /**< @old{ONIGENC_MBCLEN_CHARFOUND_P} */
516 #define MBCLEN_CHARFOUND_LEN(ret) ONIGENC_MBCLEN_CHARFOUND_LEN(ret) /**< @old{ONIGENC_MBCLEN_CHARFOUND_LEN} */
517 #define MBCLEN_INVALID_P(ret) ONIGENC_MBCLEN_INVALID_P(ret) /**< @old{ONIGENC_MBCLEN_INVALID_P} */
518 #define MBCLEN_NEEDMORE_P(ret) ONIGENC_MBCLEN_NEEDMORE_P(ret) /**< @old{ONIGENC_MBCLEN_NEEDMORE_P} */
519 #define MBCLEN_NEEDMORE_LEN(ret) ONIGENC_MBCLEN_NEEDMORE_LEN(ret) /**< @old{ONIGENC_MBCLEN_NEEDMORE_LEN} */
520
521 /**
522 * Queries the code point of character pointed by the passed pointer. If that
523 * code point is included in ASCII that code point is returned. Otherwise -1.
524 * This can be different from just looking at the first byte. For instance it
525 * reads 2 bytes in case of UTF-16BE.
526 *
527 * @param[in] p Pointer to the character's first byte.
528 * @param[in] e End of the string that has `p`.
529 * @param[in] len Return buffer.
530 * @param[in] enc Encoding of the string.
531 * @retval -1 The character at `p` is not i ASCII.
532 * @retval otherwise A code point of the character at `p`.
533 * @post `len` (if set) is the number of bytes of `p`.
534 */
535 int rb_enc_ascget(const char *p, const char *e, int *len, rb_encoding *enc);
536
537 /**
538 * Queries the code point of character pointed by the passed pointer.
539 * Exceptions happen in case of broken input.
540 *
541 * @param[in] p Pointer to the character's first byte.
542 * @param[in] e End of the string that has `p`.
543 * @param[in] len Return buffer.
544 * @param[in] enc Encoding of the string.
545 * @exception rb_eArgError `p` is broken.
546 * @return Code point of the character pointed by `p`.
547 * @post `len` (if set) is the number of bytes of `p`.
548 */
549 unsigned int rb_enc_codepoint_len(const char *p, const char *e, int *len, rb_encoding *enc);
550
551 /**
552 * Queries the code point of character pointed by the passed pointer.
553 * Exceptions happen in case of broken input.
554 *
555 * @deprecated Use rb_enc_codepoint_len() instead.
556 * @param[in] p Pointer to the character's first byte.
557 * @param[in] e End of the string that has `p`.
558 * @param[in] enc Encoding of the string.
559 * @exception rb_eArgError `p` is broken.
560 * @return Code point of the character pointed by `p`.
561 *
562 * @internal
563 *
564 * @matz says in commit 91e5ba1cb865a2385d3e1cbfacd824496898e098 that the line
565 * below is a "prototype for obsolete function". However even today there
566 * still are some use cases of it throughout our repository. It seems it has
567 * its own niche.
568 */
569 static inline unsigned int
570 rb_enc_codepoint(const char *p, const char *e, rb_encoding *enc)
571 {
572 return rb_enc_codepoint_len(p, e, 0, enc);
573 /* ^^^
574 * This can be `NULL` in C, `nullptr` in C++, and `0` for both.
575 * We choose the most portable one here.
576 */
577 }
578
579
580 /**
581 * Identical to rb_enc_codepoint(), except it assumes the passed character is
582 * not broken.
583 *
584 * @param[in] p Pointer to the character's first byte.
585 * @param[in] e End of the string that has `p`.
586 * @param[in] enc Encoding of the string.
587 * @return Code point of the character pointed by `p`.
588 */
589 static inline OnigCodePoint
590 rb_enc_mbc_to_codepoint(const char *p, const char *e, rb_encoding *enc)
591 {
592 const OnigUChar *up = RBIMPL_CAST((const OnigUChar *)p);
593 const OnigUChar *ue = RBIMPL_CAST((const OnigUChar *)e);
594
595 return ONIGENC_MBC_TO_CODE(enc, up, ue);
596 }
597
598 /**
599 * Queries the number of bytes requested to represent the passed code point
600 * using the passed encoding.
601 *
602 * @param[in] code Code point in question.
603 * @param[in] enc Encoding to convert the code into a byte sequence.
604 * @exception rb_eArgError `enc` does not glean `code`.
605 * @return Number of bytes requested to represent `code` using `enc`.
606 */
607 int rb_enc_codelen(int code, rb_encoding *enc);
608
609 /**
610 * Identical to rb_enc_codelen(), except it returns 0 for invalid code points.
611 *
612 * @param[in] c Code point in question.
613 * @param[in] enc Encoding to convert `c` into a byte sequence.
614 * @retval 0 `c` is invalid.
615 * @return otherwise Number of bytes needed for `enc` to encode `c`.
616 */
617 static inline int
618 rb_enc_code_to_mbclen(int c, rb_encoding *enc)
619 {
620 OnigCodePoint uc = RBIMPL_CAST((OnigCodePoint)c);
621
622 return ONIGENC_CODE_TO_MBCLEN(enc, uc);
623 }
624
625 /**
626 * Identical to rb_enc_uint_chr(), except it writes back to the passed buffer
627 * instead of allocating one.
628 *
629 * @param[in] c Code point.
630 * @param[out] buf Return buffer.
631 * @param[in] enc Target encoding scheme.
632 * @retval <= 0 `c` is invalid in `enc`.
633 * @return otherwise Number of bytes written to `buf`.
634 * @post `c` is encoded according to `enc`, then written to `buf`.
635 *
636 * @internal
637 *
638 * The second argument must be typed. But its current usages prevent us from
639 * being any stricter than this. :FIXME:
640 */
641 static inline int
642 rb_enc_mbcput(unsigned int c, void *buf, rb_encoding *enc)
643 {
644 OnigCodePoint uc = RBIMPL_CAST((OnigCodePoint)c);
645 OnigUChar *ubuf = RBIMPL_CAST((OnigUChar *)buf);
646
647 return ONIGENC_CODE_TO_MBC(enc, uc, ubuf);
648 }
649
650 /**
651 * Queries the previous (left) character.
652 *
653 * @param[in] s Start of the string.
654 * @param[in] p Pointer to a character.
655 * @param[in] e End of the string.
656 * @param[in] enc Encoding.
657 * @retval NULL No previous character.
658 * @retval otherwise Pointer to the head of the previous character.
659 */
660 static inline char *
661 rb_enc_prev_char(const char *s, const char *p, const char *e, rb_encoding *enc)
662 {
663 const OnigUChar *us = RBIMPL_CAST((const OnigUChar *)s);
664 const OnigUChar *up = RBIMPL_CAST((const OnigUChar *)p);
665 const OnigUChar *ue = RBIMPL_CAST((const OnigUChar *)e);
666 OnigUChar *ur = onigenc_get_prev_char_head(enc, us, up, ue);
667
668 return RBIMPL_CAST((char *)ur);
669 }
670
671 /**
672 * Queries the left boundary of a character. This function takes a pointer
673 * that is not necessarily a head of a character, and searches for its head.
674 *
675 * @param[in] s Start of the string.
676 * @param[in] p Pointer to a possibly-middle of a character.
677 * @param[in] e End of the string.
678 * @param[in] enc Encoding.
679 * @return Pointer to the head of the character that contains `p`.
680 */
681 static inline char *
682 rb_enc_left_char_head(const char *s, const char *p, const char *e, rb_encoding *enc)
683 {
684 const OnigUChar *us = RBIMPL_CAST((const OnigUChar *)s);
685 const OnigUChar *up = RBIMPL_CAST((const OnigUChar *)p);
686 const OnigUChar *ue = RBIMPL_CAST((const OnigUChar *)e);
687 OnigUChar *ur = onigenc_get_left_adjust_char_head(enc, us, up, ue);
688
689 return RBIMPL_CAST((char *)ur);
690 }
691
692 /**
693 * Queries the right boundary of a character. This function takes a pointer
694 * that is not necessarily a head of a character, and searches for its tail.
695 *
696 * @param[in] s Start of the string.
697 * @param[in] p Pointer to a possibly-middle of a character.
698 * @param[in] e End of the string.
699 * @param[in] enc Encoding.
700 * @return Pointer to the end of the character that contains `p`.
701 */
702 static inline char *
703 rb_enc_right_char_head(const char *s, const char *p, const char *e, rb_encoding *enc)
704 {
705 const OnigUChar *us = RBIMPL_CAST((const OnigUChar *)s);
706 const OnigUChar *up = RBIMPL_CAST((const OnigUChar *)p);
707 const OnigUChar *ue = RBIMPL_CAST((const OnigUChar *)e);
708 OnigUChar *ur = onigenc_get_right_adjust_char_head(enc, us, up, ue);
709
710 return RBIMPL_CAST((char *)ur);
711 }
712
713 /**
714 * Scans the string backwards for n characters.
715 *
716 * @param[in] s Start of the string.
717 * @param[in] p Pointer to a character.
718 * @param[in] e End of the string.
719 * @param[in] n Steps.
720 * @param[in] enc Encoding.
721 * @retval NULL There are no `n` characters left.
722 * @retval otherwise Pointer to `n` character before `p`.
723 */
724 static inline char *
725 rb_enc_step_back(const char *s, const char *p, const char *e, int n, rb_encoding *enc)
726 {
727 const OnigUChar *us = RBIMPL_CAST((const OnigUChar *)s);
728 const OnigUChar *up = RBIMPL_CAST((const OnigUChar *)p);
729 const OnigUChar *ue = RBIMPL_CAST((const OnigUChar *)e);
730 const OnigUChar *ur = onigenc_step_back(enc, us, up, ue, n);
731
732 return RBIMPL_CAST((char *)ur);
733 }
734
735 /**
736 * @private
737 *
738 * This is an implementation detail of rb_enc_asciicompat(). People don't use
739 * it directly. Just always use rb_enc_asciicompat().
740 *
741 * @param[in] enc Encoding in question.
742 * @retval 1 It is ASCII compatible.
743 * @retval 0 It isn't.
744 */
745 static inline int
746 rb_enc_asciicompat_inline(rb_encoding *enc)
747 {
748 return rb_enc_mbminlen(enc)==1 && !rb_enc_dummy_p(enc);
749 }
750
751 /**
752 * Queries if the passed encoding is _in some sense_ compatible with ASCII.
753 * The concept of ASCII compatibility is nuanced, and private to our
754 * implementation. For instance SJIS is ASCII compatible to us, despite their
755 * having different characters at code point `0x5C`. This is based on some
756 * practical consideration that Japanese people confuses SJIS to be "upper
757 * compatible" with ASCII (which is in fact a wrong idea, but we just don't go
758 * strict here). An example of ASCII incompatible encoding is UTF-16. UTF-16
759 * shares code points with ASCII, but employs a completely different encoding
760 * scheme.
761 *
762 * @param[in] enc Encoding in question.
763 * @retval 0 It is incompatible.
764 * @retval 1 It is compatible.
765 */
766 static inline bool
767 rb_enc_asciicompat(rb_encoding *enc)
768 {
769 if (rb_enc_mbminlen(enc) != 1) {
770 return false;
771 }
772 else if (rb_enc_dummy_p(enc)) {
773 return false;
774 }
775 else {
776 return true;
777 }
778 }
779
780 /**
781 * Queries if the passed string is in an ASCII-compatible encoding.
782 *
783 * @param[in] str A Ruby's string to query.
784 * @retval 0 `str` is not a String, or an ASCII-incompatible string.
785 * @retval 1 Otherwise.
786 */
787 static inline bool
788 rb_enc_str_asciicompat_p(VALUE str)
789 {
790 rb_encoding *enc = rb_enc_get(str);
791
792 return rb_enc_asciicompat(enc);
793 }
794
795 /**
796 * Queries the Ruby-level counterpart instance of ::rb_cEncoding that
797 * corresponds to the passed encoding.
798 *
799 * @param[in] enc An encoding
800 * @retval RUBY_Qnil `enc` is a null pointer.
801 * @retval otherwise An instance of ::rb_cEncoding.
802 */
803 VALUE rb_enc_from_encoding(rb_encoding *enc);
804
805 RBIMPL_ATTR_PURE()
806 /**
807 * Queries if the passed encoding is either one of UTF-8/16/32.
808 *
809 * @note It does not take UTF-7, which we actually support, into account.
810 *
811 * @param[in] enc Encoding in question.
812 * @retval 0 It is not a Unicode variant.
813 * @retval otherwise It is.
814 *
815 * @internal
816 *
817 * In reality it returns 1/0, but the value is abstracted as
818 * `ONIGENC_FLAG_UNICODE`.
819 */
820 int rb_enc_unicode_p(rb_encoding *enc);
821
822 RBIMPL_ATTR_RETURNS_NONNULL()
823 /**
824 * Queries the encoding that represents ASCII-8BIT a.k.a. binary.
825 *
826 * @return The encoding that represents ASCII-8BIT.
827 *
828 * @internal
829 *
830 * This can not return NULL once the process properly boots up.
831 */
832 rb_encoding *rb_ascii8bit_encoding(void);
833
834 RBIMPL_ATTR_RETURNS_NONNULL()
835 /**
836 * Queries the encoding that represents UTF-8.
837 *
838 * @return The encoding that represents UTF-8.
839 *
840 * @internal
841 *
842 * This can not return NULL once the process properly boots up.
843 */
844 rb_encoding *rb_utf8_encoding(void);
845
846 RBIMPL_ATTR_RETURNS_NONNULL()
847 /**
848 * Queries the encoding that represents US-ASCII.
849 *
850 * @return The encoding that represents US-ASCII.
851 *
852 * @internal
853 *
854 * This can not return NULL once the process properly boots up.
855 */
856 rb_encoding *rb_usascii_encoding(void);
857
858 /**
859 * Queries the encoding that represents the current locale.
860 *
861 * @return The encoding that represents the process' locale.
862 *
863 * @internal
864 *
865 * This is dynamic. If you change the process' locale by e.g. calling
866 * `setlocale(3)`, that should also change the return value of this function.
867 *
868 * There is no official way for Ruby scripts to manipulate locales, though.
869 */
870 rb_encoding *rb_locale_encoding(void);
871
872 /**
873 * Queries the "filesystem" encoding. This is the encoding that ruby expects
874 * info from the OS' file system are in. This affects for instance return
875 * value of rb_dir_getwd(). Most notably on Windows it can be an alias of OS
876 * codepage. Most notably on Linux users can set this via default external
877 * encoding.
878 *
879 * @return The "filesystem" encoding.
880 */
881 rb_encoding *rb_filesystem_encoding(void);
882
883 /**
884 * Queries the "default external" encoding. This is used to interact with
885 * outer-process things such as File. Though not recommended, you can set this
886 * using rb_enc_set_default_external().
887 *
888 * @return The "default external" encoding.
889 */
890 rb_encoding *rb_default_external_encoding(void);
891
892 /**
893 * Queries the "default internal" encoding. This could be a null pointer.
894 * Otherwise, outer-process info are transcoded from default external encoding
895 * to this one during reading from an IO.
896 *
897 * @return The "default internal" encoding (if any).
898 */
899 rb_encoding *rb_default_internal_encoding(void);
900
901 #ifndef rb_ascii8bit_encindex
902 RBIMPL_ATTR_CONST()
903 /**
904 * Identical to rb_ascii8bit_encoding(), except it returns the encoding's index
905 * instead of the encoding itself.
906 *
907 * @return The index of encoding of ASCII-8BIT.
908 *
909 * @internal
910 *
911 * This happens to be 0.
912 */
913 int rb_ascii8bit_encindex(void);
914 #endif
915
916 /**
917 * Queries if the passed object is in ascii 8bit (== binary) encoding. The
918 * object must be capable of having inline encoding. Using this macro needs
919 * deep understanding of bit level object binary layout.
920 *
921 * @param[in] obj An object to check.
922 * @retval 1 It is.
923 * @retval 0 It isn't.
924 */
925 static inline bool
926 RB_ENCODING_IS_ASCII8BIT(VALUE obj)
927 {
928 return RB_ENCODING_GET_INLINED(obj) == rb_ascii8bit_encindex();
929 }
930
931 #ifndef rb_utf8_encindex
932 RBIMPL_ATTR_CONST()
933 /**
934 * Identical to rb_utf8_encoding(), except it returns the encoding's index
935 * instead of the encoding itself.
936 *
937 * @return The index of encoding of UTF-8.
938 */
939 int rb_utf8_encindex(void);
940 #endif
941
942 #ifndef rb_usascii_encindex
943 RBIMPL_ATTR_CONST()
944 /**
945 * Identical to rb_usascii_encoding(), except it returns the encoding's index
946 * instead of the encoding itself.
947 *
948 * @return The index of encoding of UTF-8.
949 */
950 int rb_usascii_encindex(void);
951 #endif
952
953 /**
954 * Identical to rb_locale_encoding(), except it returns the encoding's index
955 * instead of the encoding itself.
956 *
957 * @return The index of the locale encoding.
958 */
959 int rb_locale_encindex(void);
960
961 /**
962 * Identical to rb_filesystem_encoding(), except it returns the encoding's
963 * index instead of the encoding itself.
964 *
965 * @return The index of the filesystem encoding.
966 */
967 int rb_filesystem_encindex(void);
968
969 /**
970 * Identical to rb_default_external_encoding(), except it returns the
971 * Ruby-level counterpart instance of ::rb_cEncoding that corresponds to the
972 * default external encoding.
973 *
974 * @return An instance of ::rb_cEncoding of default external.
975 */
976 VALUE rb_enc_default_external(void);
977
978 /**
979 * Identical to rb_default_internal_encoding(), except it returns the
980 * Ruby-level counterpart instance of ::rb_cEncoding that corresponds to the
981 * default internal encoding.
982 *
983 * @return An instance of ::rb_cEncoding of default internal.
984 */
985 VALUE rb_enc_default_internal(void);
986
987 /**
988 * Destructively assigns the passed encoding as the default external encoding.
989 * You should not use this API. It has process-global side effects. Also it
990 * doesn't change encodings of strings that have already been read.
991 *
992 * @param[in] encoding Ruby level encoding.
993 * @exception rb_eArgError `encoding` is ::RUBY_Qnil.
994 * @post The default external encoding is `encoding`.
995 */
996 void rb_enc_set_default_external(VALUE encoding);
997
998 /**
999 * Destructively assigns the passed encoding as the default internal encoding.
1000 * You should not use this API. It has process-global side effects. Also it
1001 * doesn't change encodings of strings that have already been read.
1002 *
1003 * @param[in] encoding Ruby level encoding.
1004 * @post The default internal encoding is `encoding`.
1005 * @note Unlike rb_enc_set_default_external() you can pass ::RUBY_Qnil.
1006 */
1007 void rb_enc_set_default_internal(VALUE encoding);
1008
1009 /**
1010 * Returns a platform-depended "charmap" of the current locale. This
1011 * information is called a "Codeset name" in IEEE 1003.1 section 13
1012 * (`<langinfo.h>`). This is a very low-level API. The return value can have
1013 * no corresponding encoding when passed to rb_find_encoding().
1014 *
1015 * @param[in] klass Ignored for no reason (why...)
1016 * @return The low-level locale charmap, in Ruby's String.
1017 */
1018 VALUE rb_locale_charmap(VALUE klass);
1019
1020 RBIMPL_SYMBOL_EXPORT_END()
1021
1022 /** @cond INTERNAL_MACRO */
1023 #define RB_ENCODING_GET RB_ENCODING_GET
1024 #define RB_ENCODING_GET_INLINED RB_ENCODING_GET_INLINED
1025 #define RB_ENCODING_IS_ASCII8BIT RB_ENCODING_IS_ASCII8BIT
1026 #define RB_ENCODING_SET RB_ENCODING_SET
1027 #define RB_ENCODING_SET_INLINED RB_ENCODING_SET_INLINED
1028 #define rb_enc_asciicompat rb_enc_asciicompat
1029 #define rb_enc_code_to_mbclen rb_enc_code_to_mbclen
1030 #define rb_enc_codepoint rb_enc_codepoint
1031 #define rb_enc_left_char_head rb_enc_left_char_head
1032 #define rb_enc_mbc_to_codepoint rb_enc_mbc_to_codepoint
1033 #define rb_enc_mbcput rb_enc_mbcput
1034 #define rb_enc_mbmaxlen rb_enc_mbmaxlen
1035 #define rb_enc_mbminlen rb_enc_mbminlen
1036 #define rb_enc_name rb_enc_name
1037 #define rb_enc_prev_char rb_enc_prev_char
1038 #define rb_enc_right_char_head rb_enc_right_char_head
1039 #define rb_enc_step_back rb_enc_step_back
1040 #define rb_enc_str_asciicompat_p rb_enc_str_asciicompat_p
1041 /** @endcond */
1042
1043 #endif /* RUBY_INTERNAL_ENCODING_ENCODING_H */