/
Cool.pm6
581 lines (502 loc) Β· 21.2 KB
/
Cool.pm6
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
my class Cool { # declared in BOOTSTRAP
# class Cool is Any
## numeric methods
method abs() { self.Numeric.abs }
method conj() { self.Numeric.conj }
method sqrt() { self.Numeric.sqrt }
method sign() { self.Real.sign }
method rand() { self.Num.rand }
method sin() { self.Numeric.sin }
method asin() { self.Numeric.asin }
method cos() { self.Numeric.cos }
method acos() { self.Numeric.acos }
method tan() { self.Numeric.tan }
method atan() { self.Numeric.atan }
method atan2($y = 1e0) { self.Numeric.atan2($y.Numeric) }
method sec() { self.Numeric.sec }
method asec() { self.Numeric.asec }
method cosec() { self.Numeric.cosec }
method acosec() { self.Numeric.acosec }
method cotan() { self.Numeric.cotan }
method acotan() { self.Numeric.acotan }
method sinh() { self.Numeric.sinh }
method asinh() { self.Numeric.asinh }
method cosh() { self.Numeric.cosh }
method acosh() { self.Numeric.acosh }
method tanh() { self.Numeric.tanh }
method atanh() { self.Numeric.atanh }
method sech() { self.Numeric.sech }
method asech() { self.Numeric.asech }
method cosech() { self.Numeric.cosech }
method acosech() { self.Numeric.acosech }
method cotanh() { self.Numeric.cotanh }
method acotanh() { self.Numeric.acotanh }
method cis() { self.Numeric.cis }
method is-prime(--> Bool:D) { self.Real.is-prime }
proto method log(|) {*}
multi method log(Cool:D: ) { self.Numeric.log }
multi method log(Cool:D: $base) { self.Numeric.log($base.Numeric) }
proto method exp(|) {*}
multi method exp(Cool:D: ) { self.Numeric.exp }
multi method exp(Cool:D: $base) { self.Numeric.exp($base.Numeric) }
proto method round(|) {*}
multi method round() { self.Numeric.round() }
multi method round($base) { self.Numeric.round($base) }
method roots(Cool $n) { self.Numeric.roots($n) }
method log10() { self.Numeric.log10 }
method unpolar($n) { self.Numeric.unpolar($n.Numeric) }
method floor() { self.Numeric.floor }
method ceiling() { self.Numeric.ceiling }
method truncate() { self.Numeric.truncate }
## string methods
method chars(--> Int:D) {
self.Str.chars
}
method codes() {
self.Str.codes
}
method fmt($format = '%s') {
Rakudo::Internals.initialize-sprintf-handler;
nqp::p6box_s(
nqp::sprintf(nqp::unbox_s($format.Stringy), nqp::list(self))
)
}
method uc() {
self.Str.uc
}
method lc() {
self.Str.lc
}
method tc() {
self.Str.tc
}
method fc() {
self.Str.fc
}
method tclc() {
self.Str.tclc
}
method wordcase() { self.Str.wordcase }
method uniname() { uniname(self) }
method uninames() { uninames(self) }
method unival() { unival(self) }
method univals() { univals(self) }
method uniprop(|c) { uniprop(self, |c) }
method uniprop-int(|c) { uniprop-int(self, |c) }
method uniprop-bool(|c) { uniprop-bool(self, |c) }
method uniprop-str(|c) { uniprop-str(self, |c) }
method uniprops(|c) { uniprops(self, |c) }
method unimatch(|c) { unimatch(self, |c) }
method chomp(Cool:D:) { self.Str.chomp }
proto method chop(|) {*}
multi method chop(Cool:D:) { self.Str.chop }
multi method chop(Cool:D: Int() $n) { self.Str.chop($n) }
method ord(--> Int:D) {
self.Str.ord
}
method chr() {
self.Int.chr;
}
proto method chrs(|) {*}
multi method chrs(Cool:D:) { self.list.chrs }
proto method ords(|) {*}
multi method ords(Cool:D:) { self.Str.ords }
method flip() {
self.Str.flip
}
method trans(|c) { self.Str.trans(|c) }
method starts-with(Cool:D: |c) {
self.Str.starts-with(|c)
}
method ends-with(Cool:D: |c) {
self.Str.ends-with(|c)
}
proto method substr(|) {*}
multi method substr() { self.Str.substr }
multi method substr(\from) { self.Str.substr(from) }
multi method substr(\from, \chars) { self.Str.substr(from,chars) }
proto method substr-rw(|) {*}
multi method substr-rw(\SELF:) is rw {
(SELF = self.Str).substr-rw
}
multi method substr-rw(\SELF: \from) is rw {
(SELF = self.Str).substr-rw(from)
}
multi method substr-rw(\SELF: \from, \chars) is rw {
(SELF = self.Str).substr-rw(from,chars)
}
method substr-eq(Cool:D: |c) {
self.Str.substr-eq(|c)
}
method contains(Cool:D: |c) {
self.Str.contains(|c)
}
method indices(Cool:D: |c) {
self.Str.indices(|c)
}
method index(Cool:D: |c) {
self.Str.index(|c)
}
method rindex(Cool:D: |c) {
self.Str.rindex(|c)
}
method split(Cool: |c) {
self.Stringy.split(|c);
}
method match(Cool:D: |c) {
$/ := nqp::getlexcaller('$/');
self.Stringy.match(|c)
}
method comb(|c) { self.Str.comb(|c) }
method lines(Cool:D: |c) { self.Str.lines(|c) }
method words(Cool:D: |c) { self.Str.words(|c) }
method subst(|c) {
$/ := nqp::getlexcaller('$/');
self.Stringy.subst(|c);
}
# `$value-to-subst-mutate` will show up in errors when called on non-rw
# container, so use more descriptive name instead of just `$self`
method subst-mutate(Cool:D $value-to-subst-mutate is rw: |c) {
$/ := nqp::getlexcaller('$/');
my $str = $value-to-subst-mutate.Str;
my $match := $str.subst-mutate(|c);
$value-to-subst-mutate = $str if $match; # only change if successful
$match
}
proto method IO(|) {*}
multi method IO(Cool:D:) { IO::Path.new(self) }
multi method IO(Cool:U:) { IO::Path }
method sprintf(*@args) { sprintf(self, @args) };
method printf (*@args) { printf(self, @args) };
method samecase(Cool:D: Cool $pattern) { self.Stringy.samecase($pattern) }
method path() { self.Stringy.IO }
method trim () { self.Stringy.trim };
method trim-leading () { self.Stringy.trim-leading };
method trim-trailing() { self.Stringy.trim-trailing };
method EVAL(*%opts) {
EVAL(self, context => CALLER::, |%opts);
}
multi method Real() {
nqp::if(
nqp::istype((my $numeric := self.Numeric), Failure),
$numeric,
$numeric.Real
)
}
proto method Int(|) {*}
multi method Int() {
nqp::if(
nqp::istype((my $numeric := self.Numeric), Failure),
$numeric,
$numeric.Int
)
}
proto method UInt(|) {*}
multi method UInt() {
my $got := self.Int;
$got < 0
?? Failure.new(X::OutOfRange.new(
:what('Coercion to UInt'),
:$got,
:range<0..^Inf>))
!! $got
}
method Num() {
nqp::if(
nqp::istype((my $numeric := self.Numeric), Failure),
$numeric,
$numeric.Num
)
}
method Rat() {
nqp::if(
nqp::istype((my $numeric := self.Numeric), Failure),
$numeric,
$numeric.Rat
)
}
method FatRat() {
nqp::if(
nqp::istype((my $numeric := self.Numeric), Failure),
$numeric,
$numeric.FatRat
)
}
method Complex() {
nqp::if(
nqp::istype((my $numeric := self.Numeric), Failure),
$numeric,
$numeric.Complex
)
}
}
Metamodel::ClassHOW.exclude_parent(Cool);
proto sub chop(|) {*}
multi sub chop(Cool:D $s --> Str:D) { $s.chop }
multi sub chop(Cool:D $s, Int() $n --> Str:D) { $s.chop($n) }
proto sub chomp(|) {*}
multi sub chomp(Cool $s --> Str:D) { $s.chomp }
proto sub flip(|) {*}
multi sub flip(Cool $s --> Str:D) { $s.flip }
proto sub index(|) {*}
multi sub index(Cool $s, Cool $needle) { $s.index($needle) }
multi sub index(Cool $s, Cool $needle, Cool $pos) { $s.index($needle,$pos) }
proto sub rindex(|) {*}
multi sub rindex(Cool $s, Cool $needle, Cool $pos) { $s.rindex($needle, $pos) }
multi sub rindex(Cool $s, Cool $needle) { $s.rindex($needle) }
proto sub lc(|) {*}
multi sub lc(Cool $s) { $s.lc }
proto sub ord(|) {*}
multi sub ord(Cool $s) { $s.ord }
proto sub uc(|) {*}
multi sub uc(Cool $s) { $s.uc }
proto sub tc(|) {*}
multi sub tc(Cool $s) { $s.tc }
proto sub fc(|) {*}
multi sub fc(Cool $s) { $s.fc }
proto sub tclc(|) {*}
multi sub tclc(Cool $s) { $s.tclc }
proto sub indices(|) {*}
multi sub indices(Cool $s, |c) { $s.indices(|c) }
proto sub ords($) {*}
multi sub ords(Cool:D $s) { $s.ords }
proto sub comb($, $, $?) {*}
multi sub comb(Regex $matcher, Cool $input, $limit = *) { $input.comb($matcher, $limit) }
multi sub comb(Str $matcher, Cool $input, $limit = *) { $input.comb($matcher, $limit) }
multi sub comb(Int:D $matcher, Cool $input, $limit = *) { $input.comb($matcher, $limit) }
proto sub wordcase($) is pure {*}
multi sub wordcase(Str:D $x) {$x.wordcase }
multi sub wordcase(Cool $x) {$x.Str.wordcase }
proto sub sprintf(|) {*}
multi sub sprintf(Cool:D $format, *@args) {
CATCH {
when X::Cannot::Lazy {
X::Cannot::Lazy.new(:action('(s)printf')).throw
}
default {
Rakudo::Internals.HANDLE-NQP-SPRINTF-ERRORS($_).throw
}
}
Rakudo::Internals.initialize-sprintf-handler;
@args.elems;
nqp::p6box_s(
nqp::sprintf(nqp::unbox_s($format.Stringy),
nqp::clone(nqp::getattr(@args||[], List, '$!reified'))
)
)
}
proto sub printf(|) {*}
multi sub printf(Cool:D $format, *@args) { print sprintf $format, @args }
proto sub samecase(|) {*}
multi sub samecase(Cool:D $string, Cool:D $pattern) { $string.samecase($pattern) }
proto sub split(|) {*}
multi sub split($pat, Cool:D $target, |c) { $target.split($pat, |c) }
proto sub chars($) is pure {*}
multi sub chars(Cool $x) { $x.Str.chars }
multi sub chars(Str:D $x) { nqp::p6box_i(nqp::chars($x)) }
multi sub chars(str $x --> int) { nqp::chars($x) }
# These probably belong in a separate unicodey file
proto sub uniname(|) {*}
multi sub uniname(Str:D $str) { $str ?? uniname($str.ord) !! Nil }
multi sub uniname(Int:D $code) { nqp::getuniname($code) }
proto sub uninames(|) {*}
multi sub uninames(Str:D $str) { $str.NFC.map: { uniname($_) } }
#?if jvm
multi sub unival(|) { die 'unival NYI on jvm backend' }
multi sub univals(|) { die 'univals NYI on jvm backend' }
multi sub uniprop(|) { die 'uniprop NYI on jvm backend' }
multi sub uniprop-int(|) { die 'uniprop-int NYI on jvm backend' }
multi sub uniprop-bool(|) { die 'uniprop-bool NYI on jvm backend' }
multi sub uniprop-str(|) { die 'uniprop-str NYI on jvm backend' }
multi sub uniprops(|) { die 'uniprops NYI on jvm backend' }
multi sub unimatch(|) { die 'unimatch NYI on jvm backend' }
#?endif
#?if moar
proto sub uniprop(|) {*}
multi sub uniprop(Str:D $str, |c) { $str ?? uniprop($str.ord, |c) !! Nil }
multi sub uniprop(Int:D $code) {
nqp::getuniprop_str($code,nqp::unipropcode('General_Category'));
}
multi sub uniprop(Int:D $code, Stringy:D $propname) {
# prop-mappings can be removed when MoarVM bug #448 is fixed...
## The code below was generated by tools/build/makeUNIPROP.pl6
my constant $prop-mappings = nqp::hash(
'OGr_Ext','Other_Grapheme_Extend','tc','Titlecase_Mapping',
'cjkIRG_MSource','kIRG_MSource','Dash','Dash','Pat_Syn','Pattern_Syntax',
'IDST','IDS_Trinary_Operator','IDC','ID_Continue','Dia','Diacritic',
'Cased','Cased','hst','Hangul_Syllable_Type','QMark','Quotation_Mark',
'Radical','Radical','NFD_QC','NFD_Quick_Check','jt','Joining_Type',
'cf','Case_Folding','cjkIRG_TSource','kIRG_TSource','sc','Script',
'SD','Soft_Dotted','CWCM','Changes_When_Casemapped',
'cjkOtherNumeric','kOtherNumeric','scf','Simple_Case_Folding',
'sfc','Simple_Case_Folding','isc','ISO_Comment','na1','Unicode_1_Name',
'Lower','Lowercase','Join_C','Join_Control','JSN','Jamo_Short_Name',
'bc','Bidi_Class','jg','Joining_Group','dm','Decomposition_Mapping',
'lc','Lowercase_Mapping','cjkIRG_USource','kIRG_USource',
'NFKC_CF','NFKC_Casefold','slc','Simple_Lowercase_Mapping',
'InSC','Indic_Syllabic_Category','XO_NFC','Expands_On_NFC',
'XO_NFD','Expands_On_NFD','cjkAccountingNumeric','kAccountingNumeric',
'Upper','Uppercase','WSpace','White_Space','space','White_Space',
'cjkIRG_VSource','kIRG_VSource','STerm','Sentence_Terminal',
'NFKD_QC','NFKD_Quick_Check','CWT','Changes_When_Titlecased','Math','Math',
'uc','Uppercase_Mapping','NFKC_QC','NFKC_Quick_Check','SB','Sentence_Break',
'stc','Simple_Titlecase_Mapping','Alpha','Alphabetic',
'CE','Composition_Exclusion','NChar','Noncharacter_Code_Point',
'OAlpha','Other_Alphabetic','XIDC','XID_Continue','age','Age',
'cjkPrimaryNumeric','kPrimaryNumeric','OIDS','Other_ID_Start',
'UIdeo','Unified_Ideograph','FC_NFKC','FC_NFKC_Closure','CI','Case_Ignorable',
'Hyphen','Hyphen','nv','Numeric_Value','CWKCF','Changes_When_NFKC_Casefolded',
'XO_NFKD','Expands_On_NFKD','InPC','Indic_Positional_Category',
'dt','Decomposition_Type','cjkIICore','kIICore','Bidi_M','Bidi_Mirrored',
'CWU','Changes_When_Uppercased','IDS','ID_Start','Gr_Ext','Grapheme_Extend',
'XIDS','XID_Start','XO_NFKC','Expands_On_NFKC','OUpper','Other_Uppercase',
'OMath','Other_Math','Gr_Link','Grapheme_Link','Bidi_C','Bidi_Control',
'DI','Default_Ignorable_Code_Point','CWCF','Changes_When_Casefolded',
'cjkIRG_GSource','kIRG_GSource','WB','Word_Break','NFC_QC','NFC_Quick_Check',
'cjkIRG_JSource','kIRG_JSource','ODI','Other_Default_Ignorable_Code_Point',
'LOE','Logical_Order_Exception','bpb','Bidi_Paired_Bracket',
'PCM','Prepended_Concatenation_Mark','OLower','Other_Lowercase',
'OIDC','Other_ID_Continue','VS','Variation_Selector','Ext','Extender',
'Comp_Ex','Full_Composition_Exclusion','IDSB','IDS_Binary_Operator',
'nt','Numeric_Type','cjkCompatibilityVariant','kCompatibilityVariant',
'suc','Simple_Uppercase_Mapping','Term','Terminal_Punctuation',
'lb','Line_Break','cjkIRG_HSource','kIRG_HSource','ea','East_Asian_Width',
'AHex','ASCII_Hex_Digit','cjkIRG_KSource','kIRG_KSource',
'Pat_WS','Pattern_White_Space','Hex','Hex_Digit',
'cjkIRG_KPSource','kIRG_KPSource','bpt','Bidi_Paired_Bracket_Type',
'gc','General_Category','GCB','Grapheme_Cluster_Break',
'Gr_Base','Grapheme_Base','na','Name','scx','Script_Extensions',
'Ideo','Ideographic','Name_Alias','Name_Alias','blk','Block','Dep','Deprecated',
'CWL','Changes_When_Lowercased','bmg','Bidi_Mirroring_Glyph',
'cjkRSUnicode','kRSUnicode','Unicode_Radical_Stroke','kRSUnicode',
'URS','kRSUnicode','ccc','Canonical_Combining_Class',
);
my constant $prefs = nqp::hash(
'Other_Grapheme_Extend','B','Titlecase_Mapping','tc','Dash','B',
'Emoji_Modifier_Base','B','Emoji_Modifier','B','Pattern_Syntax','B',
'IDS_Trinary_Operator','B','ID_Continue','B','Diacritic','B','Cased','B',
'Hangul_Syllable_Type','S','Quotation_Mark','B','Radical','B',
'NFD_Quick_Check','S','Joining_Type','S','Case_Folding','S','Script','S',
'Soft_Dotted','B','Changes_When_Casemapped','B','Simple_Case_Folding','S',
'ISO_Comment','S','Lowercase','B','Join_Control','B','Bidi_Class','S',
'Joining_Group','S','Decomposition_Mapping','S','Lowercase_Mapping','lc',
'NFKC_Casefold','S','Simple_Lowercase_Mapping','S',
'Indic_Syllabic_Category','S','Expands_On_NFC','B','Expands_On_NFD','B',
'Uppercase','B','White_Space','B','Sentence_Terminal','B',
'NFKD_Quick_Check','S','Changes_When_Titlecased','B','Math','B',
'Uppercase_Mapping','uc','NFKC_Quick_Check','S','Sentence_Break','S',
'Simple_Titlecase_Mapping','S','Alphabetic','B','Composition_Exclusion','B',
'Noncharacter_Code_Point','B','Other_Alphabetic','B','XID_Continue','B',
'Age','S','Other_ID_Start','B','Unified_Ideograph','B','FC_NFKC_Closure','S',
'Case_Ignorable','B','Hyphen','B','Numeric_Value','nv',
'Changes_When_NFKC_Casefolded','B','Expands_On_NFKD','B',
'Indic_Positional_Category','S','Decomposition_Type','S','Bidi_Mirrored','B',
'Changes_When_Uppercased','B','ID_Start','B','Grapheme_Extend','B',
'XID_Start','B','Expands_On_NFKC','B','Other_Uppercase','B','Other_Math','B',
'Grapheme_Link','B','Bidi_Control','B','Default_Ignorable_Code_Point','B',
'Changes_When_Casefolded','B','Word_Break','S','NFC_Quick_Check','S',
'Other_Default_Ignorable_Code_Point','B','Logical_Order_Exception','B',
'Prepended_Concatenation_Mark','B','Other_Lowercase','B',
'Other_ID_Continue','B','Variation_Selector','B','Extender','B',
'Full_Composition_Exclusion','B','IDS_Binary_Operator','B','Numeric_Type','S',
'kCompatibilityVariant','S','Simple_Uppercase_Mapping','S',
'Terminal_Punctuation','B','Line_Break','S','East_Asian_Width','S',
'ASCII_Hex_Digit','B','Pattern_White_Space','B','Hex_Digit','B',
'Bidi_Paired_Bracket_Type','S','General_Category','S',
'Grapheme_Cluster_Break','S','Grapheme_Base','B','Name','na','Ideographic','B',
'Block','S','Emoji_Presentation','B','Emoji','B','Deprecated','B',
'Changes_When_Lowercased','B','Bidi_Mirroring_Glyph','bmg',
'Canonical_Combining_Class','S',
);
## End generated code
$propname := nqp::atkey($prop-mappings, $propname) if nqp::existskey($prop-mappings,$propname);
my $prop := nqp::unipropcode($propname);
my str $pref = nqp::ifnull(nqp::atkey($prefs, $propname),'');
nqp::if(
nqp::iseq_s($pref, 'S'),
nqp::getuniprop_str($code,$prop),
nqp::if(
nqp::iseq_s($pref, 'I'),
nqp::getuniprop_int($code,$prop),
nqp::if(
nqp::iseq_s($pref, 'B'),
nqp::p6bool(nqp::getuniprop_bool($code,$prop)),
nqp::if(
nqp::iseq_s($pref, 'lc'),
nqp::lc(nqp::chr(nqp::unbox_i($code))),
nqp::if(
nqp::iseq_s($pref, 'tc'),
nqp::tc(nqp::chr(nqp::unbox_i($code))),
nqp::if(
nqp::iseq_s($pref, 'uc'),
nqp::uc(nqp::chr(nqp::unbox_i($code))),
nqp::if(
nqp::iseq_s($pref, 'na'),
nqp::getuniname($code),
nqp::if(
nqp::iseq_s($pref, 'nv'),
unival($code),
nqp::if(
nqp::iseq_s($pref, 'bmg'),
nqp::stmts(
(my int $bmg-ord = nqp::getuniprop_int($code, $prop)),
$bmg-ord ?? nqp::chr($bmg-ord) !! ''),
nqp::stmts(
(my $result := nqp::getuniprop_str($code,$prop)),
nqp::if(
nqp::istrue($result),
nqp::stmts(
nqp::bindkey($prefs, $propname, 'S'),
$result),
nqp::stmts(
nqp::bindkey($prefs, $propname, 'I'),
nqp::getuniprop_int($code,$prop)))))))))))))
}
# Unicode functions
proto sub uniprop-int(|) {*}
multi sub uniprop-int(Str:D $str, Stringy:D $propname) {
$str ?? uniprop-int($str.ord, $propname) !! Nil }
multi sub uniprop-int(Int:D $code, Stringy:D $propname) {
nqp::getuniprop_int($code,nqp::unipropcode($propname));
}
proto sub uniprop-bool(|) {*}
multi sub uniprop-bool(Str:D $str, Stringy:D $propname) {
$str ?? uniprop-bool($str.ord, $propname) !! Nil
}
multi sub uniprop-bool(Int:D $code, Stringy:D $propname) {
nqp::p6bool(nqp::getuniprop_bool($code,nqp::unipropcode($propname)));
}
proto sub uniprop-str(|) {*}
multi sub uniprop-str(Str:D $str, Stringy:D $propname) {
$str ?? uniprop-str($str.ord, $propname) !! Nil
}
multi sub uniprop-str(Int:D $code, Stringy:D $propname) {
nqp::getuniprop_str($code,nqp::unipropcode($propname));
}
proto sub uniprops(|) {*}
multi sub uniprops(Str:D $str, Stringy:D $propname = "General_Category") {
$str.ords.map: { uniprop($_, $propname) }
}
proto sub unival(|) {*}
multi sub unival(Str:D $str) { $str ?? unival($str.ord) !! Nil }
multi sub unival(Int:D $code) {
state $nuprop = nqp::unipropcode("Numeric_Value_Numerator");
state $deprop = nqp::unipropcode("Numeric_Value_Denominator");
my $nu = nqp::getuniprop_str($code, $nuprop);
my $de = nqp::getuniprop_str($code, $deprop);
!$de || $de eq '1' ?? $nu.Int !! $nu / $de;
}
proto sub univals(|) {*}
multi sub univals(Str:D $str) { $str.ords.map: { unival($_) } }
proto sub unimatch(|) {*}
multi sub unimatch(Str:D $str, |c) { $str ?? unimatch($str.ord, |c) !! Nil }
# This multi below can be removed when MoarVM bug #448 is fixed
multi sub unimatch(Int:D $code, Stringy:D $pvalname, Stringy:D $propname) {
uniprop($code, $propname) eq $pvalname;
}
multi sub unimatch(Int:D $code, Stringy:D $pvalname, Stringy:D $propname = $pvalname) {
my $prop := nqp::unipropcode($propname);
nqp::p6bool(nqp::matchuniprop($code,$prop,nqp::unipvalcode($prop,$pvalname)));
}
#?endif
# vim: ft=perl6 expandtab sw=4