Debian Codegraph @

include/ruby/internal/encoding/string.h

raw
1 #ifndef RUBY_INTERNAL_ENCODING_STRING_H /*-*-C++-*-vi:se ft=cpp:*/
2 #define RUBY_INTERNAL_ENCODING_STRING_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 to manipulate encodings of strings.
22 */
23
24 #include "ruby/internal/dllexport.h"
25 #include "ruby/internal/value.h"
26 #include "ruby/internal/encoding/encoding.h"
27 #include "ruby/internal/attr/nonnull.h"
28 #include "ruby/internal/intern/string.h" /* rbimpl_strlen */
29
30 RBIMPL_SYMBOL_EXPORT_BEGIN()
31
32 /**
33 * Identical to rb_str_new(), except it additionally takes an encoding.
34 *
35 * @param[in] ptr A memory region of `len` bytes length.
36 * @param[in] len Length of `ptr`, in bytes, not including the
37 * terminating NUL character.
38 * @param[in] enc Encoding of `ptr`.
39 * @exception rb_eNoMemError Failed to allocate `len+1` bytes.
40 * @exception rb_eArgError `len` is negative.
41 * @return An instance of ::rb_cString, of `len` bytes length, of `enc`
42 * encoding, whose contents are verbatim copy of `ptr`.
43 * @pre At least `len` bytes of continuous memory region shall be
44 * accessible via `ptr`.
45 * @note `enc` can be a null pointer. It can also be seen as a routine
46 * identical to rb_usascii_str_new() then.
47 */
48 VALUE rb_enc_str_new(const char *ptr, long len, rb_encoding *enc);
49
50 RBIMPL_ATTR_NONNULL((1))
51 /**
52 * Identical to rb_enc_str_new(), except it assumes the passed pointer is a
53 * pointer to a C string. It can also be seen as a routine identical to
54 * rb_str_new_cstr(), except it additionally takes an encoding.
55 *
56 * @param[in] ptr A C string.
57 * @param[in] enc Encoding of `ptr`.
58 * @exception rb_eNoMemError Failed to allocate memory.
59 * @return An instance of ::rb_cString, of `enc` encoding, whose contents
60 * are verbatim copy of `ptr`.
61 * @pre `ptr` must not be a null pointer.
62 * @pre Because `ptr` is a C string it makes no sense for `enc` to be
63 * something like UTF-32.
64 * @note `enc` can be a null pointer. It can also be seen as a routine
65 * identical to rb_usascii_str_new_cstr() then.
66 */
67 VALUE rb_enc_str_new_cstr(const char *ptr, rb_encoding *enc);
68
69 /**
70 * Identical to rb_enc_str_new(), except it takes a C string literal. It can
71 * also be seen as a routine identical to rb_str_new_static(), except it
72 * additionally takes an encoding.
73 *
74 * @param[in] ptr A C string literal.
75 * @param[in] len `strlen(ptr)`.
76 * @param[in] enc Encoding of `ptr`.
77 * @exception rb_eArgError `len` out of range of `size_t`.
78 * @pre `ptr` must be a C string constant.
79 * @return An instance of ::rb_cString, of `enc` encoding, whose backend
80 * storage is the passed C string literal.
81 * @warning It is a very bad idea to write to a C string literal (often
82 * immediate SEGV shall occur). Consider return values of this
83 * function be read-only.
84 * @note `enc` can be a null pointer. It can also be seen as a routine
85 * identical to rb_usascii_str_new_static() then.
86 */
87 VALUE rb_enc_str_new_static(const char *ptr, long len, rb_encoding *enc);
88
89 /**
90 * Identical to rb_enc_str_new(), except it returns a "f"string. It can also
91 * be seen as a routine identical to rb_interned_str(), except it additionally
92 * takes an encoding.
93 *
94 * @param[in] ptr A memory region of `len` bytes length.
95 * @param[in] len Length of `ptr`, in bytes, not including the
96 * terminating NUL character.
97 * @param[in] enc Encoding of `ptr`.
98 * @exception rb_eArgError `len` is negative.
99 * @return A found or created instance of ::rb_cString, of `len` bytes
100 * length, of `enc` encoding, whose contents are identical to that
101 * of `ptr`.
102 * @pre At least `len` bytes of continuous memory region shall be
103 * accessible via `ptr`.
104 * @note `enc` can be a null pointer.
105 */
106 VALUE rb_enc_interned_str(const char *ptr, long len, rb_encoding *enc);
107
108 RBIMPL_ATTR_NONNULL((1))
109 /**
110 * Identical to rb_enc_str_new_cstr(), except it returns a "f"string. It can
111 * also be seen as a routine identical to rb_interned_str_cstr(), except it
112 * additionally takes an encoding.
113 *
114 * @param[in] ptr A memory region of `len` bytes length.
115 * @param[in] enc Encoding of `ptr`.
116 * @return A found or created instance of ::rb_cString of `enc` encoding,
117 * whose contents are identical to that of `ptr`.
118 * @pre At least `len` bytes of continuous memory region shall be
119 * accessible via `ptr`.
120 * @note `enc` can be a null pointer.
121 */
122 VALUE rb_enc_interned_str_cstr(const char *ptr, rb_encoding *enc);
123
124 /**
125 * Counts the number of characters of the passed string, according to the
126 * passed encoding. This has to be complicated. The passed string could be
127 * invalid and/or broken. This routine would scan from the beginning til the
128 * end, byte by byte, to seek out character boundaries. Could be super slow.
129 *
130 * @param[in] head Leftmost pointer to the string.
131 * @param[in] tail Rightmost pointer to the string.
132 * @param[in] enc Encoding of the string.
133 * @return Number of characters exist in `head` .. `tail`. The definition
134 * of "character" depends on the passed `enc`.
135 */
136 long rb_enc_strlen(const char *head, const char *tail, rb_encoding *enc);
137
138 /**
139 * Queries the n-th character. Like rb_enc_strlen() this function can be fast
140 * or slow depending on the contents. Don't expect characters to be uniformly
141 * distributed across the entire string.
142 *
143 * @param[in] head Leftmost pointer to the string.
144 * @param[in] tail Rightmost pointer to the string.
145 * @param[in] nth Requested index of characters.
146 * @param[in] enc Encoding of the string.
147 * @return Pointer to the first byte of the character that is `nth`
148 * character ahead of `head`, or `tail` if there is no such
149 * character (OOB etc). The definition of "character" depends on
150 * the passed `enc`.
151 */
152 char *rb_enc_nth(const char *head, const char *tail, long nth, rb_encoding *enc);
153
154 /**
155 * Identical to rb_enc_get_index(), except the return type.
156 *
157 * @param[in] obj Object in question.
158 * @exception rb_eTypeError `obj` is incapable of having an encoding.
159 * @return `obj`'s encoding.
160 */
161 VALUE rb_obj_encoding(VALUE obj);
162
163 /**
164 * Identical to rb_str_cat(), except it additionally takes an encoding.
165 *
166 * @param[out] str Destination object.
167 * @param[in] ptr Contents to append.
168 * @param[in] len Length of `src`, in bytes.
169 * @param[in] enc Encoding of `ptr`.
170 * @exception rb_eArgError `len` is negative.
171 * @exception rb_eEncCompatError `enc` is not compatible with `str`.
172 * @return The passed `dst`.
173 * @post The contents of `ptr` is copied, transcoded into `dst`'s
174 * encoding, then pasted into `dst`'s end.
175 */
176 VALUE rb_enc_str_buf_cat(VALUE str, const char *ptr, long len, rb_encoding *enc);
177
178 /**
179 * Encodes the passed code point into a series of bytes.
180 *
181 * @param[in] code Code point.
182 * @param[in] enc Target encoding scheme.
183 * @exception rb_eRangeError `enc` does not glean `code`.
184 * @return An instance of ::rb_cString, of `enc` encoding, whose sole
185 * contents is `code` represented in `enc`.
186 * @note No way to encode code points bigger than UINT_MAX.
187 *
188 * @internal
189 *
190 * In other languages, APIs like this one could be seen as the primitive
191 * routines where encodings' "encode" feature are implemented. However in case
192 * of Ruby this is not the primitive one. We directly manipulate encoded
193 * strings. Encoding conversion routines transcode an encoded string directly
194 * to another one; not via a code point array.
195 */
196 VALUE rb_enc_uint_chr(unsigned int code, rb_encoding *enc);
197
198 /**
199 * Identical to rb_external_str_new(), except it additionally takes an
200 * encoding. However the whole point of rb_external_str_new() is to encode a
201 * string into default external encoding. Being able to specify arbitrary
202 * encoding just ruins the designed purpose the function meseems.
203 *
204 * @param[in] ptr A memory region of `len` bytes length.
205 * @param[in] len Length of `ptr`, in bytes, not including the
206 * terminating NUL character.
207 * @param[in] enc Target encoding scheme.
208 * @exception rb_eArgError `len` is negative.
209 * @return An instance of ::rb_cString. In case encoding conversion from
210 * "default internal" to `enc` is fully defined over the given
211 * contents, then the return value is a string of `enc` encoding,
212 * whose contents are the converted ones. Otherwise the string is
213 * a junk.
214 * @warning It doesn't raise on a conversion failure and silently ends up in
215 * a corrupted output. You can know the failure by querying
216 * `valid_encoding?` of the result object.
217 *
218 * @internal
219 *
220 * @shyouhei has no idea why this one does not follow the naming convention
221 * that others obey. It seems to him that this should have been called
222 * `rb_enc_external_str_new`.
223 */
224 VALUE rb_external_str_new_with_enc(const char *ptr, long len, rb_encoding *enc);
225
226 /**
227 * Identical to rb_str_export(), except it additionally takes an encoding.
228 *
229 * @param[in] obj Target object.
230 * @param[in] enc Target encoding.
231 * @exception rb_eTypeError No implicit conversion to String.
232 * @return Converted ruby string of `enc` encoding.
233 */
234 VALUE rb_str_export_to_enc(VALUE obj, rb_encoding *enc);
235
236 /**
237 * Encoding conversion main routine.
238 *
239 * @param[in] str String to convert.
240 * @param[in] from Source encoding.
241 * @param[in] to Destination encoding.
242 * @return A copy of `str`, with conversion from `from` to `to` applied.
243 * @note `from` can be a null pointer. `str`'s encoding is taken then.
244 * @note `to` can be a null pointer. No-op then.
245 */
246 VALUE rb_str_conv_enc(VALUE str, rb_encoding *from, rb_encoding *to);
247
248 /**
249 * Identical to rb_str_conv_enc(), except it additionally takes IO encoder
250 * options. The extra arguments can be constructed using io_extract_modeenc()
251 * etc.
252 *
253 * @param[in] str String to convert.
254 * @param[in] from Source encoding.
255 * @param[in] to Destination encoding.
256 * @param[in] ecflags A set of enum ::ruby_econv_flag_type.
257 * @param[in] ecopts Optional hash.
258 * @return A copy of `str`, with conversion from `from` to `to` applied.
259 * @note `from` can be a null pointer. `str`'s encoding is taken then.
260 * @note `to` can be a null pointer. No-op then.
261 * @note `ecopts` can be ::RUBY_Qnil, which is equivalent to passing an
262 * empty hash.
263 */
264 VALUE rb_str_conv_enc_opts(VALUE str, rb_encoding *from, rb_encoding *to, int ecflags, VALUE ecopts);
265
266 /**
267 * Scans the passed string to collect its code range. Because a Ruby's string
268 * is mutable, its contents change from time to time; so does its code range.
269 * A long-lived string tends to fall back to ::RUBY_ENC_CODERANGE_UNKNOWN.
270 * This API scans it and re-assigns a fine-grained code range constant.
271 *
272 * @param[out] str A string.
273 * @return An enum ::ruby_coderange_type.
274 */
275 int rb_enc_str_coderange(VALUE str);
276
277 /**
278 * Scans the passed string until it finds something odd. Returns the number of
279 * bytes scanned. As the name implies this is suitable for repeated call. One
280 * of its application is `IO#readlines`. The method reads from its receiver's
281 * read buffer, maybe more than once, looking for newlines. But "newline" can
282 * be different among encodings. This API is used to detect broken contents to
283 * properly mark them as such.
284 *
285 * @param[in] str String to scan.
286 * @param[in] end End of `str`.
287 * @param[in] enc `str`'s encoding.
288 * @param[out] cr Return buffer.
289 * @return Distance between `str` and first such byte where broken.
290 * @post `cr` has the code range type.
291 */
292 long rb_str_coderange_scan_restartable(const char *str, const char *end, rb_encoding *enc, int *cr);
293
294 /**
295 * Queries if the passed string is "ASCII only". An ASCII only string is a
296 * string who doesn't have any non-ASCII characters at all. This doesn't
297 * necessarily mean the string is in ASCII encoding. For instance a String of
298 * CP932 encoding can quite much be ASCII only, depending on its contents.
299 *
300 * @param[in] str String in question.
301 * @retval 1 It doesn't have non-ASCII characters.
302 * @retval 0 It has characters that are out of ASCII.
303 */
304 int rb_enc_str_asciionly_p(VALUE str);
305
306 RBIMPL_ATTR_NONNULL(())
307 /**
308 * Looks for the passed string in the passed buffer.
309 *
310 * @param[in] x Buffer that potentially includes `y`.
311 * @param[in] m Number of bytes of `x`.
312 * @param[in] y Query string.
313 * @param[in] n Number of bytes of `y`.
314 * @param[in] enc Encoding of both `x` and `y`.
315 * @retval -1 Not found.
316 * @retval otherwise Found index in `x`.
317 * @note This API can match at a non-character-boundary.
318 */
319 long rb_memsearch(const void *x, long m, const void *y, long n, rb_encoding *enc);
320
321 /** @cond INTERNAL_MACRO */
322 RBIMPL_ATTR_NONNULL(())
323 static inline VALUE
324 rbimpl_enc_str_new_cstr(const char *str, rb_encoding *enc)
325 {
326 long len = rbimpl_strlen(str);
327
328 return rb_enc_str_new_static(str, len, enc);
329 }
330
331 #define rb_enc_str_new(str, len, enc) \
332 ((RBIMPL_CONSTANT_P(str) && \
333 RBIMPL_CONSTANT_P(len) ? \
334 rb_enc_str_new_static: \
335 rb_enc_str_new) ((str), (len), (enc)))
336
337 #define rb_enc_str_new_cstr(str, enc) \
338 ((RBIMPL_CONSTANT_P(str) ? \
339 rbimpl_enc_str_new_cstr : \
340 rb_enc_str_new_cstr) ((str), (enc)))
341
342 /** @endcond */
343
344 RBIMPL_SYMBOL_EXPORT_END()
345
346 #endif /* RUBY_INTERNAL_ENCODING_STRING_H */