-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathstring.h
1757 lines (1635 loc) · 67.4 KB
/
string.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#ifndef RBIMPL_INTERN_STRING_H /*-*-C++-*-vi:se ft=cpp:*/
#define RBIMPL_INTERN_STRING_H
/**
* @file
* @author Ruby developers <ruby-core@ruby-lang.org>
* @copyright This file is a part of the programming language Ruby.
* Permission is hereby granted, to either redistribute and/or
* modify this file, provided that the conditions mentioned in the
* file COPYING are met. Consult the file for details.
* @warning Symbols prefixed with either `RBIMPL` or `rbimpl` are
* implementation details. Don't take them as canon. They could
* rapidly appear then vanish. The name (path) of this header file
* is also an implementation detail. Do not expect it to persist
* at the place it is now. Developers are free to move it anywhere
* anytime at will.
* @note To ruby-core: remember that this header can be possibly
* recursively included from extension libraries written in C++.
* Do not expect for instance `__VA_ARGS__` is always available.
* We assume C99 for ruby itself but we don't assume languages of
* extension libraries. They could be written in C++98.
* @brief Public APIs related to ::rb_cString.
*/
#include "ruby/internal/config.h"
#ifdef STDC_HEADERS
# include <stddef.h>
#endif
#ifdef HAVE_STRING_H
# include <string.h>
#endif
#ifdef HAVE_STDINT_H
# include <stdint.h>
#endif
#include "ruby/internal/attr/deprecated.h"
#include "ruby/internal/attr/nonnull.h"
#include "ruby/internal/attr/pure.h"
#include "ruby/internal/constant_p.h"
#include "ruby/internal/dllexport.h"
#include "ruby/internal/value.h"
#include "ruby/internal/variable.h" /* rb_gvar_setter_t */
#include "ruby/st.h" /* st_index_t */
RBIMPL_SYMBOL_EXPORT_BEGIN()
/* string.c */
/**
* Allocates an instance of ::rb_cString.
*
* @param[in] ptr A memory region of `len` bytes length.
* @param[in] len Length of `ptr`, in bytes, not including the
* terminating NUL character.
* @exception rb_eNoMemError Failed to allocate `len+1` bytes.
* @exception rb_eArgError `len` is negative.
* @return An instance of ::rb_cString, of `len` bytes length, of
* "binary" encoding, whose contents are verbatim copy of `ptr`.
* @pre At least `len` bytes of continuous memory region shall be
* accessible via `ptr`.
*/
VALUE rb_str_new(const char *ptr, long len);
/**
* Identical to rb_str_new(), except it assumes the passed pointer is a pointer
* to a C string.
*
* @param[in] ptr A C string.
* @exception rb_eNoMemError Failed to allocate memory.
* @exception rb_eArgError `ptr` is a null pointer.
* @return An instance of ::rb_cString, of "binary" encoding, whose
* contents are verbatim copy of `ptr`.
* @pre `ptr` must not be a null pointer.
*/
VALUE rb_str_new_cstr(const char *ptr);
/**
* Identical to rb_str_new_cstr(), except it takes a Ruby's string instead of
* C's. Implementation wise it creates a string that shares the backend memory
* region with the receiver. So the name. But there is no way for extension
* libraries to know if a string is of such variant.
*
* @param[in] str An object of ::RString.
* @return An allocated instance of ::rb_cString, which shares the
* encoding, length, and contents with the passed string.
* @pre `str` must not be any arbitrary object except ::RString.
* @note Use #StringValue to enforce the precondition.
*/
VALUE rb_str_new_shared(VALUE str);
/**
* Creates a frozen copy of the string, if necessary. This function does
* nothing when the passed string is already frozen. Otherwise, it allocates a
* copy of it, which is frozen. The passed string is untouched either ways.
*
* @param[in] str An object of ::RString.
* @return Something frozen.
* @pre `str` must not be any arbitrary object except ::RString.
* @note Use #StringValue to enforce the precondition.
*/
VALUE rb_str_new_frozen(VALUE str);
/**
* Identical to rb_str_new(), except it takes the class of the allocating
* object.
*
* @param[in] obj A string-ish object.
* @param[in] ptr A memory region of `len` bytes length.
* @param[in] len Length of `ptr`, in bytes, not including the
* terminating NUL character.
* @exception rb_eNoMemError Failed to allocate `len+1` bytes.
* @exception rb_eArgError `len` is negative.
* @return An instance of the class of `obj`, of `len` bytes length, of
* "binary" encoding, whose contents are verbatim copy of `ptr`.
* @pre At least `len` bytes of continuous memory region shall be
* accessible via `ptr`.
*
* @internal
*
* Why it doesn't take an instance of ::rb_cClass?
*/
VALUE rb_str_new_with_class(VALUE obj, const char *ptr, long len);
/**
* Identical to rb_str_new(), except it generates a string of "default
* external" encoding.
*
* @param[in] ptr A memory region of `len` bytes length.
* @param[in] len Length of `ptr`, in bytes, not including the
* terminating NUL character.
* @exception rb_eNoMemError Failed to allocate `len+1` bytes.
* @exception rb_eArgError `len` is negative.
* @return An instance of ::rb_cString. In case encoding conversion from
* "default internal" to "default external" is fully defined over
* the given contents, then the return value is a string of
* "default external" encoding, whose contents are the converted
* ones. Otherwise the string is a junk.
* @warning It doesn't raise on a conversion failure and silently ends up in
* a corrupted output. You can know the failure by querying
* `valid_encoding?` of the result object.
*/
VALUE rb_external_str_new(const char *ptr, long len);
RBIMPL_ATTR_NONNULL(())
/**
* Identical to rb_external_str_new(), except it assumes the passed pointer is
* a pointer to a C string. It can also be seen as a routine identical to
* rb_str_new_cstr(), except it generates a string of "default external"
* encoding.
*
* @param[in] ptr A C string.
* @exception rb_eNoMemError Failed to allocate memory.
* @return An instance of ::rb_cString. In case encoding conversion from
* "default internal" to "default external" is fully defined over
* the given contents, then the return value is a string of
* "default external" encoding, whose contents are the converted
* ones. Otherwise the string is a junk.
* @warning It doesn't raise on a conversion failure and silently ends up in
* a corrupted output. You can know the failure by querying
* `valid_encoding?` of the result object.
* @pre `ptr` must not be a null pointer.
*/
VALUE rb_external_str_new_cstr(const char *ptr);
/**
* Identical to rb_str_new(), except it generates a string of "locale"
* encoding. It can also be seen as a routine identical to
* rb_external_str_new(), except it generates a string of "locale" encoding
* instead of "default external" encoding.
*
* @param[in] ptr A memory region of `len` bytes length.
* @param[in] len Length of `ptr`, in bytes, not including the
* terminating NUL character.
* @exception rb_eNoMemError Failed to allocate `len+1` bytes.
* @exception rb_eArgError `len` is negative.
* @return An instance of ::rb_cString. In case encoding conversion from
* "default internal" to "locale" is fully defined over the given
* contents, then the return value is a string of "locale"
* encoding, whose contents are the converted ones. Otherwise the
* string is a junk.
* @warning It doesn't raise on a conversion failure and silently ends up in
* a corrupted output. You can know the failure by querying
* `valid_encoding?` of the result object.
*/
VALUE rb_locale_str_new(const char *ptr, long len);
RBIMPL_ATTR_NONNULL(())
/**
* Identical to rb_locale_str_new(), except it assumes the passed pointer is a
* pointer to a C string. It can also be seen as a routine identical to
* rb_external_str_new_cstr(), except it generates a string of "locale"
* encoding instead of "default external".
*
* @param[in] ptr A C string.
* @exception rb_eNoMemError Failed to allocate memory.
* @return An instance of ::rb_cString. In case encoding conversion from
* "default internal" to "locale" is fully defined over the given
* contents, then the return value is a string of "locale"
* encoding, whose contents are the converted ones. Otherwise the
* string is a junk.
* @warning It doesn't raise on a conversion failure and silently ends up in
* a corrupted output. You can know the failure by querying
* `valid_encoding?` of the result object.
* @pre `ptr` must not be a null pointer.
*/
VALUE rb_locale_str_new_cstr(const char *ptr);
/**
* Identical to rb_str_new(), except it generates a string of "filesystem"
* encoding. It can also be seen as a routine identical to
* rb_external_str_new(), except it generates a string of "filesystem" encoding
* instead of "default external" encoding.
*
* @param[in] ptr A memory region of `len` bytes length.
* @param[in] len Length of `ptr`, in bytes, not including the
* terminating NUL character.
* @exception rb_eNoMemError Failed to allocate `len+1` bytes.
* @exception rb_eArgError `len` is negative.
* @return An instance of ::rb_cString. In case encoding conversion from
* "default internal" to "filesystem" is fully defined over the
* given contents, then the return value is a string of
* "filesystem" encoding, whose contents are the converted ones.
* Otherwise the string is a junk.
* @warning It doesn't raise on a conversion failure and silently ends up in
* a corrupted output. You can know the failure by querying
* `valid_encoding?` of the result object.
*/
VALUE rb_filesystem_str_new(const char *ptr, long len);
RBIMPL_ATTR_NONNULL(())
/**
* Identical to rb_filesystem_str_new(), except it assumes the passed pointer
* is a pointer to a C string. It can also be seen as a routine identical to
* rb_external_str_new_cstr(), except it generates a string of "filesystem"
* encoding instead of "default external".
*
* @param[in] ptr A C string.
* @exception rb_eNoMemError Failed to allocate memory.
* @return An instance of ::rb_cString. In case encoding conversion from
* "default internal" to "filesystem" is fully defined over the
* given contents, then the return value is a string of
* "filesystem" encoding, whose contents are the converted ones.
* Otherwise the string is a junk.
* @warning It doesn't raise on a conversion failure and silently ends up in
* a corrupted output. You can know the failure by querying
* `valid_encoding?` of the result object.
* @pre `ptr` must not be a null pointer.
*/
VALUE rb_filesystem_str_new_cstr(const char *ptr);
/**
* Allocates a "string buffer". A string buffer here is an instance of
* ::rb_cString, whose capacity is bigger than the length of it. If you can
* say that a string grows to a specific amount of bytes, this could be
* effective than resizing a string over and over again and again.
*
* @param[in] capa Designed capacity of the generating string.
* @return An empty string, of "binary" encoding, whose capacity is `capa`.
*/
VALUE rb_str_buf_new(long capa);
RBIMPL_ATTR_NONNULL(())
/**
* This is a rb_str_buf_new() + rb_str_buf_cat() combo.
*
* @param[in] ptr A C string.
* @exception rb_eNoMemError Failed to allocate memory.
* @return An instance of ::rb_cString, of "binary" encoding, whose
* contents are verbatim copy of `ptr`.
* @pre `ptr` must not be a null pointer.
*
* @internal
*
* This must be identical to rb_str_new_cstr(), except done in inefficient way?
* @shyouhei doesn't understand why this is not a simple alias.
*/
VALUE rb_str_buf_new_cstr(const char *ptr);
/**
* Allocates a "temporary" string. This is a hidden empty string. Handy on
* occasions.
*
* @param[in] len Designed length of the string.
* @return A hidden, empty string.
* @see rb_obj_hide()
*/
VALUE rb_str_tmp_new(long len);
/**
* Identical to rb_str_new(), except it generates a string of "US ASCII"
* encoding. This is different from rb_external_str_new(), not only for the
* output encoding, but also it doesn't convert the contents.
*
* @param[in] ptr A memory region of `len` bytes length.
* @param[in] len Length of `ptr`, in bytes, not including the
* terminating NUL character.
* @exception rb_eNoMemError Failed to allocate `len+1` bytes.
* @exception rb_eArgError `len` is negative.
* @return An instance of ::rb_cString, of `len` bytes length, of
* "US ASCII" encoding, whose contents are verbatim copy of `ptr`.
*/
VALUE rb_usascii_str_new(const char *ptr, long len);
/**
* Identical to rb_str_new_cstr(), except it generates a string of "US ASCII"
* encoding. It can also be seen as a routine Identical to
* rb_usascii_str_new(), except it assumes the passed pointer is a pointer to a
* C string.
*
* @param[in] ptr A C string.
* @exception rb_eNoMemError Failed to allocate memory.
* @exception rb_eArgError `ptr` is a null pointer.
* @return An instance of ::rb_cString, of "US ASCII" encoding, whose
* contents are verbatim copy of `ptr`.
* @pre `ptr` must not be a null pointer.
*/
VALUE rb_usascii_str_new_cstr(const char *ptr);
/**
* Identical to rb_str_new(), except it generates a string of "UTF-8" encoding.
*
* @param[in] ptr A memory region of `len` bytes length.
* @param[in] len Length of `ptr`, in bytes, not including the
* terminating NUL character.
* @exception rb_eNoMemError Failed to allocate `len+1` bytes.
* @exception rb_eArgError `len` is negative.
* @return An instance of ::rb_cString, of `len` bytes length, of
* "UTF-8" encoding, whose contents are verbatim copy of `ptr`.
*/
VALUE rb_utf8_str_new(const char *ptr, long len);
/**
* Identical to rb_str_new_cstr(), except it generates a string of "UTF-8"
* encoding. It can also be seen as a routine Identical to
* rb_usascii_str_new(), except it assumes the passed pointer is a pointer to a
* C string.
*
* @param[in] ptr A C string.
* @exception rb_eNoMemError Failed to allocate memory.
* @exception rb_eArgError `ptr` is a null pointer.
* @return An instance of ::rb_cString, of "UTF-8" encoding, whose contents
* are verbatim copy of `ptr`.
* @pre `ptr` must not be a null pointer.
*/
VALUE rb_utf8_str_new_cstr(const char *ptr);
/**
* @name Special strings that are backended by C string literals.
*
* *_str_new_static functions are intended for C string literals.
* They require memory in the range [ptr, ptr+len] to always be readable.
* Note that this range covers a total of len + 1 bytes.
*
* @{
*/
/**
* Identical to rb_str_new(), except it takes a C string literal.
*
* @param[in] ptr A C string literal.
* @param[in] len `strlen(ptr)`.
* @exception rb_eArgError `len` out of range of `size_t`.
* @pre `ptr` must be a C string constant.
* @return An instance of ::rb_cString, of "binary" encoding, whose backend
* storage is the passed C string literal.
* @warning It is a very bad idea to write to a C string literal (often
* immediate SEGV shall occur). Consider return values of this
* function be read-only.
*
* @internal
*
* Surprisingly it can take NULL, and generates an empty string.
*/
VALUE rb_str_new_static(const char *ptr, long len);
/**
* Identical to rb_str_new_static(), except it generates a string of "US ASCII"
* encoding instead of "binary". It can also be seen as a routine identical to
* rb_usascii_str_new(), except it takes a C string literal.
*
* @param[in] ptr A C string literal.
* @param[in] len `strlen(ptr)`.
* @exception rb_eArgError `len` out of range of `size_t`.
* @pre `ptr` must be a C string constant.
* @return An instance of ::rb_cString, of "US ASCII" encoding, whose
* backend storage is the passed C string literal.
* @warning It is a very bad idea to write to a C string literal (often
* immediate SEGV shall occur). Consider return values of this
* function be read-only.
*/
VALUE rb_usascii_str_new_static(const char *ptr, long len);
/**
* Identical to rb_str_new_static(), except it generates a string of "UTF-8"
* encoding instead of "binary". It can also be seen as a routine identical to
* rb_utf8_str_new(), except it takes a C string literal.
*
* @param[in] ptr A C string literal.
* @param[in] len `strlen(ptr)`.
* @exception rb_eArgError `len` out of range of `size_t`.
* @pre `ptr` must be a C string constant.
* @return An instance of ::rb_cString, of "UTF-8" encoding, whose backend
* storage is the passed C string literal.
* @warning It is a very bad idea to write to a C string literal (often
* immediate SEGV shall occur). Consider return values of this
* function be read-only.
*/
VALUE rb_utf8_str_new_static(const char *ptr, long len);
/** @} */
/**
* Identical to rb_interned_str(), except it takes a Ruby's string instead of
* C's. It can also be seen as a routine identical to rb_str_new_shared(),
* except it returns an infamous "f"string.
*
* @param[in] str An object of ::RString.
* @return An instance of ::rb_cString, either cached or allocated, which
* has the identical encoding, length, and contents with the passed
* string.
* @pre `str` must not be any arbitrary object except ::RString.
* @note Use #StringValue to enforce the precondition.
*
* @internal
*
* It actually finds or creates a fstring of the needed property, and
* destructively modifies the receiver behind-the-scene so that it becomes a
* shared string whose parent is the returning fstring.
*/
VALUE rb_str_to_interned_str(VALUE str);
/**
* Identical to rb_str_new(), except it returns an infamous "f"string. What is
* a fstring? Well it is a special subkind of strings that is immutable,
* deduped globally, and managed by our GC. It is much like a Symbol (in fact
* Symbols are dynamic these days and are backended using fstrings). This
* concept has been silently introduced at some point in 2.x era. Since then
* it gained wider acceptance in the core. Starting from 3.x extension
* libraries can also generate ones.
*
* @param[in] ptr A memory region of `len` bytes length.
* @param[in] len Length of `ptr`, in bytes, not including the
* terminating NUL character.
* @exception rb_eArgError `len` is negative.
* @return A found or created instance of ::rb_cString, of `len` bytes
* length, of "binary" encoding, whose contents are identical to
* that of `ptr`.
* @pre At least `len` bytes of continuous memory region shall be
* accessible via `ptr`.
*/
VALUE rb_interned_str(const char *ptr, long len);
RBIMPL_ATTR_NONNULL(())
/**
* Identical to rb_interned_str(), except it assumes the passed pointer is a
* pointer to a C's string. It can also be seen as a routine identical to
* rb_str_to_interned_str(), except it takes a C's string instead of Ruby's.
* Or it can also be seen as a routine identical to rb_str_new_cstr(), except
* it returns an infamous "f"string.
*
* @param[in] ptr A C string.
* @exception rb_eNoMemError Failed to allocate memory.
* @return An instance of ::rb_cString, of "binary" encoding, whose
* contents are verbatim copy of `ptr`.
* @pre `ptr` must not be a null pointer.
*/
VALUE rb_interned_str_cstr(const char *ptr);
/**
* Destroys the given string for no reason.
*
* @warning DO NOT USE IT.
* @warning Leave this task to our GC.
* @warning It was a bad idea at the first place to let you know about it.
*
* @param[out] str The string to be executed.
* @post The given string no longer exists.
* @note Maybe `String#clear` could be what you want.
*
* @internal
*
* Should have moved this to `internal/string.h`.
*/
void rb_str_free(VALUE str);
/**
* Replaces the contents of the former with the latter.
*
* @param[out] dst Destination object.
* @param[in] src Source object.
* @pre Both objects must not be any arbitrary objects except
* ::RString.
* @post `dst`'s former components are abandoned. It now has the
* identical encoding, length, and contents to `src`.
* @see rb_str_replace()
*
* @internal
*
* @shyouhei doesn't understand why this is useful to extension libraries.
* Just use rb_str_replace(). What's wrong with that?
*/
void rb_str_shared_replace(VALUE dst, VALUE src);
/**
* Identical to rb_str_cat_cstr(), except it takes Ruby's string instead of
* C's. It can also be seen as a routine identical to rb_str_shared_replace(),
* except it appends instead of replaces.
*
* @param[out] dst Destination object.
* @param[in] src Source object.
* @exception rb_eEncCompatError Can't mix the encodings.
* @exception rb_eArgError Result string too big.
* @return The passed `dst`.
* @pre Both objects must not be any arbitrary objects except
* ::RString.
* @post `dst` has the contents of `src` appended, with encoding
* converted into `dst`'s one, into the end of `dst`.
*/
VALUE rb_str_buf_append(VALUE dst, VALUE src);
/** @alias{rb_str_cat} */
VALUE rb_str_buf_cat(VALUE, const char*, long);
/** @alias{rb_str_cat_cstr} */
VALUE rb_str_buf_cat2(VALUE, const char*);
RBIMPL_ATTR_NONNULL(())
/**
* Identical to rb_str_cat_cstr(), except it additionally assumes the source
* string be a NUL terminated ASCII string.
*
* @param[out] dst Destination object.
* @param[in] src Source string.
* @exception rb_eArgError Result string too big.
* @return The passed `dst`.
* @pre `dst` must not be any arbitrary object except ::RString.
* @pre `src` must be a NUL terminated ASCII string.
* @post `dst` has the contents of `src` appended, with encoding
* converted into `dst`'s one, into the end of `dst`.
*/
VALUE rb_str_buf_cat_ascii(VALUE dst, const char *src);
/**
* Try converting an object to its stringised representation using its `to_s`
* method, if any. If there is no such thing, it resorts to rb_any_to_s()
* output.
*
* @param[in] obj Arbitrary ruby object to stringise.
* @return An instance of ::rb_cString.
*/
VALUE rb_obj_as_string(VALUE obj);
/**
* Try converting an object to its stringised representation using its `to_str`
* method, if any. If there is no such thing, returns ::RUBY_Qnil.
*
* @param[in] obj Arbitrary ruby object to stringise.
* @exception rb_eTypeError `obj.to_str` returned something non-String.
* @retval RUBY_Qnil No conversion from obj to String defined.
* @return otherwise Stringised representation of `obj`.
* @see rb_io_check_io
* @see rb_check_array_type
* @see rb_check_hash_type
*/
VALUE rb_check_string_type(VALUE obj);
/**
* Asserts that the given string's encoding is (Ruby's definition of) ASCII
* compatible.
*
* @param[in] obj An instance of ::rb_cString.
* @exception rb_eEncCompatError `obj` is ASCII incompatible.
*
* @internal
*
* @shyouhei doesn't know if this is an Easter egg or an official feature, but
* this function can in fact take non-strings such as Symbols, Regexps, IOs,
* etc. However if something unsupported is passed, it causes SEGV. It seems
* the feature is kind of untested.
*/
void rb_must_asciicompat(VALUE obj);
/**
* Duplicates a string.
*
* @param[in] str String in question to duplicate.
* @return A duplicated new instance.
* @pre `str` must be of ::RString.
*/
VALUE rb_str_dup(VALUE str);
/**
* I guess there is no use case of this function in extension libraries, but
* this is a routine identical to rb_str_dup(), except it always creates an
* instance of ::rb_cString regardless of the given object's class. This makes
* the most sense when the passed string is formerly hidden by rb_obj_hide().
*
* @param[in] str A string, possibly hidden.
* @return A duplicated new instance of ::rb_cString.
*/
VALUE rb_str_resurrect(VALUE str);
/**
* Obtains a "temporary lock" of the string. This advisory locking mechanism
* prevents other cooperating threads from tampering the receiver. The same
* thing could be done via freeze mechanism, but this one can also be unlocked
* using rb_str_unlocktmp().
*
* @param[out] str String to lock.
* @exception rb_eRuntimeError `str` already locked.
* @return The given string.
* @post The string is locked.
*/
VALUE rb_str_locktmp(VALUE str);
/**
* Releases a lock formerly obtained by rb_str_locktmp().
*
* @param[out] str String to unlock.
* @exception rb_eRuntimeError `str` already unlocked.
* @return The given string.
* @post The string is locked.
*/
VALUE rb_str_unlocktmp(VALUE str);
/** @alias{rb_str_new_frozen} */
VALUE rb_str_dup_frozen(VALUE);
/** @alias{rb_str_new_frozen} */
#define rb_str_dup_frozen rb_str_new_frozen
/**
* Generates a new string, concatenating the former to the latter. It can also
* be seen as a routine identical to rb_str_append(), except it doesn't tamper
* the passed strings to create a new one instead.
*
* @param[in] lhs Source string #1.
* @param[in] rhs Source string #2.
* @exception rb_eEncCompatError Can't mix the encodings.
* @exception rb_eArgError Result string too big.
* @return A new string containing `rhs` concatenated to `lhs`.
* @pre Both objects must not be any arbitrary objects except ::RString.
* @note This operation doesn't commute. Don't get confused by the
* "plus" terminology. For historical reasons there are some
* noncommutative `+`s in Ruby. This is one of such things. There
* has been a long discussion around `+`s in programming languages.
*/
VALUE rb_str_plus(VALUE lhs, VALUE rhs);
/**
* Repetition of a string.
*
* @param[in] str String to repeat.
* @param[in] num Count, something numeric.
* @exception rb_eArgError `num` is negative.
* @return A new string repeating `num` times of `str`.
*/
VALUE rb_str_times(VALUE str, VALUE num);
/**
* Byte offset to character offset conversion. This makes sense when the
* receiver is in a multibyte encoding. The string's i-th character does not
* always sit at its i-th byte. This function scans the contents to find the
* character index that matches the byte index. Generally speaking this is an
* `O(n)` operation. Could be slow.
*
* @param[in] str The string to scan.
* @param[in] pos Offset, in bytes.
* @return Offset, in characters.
*/
long rb_str_sublen(VALUE str, long pos);
/**
* This is the implementation of two-argumented `String#slice`.
*
* - Returns the substring of the given `len` found in `str` at offset `beg`:
*
* ```ruby
* 'foo'[0, 2] # => "fo"
* 'foo'[0, 0] # => ""
* ```
*
* - Counts backward from the end of `str` if `beg` is negative:
*
* ```ruby
* 'foo'[-2, 2] # => "oo"
* ```
*
* - Special case: returns a new empty string if `beg` is equal to the length
* of `str`:
*
* ```ruby
* 'foo'[3, 2] # => ""
* ```
*
* - Returns a null pointer if `beg` is out of range:
*
* ```ruby
* 'foo'[4, 2] # => nil
* 'foo'[-4, 2] # => nil
* ```
*
* - Returns the trailing substring of `str` if `len` is large:
*
* ```ruby
* 'foo'[1, 50] # => "oo"
* ```
*
* - Returns a null pointer if `len` is negative:
*
* ```ruby
* 'foo'[0, -1] # => nil
* ```
*
* @param[in] str The string to slice.
* @param[in] beg Requested offset of the substring.
* @param[in] len Requested length of the substring.
* @retval RUBY_Qnil Parameters out of range.
* @retval otherwise A new string whose contents is the specified
* substring of `str`.
* @pre `str` must not be any arbitrary objects except ::RString.
*/
VALUE rb_str_substr(VALUE str, long beg, long len);
/**
* Identical to rb_str_substr(), except the numbers are interpreted as byte
* offsets instead of character offsets.
*
* @param[in] str The string to slice.
* @param[in] beg Requested offset of the substring.
* @param[in] len Requested length of the substring.
* @return A new string whose contents is the specified substring of `str`.
* @pre `str` must not be any arbitrary objects except ::RString.
* @pre `beg` and `len` must not point to OOB contents.
*/
VALUE rb_str_subseq(VALUE str, long beg, long len);
/**
* Identical to rb_str_substr(), except it returns a C's string instead of
* Ruby's.
*
* @param[in] str The string to slice.
* @param[in] beg Requested offset of the substring.
* @param[in,out] len Requested length of the substring.
* @retval NULL Parameters out of range.
* @retval otherwise A pointer inside of `str`'s backend storage where
* the specified substring exist.
* @pre `str` must not be any arbitrary objects except ::RString.
* @post `len` is updated to have the length of the return value.
*/
char *rb_str_subpos(VALUE str, long beg, long *len);
/**
* Declares that the string is about to be modified. This for instance let the
* string have a dedicated backend storage.
*
* @param[out] str String about to be modified.
* @exception rb_eRuntimeError `str` is `locktmp`-ed.
* @exception rb_eFrozenError `str` is frozen.
* @pre `str` must not be any arbitrary objects except ::RString.
* @post Upon successful return the passed string is eligible to be
* modified.
*/
void rb_str_modify(VALUE str);
/**
* Identical to rb_str_modify(), except it additionally expands the capacity of
* the receiver.
*
* @param[out] str Target string to modify.
* @param[in] capa Additional capacity to add.
* @exception rb_eArgError `capa` is negative.
* @exception rb_eRuntimeError `str` is `locktmp`-ed.
* @exception rb_eFrozenError `str` is frozen.
* @pre `str` must not be any arbitrary objects except ::RString.
* @post Upon successful return the passed string is modified so that
* its capacity is increased for `capa` bytes.
*/
void rb_str_modify_expand(VALUE str, long capa);
/**
* This is the implementation of `String#freeze`.
*
* @param[out] str Target string to freeze.
* @return The passed string.
* @post Upon successful return the passed string is frozen.
*/
VALUE rb_str_freeze(VALUE str);
/**
* Overwrites the length of the string. Typically this is used to shrink a
* string that was formerly expanded.
*
* ```CXX
* extern int fd;
* auto str = rb_eval_string("'...'");
* rb_str_modify_expand(str, BUFSIZ);
* if (auto len = recv(fd, RSTRING_PTR(str), BUFSIZ, 0); len >= 0) {
* rb_str_set_len(str, len);
* }
* else {
* rb_sys_fail("recv(2)");
* }
* ```
*
* @param[out] str String to shrink.
* @param[in] len New length of the string.
* @exception rb_eRuntimeError `str` is `locktmp`-ed.
* @exception rb_eFrozenError `str` is frozen.
* @pre `str` must not be any arbitrary objects except ::RString.
* @post Upon successful return `str`'s length is set to `len`.
*/
void rb_str_set_len(VALUE str, long len);
/**
* Overwrites the length of the string. In contrast to rb_str_set_len(), this
* function can also expand a string.
*
* @param[out] str String to shrink.
* @param[in] len New length of the string.
* @exception rb_eArgError `len` is negative.
* @exception rb_eRuntimeError `str` is `locktmp`-ed.
* @exception rb_eFrozenError `str` is frozen.
* @return The passed `str`.
* @pre `str` must not be any arbitrary objects except ::RString.
* @post Upon successful return `str` is either expanded or shrunken to
* have its length be `len`.
*/
VALUE rb_str_resize(VALUE str, long len);
/**
* Destructively appends the passed contents to the string.
*
* @param[out] dst Destination object.
* @param[in] src Contents to append.
* @param[in] srclen Length of `src`.
* @exception rb_eArgError `srclen` is negative.
* @return The passed `dst`.
* @pre `dst` must not be any arbitrary objects except ::RString.
* @post `dst` has the contents of `ptr` appended.
*/
VALUE rb_str_cat(VALUE dst, const char *src, long srclen);
/**
* Identical to rb_str_cat(), except it assumes the passed pointer is a pointer
* to a C string.
*
* @param[out] dst Destination object.
* @param[in] src Contents to append.
* @exception rb_eArgError Result string too big.
* @exception rb_eArgError `src` is a null pointer.
* @return The passed `dst`.
* @pre `dst` must not be any arbitrary objects except ::RString.
* @pre `src` must not be a null pointer.
* @post `dst` has the contents of `src` appended.
*/
VALUE rb_str_cat_cstr(VALUE dst, const char *src);
/** @alias{rb_str_cat_cstr} */
VALUE rb_str_cat2(VALUE, const char*);
/**
* Identical to rb_str_buf_append(), except it converts the right hand side
* before concatenating.
*
* @param[out] dst Destination object.
* @param[in] src Source object.
* @exception rb_eEncCompatError Can't mix the encodings.
* @exception rb_eArgError Result string too big.
* @return The passed `dst`.
* @pre `dst` must not be any arbitrary objects except ::RString.
* @post `dst` has the contents of `src` appended, with encoding
* converted into `dst`'s one, into the end of `dst`.
*/
VALUE rb_str_append(VALUE dst, VALUE src);
/**
* Identical to rb_str_append(), except it also accepts an integer as a
* codepoint. This resembles `String#<<`.
*
* @param[out] dst Destination object.
* @param[in] src Source object, String or Numeric.
* @exception rb_eRangeError Source numeric is out of range.
* @exception rb_eEncCompatError Source string too long.
* @exception rb_eArgError Result string too big.
* @return The passed `dst`.
* @pre `dst` must not be any arbitrary objects except ::RString.
* @post `dst` has the contents of `src` appended, with encoding
* converted into `dst`'s one, into the end of `dst`.
*/
VALUE rb_str_concat(VALUE dst, VALUE src);
/* random.c */
/**
* This is a universal hash function.
*
* @warning This function changes its value per process.
* @param[in] ptr Target message.
* @param[in] len Length of `ptr` in bytes.
* @return A pseudorandom number suitable for Hash's hash value.
* @see Aumasson, JP., Bernstein, D.J., "SipHash: A Fast Short-Input
* PRF", In proceedings of 13th International Conference on
* Cryptology in India (INDOCRYPT 2012), LNCS 7668, pp. 489-508,
* 2012. http://doi.org/10.1007/978-3-642-34931-7_28
*/
st_index_t rb_memhash(const void *ptr, long len);
/**
* Starts a series of hashing. Suppose you have a struct:
*
* ```CXX
* struct foo_tag {
* unsigned char bar;
* uint32_t baz;
* };
* ```
*
* It is not a wise idea to call rb_memhash() over it, because there could be
* padding bits. Instead you should explicitly iterate over each fields:
*
* ```CXX
* foo_tag foo = { 0, 0, };
* st_index_t hash = 0;
*
* hash = rb_hash_start(0);
* hash = rb_hash_uint(hash, foo.bar);
* hash = rb_hash_uint32(hash, foo.baz);
* hash = rb_hash_end(hash);
* ```
*
* @param[in] i Initial value.
* @return A hash value.
*/
st_index_t rb_hash_start(st_index_t i);
/** @alias{st_hash_uint32} */
#define rb_hash_uint32(h, i) st_hash_uint32((h), (i))
/** @alias{st_hash_uint} */
#define rb_hash_uint(h, i) st_hash_uint((h), (i))
/** @alias{st_hash_end} */
#define rb_hash_end(h) st_hash_end(h)
/* string.c */
/**
* Calculates a hash value of a string. This is one of the two functions that
* constructs struct ::st_hash_type.
*
* @param[in] str An object of ::RString.
* @return A hash value.
* @pre `str` must not be any arbitrary object except ::RString.
*
* @internal
*
* Although safe to call, there must be no particular use case of this function
* for extension libraries. Only ruby internals must know about it.
*
* This is not a simple alias of rb_memhash(), because it considers the passed
* string's encoding as well as its contents.
*/
st_index_t rb_str_hash(VALUE str);
/**
* Compares two strings. This is one of the two functions that constructs
* struct ::st_hash_type.
*
* @param[in] str1 A string.
* @param[in] str2 Another string.
* @retval 1 They have identical contents, length, and encodings.
* @retval 0 Otherwise.
* @pre Both objects must not be any arbitrary objects except
* ::RString.
*
* @internal
*
* In contrast to rb_str_hash(), this could be handy for comparison that only
* concerns equality. rb_str_cmp() returns 1, 0, -1.
*/
int rb_str_hash_cmp(VALUE str1, VALUE str2);
/**
* Checks if two strings are comparable each other or not. Because
* rb_str_cmp() must return "lesser than" or "greater than" information,
* comparing two strings needs a stricter restriction. Both sides must be in a
* same set of strings which have total order. This is to check that property.
* Intuitive it sounds? But they can have different encodings. A character
* and another might or might not appear in the same order in their codepoints.
* It is complicated than you think.
*
* @param[in] str1 A string.
* @param[in] str2 Another string.
* @retval 1 They agree on a total order.
* @retval 0 Otherwise.
* @pre Both objects must not be any arbitrary objects except
* ::RString.
*/