/
api.c
3489 lines (2614 loc) · 88.2 KB
/
api.c
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
/*
Copyright (C) 2001-2010, Parrot Foundation.
$Id$
=head1 NAME
src/string/api.c - Parrot Strings
=head1 DESCRIPTION
This file implements the non-ICU parts of the Parrot string subsystem.
Note that C<bufstart> and C<buflen> are used by the memory subsystem. The
string functions may only use C<buflen> to determine if there is some space
left beyond C<bufused>. This is the I<only> valid usage of these two data
members, beside setting C<bufstart>/C<buflen> for external strings.
=head2 Functions
=over 4
=cut
*/
#include "parrot/parrot.h"
#include "private_cstring.h"
#include "api.str"
/* for parrot/interpreter.h */
#if PARROT_CATCH_NULL
STRING *STRINGNULL;
#endif
#define nonnull_encoding_name(s) (s) ? (s)->encoding->name : "null string"
#define ASSERT_STRING_SANITY(s) \
PARROT_ASSERT((s)->encoding); \
PARROT_ASSERT((s)->charset); \
PARROT_ASSERT(!PObj_on_free_list_TEST(s))
/* HEADERIZER HFILE: include/parrot/string_funcs.h */
/* HEADERIZER BEGIN: static */
/* Don't modify between HEADERIZER BEGIN / HEADERIZER END. Your changes will be lost. */
PARROT_INLINE
PARROT_IGNORABLE_RESULT
PARROT_CAN_RETURN_NULL
static const CHARSET * string_rep_compatible(SHIM_INTERP,
ARGIN(const STRING *a),
ARGIN(const STRING *b),
ARGOUT(const ENCODING **e))
__attribute__nonnull__(2)
__attribute__nonnull__(3)
__attribute__nonnull__(4)
FUNC_MODIFIES(*e);
PARROT_DOES_NOT_RETURN
PARROT_COLD
static void throw_illegal_escape(PARROT_INTERP)
__attribute__nonnull__(1);
#define ASSERT_ARGS_string_rep_compatible __attribute__unused__ int _ASSERT_ARGS_CHECK = (\
PARROT_ASSERT_ARG(a) \
, PARROT_ASSERT_ARG(b) \
, PARROT_ASSERT_ARG(e))
#define ASSERT_ARGS_throw_illegal_escape __attribute__unused__ int _ASSERT_ARGS_CHECK = (\
PARROT_ASSERT_ARG(interp))
/* Don't modify between HEADERIZER BEGIN / HEADERIZER END. Your changes will be lost. */
/* HEADERIZER END: static */
/*
=item C<INTVAL Parrot_str_is_null(PARROT_INTERP, const STRING *s)>
Tests if the given STRING is STRINGNULL.
=cut
*/
PARROT_EXPORT
PARROT_HOT
PARROT_PURE_FUNCTION
INTVAL
Parrot_str_is_null(SHIM_INTERP, ARGIN_NULLOK(const STRING *s))
{
ASSERT_ARGS(Parrot_str_is_null)
return STRING_IS_NULL(s);
}
/*
=back
=head2 Basic String Functions
Creation, enlargement, etc.
=over 4
=item C<void Parrot_str_init(PARROT_INTERP)>
Initializes the Parrot string subsystem.
=cut
*/
PARROT_EXPORT
void
Parrot_str_init(PARROT_INTERP)
{
ASSERT_ARGS(Parrot_str_init)
Hash *const_cstring_hash;
size_t i;
const size_t n_parrot_cstrings =
sizeof (parrot_cstrings) / sizeof (parrot_cstrings[0]);
if (interp->parent_interpreter)
interp->hash_seed = interp->parent_interpreter->hash_seed;
/* interp is initialized from zeroed memory, so this is fine */
else if (interp->hash_seed == 0) {
/* TT #64 - use an entropy source once available */
Parrot_srand(Parrot_intval_time());
interp->hash_seed = Parrot_uint_rand(0);
}
/* initialize the constant string table */
if (interp->parent_interpreter) {
interp->const_cstring_table =
interp->parent_interpreter->const_cstring_table;
interp->const_cstring_hash =
interp->parent_interpreter->const_cstring_hash;
return;
}
/* Set up the cstring cache, then load the basic encodings and charsets */
const_cstring_hash = parrot_new_cstring_hash(interp);
interp->const_cstring_hash = const_cstring_hash;
Parrot_charsets_encodings_init(interp);
#if PARROT_CATCH_NULL
/* initialize STRINGNULL, but not in the constant table */
STRINGNULL = Parrot_str_new_init(interp, NULL, 0,
PARROT_DEFAULT_ENCODING, PARROT_DEFAULT_CHARSET,
PObj_constant_FLAG);
#endif
interp->const_cstring_table =
mem_gc_allocate_n_zeroed_typed(interp, n_parrot_cstrings, STRING *);
for (i = 0; i < n_parrot_cstrings; ++i) {
DECL_CONST_CAST;
STRING * const s =
Parrot_str_new_init(interp,
parrot_cstrings[i].string,
parrot_cstrings[i].len,
PARROT_DEFAULT_ENCODING, PARROT_DEFAULT_CHARSET,
PObj_external_FLAG|PObj_constant_FLAG);
parrot_hash_put(interp, const_cstring_hash,
PARROT_const_cast(char *, parrot_cstrings[i].string), (void *)s);
interp->const_cstring_table[i] = s;
}
}
/*
=item C<void Parrot_str_finish(PARROT_INTERP)>
De-Initializes the Parrot string subsystem.
=cut
*/
PARROT_EXPORT
void
Parrot_str_finish(PARROT_INTERP)
{
ASSERT_ARGS(Parrot_str_finish)
/* all are shared between interpreters */
if (!interp->parent_interpreter) {
mem_internal_free(interp->const_cstring_table);
interp->const_cstring_table = NULL;
Parrot_charsets_encodings_deinit(interp);
parrot_hash_destroy(interp, interp->const_cstring_hash);
}
}
/*
=item C<STRING * Parrot_str_new_noinit(PARROT_INTERP,
parrot_string_representation_t representation, UINTVAL capacity)>
Creates and returns an empty Parrot string.
=cut
*/
PARROT_EXPORT
PARROT_CANNOT_RETURN_NULL
STRING *
Parrot_str_new_noinit(PARROT_INTERP,
parrot_string_representation_t representation, UINTVAL capacity)
{
ASSERT_ARGS(Parrot_str_new_noinit)
STRING * const s = Parrot_gc_new_string_header(interp, 0);
/* TODO adapt string creation functions */
if (representation != enum_stringrep_one)
Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_INVALID_CHARTYPE,
"Unsupported representation");
s->charset = PARROT_DEFAULT_CHARSET;
s->encoding = CHARSET_GET_PREFERRED_ENCODING(interp, s);
Parrot_gc_allocate_string_storage(interp, s,
(size_t)string_max_bytes(interp, s, capacity));
return s;
}
/*
=item C<static const CHARSET * string_rep_compatible(PARROT_INTERP, const STRING
*a, const STRING *b, const ENCODING **e)>
Find the "lowest" possible charset and encoding for the given string. E.g.
ascii <op> utf8 => utf8
=> ascii, B<if> C<STRING *b> has ascii chars only.
Returns NULL, if no compatible string representation can be found.
=cut
*/
PARROT_INLINE
PARROT_IGNORABLE_RESULT
PARROT_CAN_RETURN_NULL
static const CHARSET *
string_rep_compatible(SHIM_INTERP,
ARGIN(const STRING *a), ARGIN(const STRING *b), ARGOUT(const ENCODING **e))
{
ASSERT_ARGS(string_rep_compatible)
if (a->encoding == b->encoding && a->charset == b->charset) {
*e = a->encoding;
return a->charset;
}
/* a table could possibly simplify the logic */
if (a->encoding == Parrot_utf8_encoding_ptr
&& b->charset == Parrot_ascii_charset_ptr) {
if (a->strlen == a->bufused) {
*e = Parrot_fixed_8_encoding_ptr;
return b->charset;
}
*e = a->encoding;
return a->charset;
}
if (b->encoding == Parrot_utf8_encoding_ptr
&& a->charset == Parrot_ascii_charset_ptr) {
if (b->strlen == b->bufused) {
*e = Parrot_fixed_8_encoding_ptr;
return a->charset;
}
*e = b->encoding;
return b->charset;
}
if (a->encoding != b->encoding)
return NULL;
if (a->encoding != Parrot_fixed_8_encoding_ptr)
return NULL;
*e = Parrot_fixed_8_encoding_ptr;
if (a->charset == b->charset)
return a->charset;
if (b->charset == Parrot_ascii_charset_ptr)
return a->charset;
if (a->charset == Parrot_ascii_charset_ptr)
return b->charset;
if (a->charset == Parrot_binary_charset_ptr)
return a->charset;
if (b->charset == Parrot_binary_charset_ptr)
return b->charset;
return NULL;
}
/*
=item C<const CHARSET * Parrot_str_rep_compatible(PARROT_INTERP, const STRING
*a, const STRING *b, const ENCODING **e)>
Find the "lowest" possible charset and encoding for the given string. E.g.
ascii <op> utf8 => utf8
=> ascii, B<if> C<STRING *b> has ascii chars only.
Returns NULL, if no compatible string representation can be found.
=cut
*/
PARROT_EXPORT
PARROT_IGNORABLE_RESULT
PARROT_CAN_RETURN_NULL
const CHARSET *
Parrot_str_rep_compatible(PARROT_INTERP,
ARGIN(const STRING *a), ARGIN(const STRING *b), ARGOUT(const ENCODING **e))
{
ASSERT_ARGS(Parrot_str_rep_compatible)
return string_rep_compatible(interp, a, b, e);
}
/*
=item C<STRING* Parrot_str_clone(PARROT_INTERP, const STRING *s)>
Helper function to clone string.
=cut
*/
PARROT_WARN_UNUSED_RESULT
PARROT_CANNOT_RETURN_NULL
STRING*
Parrot_str_clone(PARROT_INTERP, ARGIN(const STRING *s))
{
ASSERT_ARGS(Parrot_str_clone)
const size_t alloc_size = s->bufused;
STRING * const result = Parrot_gc_new_string_header(interp, 0);
/* Copy encoding/charset/etc */
STRUCT_COPY(result, s);
/* Clear COW flag. We own buffer */
PObj_get_FLAGS(result) = PObj_is_string_FLAG
| PObj_is_COWable_FLAG;
/* Allocate new chunk of memory */
Parrot_gc_allocate_string_storage(interp, result, alloc_size);
/* and copy it over */
mem_sys_memcopy(Buffer_bufstart(result), Buffer_bufstart(s), alloc_size);
return result;
}
/*
=item C<STRING * Parrot_str_copy(PARROT_INTERP, const STRING *s)>
Creates and returns a shallow copy of the specified Parrot string.
=cut
*/
PARROT_EXPORT
PARROT_CANNOT_RETURN_NULL
PARROT_WARN_UNUSED_RESULT
STRING *
Parrot_str_copy(PARROT_INTERP, ARGIN(const STRING *s))
{
ASSERT_ARGS(Parrot_str_copy)
return Parrot_str_clone(interp, s);
}
/*
=item C<STRING * Parrot_str_concat(PARROT_INTERP, const STRING *a, const STRING
*b)>
Concatenates two Parrot strings. If necessary, converts the second
string's encoding and/or type to match those of the first string. If
either string is C<NULL>, then a copy of the non-C<NULL> string is
returned. If both strings are C<NULL>, return C<STRINGNULL>.
=cut
*/
PARROT_EXPORT
PARROT_CANNOT_RETURN_NULL
STRING *
Parrot_str_concat(PARROT_INTERP, ARGIN_NULLOK(const STRING *a),
ARGIN_NULLOK(const STRING *b))
{
ASSERT_ARGS(Parrot_str_concat)
const CHARSET *cs;
const ENCODING *enc = NULL;
STRING *dest;
UINTVAL total_length;
/* XXX should this be a CHARSET method? */
/* If B isn't real, we just bail */
const UINTVAL b_len = b ? Parrot_str_length(interp, b) : 0;
if (!b_len)
return STRING_IS_NULL(a) ? STRINGNULL : Parrot_str_clone(interp, a);
/* Is A real? */
if (STRING_IS_NULL(a) || Buffer_bufstart(a) == NULL)
return Parrot_str_clone(interp, b);
ASSERT_STRING_SANITY(a);
ASSERT_STRING_SANITY(b);
cs = string_rep_compatible(interp, a, b, &enc);
if (!cs) {
/* upgrade strings for concatenation */
cs = Parrot_unicode_charset_ptr;
if (a->encoding == Parrot_ucs4_encoding_ptr
|| b->encoding == Parrot_ucs4_encoding_ptr)
enc = Parrot_ucs4_encoding_ptr;
else if (a->encoding == Parrot_utf16_encoding_ptr
|| b->encoding == Parrot_utf16_encoding_ptr
|| a->encoding == Parrot_ucs2_encoding_ptr
|| b->encoding == Parrot_ucs2_encoding_ptr)
enc = Parrot_utf16_encoding_ptr;
else
enc = Parrot_utf8_encoding_ptr;
a = Parrot_unicode_charset_ptr->to_charset(interp, a);
b = Parrot_unicode_charset_ptr->to_charset(interp, b);
if (a->encoding != enc)
a = enc->to_encoding(interp, a);
if (b->encoding != enc)
b = enc->to_encoding(interp, b);
}
/* calc usable and total bytes */
total_length = a->bufused + b->bufused;
dest = Parrot_str_new_noinit(interp, enum_stringrep_one, total_length);
PARROT_ASSERT(enc);
PARROT_ASSERT(cs);
dest->encoding = enc;
dest->charset = cs;
/* Copy A first */
mem_sys_memcopy(Buffer_bufstart(dest), Buffer_bufstart(a), a->bufused);
/* Tack B on the end of A */
mem_sys_memcopy((void *)((ptrcast_t)Buffer_bufstart(dest) + a->bufused),
Buffer_bufstart(b), b->bufused);
dest->bufused = a->bufused + b->bufused;
dest->strlen = a->strlen + b_len;
return dest;
}
/*
=item C<STRING * Parrot_str_new(PARROT_INTERP, const char *buffer, const UINTVAL
len)>
Makes a Parrot string from a specified C string.
=cut
*/
PARROT_EXPORT
PARROT_WARN_UNUSED_RESULT
PARROT_MALLOC
PARROT_CANNOT_RETURN_NULL
STRING *
Parrot_str_new(PARROT_INTERP, ARGIN_NULLOK(const char *buffer), const UINTVAL len)
{
ASSERT_ARGS(Parrot_str_new)
/* Force an 8-bit encoding at some point? */
const UINTVAL buff_length = (len > 0) ? len : buffer ? strlen(buffer) : 0;
return Parrot_str_new_init(interp, buffer, buff_length,
PARROT_DEFAULT_ENCODING, PARROT_DEFAULT_CHARSET, 0);
}
/*
=item C<STRING * Parrot_str_new_from_buffer(PARROT_INTERP, Buffer *buffer, const
UINTVAL len)>
Makes a Parrot string from a Buffer.
The Buffer is nulled afterwards, as only one PObj can point at a given string
pool object.
=cut
*/
PARROT_EXPORT
PARROT_WARN_UNUSED_RESULT
PARROT_MALLOC
PARROT_CANNOT_RETURN_NULL
STRING *
Parrot_str_new_from_buffer(PARROT_INTERP, ARGMOD(Buffer *buffer), const UINTVAL len)
{
ASSERT_ARGS(Parrot_str_new_from_buffer)
STRING * const result = Parrot_gc_new_string_header(interp, 0);
Buffer_bufstart(result) = Buffer_bufstart(buffer);
Buffer_buflen(result) = Buffer_buflen(buffer);
result->bufused = len;
result->strlen = len;
result->encoding = Parrot_fixed_8_encoding_ptr;
result->charset = Parrot_binary_charset_ptr;
Buffer_buflen(buffer) = 0;
Buffer_bufstart(buffer) = NULL;
return result;
}
/*
=item C<const char* string_primary_encoding_for_representation(PARROT_INTERP,
parrot_string_representation_t representation)>
Returns the primary encoding for the specified representation.
This is needed for packfile unpacking, unless we just always use UTF-8 or BOCU.
=cut
*/
PARROT_EXPORT
PARROT_CANNOT_RETURN_NULL
PARROT_OBSERVER
const char*
string_primary_encoding_for_representation(PARROT_INTERP,
parrot_string_representation_t representation)
{
ASSERT_ARGS(string_primary_encoding_for_representation)
if (representation == enum_stringrep_one)
return "ascii";
Parrot_ex_throw_from_c_args(interp, NULL,
EXCEPTION_INVALID_STRING_REPRESENTATION,
"string_primary_encoding_for_representation: "
"invalid string representation");
}
/*
=item C<STRING * Parrot_str_new_constant(PARROT_INTERP, const char *buffer)>
Creates and returns a constant Parrot string.
=cut
*/
PARROT_EXPORT
PARROT_WARN_UNUSED_RESULT
PARROT_CANNOT_RETURN_NULL
STRING *
Parrot_str_new_constant(PARROT_INTERP, ARGIN(const char *buffer))
{
ASSERT_ARGS(Parrot_str_new_constant)
DECL_CONST_CAST;
Hash * const cstring_cache = (Hash *)interp->const_cstring_hash;
STRING *s = (STRING *)parrot_hash_get(interp,
cstring_cache, buffer);
if (s)
return s;
s = Parrot_str_new_init(interp, buffer, strlen(buffer),
PARROT_DEFAULT_ENCODING, PARROT_DEFAULT_CHARSET,
PObj_external_FLAG|PObj_constant_FLAG);
parrot_hash_put(interp, cstring_cache,
PARROT_const_cast(char *, buffer), (void *)s);
return s;
}
/*
=item C<STRING * string_make(PARROT_INTERP, const char *buffer, UINTVAL len,
const char *charset_name, UINTVAL flags)>
Creates and returns a new Parrot string using C<len> bytes of string data read
from C<buffer>.
The value of C<charset_name> specifies the string's representation.
The currently recognised values are:
'iso-8859-1'
'ascii'
'binary'
'unicode'
The encoding is implicitly guessed; C<unicode> implies the C<utf-8> encoding,
and the other three assume C<fixed-8> encoding.
If C<charset> is unspecified, the default charset 'ascii' will be used.
The value of C<flags> is optionally one or more C<PObj_*> flags C<OR>-ed
together.
=cut
*/
PARROT_EXPORT
PARROT_WARN_UNUSED_RESULT
PARROT_CANNOT_RETURN_NULL
STRING *
string_make(PARROT_INTERP, ARGIN_NULLOK(const char *buffer),
UINTVAL len, ARGIN_NULLOK(const char *charset_name), UINTVAL flags)
{
ASSERT_ARGS(string_make)
const CHARSET *charset;
if (charset_name) {
charset = Parrot_find_charset(interp, charset_name);
if (!charset)
Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_UNIMPLEMENTED,
"Can't make '%s' charset strings", charset_name);
}
else
charset = Parrot_get_charset(interp, 0);
return Parrot_str_new_init(interp, buffer, len,
charset->preferred_encoding, charset, flags);
}
/*
=item C<STRING * string_make_from_charset(PARROT_INTERP, const char *buffer,
UINTVAL len, INTVAL charset_nr, UINTVAL flags)>
Creates and returns a new Parrot string using C<len> bytes of string data read
from C<buffer>.
The value of C<charset_name> specifies the string's representation. It must be
a valid charset identifier.
'iso-8859-1'
'ascii'
'binary'
'unicode'
The encoding is implicitly guessed; C<unicode> implies the C<utf-8> encoding,
and the other three assume C<fixed-8> encoding.
The value of C<flags> is optionally one or more C<PObj_*> flags C<OR>-ed
together.
=cut
*/
PARROT_EXPORT
PARROT_WARN_UNUSED_RESULT
PARROT_CANNOT_RETURN_NULL
STRING *
string_make_from_charset(PARROT_INTERP, ARGIN_NULLOK(const char *buffer),
UINTVAL len, INTVAL charset_nr, UINTVAL flags)
{
ASSERT_ARGS(string_make_from_charset)
const CHARSET *charset = Parrot_get_charset(interp, charset_nr);
if (!charset)
Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_UNIMPLEMENTED,
"Invalid charset number '%d' specified", charset_nr);
return Parrot_str_new_init(interp, buffer, len,
charset->preferred_encoding, charset, flags);
}
/*
=item C<STRING * Parrot_str_new_init(PARROT_INTERP, const char *buffer, UINTVAL
len, const ENCODING *encoding, const CHARSET *charset, UINTVAL flags)>
Given a buffer, its length, an encoding, a character set, and STRING flags,
creates and returns a new string. Don't call this directly.
=cut
*/
PARROT_EXPORT
PARROT_WARN_UNUSED_RESULT
PARROT_CANNOT_RETURN_NULL
STRING *
Parrot_str_new_init(PARROT_INTERP, ARGIN_NULLOK(const char *buffer), UINTVAL len,
ARGIN(const ENCODING *encoding), ARGIN(const CHARSET *charset), UINTVAL flags)
{
ASSERT_ARGS(Parrot_str_new_init)
DECL_CONST_CAST;
STRING * const s = Parrot_gc_new_string_header(interp, flags);
s->encoding = encoding;
s->charset = charset;
if (flags & PObj_external_FLAG) {
/*
* fast path for external (constant) strings - don't allocate
* and copy data
*/
/* The following cast discards the 'const'. That raises
a warning with gcc, but is ok since the caller indicated
it was safe by setting PObj_external_FLAG.
(The cast is necessary to pacify TenDRA's tcc.)
*/
Buffer_bufstart(s) = PARROT_const_cast(char *, buffer);
Buffer_buflen(s) = s->bufused = len;
if (encoding == Parrot_fixed_8_encoding_ptr)
s->strlen = len;
else
s->strlen = CHARSET_CODEPOINTS(interp, s);
return s;
}
Parrot_gc_allocate_string_storage(interp, s, len);
if (buffer) {
mem_sys_memcopy(Buffer_bufstart(s), buffer, len);
s->bufused = len;
if (encoding == Parrot_fixed_8_encoding_ptr)
s->strlen = len;
else
s->strlen = CHARSET_CODEPOINTS(interp, s);
}
else
s->strlen = s->bufused = 0;
return s;
}
/*
=back
=head2 Ordinary user-visible string operations
=over 4
=item C<UINTVAL Parrot_str_byte_length(PARROT_INTERP, const STRING *s)>
Returns the number of characters in the specified Parrot string.
=cut
*/
PARROT_EXPORT
PARROT_PURE_FUNCTION
UINTVAL
Parrot_str_byte_length(SHIM_INTERP, ARGIN_NULLOK(const STRING *s))
{
ASSERT_ARGS(Parrot_str_byte_length)
return STRING_IS_NULL(s) ? 0 : s->bufused;
}
/*
=item C<INTVAL Parrot_str_indexed(PARROT_INTERP, const STRING *s, UINTVAL idx)>
Returns the character (or glyph, depending upon the string's encoding). This
abstracts the process of finding the Nth character in a (possibly Unicode or
JIS-encoded) string, the idea being that once the encoding functions are
fleshed out, this function can do the right thing.
Note that this is not range-checked.
=cut
*/
PARROT_EXPORT
PARROT_WARN_UNUSED_RESULT
INTVAL
Parrot_str_indexed(PARROT_INTERP, ARGIN(const STRING *s), UINTVAL idx)
{
ASSERT_ARGS(Parrot_str_indexed)
ASSERT_STRING_SANITY(s);
return (INTVAL)CHARSET_GET_CODEPOINT(interp, s, idx);
}
/*
=item C<INTVAL Parrot_str_find_index(PARROT_INTERP, const STRING *s, const
STRING *s2, INTVAL start)>
Returns the character position of the second Parrot string in the first at or
after C<start>. The return value is a (0 based) offset in characters, not
bytes. If second string is not found in the first string, returns -1.
=cut
*/
PARROT_EXPORT
PARROT_WARN_UNUSED_RESULT
INTVAL
Parrot_str_find_index(PARROT_INTERP, ARGIN(const STRING *s),
ARGIN(const STRING *s2), INTVAL start)
{
ASSERT_ARGS(Parrot_str_find_index)
UINTVAL len;
if (start < 0)
return -1;
len = Parrot_str_length(interp, s);
if (!len)
return -1;
if (start >= (INTVAL)len)
return -1;
/* Short circuit when s is the same as s2 */
if (s == s2)
return start == 0 ? 0 : -1;
if (!Parrot_str_length(interp, s2))
return -1;
else {
DECL_CONST_CAST;
STRING *src = PARROT_const_cast(STRING *, s);
STRING *search = PARROT_const_cast(STRING *, s2);
return CHARSET_INDEX(interp, src, search, (UINTVAL)start);
}
}
/*
=item C<INTVAL string_ord(PARROT_INTERP, const STRING *s, INTVAL idx)>
Returns the codepoint at a given index into a string. Negative indexes are
treated as counting from the end of the string.
=cut
*/
PARROT_EXPORT
PARROT_WARN_UNUSED_RESULT
INTVAL
string_ord(PARROT_INTERP, ARGIN(const STRING *s), INTVAL idx)
{
ASSERT_ARGS(string_ord)
UINTVAL len;
UINTVAL true_index = (UINTVAL)idx;
if (STRING_IS_NULL(s))
Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_ORD_OUT_OF_STRING,
"Cannot get character of NULL string");
len = Parrot_str_length(interp, s);
if (len == 0)
Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_ORD_OUT_OF_STRING,
"Cannot get character of empty string");
if (idx < 0) {
if ((INTVAL)(idx + len) < 0)
Parrot_ex_throw_from_c_args(interp, NULL,
EXCEPTION_ORD_OUT_OF_STRING,
"Cannot get character before beginning of string");
true_index = (UINTVAL)(len + idx);
}
if (true_index > (len - 1))
Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_ORD_OUT_OF_STRING,
"Cannot get character past end of string");
return Parrot_str_indexed(interp, s, true_index);
}
/*
=item C<STRING * string_chr(PARROT_INTERP, UINTVAL character)>
Returns a single-character Parrot string.
=cut
*/
PARROT_EXPORT
PARROT_CANNOT_RETURN_NULL
PARROT_WARN_UNUSED_RESULT
STRING *
string_chr(PARROT_INTERP, UINTVAL character)
{
ASSERT_ARGS(string_chr)
if (character > 0xff)
return Parrot_unicode_charset_ptr->string_from_codepoint(interp,
character);
else if (character > 0x7f)
return Parrot_iso_8859_1_charset_ptr->string_from_codepoint(interp,
character);
else
return Parrot_ascii_charset_ptr->string_from_codepoint(interp,
character);
}
/*
=back
=head2 Vtable Dispatch Functions
=over 4
=item C<INTVAL Parrot_str_length(PARROT_INTERP, const STRING *s)>
Returns the number of characters in the specified Parrot string.
=cut
*/
PARROT_EXPORT
PARROT_PURE_FUNCTION
PARROT_WARN_UNUSED_RESULT
INTVAL
Parrot_str_length(SHIM_INTERP, ARGIN_NULLOK(const STRING *s))
{
ASSERT_ARGS(Parrot_str_length)
return STRING_IS_NULL(s) ? 0 : s->strlen;
}
/*
=item C<INTVAL string_max_bytes(PARROT_INTERP, const STRING *s, UINTVAL nchars)>
Returns the number of bytes required to safely contain the specified number
of characters in the specified Parrot string's representation.
=cut
*/
PARROT_EXPORT
PARROT_WARN_UNUSED_RESULT
PARROT_PURE_FUNCTION
INTVAL
string_max_bytes(SHIM_INTERP, ARGIN(const STRING *s), UINTVAL nchars)
{
ASSERT_ARGS(string_max_bytes)
PARROT_ASSERT(s->encoding);
return ENCODING_MAX_BYTES_PER_CODEPOINT(interp, s) * nchars;
}
/*
=item C<STRING * Parrot_str_repeat(PARROT_INTERP, const STRING *s, UINTVAL num)>