5 CardinalString - Cardinal String class and related functions
9 This file sets up the C<CardinalString> type.
19 .namespace ['CardinalString']
21 .include 'cclass.pasm'
23 .sub 'onload' :anon :init :load
24 .local pmc cardinalmeta, strproto
25 cardinalmeta = get_hll_global ['CardinalObject'], '!CARDINALMETA'
26 strproto = cardinalmeta.'new_class'('CardinalString', 'parent'=>'String CardinalObject')
27 cardinalmeta.'register'('CardinalString', 'parent'=>'CardinalObject', 'protoobject'=>strproto)
31 .sub 'ACCEPTS' :method
33 .return 'infix:eq'(topic, self)
39 retv = new 'CardinalInteger'
47 .sub 'reverse' :method
50 retv = self.'split'('')
51 retv = retv.'reverse'()
57 .sub split :method :multi('CardinalString')
66 retv = new 'CardinalArray'
69 split pieces, delim, objst
76 tmps = new 'CardinalString'
94 retv = new 'CardinalString'
107 retv = new 'CardinalString'
119 retv = new 'CardinalString'
123 if len == 0 goto done
125 substr fchr, tmps, 0, 1
142 retv = new 'CardinalString'
146 if len == 0 goto done
148 substr fchr, tmps, 0, 1
159 .sub capitalize :method
165 retv = new 'CardinalString'
169 if len == 0 goto done
173 .local int pos, is_ws, is_lc
177 if pos == len goto done
178 is_ws = is_cclass .CCLASS_WHITESPACE, tmps, pos
186 is_lc = is_cclass .CCLASS_LOWERCASE, tmps, pos
187 unless is_lc goto advance
188 $S1 = substr tmps, pos, 1
190 substr tmps, pos, 1, $S1
191 ## the length may have changed after replacement, so measure it again
204 retv = new 'CardinalString'
208 if len == 0 goto done
210 substr tmps,tmps, 0, len
219 Returns a Perl representation of the Str.
239 $P0 = new 'CardinalString'
254 .include 'cclass.pasm'
259 our Str multi Str::lc ( Str $string )
261 Returns the input string after converting each character to its lowercase
269 s = new 'CardinalString'
277 our Str multi Str::lcfirst ( Str $string )
279 Like C<lc>, but only affects the first character.
286 s = new 'CardinalString'
288 .return s.'lcfirst'()
294 our Str multi Str::uc ( Str $string )
296 Returns the input string after converting each character to its uppercase
297 form, if lowercase. This is not a Unicode "titlecase" operation, but a
305 s = new 'CardinalString'
313 our Str multi Str::ucfirst ( Str $string )
315 Performs a Unicode "titlecase" operation on the first character of the string.
322 s = new 'CardinalString'
324 .return s.'ucfirst'()
330 our Str multi Str::capitalize ( Str $string )
332 Has the effect of first doing an C<lc> on the entire string, then performing a
333 C<s:g/(\w+)/{ucfirst $1}/> on it.
340 s = new 'CardinalString'
342 .return s.'capitalize'()
348 our CardinalArray multi Str::split ( Str $delimiter , Str $input = $+_, Int $limit = inf )
349 our CardinalArray multi Str::split ( Rule $delimiter = /\s+/, Str $input = $+_, Int $limit = inf )
350 our CardinalArray multi Str::split ( Str $input : Str $delimiter , Int $limit = inf )
351 our CardinalArray multi Str::split ( Str $input : Rule $delimiter , Int $limit = inf )
353 String delimiters must not be treated as rules but as constants. The
354 default is no longer S<' '> since that would be interpreted as a constant.
355 P5's C<< split('S< >') >> will translate to C<.words> or some such. Null trailing fields
356 are no longer trimmed by default. We might add some kind of :trim flag or
357 introduce a trimlist function of some sort.
359 B<Note:> partial implementation only
368 a = new 'CardinalString'
369 b = new 'CardinalString'
380 B<Note:> partial implementation only
385 .param pmc args :slurpy
389 flatargs = new 'CardinalArray'
391 unless args goto have_flatargs
393 $I0 = isa $P0, 'CardinalArray'
399 unless args goto have_flatargs
401 $I0 = isa $P0, 'CardinalArray'
402 if $I0 goto arg_array
406 $I0 = elements flatargs
407 splice flatargs, $P0, $I0, 0
410 $S0 = join sep, flatargs
417 multi substr (Str $s, StrPos $start : StrPos $end, $replace)
418 multi substr (Str $s, StrPos $start, StrLen $length : $replace)
419 multi substr (Str $s, StrLen $offset : StrLen $length, $replace)
421 B<Note:> partial implementation only
428 .param int len :optional
429 .param int has_len :opt_flag
433 s = new 'CardinalString'
438 $S0 = substr x, start, len
444 our Str method Str::chop ( Str $string: )
446 Returns string with one Char removed from the end.
453 s = new 'CardinalString'
460 =head2 TODO Functions
466 our Char multi P5emul::Str::p5chop ( Str $string is rw )
467 our Char multi P5emul::Str::p5chop ( Str *@strings = ($+_) is rw )
469 Trims the last character from C<$string>, and returns it. Called with a
470 list, it chops each item in turn, and returns the last character
475 our Int multi P5emul::Str::p5chomp ( Str $string is rw )
476 our Int multi P5emul::Str::p5chomp ( Str *@strings = ($+_) is rw )
478 Related to C<p5chop>, only removes trailing chars that match C</\n/>. In
479 either case, it returns the number of chars removed.
483 our Str method Str::chomp ( Str $string: )
485 Returns string with newline removed from the end. An arbitrary
486 terminator can be removed if the input filehandle has marked the
487 string for where the "newline" begins. (Presumably this is stored
488 as a property of the string.) Otherwise a standard newline is removed.
490 Note: Most users should just let their I/O handles autochomp instead.
491 (Autochomping is the default.)
495 This word is banned in Cardinal. You must specify units.
499 Needs to be in terms of StrPos, not Int.
509 Needs to be in terms of StrPos, not Int.
517 Should replace vec with declared arrays of bit, uint2, uint4, etc.
521 our CardinalArray multi Str::words ( Rule $matcher = /\S+/, Str $input = $+_, Int $limit = inf )
522 our CardinalArray multi Str::words ( Str $input : Rule $matcher = /\S+/, Int $limit = inf )
532 # vim: expandtab shiftwidth=4 ft=pir: