forked from rebolsource/r3
/
l-types.c
1317 lines (1086 loc) · 30.7 KB
/
l-types.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
//
// File: %l-types.c
// Summary: "special lexical type converters"
// Section: lexical
// Project: "Rebol 3 Interpreter and Run-time (Ren-C branch)"
// Homepage: https://github.com/metaeducation/ren-c/
//
//=////////////////////////////////////////////////////////////////////////=//
//
// Copyright 2012 REBOL Technologies
// Copyright 2012-2017 Rebol Open Source Contributors
// REBOL is a trademark of REBOL Technologies
//
// See README.md and CREDITS.md for more information.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
//=////////////////////////////////////////////////////////////////////////=//
//
#include "sys-core.h"
#include "sys-deci-funcs.h"
#include "sys-dec-to-char.h"
#include <errno.h>
//
// The scanning code in R3-Alpha used NULL to return failure during the scan
// of a value, possibly leaving the value itself in an incomplete or invalid
// state. Rather than write stray incomplete values into these spots, Ren-C
// puts "unreadable blank"
//
#define return_NULL \
do { SET_UNREADABLE_BLANK(out); return NULL; } while (TRUE)
//
// MAKE_Fail: C
//
void MAKE_Fail(REBVAL *out, enum Reb_Kind kind, const REBVAL *arg)
{
UNUSED(out);
UNUSED(kind);
UNUSED(arg);
fail ("Datatype does not have a MAKE handler registered");
}
//
// make: native [
//
// {Constructs or allocates the specified datatype.}
//
// return: [any-value!]
// {Constructed value.}
// type [any-value!]
// {The datatype -or- an examplar value of the type to construct}
// def [any-value!]
// {Definition or size of the new value (binding may be modified)}
// ]
//
REBNATIVE(make)
{
INCLUDE_PARAMS_OF_MAKE;
REBVAL *type = ARG(type);
REBVAL *arg = ARG(def);
#if !defined(NDEBUG)
if (IS_GOB(type)) {
//
// !!! It appears that GOBs had some kind of inheritance mechanism, by
// which you would write:
//
// gob1: make gob! [...]
// gob2: make gob1 [...]
//
// The new plan is that MAKE operates on a definition spec, and that
// this type slot is always a value or exemplar. So if the feature
// is needed, it should be something like:
//
// gob1: make gob! [...]
// gob2: make gob! [gob1 ...]
//
// Or perhaps not use make at all, but some other operation.
//
assert(FALSE);
}
else if (IS_EVENT(type)) {
assert(FALSE); // ^-- same for events (?)
}
#endif
enum Reb_Kind kind;
if (IS_DATATYPE(type))
kind = VAL_TYPE_KIND(type);
else
kind = VAL_TYPE(type);
MAKE_FUNC dispatcher = Make_Dispatch[kind];
if (dispatcher == NULL)
fail (Error_Bad_Make(kind, arg));
if (IS_VARARGS(arg)) {
//
// Converting a VARARGS! to an ANY-ARRAY! involves spooling those
// varargs to the end and making an array out of that. It's not known
// how many elements that will be, so they're gathered to the data
// stack to find the size, then an array made. Note that | will stop
// varargs gathering.
//
// !!! MAKE should likely not be allowed to THROW in the general
// case--especially if it is the implementation of construction
// syntax (arbitrary code should not run during LOAD). Since
// vararg spooling may involve evaluation (e.g. to create an array)
// it may be a poor fit for the MAKE umbrella.
//
// Temporarily putting the code here so that the make dispatchers
// do not have to bubble up throws, but it is likely that this
// should not have been a MAKE operation in the first place.
//
// !!! This MAKE will be destructive to its input (the varargs will
// be fetched and exhausted). That's not necessarily obvious, but
// with a TO conversion it would be even less obvious...
//
if (dispatcher != &MAKE_Array)
fail (Error_Bad_Make(kind, arg));
REBDSP dsp_orig = DSP;
do {
REB_R r = Do_Vararg_Op_May_Throw(D_OUT, arg, VARARG_OP_TAKE);
if (r == R_OUT_IS_THROWN) {
DS_DROP_TO(dsp_orig);
return R_OUT_IS_THROWN;
}
if (r == R_VOID)
break;
assert(r == R_OUT);
DS_PUSH(D_OUT);
SET_END(D_OUT); // expected by Do_Vararg_Op
} while (TRUE);
Init_Any_Array(D_OUT, kind, Pop_Stack_Values(dsp_orig));
return R_OUT;
}
dispatcher(D_OUT, kind, arg); // may fail()
return R_OUT;
}
//
// TO_Fail: C
//
void TO_Fail(REBVAL *out, enum Reb_Kind kind, const REBVAL *arg)
{
UNUSED(out);
UNUSED(kind);
UNUSED(arg);
fail ("Datatype does not have a TO handler registered");
}
//
// to: native [
//
// {Converts to a specified datatype.}
//
// type [any-value!]
// {The datatype -or- an exemplar value of the target type}
// value [any-value!]
// {The source value to convert}
// ]
//
REBNATIVE(to)
{
INCLUDE_PARAMS_OF_TO;
REBVAL *type = ARG(type);
REBVAL *arg = ARG(value);
enum Reb_Kind kind;
if (IS_DATATYPE(type))
kind = VAL_TYPE_KIND(type);
else
kind = VAL_TYPE(type);
// !!! The only thing you can TO convert a blank into is a BLANK!. This
// allows one to sort of opt-out:
//
// kind: get-kind-maybe-blank x y z
// if blank? converted: to kind value [...]
//
// Is this a good rule, or should types be able to have a custom behavior
// for the TO of a blank conversion into them?
//
if (IS_BLANK(arg)) {
if (kind == REB_BLANK)
return R_BLANK;
fail (arg);
}
TO_FUNC dispatcher = To_Dispatch[kind];
if (dispatcher == NULL)
fail (arg);
dispatcher(D_OUT, kind, arg); // may fail();
return R_OUT;
}
//
// Scan_Hex: C
//
// Scans hex while it is valid and does not exceed the maxlen.
// If the hex string is longer than maxlen - it's an error.
// If a bad char is found less than the minlen - it's an error.
// String must not include # - ~ or other invalid chars.
// If minlen is zero, and no string, that's a valid zero value.
//
// Note, this function relies on LEX_WORD lex values having a LEX_VALUE
// field of zero, except for hex values.
//
const REBYTE *Scan_Hex(
REBVAL *out,
const REBYTE *cp,
REBCNT minlen,
REBCNT maxlen
) {
TRASH_CELL_IF_DEBUG(out);
if (maxlen > MAX_HEX_LEN)
return_NULL;
REBI64 i = 0;
REBCNT cnt = 0;
REBYTE lex;
while ((lex = Lex_Map[*cp]) > LEX_WORD) {
REBYTE v;
if (++cnt > maxlen)
return_NULL;
v = cast(REBYTE, lex & LEX_VALUE); // char num encoded into lex
if (!v && lex < LEX_NUMBER)
return_NULL; // invalid char (word but no val)
i = (i << 4) + v;
cp++;
}
if (cnt < minlen)
return_NULL;
Init_Integer(out, i);
return cp;
}
//
// Scan_Hex2: C
//
// Decode a %xx hex encoded byte into a char.
//
// The % should already be removed before calling this.
//
// We don't allow a %00 in files, urls, email, etc... so
// a return of 0 is used to indicate an error.
//
REBOOL Scan_Hex2(const REBYTE *bp, REBUNI *n, REBOOL unicode)
{
REBUNI c1, c2;
REBYTE d1, d2;
REBYTE lex;
if (unicode) {
const REBUNI *up = cast(const REBUNI*, bp);
c1 = up[0];
c2 = up[1];
} else {
c1 = bp[0];
c2 = bp[1];
}
lex = Lex_Map[c1];
d1 = lex & LEX_VALUE;
if (lex < LEX_WORD || (!d1 && lex < LEX_NUMBER)) return FALSE;
lex = Lex_Map[c2];
d2 = lex & LEX_VALUE;
if (lex < LEX_WORD || (!d2 && lex < LEX_NUMBER)) return FALSE;
*n = (REBUNI)((d1 << 4) + d2);
return TRUE;
}
//
// Scan_Hex_Value: C
//
// Given a string, scan it as hex. Chars can be 8 or 16 bit.
// Result is 32 bits max.
// Throw errors.
//
REBCNT Scan_Hex_Value(const void *p, REBCNT len, REBOOL unicode)
{
REBUNI c;
REBCNT n;
REBYTE lex;
REBCNT num = 0;
const REBYTE *bp = unicode ? NULL : cast(const REBYTE *, p);
const REBUNI *up = unicode ? cast(const REBUNI *, p) : NULL;
if (len > 8) goto bad_hex;
for (n = 0; n < len; n++) {
c = unicode ? up[n] : cast(REBUNI, bp[n]);
if (c > 255) goto bad_hex;
lex = Lex_Map[c];
if (lex <= LEX_WORD) goto bad_hex;
c = lex & LEX_VALUE;
if (!c && lex < LEX_NUMBER) goto bad_hex;
num = (num << 4) + c;
}
return num;
bad_hex:
fail (Error_Invalid_Chars_Raw());
}
//
// Scan_Dec_Buf: C
//
// Validate a decimal number. Return on first invalid char (or end).
// Returns NULL if not valid.
//
// Scan is valid for 1 1.2 1,2 1'234.5 1x 1.2x 1% 1.2% etc.
//
// !!! Is this redundant with Scan_Decimal? Appears to be similar code.
//
const REBYTE *Scan_Dec_Buf(
REBYTE *out, // may live in data stack (do not call DS_PUSH, GC, eval)
const REBYTE *cp,
REBCNT len // max size of buffer
) {
assert(len >= MAX_NUM_LEN);
REBYTE *bp = out;
REBYTE *be = bp + len - 1;
if (*cp == '+' || *cp == '-')
*bp++ = *cp++;
REBOOL digit_present = FALSE;
while (IS_LEX_NUMBER(*cp) || *cp == '\'') {
if (*cp != '\'') {
*bp++ = *cp++;
if (bp >= be)
return NULL;
digit_present = TRUE;
}
else
++cp;
}
if (*cp == ',' || *cp == '.')
cp++;
*bp++ = '.';
if (bp >= be)
return NULL;
while (IS_LEX_NUMBER(*cp) || *cp == '\'') {
if (*cp != '\'') {
*bp++ = *cp++;
if (bp >= be)
return NULL;
digit_present = TRUE;
}
else
++cp;
}
if (NOT(digit_present))
return NULL;
if (*cp == 'E' || *cp == 'e') {
*bp++ = *cp++;
if (bp >= be)
return NULL;
digit_present = FALSE;
if (*cp == '-' || *cp == '+') {
*bp++ = *cp++;
if (bp >= be)
return NULL;
}
while (IS_LEX_NUMBER(*cp)) {
*bp++ = *cp++;
if (bp >= be)
return NULL;
digit_present = TRUE;
}
if (NOT(digit_present))
return NULL;
}
*bp = '\0';
return cp;
}
//
// Scan_Decimal: C
//
// Scan and convert a decimal value. Return zero if error.
//
const REBYTE *Scan_Decimal(
REBVAL *out, // may live in data stack (do not call DS_PUSH, GC, eval)
const REBYTE *cp,
REBCNT len,
REBOOL dec_only
) {
TRASH_CELL_IF_DEBUG(out);
REBYTE buf[MAX_NUM_LEN + 4];
REBYTE *ep = buf;
if (len > MAX_NUM_LEN)
return_NULL;
const REBYTE *bp = cp;
if (*cp == '+' || *cp == '-')
*ep++ = *cp++;
REBOOL digit_present = FALSE;
while (IS_LEX_NUMBER(*cp) || *cp == '\'') {
if (*cp != '\'') {
*ep++ = *cp++;
digit_present = TRUE;
}
else
++cp;
}
if (*cp == ',' || *cp == '.')
++cp;
*ep++ = '.';
while (IS_LEX_NUMBER(*cp) || *cp == '\'') {
if (*cp != '\'') {
*ep++ = *cp++;
digit_present = TRUE;
}
else
++cp;
}
if (NOT(digit_present))
return_NULL;
if (*cp == 'E' || *cp == 'e') {
*ep++ = *cp++;
digit_present = FALSE;
if (*cp == '-' || *cp == '+')
*ep++ = *cp++;
while (IS_LEX_NUMBER(*cp)) {
*ep++ = *cp++;
digit_present = TRUE;
}
if (NOT(digit_present))
return_NULL;
}
if (*cp == '%') {
if (dec_only)
return_NULL;
++cp; // ignore it
}
*ep = '\0';
if (cast(REBCNT, cp - bp) != len)
return_NULL;
VAL_RESET_HEADER(out, REB_DECIMAL);
const char *se;
VAL_DECIMAL(out) = STRTOD(s_cast(buf), &se);
// !!! TBD: need check for NaN, and INF
if (fabs(VAL_DECIMAL(out)) == HUGE_VAL)
fail (Error_Overflow_Raw());
return cp;
}
//
// Scan_Integer: C
//
// Scan and convert an integer value. Return zero if error.
// Allow preceding + - and any combination of ' marks.
//
const REBYTE *Scan_Integer(
REBVAL *out, // may live in data stack (do not call DS_PUSH, GC, eval)
const REBYTE *cp,
REBCNT len
) {
TRASH_CELL_IF_DEBUG(out);
// Super-fast conversion of zero and one (most common cases):
if (len == 1) {
if (*cp == '0') {
Init_Integer(out, 0);
return cp + 1;
}
if (*cp == '1') {
Init_Integer(out, 1);
return cp + 1;
}
}
REBYTE buf[MAX_NUM_LEN + 4];
if (len > MAX_NUM_LEN)
return_NULL; // prevent buffer overflow
REBYTE *bp = buf;
REBOOL neg = FALSE;
REBINT num = cast(REBINT, len);
// Strip leading signs:
if (*cp == '-') {
*bp++ = *cp++;
--num;
neg = TRUE;
}
else if (*cp == '+') {
++cp;
--num;
}
// Remove leading zeros:
for (; num > 0; num--) {
if (*cp == '0' || *cp == '\'')
++cp;
else
break;
}
if (num == 0) { // all zeros or '
// return early to avoid platform dependant error handling in CHR_TO_INT
Init_Integer(out, 0);
return cp;
}
// Copy all digits, except ' :
for (; num > 0; num--) {
if (*cp >= '0' && *cp <= '9')
*bp++ = *cp++;
else if (*cp == '\'')
++cp;
else
return_NULL;
}
*bp = '\0';
// Too many digits?
len = bp - &buf[0];
if (neg)
--len;
if (len > 19) {
// !!! magic number :-( How does it relate to MAX_INT_LEN (also magic)
return_NULL;
}
// Convert, check, and return:
errno = 0;
VAL_RESET_HEADER(out, REB_INTEGER);
VAL_INT64(out) = CHR_TO_INT(buf);
if (errno != 0)
return_NULL; // overflow
if ((VAL_INT64(out) > 0 && neg) || (VAL_INT64(out) < 0 && !neg))
return_NULL;
return cp;
}
//
// Scan_Money: C
//
// Scan and convert money. Return zero if error.
//
const REBYTE *Scan_Money(
REBVAL *out, // may live in data stack (do not call DS_PUSH, GC, eval)
const REBYTE *cp,
REBCNT len
) {
TRASH_CELL_IF_DEBUG(out);
const REBYTE *end;
if (*cp == '$') {
++cp;
--len;
}
if (len == 0)
return_NULL;
Init_Money(out, string_to_deci(cp, &end));
if (end != cp + len)
return_NULL;
return end;
}
//
// Scan_Date: C
//
// Scan and convert a date. Also can include a time and zone.
//
const REBYTE *Scan_Date(
REBVAL *out, // may live in data stack (do not call DS_PUSH, GC, eval)
const REBYTE *cp,
REBCNT len
) {
TRASH_CELL_IF_DEBUG(out);
const REBYTE *end = cp + len;
// Skip spaces:
for (; *cp == ' ' && cp != end; cp++);
// Skip day name, comma, and spaces:
const REBYTE *ep;
for (ep = cp; *ep != ',' && ep != end; ep++);
if (ep != end) {
cp = ep + 1;
while (*cp == ' ' && cp != end) cp++;
}
if (cp == end)
return_NULL;
REBINT num;
// Day or 4-digit year:
ep = Grab_Int(cp, &num);
if (num < 0)
return_NULL;
REBINT day;
REBINT month;
REBINT year;
REBINT tz = 0;
REBCNT size = cast(REBCNT, ep - cp);
if (size >= 4) {
// year is set in this branch (we know because day is 0)
// Ex: 2009/04/20/19:00:00+0:00
year = num;
day = 0;
}
else if (size) {
// year is not set in this branch (we know because day ISN'T 0)
// Ex: 12-Dec-2012
day = num;
if (day == 0)
return_NULL;
// !!! Clang static analyzer doesn't know from test of `day` below
// how it connects with year being set or not. Suppress warning.
year = MIN_I32; // !!! Garbage, should not be read.
}
else
return_NULL;
cp = ep;
// Determine field separator:
if (*cp != '/' && *cp != '-' && *cp != '.' && *cp != ' ')
return_NULL;
REBYTE sep = *cp++;
// Month as number or name:
ep = Grab_Int(cp, &num);
if (num < 0)
return_NULL;
size = cast(REBCNT, ep - cp);
if (size > 0)
month = num; // got a number
else { // must be a word
for (ep = cp; IS_LEX_WORD(*ep); ep++)
NOOP; // scan word
size = cast(REBCNT, ep - cp);
if (size < 3)
return_NULL;
for (num = 0; num < 12; num++) {
if (!Compare_Bytes(cb_cast(Month_Names[num]), cp, size, TRUE))
break;
}
month = num + 1;
}
if (month < 1 || month > 12)
return_NULL;
cp = ep;
if (*cp++ != sep)
return_NULL;
// Year or day (if year was first):
ep = Grab_Int(cp, &num);
if (*cp == '-' || num < 0)
return_NULL;
size = cast(REBCNT, ep - cp);
if (size == 0)
return_NULL;
if (day == 0) {
// year already set, but day hasn't been
day = num;
}
else {
// day has been set, but year hasn't been.
if (size >= 3)
year = num;
else {
// !!! Originally this allowed shorthands, so that 96 = 1996, etc.
//
// if (num >= 70)
// year = 1900 + num;
// else
// year = 2000 + num;
//
// It was trickier than that, because it actually used the current
// year (from the clock) to guess what the short year meant. That
// made it so the scanner would scan the same source code
// differently based on the clock, which is bad. By allowing
// short dates to be turned into their short year equivalents, the
// user code can parse such dates and fix them up after the fact
// according to their requirements, `if date/year < 100 [...]`
//
year = num;
}
}
if (year > MAX_YEAR || day < 1 || day > Month_Max_Days[month-1])
return_NULL;
// Check February for leap year or century:
if (month == 2 && day == 29) {
if (
((year % 4) != 0) || // not leap year
((year % 100) == 0 && // century?
(year % 400) != 0)
){
return_NULL; // not leap century
}
}
cp = ep;
VAL_TIME(out) = NO_TIME;
if (cp >= end)
goto end_date;
if (*cp == '/' || *cp == ' ') {
sep = *cp++;
if (cp >= end)
goto end_date;
cp = Scan_Time(out, cp, 0);
if (
cp == NULL
|| !IS_TIME(out)
|| (VAL_TIME(out) < 0)
|| (VAL_TIME(out) >= TIME_SEC(24 * 60 * 60))
){
return_NULL;
}
}
if (*cp == sep) cp++;
// Time zone can be 12:30 or 1230 (optional hour indicator)
if (*cp == '-' || *cp == '+') {
if (cp >= end)
goto end_date;
ep = Grab_Int(cp + 1, &num);
if (ep - cp == 0)
return_NULL;
if (*ep != ':') {
if (num < -1500 || num > 1500)
return_NULL;
int h = (num / 100);
int m = (num - (h * 100));
tz = (h * 60 + m) / ZONE_MINS;
}
else {
if (num < -15 || num > 15)
return_NULL;
tz = num * (60 / ZONE_MINS);
if (*ep == ':') {
ep = Grab_Int(ep + 1, &num);
if (num % ZONE_MINS != 0)
return_NULL;
tz += num / ZONE_MINS;
}
}
if (ep != end)
return_NULL;
if (*cp == '-')
tz = -tz;
cp = ep;
}
end_date:
Set_Date_UTC(out, year, month, day, VAL_TIME(out), tz);
return cp;
}
//
// Scan_File: C
//
// Scan and convert a file name.
//
const REBYTE *Scan_File(
REBVAL *out, // may live in data stack (do not call DS_PUSH, GC, eval)
const REBYTE *cp,
REBCNT len
) {
TRASH_CELL_IF_DEBUG(out);
if (*cp == '%') {
cp++;
len--;
}
REBUNI term = 0;
const REBYTE *invalid;
if (*cp == '"') {
cp++;
len--;
term = '"';
invalid = cb_cast(":;\"");
}
else {
term = 0;
invalid = cb_cast(":;()[]\"");
}
REB_MOLD mo;
CLEARS(&mo);
cp = Scan_Item_Push_Mold(&mo, cp, cp + len, term, invalid);
if (cp == NULL) {
Drop_Mold(&mo);
return_NULL;
}
Init_File(out, Pop_Molded_String(&mo));
return cp;
}
//
// Scan_Email: C
//
// Scan and convert email.
//
const REBYTE *Scan_Email(
REBVAL *out, // may live in data stack (do not call DS_PUSH, GC, eval)
const REBYTE *cp,
REBCNT len
) {
TRASH_CELL_IF_DEBUG(out);
REBSER *series = Make_Binary(len);
REBOOL at = FALSE;
REBYTE *str = BIN_HEAD(series);
for (; len > 0; len--) {
if (*cp == '@') {
if (at) return_NULL;
at = TRUE;
}
if (*cp == '%') {
REBUNI n;
if (len <= 2 || !Scan_Hex2(cp + 1, &n, FALSE))
return_NULL;
*str++ = cast(REBYTE, n);
cp += 3;
len -= 2;
}
else
*str++ = *cp++;
}
*str = 0;
if (NOT(at))
return_NULL;
SET_SERIES_LEN(series, cast(REBCNT, str - BIN_HEAD(series)));
Init_Email(out, series);
return cp;
}
//
// Scan_URL: C
//
// Scan and convert a URL.
//
const REBYTE *Scan_URL(
REBVAL *out, // may live in data stack (do not call DS_PUSH, GC, eval)
const REBYTE *cp,
REBCNT len
) {
TRASH_CELL_IF_DEBUG(out);
// !!! Need to check for any possible scheme followed by ':'
// for (n = 0; n < URL_MAX; n++) {
// if (str = Match_Bytes(cp, (REBYTE *)(URL_Schemes[n]))) break;
// }
// if (n >= URL_MAX) return_NULL;
// if (*str != ':') return_NULL;
REBSER *series = Make_Binary(len);
REBYTE *str = BIN_HEAD(series);
for (; len > 0; len--) {
//if (*cp == '%' && len > 2 && Scan_Hex2(cp+1, &n, FALSE)) {
if (*cp == '%') {
REBUNI n;
if (len <= 2 || !Scan_Hex2(cp + 1, &n, FALSE))
return_NULL;
*str++ = cast(REBYTE, n);
cp += 3;
len -= 2;
}
else
*str++ = *cp++;
}
*str = 0;