12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
* Copyright © 2002 ActiveState Corporation.
* Copyright © 2003-2009 Donal K. Fellows.
*
* See the file "license.terms" for information on usage and redistribution of
* this file, and for a DISCLAIMER OF ALL WARRANTIES.
*/
#include <math.h>
#include "tclInt.h"
#include "tclCompile.h"
#include "tclRegexp.h"
#include "tclStringTrim.h"
static inline Tcl_Obj * During(Tcl_Interp *interp, int resultCode,
Tcl_Obj *oldOptions, Tcl_Obj *errorInfo);
|
<
|
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
* Copyright © 2002 ActiveState Corporation.
* Copyright © 2003-2009 Donal K. Fellows.
*
* See the file "license.terms" for information on usage and redistribution of
* this file, and for a DISCLAIMER OF ALL WARRANTIES.
*/
#include "tclInt.h"
#include "tclCompile.h"
#include "tclRegexp.h"
#include "tclStringTrim.h"
static inline Tcl_Obj * During(Tcl_Interp *interp, int resultCode,
Tcl_Obj *oldOptions, Tcl_Obj *errorInfo);
|
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
|
"\xE2\x80\xAF" /* narrow no-break space (U+202f) */
"\xE2\x81\x9F" /* medium mathematical space (U+205f) */
"\xE2\x81\xA0" /* word joiner (U+2060) */
"\xE3\x80\x80" /* ideographic space (U+3000) */
"\xEF\xBB\xBF" /* zero width no-break space (U+feff) */
;
/*
* Definitions for [lseq] command
*/
static const char *const seq_operations[] = {
"..", "to", "count", "by", NULL
};
typedef enum Sequence_Operators {
RANGE_DOTS, RANGE_TO, RANGE_COUNT, RANGE_BY
} SequenceOperators;
static const char *const seq_step_keywords[] = {"by", NULL};
typedef enum Step_Operators {
STEP_BY = 4
} SequenceByMode;
typedef enum Sequence_Decoded {
NoneArg, NumericArg, RangeKeywordArg, ByKeywordArg
} SequenceDecoded;
/*
*----------------------------------------------------------------------
*
* Tcl_PwdObjCmd --
*
* This procedure is invoked to process the "pwd" Tcl command. See the
|
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
|
59
60
61
62
63
64
65
66
67
68
69
70
71
72
|
"\xE2\x80\xAF" /* narrow no-break space (U+202f) */
"\xE2\x81\x9F" /* medium mathematical space (U+205f) */
"\xE2\x81\xA0" /* word joiner (U+2060) */
"\xE3\x80\x80" /* ideographic space (U+3000) */
"\xEF\xBB\xBF" /* zero width no-break space (U+feff) */
;
/*
*----------------------------------------------------------------------
*
* Tcl_PwdObjCmd --
*
* This procedure is invoked to process the "pwd" Tcl command. See the
|
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
|
if (retVal == NULL) {
return TCL_ERROR;
}
Tcl_SetObjResult(interp, retVal);
Tcl_DecrRefCount(retVal);
return TCL_OK;
}
/*
*----------------------------------------------------------------------
*
* SequenceIdentifyArgument --
*
* Given a Tcl_Obj, identify if it is a keyword or a number
*
* Return Value
* 0 - failure, unexpected value
* 1 - value is a number
* 2 - value is an operand keyword
* 3 - value is a by keyword
*
* The decoded value will be assigned to the appropriate
* pointer, if supplied.
*/
static SequenceDecoded
SequenceIdentifyArgument(
Tcl_Interp *interp, /* for error reporting */
Tcl_Obj *argPtr, /* Argument to decode */
Tcl_WideInt *intValuePtr, /* Return numeric value */
int *keywordIndexPtr) /* Return keyword enum */
{
int status;
Tcl_WideInt number;
SequenceOperators opmode;
SequenceByMode bymode;
status = Tcl_GetWideIntFromObj(NULL, argPtr, &number);
if (status != TCL_OK) {
/* Check for an index expression */
long value;
Tcl_InterpState savedstate;
savedstate = Tcl_SaveInterpState(interp, status);
if (Tcl_ExprLongObj(interp, argPtr, &value) != TCL_OK) {
status = Tcl_RestoreInterpState(interp, savedstate);
} else {
status = Tcl_RestoreInterpState(interp, savedstate);
if (intValuePtr) {
*intValuePtr = value;
}
return NumericArg;
}
} else {
if (intValuePtr) {
*intValuePtr = number;
}
return NumericArg;
}
status = Tcl_GetIndexFromObj(NULL, argPtr, seq_operations,
"range operation", 0, &opmode);
if (status == TCL_OK) {
if (keywordIndexPtr) {
*keywordIndexPtr = opmode;
}
return RangeKeywordArg;
}
status = Tcl_GetIndexFromObj(NULL, argPtr, seq_step_keywords,
"step keyword", 0, &bymode);
if (status == TCL_OK) {
if (keywordIndexPtr) {
*keywordIndexPtr = bymode;
}
return ByKeywordArg;
}
return NoneArg;
}
/*
*----------------------------------------------------------------------
*
* Tcl_RangeObjCmd --
*
* This procedure is invoked to process the "range" Tcl command. See
* the user documentation for details on what it does.
*
* Enumerated possible argument patterns:
*
* 1:
* range n
* 2:
* range n n
* 3:
* range n n n
* range n 'to' n
* range n 'count' n
* range n 'by' n
* 4:
* range n 'to' n n
* range n n 'by' n
* range n 'count' n n
* 5:
* range n 'to' n 'by' n
* range n 'count' n 'by' n
*
* Results:
* A standard Tcl object result.
*
* Side effects:
* See the user documentation.
*
*----------------------------------------------------------------------
*/
int
Tcl_RangeObjCmd(
TCL_UNUSED(ClientData),
Tcl_Interp *interp, /* Current interpreter. */
int objc, /* Number of arguments. */
Tcl_Obj *const objv[]) /* The argument objects. */
{
Tcl_WideInt elementCount = -1;
Tcl_WideInt start = 0, end = 0, step = 0, number = 0;
Tcl_WideInt values[5];
int status, keyword;
Tcl_Obj *arithSeriesPtr;
SequenceOperators opmode;
SequenceDecoded decoded;
int i, arg_key = 0, value_i = 0;
/*
* Create a decoding key by looping through the arguments and identify
* what kind of argument each one is. Encode each argument as a decimal
* digit.
*/
if (objc > 6) {
/* Too many arguments */
arg_key=0;
} else for (i=1; i<objc; i++) {
arg_key = (arg_key * 10);
decoded = SequenceIdentifyArgument(interp, objv[i], &number, &keyword);
switch (decoded) {
case NoneArg:
/*
* Unrecognizable argument
* Reproduce operation error message
*/
status = Tcl_GetIndexFromObj(interp, objv[i], seq_operations,
"operation", 0, &opmode);
goto done;
case NumericArg:
arg_key += NumericArg;
values[value_i] = number;
value_i++;
break;
case RangeKeywordArg:
arg_key += RangeKeywordArg;
values[value_i] = keyword;
value_i++;
break;
case ByKeywordArg:
arg_key += ByKeywordArg;
values[value_i] = keyword;
value_i++;
break;
default:
arg_key += 9; // Error state
value_i++;
break;
}
}
/*
* The key encoding defines a valid set of arguments, or indicates an
* error condition; process the values accordningly.
*/
switch (arg_key) {
/* No argument */
case 0:
Tcl_WrongNumArgs(interp, 1, objv,
"n ??op? n ??by? n??");
status = TCL_ERROR;
goto done;
break;
/* range n */
case 1:
start = 0;
elementCount = (values[0] <= 0 ? 0 : values[0]);
end = values[0]-1;
step = 1;
break;
/* range n n */
case 11:
start = values[0];
end = values[1];
step = (start <= end) ? 1 : -1;
if (start <= end) {
elementCount = step ? (end-start+step)/step : 0; // 0 step -> empty list
} else {
elementCount = step ? (start-end-step)/(-step) : 0; // 0 step -> empty list
}
if (elementCount < 0) elementCount = 0;
break;
/* range n n n */
case 111:
start = values[0];
end = values[1];
step = values[2];
if (start <= end) {
elementCount = step ? (end-start+step)/step : 0; // 0 step -> empty list
} else {
elementCount = step ? (start-end-step)/(-step) : 0; // 0 step -> empty list
}
if (elementCount < 0) elementCount = 0;
break;
/* range n 'to' n */
/* range n 'count' n */
/* range n 'by' n */
case 121:
opmode = (SequenceOperators)values[1];
switch (opmode) {
case RANGE_DOTS:
case RANGE_TO:
start = values[0];
end = values[2];
step = (start <= end) ? 1 : -1;
elementCount = step ? (start-end+step)/step : 0; // 0 step -> empty list
break;
case RANGE_BY:
start = 0;
elementCount = values[0];
step = values[2];
end = start + (step * elementCount);
elementCount = step ? (start-end+step)/step : 0; // 0 step -> empty list
break;
case RANGE_COUNT:
start = values[0];
elementCount = (values[2] >= 0 ? values[2] : 0);
step = 1;
end = start + (step * elementCount);
break;
default:
status = TCL_ERROR;
goto done;
}
break;
/* range n 'to' n n */
/* range n 'count' n n */
case 1211:
opmode = (SequenceOperators)values[1];
switch (opmode) {
case RANGE_DOTS:
case RANGE_TO:
start = values[0];
end = values[2];
step = values[3];
break;
case RANGE_COUNT:
start = values[0];
elementCount = (values[2] >= 0 ? values[2] : 0);
step = values[3];
end = start + (step * elementCount);
break;
case RANGE_BY:
/* Error case */
status = TCL_ERROR;
goto done;
break;
default:
status = TCL_ERROR;
goto done;
break;
}
break;
/* range n n 'by' n */
case 1121:
start = values[0];
end = values[1];
opmode = (SequenceOperators)values[2];
switch (opmode) {
case RANGE_BY:
step = values[3];
break;
case RANGE_DOTS:
case RANGE_TO:
case RANGE_COUNT:
default:
status = TCL_ERROR;
goto done;
break;
}
if (start <= end) {
elementCount = step ? (end-start+step)/step : 0; // 0 step -> empty list
} else {
elementCount = step ? (start-end-step)/(-step) : 0; // 0 step -> empty list
}
break;
/* range n 'to' n 'by' n */
/* range n 'count' n 'by' n */
case 12121:
start = values[0];
opmode = (SequenceOperators)values[3];
switch (opmode) {
case RANGE_BY:
step = values[4];
break;
default:
status = TCL_ERROR;
goto done;
break;
}
opmode = (SequenceOperators)values[1];
switch (opmode) {
case RANGE_DOTS:
case RANGE_TO:
start = values[0];
end = values[2];
if ((step == 0) ||
(start <= end && step < 0) ||
(start >= end && step > 0)) {
elementCount = 0;
} else if (start <= end) {
elementCount = step ? (end-start+step)/step : 0; // 0 step -> empty list
} else if (step > 0) {
elementCount = step ? (start-end-step)/(-step) : 0; // 0 step -> empty list
}
break;
case RANGE_COUNT:
start = values[0];
elementCount = (values[2] >= 0 ? values[2] : 0);
end = start + (step * elementCount);
break;
default:
status = TCL_ERROR;
goto done;
break;
}
break;
/* Error cases: incomplete arguments */
case 12:
opmode = values[1]; goto KeywordError; break;
case 112:
opmode = values[2]; goto KeywordError; break;
case 1212:
opmode = values[3]; goto KeywordError; break;
KeywordError:
status = TCL_ERROR;
switch (opmode) {
case RANGE_DOTS:
case RANGE_TO:
Tcl_SetObjResult(interp, Tcl_ObjPrintf(
"missing \"to\" value."));
break;
case RANGE_COUNT:
Tcl_SetObjResult(interp, Tcl_ObjPrintf(
"missing \"count\" value."));
break;
case RANGE_BY:
Tcl_SetObjResult(interp, Tcl_ObjPrintf(
"missing \"by\" value."));
break;
}
status = TCL_ERROR;
goto done;
break;
/* All other argument errors */
default:
Tcl_WrongNumArgs(interp, 1, objv, "n ??op? n ??by? n??");
goto done;
break;
}
/*
* Success! Now lets create the series object.
*/
arithSeriesPtr = Tcl_NewArithSeriesObj(start, end, step, elementCount);
Tcl_SetObjResult(interp, arithSeriesPtr);
status = TCL_OK;
done:
return status;
}
/*
*----------------------------------------------------------------------
*
* Tcl_RegexpObjCmd --
*
* This procedure is invoked to process the "regexp" Tcl command. See
|
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
|
99
100
101
102
103
104
105
106
107
108
109
110
111
112
|
if (retVal == NULL) {
return TCL_ERROR;
}
Tcl_SetObjResult(interp, retVal);
Tcl_DecrRefCount(retVal);
return TCL_OK;
}
/*
*----------------------------------------------------------------------
*
* Tcl_RegexpObjCmd --
*
* This procedure is invoked to process the "regexp" Tcl command. See
|