forked from pikelang/Pike
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuiltin_functions.c
10870 lines (9752 loc) · 286 KB
/
builtin_functions.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
/*
|| This file is part of Pike. For copyright information see COPYRIGHT.
|| Pike is distributed under GPL, LGPL and MPL. See the file COPYING
|| for more information.
*/
#include "global.h"
#include "interpret.h"
#include "svalue.h"
#include "pike_macros.h"
#include "object.h"
#include "program.h"
#include "array.h"
#include "pike_error.h"
#include "constants.h"
#include "mapping.h"
#include "stralloc.h"
#include "multiset.h"
#include "pike_types.h"
#include "pike_rusage.h"
#include "operators.h"
#include "fsort.h"
#include "callback.h"
#include "gc.h"
#include "backend.h"
#include "main.h"
#include "pike_memory.h"
#include "threads.h"
#include "time_stuff.h"
#include "version.h"
#include "encode.h"
#include <ctype.h>
#include "module_support.h"
#include "opcodes.h"
#include "cyclic.h"
#include "signal_handler.h"
#include "builtin_functions.h"
#include "bignum.h"
#include "peep.h"
#include "docode.h"
#include "lex.h"
#include "pike_float.h"
#include "pike_compiler.h"
#include "port.h"
#include "siphash24.h"
#include <errno.h>
#ifdef HAVE_POLL
#ifdef HAVE_POLL_H
#include <poll.h>
#elif defined(HAVE_SYS_POLL_H)
#include <sys/poll.h>
#endif /* HAVE_POLL_H || HAVE_SYS_POLL_H */
#endif /* HAVE_POLL */
#ifdef HAVE_CRYPT_H
#include <crypt.h>
#endif
/* #define DIFF_DEBUG */
/* #define ENABLE_DYN_DIFF */
/*! @decl int equal(mixed a, mixed b)
*!
*! This function checks if the values @[a] and @[b] are equivalent.
*!
*! @returns
*! If either of the values is an object the (normalized) result
*! of calling @[lfun::_equal()] will be returned.
*!
*! Returns @expr{1@} if both values are false (zero, destructed objects,
*! prototype functions, etc).
*!
*! Returns @expr{0@} (zero) if the values have different types.
*!
*! Otherwise depending on the type of the values:
*! @mixed
*! @type int
*! @type float
*! @type string
*! @type program
*! Returns the same as @expr{a == b@}.
*! @type array
*! @type mapping
*! @type multiset
*! @type object
*! The contents of @[a] and @[b] are checked recursively, and
*! if all their contents are @[equal] and in the same place,
*! they are considered equal.
*!
*! Note that for objects this case is only reached if neither
*! @[a] nor @[b] implements @[lfun::_equal()].
*! @type type
*! Returns @expr{(a <= b) && (b <= a)@}.
*! @endmixed
*!
*! @seealso
*! @[copy_value()], @[`==()]
*/
PMOD_EXPORT void f_equal(INT32 args)
{
int i;
if(args != 2)
SIMPLE_WRONG_NUM_ARGS_ERROR("equal", 2);
i=is_equal(Pike_sp-2,Pike_sp-1);
pop_n_elems(args);
push_int(i);
}
/*! @decl array aggregate(mixed ... elements)
*!
*! Construct an array with the arguments as indices.
*!
*! This function could be written in Pike as:
*! @code
*! array aggregate(mixed ... elems) { return elems; }
*! @endcode
*!
*! @note
*! Arrays are dynamically allocated there is no need to declare them
*! like @expr{int a[10]=allocate(10);@} (and it isn't possible either) like
*! in C, just @expr{array(int) a=allocate(10);@} will do.
*!
*! @seealso
*! @[sizeof()], @[arrayp()], @[allocate()]
*/
PMOD_EXPORT void debug_f_aggregate(INT32 args)
{
struct array *a;
a=aggregate_array(args);
push_array(a); /* beware, macro */
}
static node *optimize_f_aggregate(node *n)
{
/* Split long argument lists into multiple function calls.
*
* aggregate(...) ==> `+(aggregate(...arg32), aggregate(arg33...), ...)
*
* Also removes splices.
*
* Note: We assume that the argument list is in left-recursive form.
*/
node *args = CDR(n);
node *new_args = NULL;
node *add_args = NULL;
int count;
if (!args) return NULL;
args->parent = NULL;
for (count = 0; args->token == F_ARG_LIST; args = CAR(args)) {
if (CDR(args) && CDR(args)->token == F_PUSH_ARRAY) {
/* Splices have a weight of 16. */
count += 16;
} else {
count++;
}
if (!CAR(args)) break;
CAR(args)->parent = args;
}
if (args->token == F_PUSH_ARRAY) {
/* Last argument is a splice */
count += 16;
} else if (args->token != F_ARG_LIST) {
count++;
}
/* Ignore cases with 32 or less arguments. */
if (count <= 32) {
CDR(n)->parent = n;
return NULL;
}
/*
* Perform the actual rewrite.
*
* Start with the last arg, and work towards the first.
*/
count = 0;
if (args->token != F_ARG_LIST) {
if (args->token == F_PUSH_ARRAY) {
/* Splice operator. */
add_args = copy_node(CAR(args));
} else {
new_args = copy_node(args);
count = 1;
}
args = args->parent;
}
for(; args; args = args->parent) {
if (!CDR(args)) continue;
if (CDR(args)->token == F_PUSH_ARRAY) {
if (count) {
add_args = mknode(F_ARG_LIST, add_args,
mkapplynode(copy_node(CAR(n)), new_args));
new_args = NULL;
count = 0;
}
add_args = mknode(F_ARG_LIST, add_args, copy_node(CADR(args)));
} else {
new_args = mknode(F_ARG_LIST, new_args, copy_node(CDR(args)));
count++;
if (count > 31) {
add_args = mknode(F_ARG_LIST, add_args,
mkapplynode(copy_node(CAR(n)), new_args));
new_args = NULL;
count = 0;
}
}
}
if (count) {
add_args = mknode(F_ARG_LIST, add_args,
mkapplynode(copy_node(CAR(n)), new_args));
new_args = NULL;
count = 0;
}
CDR(n)->parent = n;
return mkefuncallnode("`+", add_args);
}
#define MK_HASHMEM(NAME, TYPE) ATTRIBUTE((const)) \
static inline size_t NAME(const TYPE *str, ptrdiff_t len, ptrdiff_t maxn) \
{ \
size_t ret,c; \
\
ret = len*92873743; \
\
len = MINIMUM(maxn,len); \
for(; len>=0; len--) \
{ \
c=str++[0]; \
ret ^= ( ret << 4 ) + c ; \
ret &= 0x7fffffff; \
} \
return ret; \
}
MK_HASHMEM(simple_hashmem, unsigned char)
MK_HASHMEM(simple_hashmem1, p_wchar1)
MK_HASHMEM(simple_hashmem2, p_wchar2)
/*! @decl int hash_7_4(string s)
*! @decl int hash_7_4(string s, int max)
*!
*! Return an integer derived from the string @[s]. The same string
*! always hashes to the same value, also between processes.
*!
*! If @[max] is given, the result will be >= 0 and < @[max],
*! otherwise the result will be >= 0 and <= 0x7fffffff.
*!
*! @note
*! This function is provided for backward compatibility with
*! code written for Pike up and including version 7.4.
*!
*! This function is byte-order dependant for wide strings.
*!
*! @seealso
*! @[hash_7_6()], @[hash_7_0]
*/
static void f_hash_7_4(INT32 args)
{
size_t i = 0;
struct pike_string *s = Pike_sp[-args].u.string;
if(!args)
SIMPLE_WRONG_NUM_ARGS_ERROR("7.4::hash",1);
if(TYPEOF(Pike_sp[-args]) != T_STRING)
SIMPLE_ARG_TYPE_ERROR("7.4::hash", 1, "string");
i = simple_hashmem((unsigned char *)s->str, s->len<<s->size_shift,
100<<s->size_shift);
if(args > 1)
{
if(TYPEOF(Pike_sp[1-args]) != T_INT)
SIMPLE_ARG_TYPE_ERROR("7.4::hash",2,"int");
if(!Pike_sp[1-args].u.integer)
PIKE_ERROR("7.4::hash", "Modulo by zero.\n", Pike_sp, args);
i%=(unsigned INT32)Pike_sp[1-args].u.integer;
}
pop_n_elems(args);
push_int64(i);
}
ATTRIBUTE((const)) static inline size_t hashstr(const unsigned char *str, ptrdiff_t maxn)
{
size_t ret,c;
if(!(ret=str++[0]))
return ret;
for(; maxn>=0; maxn--)
{
c=str++[0];
if(!c) break;
ret ^= ( ret << 4 ) + c ;
ret &= 0x7fffffff;
}
return ret;
}
/*! @decl int hash_7_0(string s)
*! @decl int hash_7_0(string s, int max)
*!
*! Return an integer derived from the string @[s]. The same string
*! always hashes to the same value, also between processes.
*!
*! If @[max] is given, the result will be >= 0 and < @[max],
*! otherwise the result will be >= 0 and <= 0x7fffffff.
*!
*! @note
*! This function is provided for backward compatibility with
*! code written for Pike up and including version 7.0.
*!
*! This function is not NUL-safe, and is byte-order dependant.
*!
*! @seealso
*! @[hash()], @[hash_7_4]
*/
static void f_hash_7_0( INT32 args )
{
struct pike_string *s = Pike_sp[-args].u.string;
unsigned int i;
if(!args)
SIMPLE_WRONG_NUM_ARGS_ERROR("7.0::hash",1);
if(TYPEOF(Pike_sp[-args]) != T_STRING)
SIMPLE_ARG_TYPE_ERROR("7.0::hash", 1, "string");
if( s->size_shift )
{
f_hash_7_4( args );
return;
}
i = (unsigned int)hashstr( (unsigned char *)s->str,
MINIMUM(100,s->len));
if(args > 1)
{
if(TYPEOF(Pike_sp[1-args]) != T_INT)
SIMPLE_ARG_TYPE_ERROR("7.0::hash",2,"int");
if(!Pike_sp[1-args].u.integer)
PIKE_ERROR("7.0::hash", "Modulo by zero.\n", Pike_sp, args);
i%=(unsigned INT32)Pike_sp[1-args].u.integer;
}
pop_n_elems(args);
push_int( i );
}
/*! @decl int hash_8_0(string s)
*! @decl int hash_8_0(string s, int max)
*!
*! Return an integer derived from the string @[s]. The same string
*! always hashes to the same value, also between processes,
*! architectures, and Pike versions (see compatibility notes below,
*! though).
*!
*! If @[max] is given, the result will be >= 0 and < @[max],
*! otherwise the result will be >= 0 and <= 0x7fffffff.
*!
*! @deprecated
*!
*! Use @[hash_value()] for in-process hashing (eg, for implementing
*! @[lfun::_hash()]) or one of the cryptographic hash functions.
*!
*! @note
*! This function is really bad at hashing strings. Similar string
*! often return similar hash values.
*!
*! It is especially bad for url:s, paths and similarly formatted
*! strings.
*!
*! @note
*! The hash algorithm was changed in Pike 7.5. If you want a hash
*! that is compatible with Pike 7.4 and earlier, use @[hash_7_4()].
*! The difference only affects wide strings.
*!
*! The hash algorithm was also changed in Pike 7.1. If you want a hash
*! that is compatible with Pike 7.0 and earlier, use @[hash_7_0()].
*!
*! @note
*! This hash function differs from the one provided by @[hash_value()],
*! in that @[hash_value()] returns a process specific value.
*!
*! @seealso
*! @[hash()], @[hash_7_0()], @[hash_7_4()], @[hash_value]
*/
static void f_hash_8_0(INT32 args)
{
size_t i = 0;
struct pike_string *s;
if(!args)
SIMPLE_WRONG_NUM_ARGS_ERROR("hash",1);
if(TYPEOF(Pike_sp[-args]) != T_STRING)
SIMPLE_ARG_TYPE_ERROR("hash", 1, "string");
s = Pike_sp[-args].u.string;
switch(s->size_shift) {
case 0:
i = simple_hashmem(STR0(s), s->len, 100);
break;
case 1:
i = simple_hashmem1(STR1(s), s->len, 100);
break;
case 2:
i = simple_hashmem2(STR2(s), s->len, 100);
break;
}
if(args > 1)
{
if(TYPEOF(Pike_sp[1-args]) != T_INT)
SIMPLE_ARG_TYPE_ERROR("hash_8_0",2,"int");
if(Pike_sp[1-args].u.integer <= 0)
PIKE_ERROR("hash_8_0", "Modulo < 1.\n", Pike_sp, args);
i%=(unsigned INT32)Pike_sp[1-args].u.integer;
}
pop_n_elems(args);
push_int64(i);
}
/*! @decl int hash(string s)
*! @decl int hash(string s, int max)
*!
*! Return an integer derived from the string @[s]. The same string
*! always hashes to the same value, also between processes,
*! architectures, and Pike versions (see compatibility notes below,
*! though).
*!
*! If @[max] is given, the result will be >= 0 and < @[max],
*! otherwise the result will be >= 0 and <= 0x7fffffff.
*!
*! @note
*! The hash algorithm was changed in Pike 8.1. If you want a hash
*! that is compatible with Pike 8.0 and earlier, use @[hash_8_0()].
*!
*! The hash algorithm was also changed in Pike 7.5. If you want a hash
*! that is compatible with Pike 7.4 and earlier, use @[hash_7_4()].
*! The difference with regards to @[hash_8_0()] only affects wide strings.
*!
*! The hash algorithm was also changed in Pike 7.1. If you want a hash
*! that is compatible with Pike 7.0 and earlier, use @[hash_7_0()].
*!
*! @note
*! This hash function differs from the one provided by @[hash_value()],
*! in that @[hash_value()] returns a process specific value.
*!
*! @seealso
*! @[hash_7_0()], @[hash_7_4()], @[hash_8_0()], @[hash_value]
*/
PMOD_EXPORT void f_hash( INT32 args )
{
size_t res;
if( TYPEOF(Pike_sp[-args]) != PIKE_T_STRING )
PIKE_ERROR("hash","Argument is not a string\n",Pike_sp,args);
res = pike_string_siphash24(Pike_sp[-args].u.string, 0) & 0x7fffffff;
if( args > 1 ) {
if(TYPEOF(Pike_sp[1-args]) != T_INT)
SIMPLE_ARG_TYPE_ERROR("hash",2,"int");
if(Pike_sp[1-args].u.integer <= 0)
PIKE_ERROR("hash", "Modulo < 1.\n", Pike_sp, args);
res %= Pike_sp[1-args].u.integer;
}
pop_n_elems(args);
push_int(res);
}
/*! @decl int hash_value (mixed value)
*!
*! Return a hash value for the argument. It's an integer in the
*! native integer range.
*!
*! The hash will be the same for the same value in the running
*! process only (the memory address is typically used as the basis
*! for the hash value).
*!
*! If the value is an object with an @[lfun::__hash], that function
*! is called and its result returned.
*!
*! @note
*! This is the hashing method used by mappings.
*!
*! @seealso
*! @[lfun::__hash()]
*/
void f_hash_value(INT32 args)
{
size_t h;
if(!args)
SIMPLE_WRONG_NUM_ARGS_ERROR("hash_value",1);
h = hash_svalue (Pike_sp - args);
pop_n_elems (args);
/* NB: We assume that INT_TYPE has the same width as size_t. */
push_int (h);
}
/*! @decl mixed copy_value(mixed value)
*!
*! Copy a value recursively.
*!
*! If the result value is changed destructively (only possible for
*! multisets, arrays and mappings) the copied value will not be changed.
*!
*! The resulting value will always be equal to the copied (as tested with
*! the function @[equal()]), but they may not the the same value (as tested
*! with @[`==()]).
*!
*! @seealso
*! @[equal()]
*/
PMOD_EXPORT void f_copy_value(INT32 args)
{
if(!args)
SIMPLE_WRONG_NUM_ARGS_ERROR("copy_value",1);
pop_n_elems(args-1);
push_undefined(); /* Placeholder */
copy_svalues_recursively_no_free(Pike_sp-1,Pike_sp-2,1,0);
free_svalue(Pike_sp-2);
move_svalue (Pike_sp - 2, Pike_sp - 1);
Pike_sp--;
dmalloc_touch_svalue(Pike_sp-1);
}
struct case_info {
INT32 low; /* low end of range. */
INT16 mode;
INT16 data;
};
#define CIM_NONE 0 /* Case-less */
#define CIM_UPPERDELTA 1 /* Upper-case, delta to lower-case in data */
#define CIM_LOWERDELTA 2 /* Lower-case, -delta to upper-case in data */
#define CIM_CASEBIT 3 /* Some case, case mask in data */
#define CIM_CASEBITOFF 4 /* Same as above, but also offset by data */
#define CIM_LONGUPPERDELTA 5 /* Upper-case, delta + 0x7fff. */
#define CIM_LONGLOWERDELTA 6 /* Lower-case, delta + 0x7fff. */
static const struct case_info case_info[] = {
#include "case_info.h"
{ 0x7fffffff, CIM_NONE, 0x0000, }, /* End sentinel. */
};
static struct case_info *find_ci(INT32 c)
{
static struct case_info *cache = NULL;
struct case_info *ci = cache;
int lo = 0;
int hi = NELEM(case_info);
if ((c < 0) || (c > 0xeffff)) {
/* Negative, or plane 15 and above. */
return NULL;
}
if ((ci) && (ci[0].low <= c) && (ci[1].low > c)) {
return ci;
}
while (lo != hi-1) {
int mid = (lo + hi)/2;
if (case_info[mid].low < c) {
lo = mid;
} else if (case_info[mid].low == c) {
lo = mid;
break;
} else {
hi = mid;
}
}
return(cache = (struct case_info *)case_info + lo);
}
static struct case_info *find_ci_shift0(INT32 c)
{
static struct case_info *cache = NULL;
struct case_info *ci = cache;
int lo = 0;
int hi = CASE_INFO_SHIFT0_HIGH;
if ((c < 0) || (c > 0xefffff)) {
/* Negative, or plane 15 and above. */
return NULL;
}
if ((ci) && (ci[0].low <= c) && (ci[1].low > c)) {
return ci;
}
while (lo != hi-1) {
int mid = (lo + hi)>>1;
if (case_info[mid].low < c) {
lo = mid;
} else if (case_info[mid].low == c) {
lo = mid;
break;
} else {
hi = mid;
}
}
return(cache = (struct case_info *)case_info + lo);
}
#define DO_LOWER_CASE(C) do {\
INT32 c = C; \
if(c<0xb5){if(c >= 'A' && c <= 'Z' ) C=c+0x20; } \
/*else if(c==0xa77d) C=0x1d79;*/ else { \
struct case_info *ci = find_ci(c); \
if (ci) { \
switch(ci->mode) { \
case CIM_NONE: case CIM_LOWERDELTA: case CIM_LONGLOWERDELTA: break; \
case CIM_UPPERDELTA: C = c + ci->data; break; \
case CIM_CASEBIT: C = c | ci->data; break; \
case CIM_CASEBITOFF: C = ((c - ci->data) | ci->data) + ci->data; break; \
case CIM_LONGUPPERDELTA: \
C = c + ci->data + ( ci->data>0 ? 0x7fff : -0x8000 ); break; \
DO_IF_DEBUG( default: Pike_fatal("lower_case(): Unknown case_info mode: %d\n", ci->mode); ) \
} \
}} \
} while(0)
#define DO_LOWER_CASE_SHIFT0(C) do {\
INT32 c = C; \
if(c<0xb5){if(c >= 'A' && c <= 'Z' ) C=c+0x20;}else {\
struct case_info *ci = find_ci_shift0(c); \
if (ci) { \
switch(ci->mode) { \
case CIM_NONE: case CIM_LOWERDELTA: break; \
case CIM_UPPERDELTA: C = c + ci->data; break; \
case CIM_CASEBIT: C = c | ci->data; break; \
case CIM_CASEBITOFF: C = ((c - ci->data) | ci->data) + ci->data; break; \
DO_IF_DEBUG( default: Pike_fatal("lower_case(): Unknown case_info mode: %d\n", ci->mode); ) \
} \
}} \
} while(0)
#define DO_UPPER_CASE(C) do {\
INT32 c = C; \
if(c<0xb5){if(c >= 'a' && c <= 'z' ) C=c-0x20; } \
/*else if(c==0x1d79) C=0xa77d;*/ else {\
struct case_info *ci = find_ci(c); \
if (ci) { \
switch(ci->mode) { \
case CIM_NONE: case CIM_UPPERDELTA: case CIM_LONGUPPERDELTA: break; \
case CIM_LOWERDELTA: C = c - ci->data; break; \
case CIM_CASEBIT: C = c & ~ci->data; break; \
case CIM_CASEBITOFF: C = ((c - ci->data)& ~ci->data) + ci->data; break; \
case CIM_LONGLOWERDELTA: \
C = c - ci->data - ( ci->data>0 ? 0x7fff : -0x8000 ); break; \
DO_IF_DEBUG( default: Pike_fatal("upper_case(): Unknown case_info mode: %d\n", ci->mode); ) \
} \
}} \
} while(0)
#define DO_UPPER_CASE_SHIFT0(C) do {\
INT32 c = C; \
if(c<0xb5){if(c >= 'a' && c <= 'z' ) C=c-0x20;}else {\
struct case_info *ci = find_ci_shift0(c); \
if (ci) { \
switch(ci->mode) { \
case CIM_NONE: case CIM_UPPERDELTA: break; \
case CIM_LOWERDELTA: C = c - ci->data; break; \
case CIM_CASEBIT: C = c & ~ci->data; break; \
case CIM_CASEBITOFF: C = ((c - ci->data)& ~ci->data) + ci->data; break; \
DO_IF_DEBUG( default: Pike_fatal("lower_case(): Unknown case_info mode: %d\n", ci->mode); ) \
} \
}} \
} while(0)
/*! @decl string lower_case(string s)
*! @decl int lower_case(int c)
*!
*! Convert a string or character to lower case.
*!
*! @returns
*! Returns a copy of the string @[s] with all upper case characters
*! converted to lower case, or the character @[c] converted to lower
*! case.
*!
*! @note
*! Assumes the string or character to be coded according to
*! ISO-10646 (aka Unicode). If they are not, @[Charset.decoder] can
*! do the initial conversion for you.
*!
*! @note
*! Prior to Pike 7.5 this function only accepted strings.
*!
*! @seealso
*! @[upper_case()], @[Charset.decoder]
*/
PMOD_EXPORT void f_lower_case(INT32 args)
{
ptrdiff_t i;
struct pike_string *orig;
struct pike_string *ret;
check_all_args("lower_case", args, BIT_STRING|BIT_INT, 0);
if (TYPEOF(Pike_sp[-args]) == T_INT) {
/* NOTE: Performs the case change in place. */
DO_LOWER_CASE(Pike_sp[-args].u.integer);
pop_n_elems(args-1);
return;
}
orig = Pike_sp[-args].u.string;
if( orig->flags & STRING_IS_LOWERCASE )
return;
ret = begin_wide_shared_string(orig->len, orig->size_shift);
memcpy(ret->str, orig->str, orig->len << orig->size_shift);
i = orig->len;
if (!orig->size_shift) {
p_wchar0 *str = STR0(ret);
while(i--) {
DO_LOWER_CASE_SHIFT0(str[i]);
}
} else if (orig->size_shift == 1) {
p_wchar1 *str = STR1(ret);
while(i--) {
DO_LOWER_CASE(str[i]);
}
} else if (orig->size_shift == 2) {
p_wchar2 *str = STR2(ret);
while(i--) {
DO_LOWER_CASE(str[i]);
}
#ifdef PIKE_DEBUG
} else {
Pike_fatal("lower_case(): Bad string shift:%d\n", orig->size_shift);
#endif
}
ret = end_shared_string(ret);
ret->flags |= STRING_IS_LOWERCASE;
pop_n_elems(args);
push_string(ret);
}
/*! @decl string upper_case(string s)
*! @decl int upper_case(int c)
*!
*! Convert a string or character to upper case.
*!
*! @returns
*! Returns a copy of the string @[s] with all lower case characters
*! converted to upper case, or the character @[c] converted to upper
*! case.
*!
*! @note
*! Assumes the string or character to be coded according to
*! ISO-10646 (aka Unicode). If they are not, @[Charset.decoder] can
*! do the initial conversion for you.
*!
*! @note
*! Prior to Pike 7.5 this function only accepted strings.
*!
*! @seealso
*! @[lower_case()], @[Charset.decoder]
*/
PMOD_EXPORT void f_upper_case(INT32 args)
{
ptrdiff_t i;
struct pike_string *orig;
struct pike_string *ret;
check_all_args("upper_case", args, BIT_STRING|BIT_INT, 0);
if (TYPEOF(Pike_sp[-args]) == T_INT) {
/* NOTE: Performs the case change in place. */
DO_UPPER_CASE(Pike_sp[-args].u.integer);
pop_n_elems(args-1);
return;
}
orig = Pike_sp[-args].u.string;
if( orig->flags & STRING_IS_UPPERCASE )
{
return;
}
ret=begin_wide_shared_string(orig->len,orig->size_shift);
memcpy(ret->str, orig->str, orig->len << orig->size_shift);
i = orig->len;
if (!orig->size_shift) {
p_wchar0 *str = STR0(ret);
while(i--) {
if(str[i]!=0xff && str[i]!=0xb5) {
DO_UPPER_CASE_SHIFT0(str[i]);
} else {
/* Ok, so our shiftsize 0 string contains 0xff or 0xb5 which
prompts for a shiftsize 1 string. */
int j = orig->len;
struct pike_string *wret = begin_wide_shared_string(j, 1);
p_wchar1 *wstr = STR1(wret);
/* Copy what we have done */
while(--j>i)
wstr[j] = str[j];
/* upper case the rest */
i++;
while(i--)
switch( str[i] ) {
case 0xff: wstr[i] = 0x178; break;
case 0xb5: wstr[i] = 0x39c; break;
default:
DO_UPPER_CASE_SHIFT0(str[i]);
wstr[i] = str[i];
break;
}
/* Discard the too narrow string and use the new one instead. */
do_free_unlinked_pike_string(ret);
ret = wret;
break;
}
}
} else if (orig->size_shift == 1) {
p_wchar1 *str = STR1(ret);
while(i--) {
DO_UPPER_CASE(str[i]);
}
} else if (orig->size_shift == 2) {
p_wchar2 *str = STR2(ret);
while(i--) {
DO_UPPER_CASE(str[i]);
}
#ifdef PIKE_DEBUG
} else {
Pike_fatal("lower_case(): Bad string shift:%d\n", orig->size_shift);
#endif
}
pop_n_elems(args);
ret = end_shared_string(ret);
ret->flags |= STRING_IS_UPPERCASE;
push_string(ret);
}
/*! @decl void random_seed(int seed)
*!
*! This function sets the initial value for the random generator.
*!
*! @seealso
*! @[random()]
*!
*! @deprecated
*! @[Random.Deterministic]
*/
static void f_random_seed(INT32 args)
{
INT_TYPE i;
pop_n_elems(args);
}
/*! @decl int query_num_arg()
*!
*! Returns the number of arguments given when the previous function was
*! called.
*!
*! This is useful for functions that take a variable number of arguments.
*!
*! @seealso
*! @[call_function()]
*/
void f_query_num_arg(INT32 args)
{
pop_n_elems(args);
push_int(Pike_fp ? Pike_fp->args : 0);
}
/*! @decl int search(string haystack, string|int needle, int|void start, @
*! int|void end)
*! @decl int search(array haystack, mixed needle, int|void start, int|void end)
*! @decl mixed search(mapping haystack, mixed needle, mixed|void start)
*! @decl mixed search(object haystack, mixed needle, mixed|void start, @
*! mixed ... extra_args)
*!
*! Search for @[needle] in @[haystack].
*!
*! @param haystack
*! Item to search in. This can be one of:
*! @mixed
*! @type string
*! When @[haystack] is a string @[needle] must be a string or an int,
*! and the first occurrence of the string or int is returned.
*!
*! @type array
*! When @[haystack] is an array, @[needle] is compared only to
*! one value at a time in @[haystack].
*!
*! @type mapping
*! When @[haystack] is a mapping, @[search()] tries to find the index
*! connected to the data @[needle]. That is, it tries to lookup the
*! mapping backwards.
*!
*! @type object
*! When @[haystack] is an object implementing @[lfun::_search()],
*! the result of calling @[lfun::_search()] with @[needle], @[start]
*! and any @[extra_args] will be returned.
*!
*! If @[haystack] is an object that doesn't implement @[lfun::_search()]
*! it is assumed to be an @[Iterator], and implement
*! @[Iterator()->index()], @[Iterator()->value()], and
*! @[Iterator()->next()]. @[search()] will then start comparing
*! elements with @[`==()] until a match with @[needle] is found.
*! If @[needle] is found @[haystack] will be advanced to the element,
*! and the iterator index will be returned. If @[needle] is not
*! found, @[haystack] will be advanced to the end.
*! @endmixed
*!
*! @param start
*! If the optional argument @[start] is present search is started at
*! this position. This has no effect on mappings.
*!
*! @param end
*! If the optional argument @[end] is present, the search will terminate
*! at this position (exclusive) if not found earlier.
*!
*! @returns
*! Returns the position of @[needle] in @[haystack] if found.
*!
*! If not found the returned value depends on the type of @[haystack]:
*! @mixed
*! @type string|array
*! @expr{-1@}.
*! @type mapping|object(Iterator)
*! @[UNDEFINED].
*! @type object
*! The value returned by @[lfun::_search()].
*! @endmixed
*!
*! @note
*! If @[start] is supplied to an iterator object without an
*! @[lfun::_search()], @[haystack] will need to implement
*! @[Iterator()->set_index()].
*!
*! @note
*! For mappings and object @[UNDEFINED] will be returned when not found.
*! In all other cases @expr{-1@} will be returned when not found.
*!
*! @seealso
*! @[indices()], @[values()], @[zero_type()], @[has_value()],
*! @[has_prefix()], @[has_suffix()]
*/
PMOD_EXPORT void f_search(INT32 args)
{
ptrdiff_t start;
ptrdiff_t end;
if(args < 2)
SIMPLE_WRONG_NUM_ARGS_ERROR("search", 2);
switch(TYPEOF(Pike_sp[-args]))
{
case T_STRING:
{
struct pike_string *haystack = Pike_sp[-args].u.string;
start=0;
end = haystack->len;
if(args > 2)
{
if(TYPEOF(Pike_sp[2-args]) != T_INT)
SIMPLE_ARG_TYPE_ERROR("search", 3, "int");
start=Pike_sp[2-args].u.integer;
if(start<0) {