chinwag 0.1.5 → 1.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Rakefile +1 -1
- data/ext/chinwag/chinwag.c +46 -41
- data/ext/chinwag/chinwag.h +20 -12
- data/ext/chinwag/config.c +6 -6
- data/ext/chinwag/dict.c +211 -107
- data/ext/chinwag/dict.h +93 -41
- data/ext/chinwag/latin.h +2 -2
- data/ext/chinwag/rb_chinwag_ext.c +255 -167
- data/ext/chinwag/seuss.h +2 -2
- data/ext/chinwag/tokenize.c +7 -5
- data/ext/chinwag/tokenize.h +5 -2
- metadata +2 -2
@@ -1,7 +1,7 @@
|
|
1
1
|
#include "rb_chinwag_ext.h"
|
2
2
|
|
3
3
|
VALUE m_chinwag;
|
4
|
-
VALUE
|
4
|
+
VALUE c_cwdict;
|
5
5
|
|
6
6
|
VALUE default_dict;
|
7
7
|
VALUE default_output_type;
|
@@ -31,7 +31,7 @@ cw_t get_cw_t_for_symbol(VALUE symbol)
|
|
31
31
|
|
32
32
|
VALUE m_cw_generate(int argc, VALUE* argv, VALUE obj)
|
33
33
|
{
|
34
|
-
|
34
|
+
cwdict_t* d;
|
35
35
|
VALUE result;
|
36
36
|
cw_t cw_type = get_cw_t_for_symbol(default_output_type);
|
37
37
|
long min = NUM2LONG(default_min_output);
|
@@ -45,9 +45,9 @@ VALUE m_cw_generate(int argc, VALUE* argv, VALUE obj)
|
|
45
45
|
}
|
46
46
|
|
47
47
|
// do stuff with 'em
|
48
|
-
if(argc == 0) Data_Get_Struct(default_dict,
|
48
|
+
if(argc == 0) Data_Get_Struct(default_dict, cwdict_t, d);
|
49
49
|
|
50
|
-
if(argc >= 1) Data_Get_Struct(argv[0],
|
50
|
+
if(argc >= 1) Data_Get_Struct(argv[0], cwdict_t, d);
|
51
51
|
|
52
52
|
if(argc >= 2) cw_type = get_cw_t_for_symbol(argv[1]);
|
53
53
|
|
@@ -75,7 +75,7 @@ VALUE m_cw_generate(int argc, VALUE* argv, VALUE obj)
|
|
75
75
|
rb_raise(rb_eRangeError,"out of range (1..10000)");
|
76
76
|
}
|
77
77
|
|
78
|
-
if(!
|
78
|
+
if(!cwdict_valid(*d, &e))
|
79
79
|
{
|
80
80
|
rb_raise(rb_eException, "%s", e);
|
81
81
|
free(e);
|
@@ -107,14 +107,14 @@ VALUE m_cw_generate(int argc, VALUE* argv, VALUE obj)
|
|
107
107
|
return result;
|
108
108
|
}
|
109
109
|
|
110
|
-
VALUE
|
111
|
-
VALUE
|
110
|
+
VALUE c_cwdict_clone(VALUE obj);
|
111
|
+
VALUE c_cwdict_close(VALUE obj);
|
112
112
|
VALUE m_set_d_dict(VALUE obj, VALUE new)
|
113
113
|
{
|
114
|
-
VALUE original =
|
114
|
+
VALUE original = c_cwdict_clone(default_dict);
|
115
115
|
|
116
|
-
default_dict =
|
117
|
-
default_dict =
|
116
|
+
default_dict = c_cwdict_close(default_dict);
|
117
|
+
default_dict = c_cwdict_clone(new);
|
118
118
|
|
119
119
|
return original;
|
120
120
|
}
|
@@ -190,17 +190,17 @@ VALUE m_s_max(VALUE obj, VALUE num)
|
|
190
190
|
return original;
|
191
191
|
}
|
192
192
|
|
193
|
-
static void
|
193
|
+
static void c_cwdict_free(void* dict)
|
194
194
|
{
|
195
|
-
|
196
|
-
if(d->drows && d->count > 0)
|
195
|
+
cwdict_t* d = (cwdict_t*)dict;
|
196
|
+
if(d->drows && d->count > 0) cwdict_close(*d);
|
197
197
|
}
|
198
198
|
|
199
|
-
VALUE
|
199
|
+
VALUE c_cwdict_open(int argc, VALUE* argv, VALUE obj)
|
200
200
|
{
|
201
201
|
VALUE file_pathname;
|
202
202
|
FILE* file_ptr = NULL;
|
203
|
-
|
203
|
+
cwdict_t d = cwdict_open(), path_parts, file_parts;
|
204
204
|
char* tkns_ptr = NULL; char* name_ptr = NULL;
|
205
205
|
char* path_name = NULL; char* file_name = NULL;
|
206
206
|
char* name = NULL; bool used_file = false; char* file_buffer = NULL;
|
@@ -226,7 +226,7 @@ VALUE c_cw_dict_open(int argc, VALUE* argv, VALUE obj)
|
|
226
226
|
|
227
227
|
if(include(path_name, "/") || include(path_name, "\\"))
|
228
228
|
{
|
229
|
-
path_parts =
|
229
|
+
path_parts = split_into_cwdict(path_name, "/\\");
|
230
230
|
|
231
231
|
last_drow = path_parts.count - 1;
|
232
232
|
last_word = path_parts.drows[last_drow].count - 1;
|
@@ -236,7 +236,7 @@ VALUE c_cw_dict_open(int argc, VALUE* argv, VALUE obj)
|
|
236
236
|
strcpy(file_name, path_parts.drows[last_drow].words[last_word]);
|
237
237
|
file_name[len] = '\0';
|
238
238
|
|
239
|
-
|
239
|
+
cwdict_close(path_parts);
|
240
240
|
}
|
241
241
|
else
|
242
242
|
{
|
@@ -247,7 +247,7 @@ VALUE c_cw_dict_open(int argc, VALUE* argv, VALUE obj)
|
|
247
247
|
|
248
248
|
if(include(file_name, "."))
|
249
249
|
{
|
250
|
-
file_parts =
|
250
|
+
file_parts = split_into_cwdict(file_name, ".");
|
251
251
|
|
252
252
|
size_t len = strlen(file_parts.drows[0].words[0]);
|
253
253
|
|
@@ -255,7 +255,7 @@ VALUE c_cw_dict_open(int argc, VALUE* argv, VALUE obj)
|
|
255
255
|
strcpy(name, file_parts.drows[0].words[0]);
|
256
256
|
name[len] = '\0';
|
257
257
|
|
258
|
-
|
258
|
+
cwdict_close(file_parts);
|
259
259
|
}
|
260
260
|
else
|
261
261
|
{
|
@@ -282,26 +282,26 @@ VALUE c_cw_dict_open(int argc, VALUE* argv, VALUE obj)
|
|
282
282
|
}
|
283
283
|
|
284
284
|
// check if tkns references existing, embedded dictionary...
|
285
|
-
if(!tkns_ptr && !name_ptr && !used_file) d =
|
285
|
+
if(!tkns_ptr && !name_ptr && !used_file) d = cwdict_open();
|
286
286
|
else if(tkns_ptr && !used_file)
|
287
287
|
{
|
288
288
|
if(strcmp(tkns_ptr, "seussian") == 0)
|
289
289
|
{
|
290
290
|
if(!name_ptr)
|
291
|
-
d
|
291
|
+
d=cwdict_open_with_name_and_tokens("seussian",dict_seuss,DELIMITERS);
|
292
292
|
else
|
293
|
-
d
|
293
|
+
d=cwdict_open_with_name_and_tokens(name_ptr,dict_seuss,DELIMITERS);
|
294
294
|
}
|
295
295
|
else if(strcmp(tkns_ptr, "latin") == 0)
|
296
296
|
{
|
297
297
|
if(!name_ptr)
|
298
|
-
d
|
298
|
+
d=cwdict_open_with_name_and_tokens("latin", dict_latin,DELIMITERS);
|
299
299
|
else
|
300
|
-
d
|
300
|
+
d=cwdict_open_with_name_and_tokens(name_ptr, dict_latin,DELIMITERS);
|
301
301
|
}
|
302
302
|
}
|
303
303
|
// ...else, if just a name was passed...
|
304
|
-
else if(name_ptr && !used_file) d =
|
304
|
+
else if(name_ptr && !used_file) d = cwdict_open_with_name(name_ptr);
|
305
305
|
// ...else, see if file exists by passed name...
|
306
306
|
else if(used_file && name && file_ptr)
|
307
307
|
{
|
@@ -312,52 +312,52 @@ VALUE c_cw_dict_open(int argc, VALUE* argv, VALUE obj)
|
|
312
312
|
file_name);
|
313
313
|
}
|
314
314
|
|
315
|
-
d
|
315
|
+
d=cwdict_open_with_name_and_tokens(name, file_buffer, DELIMITERS);
|
316
316
|
|
317
317
|
free(file_buffer);
|
318
318
|
}
|
319
319
|
// ...else, return a blank dictionary
|
320
320
|
|
321
321
|
// create a dictionary pointer
|
322
|
-
|
322
|
+
cwdict_t* d_ptr = (cwdict_t*)malloc(sizeof(cwdict_t));
|
323
323
|
*d_ptr = d;
|
324
324
|
|
325
325
|
if(name) free(name);
|
326
326
|
if(file_name) free(file_name);
|
327
327
|
|
328
|
-
return Data_Wrap_Struct(
|
328
|
+
return Data_Wrap_Struct(c_cwdict, 0, c_cwdict_free, d_ptr);
|
329
329
|
}
|
330
330
|
|
331
|
-
VALUE
|
331
|
+
VALUE c_cwdict_close(VALUE obj)
|
332
332
|
{
|
333
|
-
|
334
|
-
|
333
|
+
cwdict_t* d;
|
334
|
+
cwdict_t empty;
|
335
335
|
|
336
336
|
// get original pointer from Ruby VM and close
|
337
|
-
Data_Get_Struct(obj,
|
337
|
+
Data_Get_Struct(obj, cwdict_t, d);
|
338
338
|
|
339
|
-
if(d->drows && d->count > 0) { *d =
|
339
|
+
if(d->drows && d->count > 0) { *d = cwdict_close(*d); }
|
340
340
|
|
341
341
|
return obj;
|
342
342
|
}
|
343
343
|
|
344
|
-
VALUE
|
344
|
+
VALUE c_cwdict_name_g(VALUE obj)
|
345
345
|
{
|
346
|
-
|
346
|
+
cwdict_t* d;
|
347
347
|
|
348
348
|
// get original pointer from Ruby VM
|
349
|
-
Data_Get_Struct(obj,
|
349
|
+
Data_Get_Struct(obj, cwdict_t, d);
|
350
350
|
|
351
351
|
if(d->name && strlen(d->name) > 0) return rb_str_new2(d->name);
|
352
352
|
return rb_str_new2("");
|
353
353
|
}
|
354
354
|
|
355
|
-
VALUE
|
355
|
+
VALUE c_cwdict_name_s(VALUE obj, VALUE name)
|
356
356
|
{
|
357
|
-
|
357
|
+
cwdict_t* d; long len = RSTRING_LEN(name);
|
358
358
|
|
359
359
|
// get original pointer from Ruby VM
|
360
|
-
Data_Get_Struct(obj,
|
360
|
+
Data_Get_Struct(obj, cwdict_t, d);
|
361
361
|
|
362
362
|
d->name = (char*)malloc(len + 1);
|
363
363
|
strcpy(d->name, StringValueCStr(name));
|
@@ -366,155 +366,235 @@ VALUE c_cw_dict_name_s(VALUE obj, VALUE name)
|
|
366
366
|
return obj;
|
367
367
|
}
|
368
368
|
|
369
|
-
VALUE
|
369
|
+
VALUE c_cwdict_length(VALUE obj)
|
370
370
|
{
|
371
|
-
|
371
|
+
cwdict_t* d;
|
372
372
|
|
373
373
|
// get original pointer from Ruby VM
|
374
|
-
Data_Get_Struct(obj,
|
374
|
+
Data_Get_Struct(obj, cwdict_t, d);
|
375
375
|
|
376
376
|
if(d->count == 0) return INT2NUM(0);
|
377
|
-
return LONG2NUM(
|
377
|
+
return LONG2NUM(cwdict_length(*d));
|
378
378
|
}
|
379
379
|
|
380
|
-
VALUE
|
380
|
+
VALUE c_cwdict_enum_length(VALUE obj, VALUE args, VALUE eobj)
|
381
381
|
{
|
382
|
-
|
382
|
+
return c_cwdict_length(obj);
|
383
|
+
}
|
384
|
+
|
385
|
+
VALUE c_cwdict_join(int argc, VALUE* argv, VALUE obj)
|
386
|
+
{
|
387
|
+
cwdict_t* d;
|
383
388
|
|
384
389
|
// raise exception if passed wrong number of arguments
|
385
390
|
if(argc > 1) rb_raise(rb_eArgError, "wrong number of arguments");
|
386
391
|
if(argc == 1) Check_Type(argv[0], T_STRING);
|
387
392
|
|
388
393
|
// get original pointer from Ruby VM
|
389
|
-
Data_Get_Struct(obj,
|
394
|
+
Data_Get_Struct(obj, cwdict_t, d);
|
390
395
|
|
391
|
-
if(argc == 0) return rb_str_new2(
|
392
|
-
return rb_str_new2(
|
396
|
+
if(argc == 0) return rb_str_new2(cwdict_join(*d, " "));
|
397
|
+
return rb_str_new2(cwdict_join(*d, StringValueCStr(argv[0])));
|
393
398
|
}
|
394
399
|
|
395
|
-
VALUE
|
400
|
+
VALUE c_cwdict_clone(VALUE obj)
|
396
401
|
{
|
397
|
-
|
402
|
+
cwdict_t* d, *new_p;
|
398
403
|
VALUE new;
|
399
404
|
|
400
405
|
// open new dict for return value
|
401
406
|
VALUE args[] = { rb_str_new2(""), rb_str_new2("") };
|
402
|
-
new =
|
407
|
+
new = c_cwdict_open(2, args, new);
|
403
408
|
|
404
409
|
// get original pointers from Ruby VM
|
405
|
-
Data_Get_Struct(obj,
|
406
|
-
Data_Get_Struct(new,
|
410
|
+
Data_Get_Struct(obj, cwdict_t, d);
|
411
|
+
Data_Get_Struct(new, cwdict_t, new_p);
|
407
412
|
|
408
413
|
// get a copy of the original dictionary
|
409
|
-
*new_p =
|
414
|
+
*new_p = cwdict_clone(*d);
|
410
415
|
|
411
416
|
return new;
|
412
417
|
}
|
413
418
|
|
414
|
-
VALUE
|
419
|
+
VALUE c_cwdict_sample(VALUE obj)
|
415
420
|
{
|
416
|
-
|
421
|
+
cwdict_t* d;
|
417
422
|
|
418
423
|
// get original pointer from Ruby VM
|
419
|
-
Data_Get_Struct(obj,
|
424
|
+
Data_Get_Struct(obj, cwdict_t, d);
|
425
|
+
|
426
|
+
return rb_str_new2(cwdict_sample(*d));
|
427
|
+
}
|
428
|
+
|
429
|
+
VALUE c_cwdict_each(VALUE obj)
|
430
|
+
{
|
431
|
+
cwdict_t *d;
|
432
|
+
|
433
|
+
// get original pointers from Ruby VM
|
434
|
+
Data_Get_Struct(obj, cwdict_t, d);
|
435
|
+
|
436
|
+
RETURN_SIZED_ENUMERATOR(obj, 0, 0, c_cwdict_enum_length);
|
437
|
+
|
438
|
+
for(U32 i = 0; i != d->count; ++i)
|
439
|
+
{
|
440
|
+
for(U32 j = 0; j != d->drows[i].count; ++j)
|
441
|
+
{
|
442
|
+
rb_yield(rb_str_new2(d->drows[i].words[j]));
|
443
|
+
}
|
444
|
+
}
|
445
|
+
|
446
|
+
return obj;
|
447
|
+
}
|
448
|
+
|
449
|
+
VALUE c_cwdict_each_index(VALUE obj)
|
450
|
+
{
|
451
|
+
long num = 0; cwdict_t *d;
|
452
|
+
|
453
|
+
// get original pointers from Ruby VM
|
454
|
+
Data_Get_Struct(obj, cwdict_t, d);
|
455
|
+
|
456
|
+
RETURN_SIZED_ENUMERATOR(obj, 0, 0, c_cwdict_enum_length);
|
457
|
+
|
458
|
+
for(U32 i = 0; i != d->count; ++i)
|
459
|
+
{
|
460
|
+
for(U32 j = 0; j != d->drows[i].count; ++j)
|
461
|
+
{
|
462
|
+
rb_yield(LONG2NUM(++num));
|
463
|
+
}
|
464
|
+
}
|
465
|
+
|
466
|
+
return obj;
|
467
|
+
}
|
468
|
+
|
469
|
+
VALUE c_cwdict_map_s(VALUE obj);
|
470
|
+
VALUE c_cwdict_map(VALUE obj)
|
471
|
+
{
|
472
|
+
VALUE new;
|
473
|
+
|
474
|
+
// get a new copy of the original dict
|
475
|
+
new = c_cwdict_clone(obj);
|
476
|
+
new = c_cwdict_map_s(new);
|
477
|
+
|
478
|
+
return new;
|
479
|
+
}
|
420
480
|
|
421
|
-
|
481
|
+
VALUE c_cwdict_map_s(VALUE obj)
|
482
|
+
{
|
483
|
+
cwdict_t *d;
|
484
|
+
|
485
|
+
// get original pointers from Ruby VM
|
486
|
+
Data_Get_Struct(obj, cwdict_t, d);
|
487
|
+
|
488
|
+
for(U32 i = 0; i != d->count; ++i)
|
489
|
+
{
|
490
|
+
for(U32 j = 0; j != d->drows[i].count; ++j)
|
491
|
+
{
|
492
|
+
VALUE ref = rb_yield(rb_str_new2(d->drows[i].words[j]));
|
493
|
+
long len = RSTRING_LEN(ref);
|
494
|
+
|
495
|
+
d->drows[i].words[j] = (char*)realloc(d->drows[i].words[j], len + 1);
|
496
|
+
strcpy(d->drows[i].words[j], StringValueCStr(ref));
|
497
|
+
d->drows[i].words[j][len] = '\0';
|
498
|
+
}
|
499
|
+
}
|
500
|
+
|
501
|
+
return obj;
|
422
502
|
}
|
423
503
|
|
424
|
-
VALUE
|
504
|
+
VALUE c_cwdict_sort(VALUE obj)
|
425
505
|
{
|
426
|
-
|
506
|
+
cwdict_t* d;
|
427
507
|
VALUE new;
|
428
508
|
|
429
509
|
// get a new copy of the original dict
|
430
|
-
new =
|
510
|
+
new = c_cwdict_clone(obj);
|
431
511
|
|
432
512
|
// get original pointer from Ruby VM
|
433
|
-
Data_Get_Struct(new,
|
513
|
+
Data_Get_Struct(new, cwdict_t, d);
|
434
514
|
|
435
|
-
*d =
|
515
|
+
*d = cwdict_sort(*d);
|
436
516
|
|
437
517
|
return new;
|
438
518
|
}
|
439
519
|
|
440
|
-
VALUE
|
520
|
+
VALUE c_cwdict_prune(VALUE obj)
|
441
521
|
{
|
442
|
-
|
522
|
+
cwdict_t* d;
|
443
523
|
VALUE new;
|
444
524
|
|
445
525
|
// get a new copy of the original dict
|
446
|
-
new =
|
526
|
+
new = c_cwdict_clone(obj);
|
447
527
|
|
448
528
|
// get original pointer from Ruby VM
|
449
|
-
Data_Get_Struct(new,
|
529
|
+
Data_Get_Struct(new, cwdict_t, d);
|
450
530
|
|
451
|
-
*d =
|
531
|
+
*d = cwdict_prune(*d, false);
|
452
532
|
|
453
533
|
return new;
|
454
534
|
}
|
455
535
|
|
456
|
-
VALUE
|
536
|
+
VALUE c_cwdict_clean(VALUE obj)
|
457
537
|
{
|
458
|
-
|
538
|
+
cwdict_t* d;
|
459
539
|
VALUE new;
|
460
540
|
|
461
541
|
// get a new copy of the original dict
|
462
|
-
new =
|
542
|
+
new = c_cwdict_clone(obj);
|
463
543
|
|
464
544
|
// get original pointer from Ruby VM
|
465
|
-
Data_Get_Struct(new,
|
545
|
+
Data_Get_Struct(new, cwdict_t, d);
|
466
546
|
|
467
|
-
*d =
|
547
|
+
*d = cwdict_prune(*d, true);
|
468
548
|
|
469
549
|
return new;
|
470
550
|
}
|
471
551
|
|
472
|
-
VALUE
|
552
|
+
VALUE c_cwdict_sort_s(VALUE obj)
|
473
553
|
{
|
474
|
-
|
554
|
+
cwdict_t* d;
|
475
555
|
|
476
556
|
// get original pointer from Ruby VM
|
477
|
-
Data_Get_Struct(obj,
|
557
|
+
Data_Get_Struct(obj, cwdict_t, d);
|
478
558
|
|
479
|
-
*d =
|
559
|
+
*d = cwdict_sort(*d);
|
480
560
|
|
481
561
|
return obj;
|
482
562
|
}
|
483
563
|
|
484
|
-
VALUE
|
564
|
+
VALUE c_cwdict_prune_s(VALUE obj)
|
485
565
|
{
|
486
|
-
|
566
|
+
cwdict_t* d;
|
487
567
|
|
488
568
|
// get original pointer from Ruby VM
|
489
|
-
Data_Get_Struct(obj,
|
569
|
+
Data_Get_Struct(obj, cwdict_t, d);
|
490
570
|
|
491
|
-
*d =
|
571
|
+
*d = cwdict_prune(*d, false);
|
492
572
|
|
493
573
|
return obj;
|
494
574
|
}
|
495
575
|
|
496
|
-
VALUE
|
576
|
+
VALUE c_cwdict_clean_s(VALUE obj)
|
497
577
|
{
|
498
|
-
|
578
|
+
cwdict_t* d;
|
499
579
|
|
500
580
|
// get original pointer from Ruby VM
|
501
|
-
Data_Get_Struct(obj,
|
581
|
+
Data_Get_Struct(obj, cwdict_t, d);
|
502
582
|
|
503
|
-
*d =
|
583
|
+
*d = cwdict_prune(*d, true);
|
504
584
|
|
505
585
|
return obj;
|
506
586
|
}
|
507
587
|
|
508
|
-
VALUE
|
588
|
+
VALUE c_cwdict_validate_s(VALUE obj)
|
509
589
|
{
|
510
590
|
char* e;
|
511
|
-
|
591
|
+
cwdict_t* d;
|
512
592
|
|
513
593
|
// get original pointer from Ruby VM
|
514
|
-
Data_Get_Struct(obj,
|
594
|
+
Data_Get_Struct(obj, cwdict_t, d);
|
515
595
|
|
516
596
|
// handle invalid state first (for error handling's sake)
|
517
|
-
if(!
|
597
|
+
if(!cwdict_valid(*d, &e))
|
518
598
|
{
|
519
599
|
rb_raise(rb_eException, "%s", e);
|
520
600
|
free(e);
|
@@ -523,27 +603,27 @@ VALUE c_cw_dict_validate_s(VALUE obj)
|
|
523
603
|
return obj;
|
524
604
|
}
|
525
605
|
|
526
|
-
VALUE
|
606
|
+
VALUE c_cwdict_named_q(VALUE obj)
|
527
607
|
{
|
528
|
-
|
608
|
+
cwdict_t* d;
|
529
609
|
|
530
610
|
// get original pointer from Ruby VM
|
531
|
-
Data_Get_Struct(obj,
|
611
|
+
Data_Get_Struct(obj, cwdict_t, d);
|
532
612
|
|
533
613
|
if(d->name && strlen(d->name) > 0) return Qtrue;
|
534
614
|
return Qfalse;
|
535
615
|
}
|
536
616
|
|
537
|
-
VALUE
|
617
|
+
VALUE c_cwdict_valid_q(VALUE obj)
|
538
618
|
{
|
539
619
|
char* e;
|
540
|
-
|
620
|
+
cwdict_t* d;
|
541
621
|
|
542
622
|
// get original pointer from Ruby VM
|
543
|
-
Data_Get_Struct(obj,
|
623
|
+
Data_Get_Struct(obj, cwdict_t, d);
|
544
624
|
|
545
625
|
// handle invalid state first (for error handling's sake)
|
546
|
-
if(!
|
626
|
+
if(!cwdict_valid(*d, &e))
|
547
627
|
{
|
548
628
|
rb_raise(rb_eException, "%s", e);
|
549
629
|
free(e);
|
@@ -554,47 +634,47 @@ VALUE c_cw_dict_valid_q(VALUE obj)
|
|
554
634
|
return Qtrue;
|
555
635
|
}
|
556
636
|
|
557
|
-
VALUE
|
637
|
+
VALUE c_cwdict_sorted_q(VALUE obj)
|
558
638
|
{
|
559
|
-
|
639
|
+
cwdict_t* d;
|
560
640
|
|
561
641
|
// get original pointer from Ruby VM
|
562
|
-
Data_Get_Struct(obj,
|
642
|
+
Data_Get_Struct(obj, cwdict_t, d);
|
563
643
|
|
564
644
|
if(d->sorted) return Qtrue;
|
565
645
|
return Qfalse;
|
566
646
|
}
|
567
647
|
|
568
|
-
VALUE
|
648
|
+
VALUE c_cwdict_include_q(VALUE obj, VALUE string)
|
569
649
|
{
|
570
|
-
|
650
|
+
cwdict_t* d;
|
571
651
|
|
572
652
|
// get original pointer from Ruby VM
|
573
|
-
Data_Get_Struct(obj,
|
653
|
+
Data_Get_Struct(obj, cwdict_t, d);
|
574
654
|
|
575
|
-
if(
|
655
|
+
if(cwdict_include(*d, StringValueCStr(string))) return Qtrue;
|
576
656
|
return Qfalse;
|
577
657
|
}
|
578
658
|
|
579
|
-
VALUE
|
659
|
+
VALUE c_cwdict_exclude_q(VALUE obj, VALUE string)
|
580
660
|
{
|
581
|
-
|
661
|
+
cwdict_t* d;
|
582
662
|
|
583
663
|
// get original pointer from Ruby VM
|
584
|
-
Data_Get_Struct(obj,
|
664
|
+
Data_Get_Struct(obj, cwdict_t, d);
|
585
665
|
|
586
|
-
if(
|
666
|
+
if(cwdict_exclude(*d, StringValueCStr(string))) return Qtrue;
|
587
667
|
return Qfalse;
|
588
668
|
}
|
589
669
|
|
590
|
-
VALUE
|
670
|
+
VALUE c_cwdict_inspect(VALUE obj)
|
591
671
|
{
|
592
|
-
|
672
|
+
cwdict_t* dict; VALUE str;
|
593
673
|
size_t count = 0; int multiplier = 1; int word_len = 0;
|
594
674
|
char* result = (char*)malloc(LARGE_BUFFER * multiplier + 1);
|
595
675
|
|
596
676
|
// get original pointer from Ruby VM
|
597
|
-
Data_Get_Struct(obj,
|
677
|
+
Data_Get_Struct(obj, cwdict_t, dict);
|
598
678
|
|
599
679
|
// add opening delimiter
|
600
680
|
strcpy(result, "["); ++count;
|
@@ -657,29 +737,29 @@ VALUE c_cw_dict_inspect(VALUE obj)
|
|
657
737
|
return str;
|
658
738
|
}
|
659
739
|
|
660
|
-
VALUE
|
740
|
+
VALUE c_cwdict_to_s(VALUE obj)
|
661
741
|
{
|
662
|
-
|
742
|
+
cwdict_t* dict;
|
663
743
|
size_t count = 0; int multiplier = 1;
|
664
744
|
|
665
745
|
// get original pointer from Ruby VM
|
666
|
-
Data_Get_Struct(obj,
|
746
|
+
Data_Get_Struct(obj, cwdict_t, dict);
|
667
747
|
|
668
748
|
// if(dict->drows && dict->count > 0) return Qnil;
|
669
|
-
return
|
749
|
+
return c_cwdict_inspect(obj);
|
670
750
|
}
|
671
751
|
|
672
|
-
VALUE
|
752
|
+
VALUE c_cwdict_append_op(VALUE obj, VALUE addend)
|
673
753
|
{
|
674
|
-
|
754
|
+
cwdict_t* d;
|
675
755
|
|
676
756
|
// get original pointer from Ruby VM
|
677
|
-
Data_Get_Struct(obj,
|
757
|
+
Data_Get_Struct(obj, cwdict_t, d);
|
678
758
|
|
679
759
|
switch(TYPE(addend))
|
680
760
|
{
|
681
761
|
case T_STRING:
|
682
|
-
*d =
|
762
|
+
*d = cwdict_place_word_strict(*d, StringValueCStr(addend));
|
683
763
|
break;
|
684
764
|
case T_ARRAY:
|
685
765
|
for(long i = 0; i != RARRAY_LEN(addend); ++i)
|
@@ -694,7 +774,7 @@ VALUE c_cw_dict_append_op(VALUE obj, VALUE addend)
|
|
694
774
|
VALUE entry = rb_ary_entry(addend, i);
|
695
775
|
char* entry_str = StringValueCStr(entry);
|
696
776
|
|
697
|
-
*d =
|
777
|
+
*d = cwdict_place_word_strict(*d, entry_str);
|
698
778
|
}
|
699
779
|
|
700
780
|
break;
|
@@ -703,46 +783,46 @@ VALUE c_cw_dict_append_op(VALUE obj, VALUE addend)
|
|
703
783
|
break;
|
704
784
|
}
|
705
785
|
|
706
|
-
if(d->sorted) return
|
786
|
+
if(d->sorted) return c_cwdict_sort(obj);
|
707
787
|
return obj;
|
708
788
|
}
|
709
789
|
|
710
|
-
VALUE
|
790
|
+
VALUE c_cwdict_add_op(VALUE obj, VALUE addend)
|
711
791
|
{
|
712
792
|
VALUE new;
|
713
793
|
|
714
794
|
// get a clone of the original
|
715
|
-
new =
|
795
|
+
new = c_cwdict_clone(obj);
|
716
796
|
|
717
|
-
return
|
797
|
+
return c_cwdict_append_op(new, addend);
|
718
798
|
}
|
719
799
|
|
720
|
-
VALUE
|
800
|
+
VALUE c_cwdict_add_assign_op(VALUE obj, VALUE addend)
|
721
801
|
{
|
722
|
-
return
|
802
|
+
return c_cwdict_append_op(obj, addend);
|
723
803
|
}
|
724
804
|
|
725
|
-
VALUE
|
805
|
+
VALUE c_cwdict_check_equality(VALUE obj, VALUE against)
|
726
806
|
{
|
727
|
-
|
807
|
+
cwdict_t* d, *comparison;
|
728
808
|
|
729
809
|
// get original pointers from Ruby VM
|
730
|
-
Data_Get_Struct(obj,
|
731
|
-
Data_Get_Struct(against,
|
810
|
+
Data_Get_Struct(obj, cwdict_t, d);
|
811
|
+
Data_Get_Struct(against, cwdict_t, comparison);
|
732
812
|
|
733
|
-
if(
|
813
|
+
if(cwdict_equal(*d, *comparison)) return Qtrue;
|
734
814
|
return Qfalse;
|
735
815
|
}
|
736
816
|
|
737
|
-
VALUE
|
817
|
+
VALUE c_cwdict_check_inequality(VALUE obj, VALUE against)
|
738
818
|
{
|
739
|
-
|
819
|
+
cwdict_t* d, *comparison;
|
740
820
|
|
741
821
|
// get original pointers from Ruby VM
|
742
|
-
Data_Get_Struct(obj,
|
743
|
-
Data_Get_Struct(against,
|
822
|
+
Data_Get_Struct(obj, cwdict_t, d);
|
823
|
+
Data_Get_Struct(against, cwdict_t, comparison);
|
744
824
|
|
745
|
-
if(
|
825
|
+
if(cwdict_inequal(*d, *comparison)) return Qtrue;
|
746
826
|
return Qfalse;
|
747
827
|
}
|
748
828
|
|
@@ -750,7 +830,10 @@ void Init_chinwag()
|
|
750
830
|
{
|
751
831
|
// setup module extension and containing class(es)
|
752
832
|
m_chinwag = rb_define_module("Chinwag");
|
753
|
-
|
833
|
+
c_cwdict = rb_define_class_under(m_chinwag, "CWDict", rb_cObject);
|
834
|
+
|
835
|
+
// note module inclusions
|
836
|
+
rb_include_module(c_cwdict, rb_mEnumerable);
|
754
837
|
|
755
838
|
// sync up module generation functions
|
756
839
|
rb_define_module_function(m_chinwag, "generate", m_cw_generate, -1);
|
@@ -760,50 +843,55 @@ void Init_chinwag()
|
|
760
843
|
rb_define_module_function(m_chinwag, "set_default_max_output",m_s_max,1);
|
761
844
|
|
762
845
|
// sync up class methods
|
763
|
-
rb_define_singleton_method(
|
764
|
-
rb_define_method(
|
846
|
+
rb_define_singleton_method(c_cwdict, "open", c_cwdict_open, -1);
|
847
|
+
rb_define_method(c_cwdict, "close", c_cwdict_close, 0);
|
848
|
+
|
849
|
+
rb_define_method(c_cwdict, "name", c_cwdict_name_g, 0);
|
850
|
+
rb_define_method(c_cwdict, "name=", c_cwdict_name_s, 1);
|
851
|
+
rb_define_method(c_cwdict, "length", c_cwdict_length, 0);
|
765
852
|
|
766
|
-
rb_define_method(
|
767
|
-
rb_define_method(
|
768
|
-
rb_define_method(
|
853
|
+
rb_define_method(c_cwdict, "join", c_cwdict_join, -1);
|
854
|
+
rb_define_method(c_cwdict, "clone", c_cwdict_clone, 0);
|
855
|
+
rb_define_method(c_cwdict, "sample", c_cwdict_sample, 0);
|
769
856
|
|
770
|
-
rb_define_method(
|
771
|
-
rb_define_method(
|
772
|
-
rb_define_method(
|
857
|
+
rb_define_method(c_cwdict, "each", c_cwdict_each, 0);
|
858
|
+
rb_define_method(c_cwdict, "each_index", c_cwdict_each_index, 0);
|
859
|
+
rb_define_method(c_cwdict, "map", c_cwdict_map, 0);
|
860
|
+
rb_define_method(c_cwdict, "map!", c_cwdict_map_s, 0);
|
773
861
|
|
774
|
-
rb_define_method(
|
775
|
-
rb_define_method(
|
776
|
-
rb_define_method(
|
862
|
+
rb_define_method(c_cwdict, "sort", c_cwdict_sort, 0);
|
863
|
+
rb_define_method(c_cwdict, "prune", c_cwdict_prune, 0);
|
864
|
+
rb_define_method(c_cwdict, "clean", c_cwdict_clean, 0);
|
777
865
|
|
778
|
-
rb_define_method(
|
779
|
-
rb_define_method(
|
780
|
-
rb_define_method(
|
781
|
-
rb_define_method(
|
866
|
+
rb_define_method(c_cwdict, "sort!", c_cwdict_sort_s, 0);
|
867
|
+
rb_define_method(c_cwdict, "prune!", c_cwdict_prune_s, 0);
|
868
|
+
rb_define_method(c_cwdict, "clean!", c_cwdict_clean_s, 0);
|
869
|
+
rb_define_method(c_cwdict, "validate!", c_cwdict_validate_s, 0);
|
782
870
|
|
783
|
-
rb_define_method(
|
784
|
-
rb_define_method(
|
785
|
-
rb_define_method(
|
786
|
-
rb_define_method(
|
787
|
-
rb_define_method(
|
871
|
+
rb_define_method(c_cwdict, "named?", c_cwdict_named_q, 0);
|
872
|
+
rb_define_method(c_cwdict, "valid?", c_cwdict_valid_q, 0);
|
873
|
+
rb_define_method(c_cwdict, "sorted?", c_cwdict_sorted_q, 0);
|
874
|
+
rb_define_method(c_cwdict, "include?", c_cwdict_include_q, 1);
|
875
|
+
rb_define_method(c_cwdict, "exclude?", c_cwdict_exclude_q, 1);
|
788
876
|
|
789
|
-
rb_define_method(
|
790
|
-
rb_define_method(
|
877
|
+
rb_define_method(c_cwdict, "inspect", c_cwdict_inspect, 0);
|
878
|
+
rb_define_method(c_cwdict, "to_s", c_cwdict_to_s, 0);
|
791
879
|
|
792
880
|
// operator methods
|
793
|
-
rb_define_method(
|
794
|
-
rb_define_method(
|
795
|
-
rb_define_method(
|
796
|
-
rb_define_method(
|
797
|
-
rb_define_method(
|
881
|
+
rb_define_method(c_cwdict, "+", c_cwdict_add_op, 1);
|
882
|
+
rb_define_method(c_cwdict, "+=", c_cwdict_add_assign_op, 1);
|
883
|
+
rb_define_method(c_cwdict, "<<", c_cwdict_append_op, 1);
|
884
|
+
rb_define_method(c_cwdict, "==", c_cwdict_check_equality, 1);
|
885
|
+
rb_define_method(c_cwdict, "!=", c_cwdict_check_inequality, 1);
|
798
886
|
|
799
887
|
// method aliases
|
800
|
-
rb_define_alias(
|
801
|
-
rb_define_alias(
|
802
|
-
rb_define_alias(
|
888
|
+
rb_define_alias(c_cwdict, "dup", "clone");
|
889
|
+
rb_define_alias(c_cwdict, "size", "length");
|
890
|
+
rb_define_alias(c_cwdict, "count", "length");
|
803
891
|
|
804
892
|
// open Seussian dict as default fall-back
|
805
893
|
VALUE args[] = { rb_str_new2("seussian") };
|
806
|
-
default_dict =
|
894
|
+
default_dict = c_cwdict_open(1, args, default_dict);
|
807
895
|
|
808
896
|
// set default output type
|
809
897
|
default_output_type = ID2SYM(rb_intern("words"));
|