tagged release 0.6.4
[parrot.git] / languages / WMLScript / runtime / wmlsstring.pir
blob8d4227991bc22a5403c3a93e45f16ae92f58100c
1 # Copyright (C) 2006-2008, The Perl Foundation.
2 # $Id$
4 =head1 NAME
6 runtime/wmlsstring.pir - WMLScript String library
8 =head1 DESCRIPTION
10 This library contains a set of string functions. A string is an array of
11 characters. Each of the characters has an index. The first character in a
12 string has an index zero (0). The length of the string is the number of
13 characters in the array.
15 The user of the String library can specify a special separator by which
16 elements in a string can be separated. These elements can be accessed
17 by specifying the separator and the element index. The first element in a
18 string has an index zero (0). Each occurrence of the separator in the string
19 separates two elements (no escaping of separators is allowed).
21 A White space character is one of the following characters:
23 =over 4
25 =item * TAB : Horizontal Tabulation
27 =item * VT : Vertical Tabulation
29 =item * FF : Form Feed
31 =item * SP : Space
33 =item * LF : Line Feed
35 =item * CR : Carriage Return
37 =back
39 See "WMLScript Standard Libraries Specification", section 9 "String".
41 =head1 FUNCTIONS
43 =cut
45 .loadlib 'wmls_ops'
46 .HLL 'WMLScript', 'wmls_group'
49 .sub 'getString'
50     new $P0, 'Hash'
52     .const .Sub _string_length = '_string_length'
53     $P0[0]  = _string_length
54     .const .Sub _string_isEmpty = '_string_isEmpty'
55     $P0[1]  = _string_isEmpty
56     .const .Sub _string_charAt = '_string_charAt'
57     $P0[2]  = _string_charAt
58     .const .Sub _string_subString = '_string_subString'
59     $P0[3]  = _string_subString
60     .const .Sub _string_find = '_string_find'
61     $P0[4]  = _string_find
62     .const .Sub _string_replace = '_string_replace'
63     $P0[5]  = _string_replace
64     .const .Sub _string_elements = '_string_elements'
65     $P0[6]  = _string_elements
66     .const .Sub _string_elementAt = '_string_elementAt'
67     $P0[7]  = _string_elementAt
68     .const .Sub _string_removeAt = '_string_removeAt'
69     $P0[8]  = _string_removeAt
70     .const .Sub _string_replaceAt = '_string_replaceAt'
71     $P0[9]  = _string_replaceAt
72     .const .Sub _string_insertAt = '_string_insertAt'
73     $P0[10] = _string_insertAt
74     .const .Sub _string_squeeze = '_string_squeeze'
75     $P0[11] = _string_squeeze
76     .const .Sub _string_trim = '_string_trim'
77     $P0[12] = _string_trim
78     .const .Sub _string_compare = '_string_compare'
79     $P0[13] = _string_compare
80     .const .Sub _string_toString = '_string_toString'
81     $P0[14] = _string_toString
82     .const .Sub _string_format = '_string_format'
83     $P0[15] = _string_format
85     .return ($P0)
86 .end
89 =head2 C<length(string)>
91 =head3 DESCRIPTION
93 Returns the length (number of characters) of the given string.
95 =head3 PARAMETERS
97 string = String
99 =head3 RETURN VALUE
101 Integer or invalid.
103 =cut
105 .sub '_string_length' :anon
106     .param pmc str
107     .local pmc res
108     $I0 = isa str, 'WmlsInvalid'
109     if $I0 goto L1
110     $S1 = str
111     $I1 = length $S1
112     new res, 'WmlsInteger'
113     set res, $I1
114     .return (res)
115   L1:
116     new res, 'WmlsInvalid'
117     .return (res)
118 .end
121 =head2 C<isEmpty(string)>
123 =head3 DESCRIPTION
125 Returns a boolean true if the string length is zero and boolean false
126 otherwise.
128 =head3 PARAMETERS
130 string = String
132 =head3 RETURN VALUE
134 Boolean or invalid.
136 =cut
138 .sub '_string_isEmpty' :anon
139     .param pmc str
140     .local pmc res
141     $I0 = isa str, 'WmlsInvalid'
142     if $I0 goto L1
143     $S1 = str
144     $I1 = length $S1
145     new res, 'WmlsBoolean'
146     set res, 0
147     if $I1 goto L2
148     set res, 1
149   L2:
150     .return (res)
151   L1:
152     new res, 'WmlsInvalid'
153     .return (res)
154 .end
157 =head2 C<charAt(string, index)>
159 =head3 DESCRIPTION
161 Returns a new string of length one containing the character at the specified
162 index of the given string.
164 If the index is of type floating-point, Float.int() is first used to calculate the
165 actual integer index.
167 =head3 PARAMETERS
169 string = String
171 index = Number (the index of the character to be returned)
173 =head3 RETURN VALUE
175 String or invalid.
177 =head3 EXCEPTIONS
179 If index is out of range then an empty string (C<"">) is returned.
181 =cut
183 .sub '_string_charAt' :anon
184     .param pmc str
185     .param pmc index_
186     .local pmc res
187     $I0 = isa str, 'WmlsInvalid'
188     if $I0 goto L1
189     $I0 = isa index_, 'WmlsInteger'
190     if $I0 goto L2
191     $I0 = isa index_, 'WmlsFloat'
192     if $I0 goto L2
193     goto L1
194   L2:
195     $S1 = str
196     $I1 = length $S1
197     $I2 = index_
198     new res, 'WmlsString'
199     if $I2 < 0 goto L3
200     if $I2 >= $I1 goto L3
201     $S0 = substr $S1, $I2, 1
202     set res, $S0
203   L3:
204     .return (res)
205   L1:
206     new res, 'WmlsInvalid'
207     .return (res)
208 .end
211 =head2 C<subString(string, startIndex, length)>
213 =head3 DESCRIPTION
215 Returns a new string that is a substring of the given string. The substring
216 begins at the specified startIndex and its length (number of characters) is
217 the given length. If the startIndex is less than 0 then 0 is used for the
218 startIndex. If the length is larger than the remaining number of characters in
219 the string, the length is replaced with the number of remaining characters.
221 If the startIndex or the length is of type floating-point, Float.int() is first used to
222 calculate the actual integer value.
224 =head3 PARAMETERS
226 string = String
228 startIndex = Number (the beginning index, inclusive)
230 length = Number (the length of the substring)
232 =head3 RETURN VALUE
234 String or invalid.
236 =head3 EXCEPTIONS
238 If startIndex is larger than the last index an empty string (C<"">) is returned.
240 If length <= 0 an empty string (C<"">) is returned.
242 =cut
244 .sub '_string_subString' :anon
245     .param pmc String
246     .param pmc startIndex
247     .param pmc Length
248     .local pmc res
249     $I0 = isa String, 'WmlsInvalid'
250     if $I0 goto L1
251     $I0 = isa startIndex, 'WmlsInteger'
252     if $I0 goto L2
253     $I0 = isa startIndex, 'WmlsFloat'
254     if $I0 goto L2
255     goto L1
256   L2:
257     $I0 = isa Length, 'WmlsInteger'
258     if $I0 goto L3
259     $I0 = isa Length, 'WmlsFloat'
260     if $I0 goto L3
261     goto L1
262   L3:
263     $S1 = String
264     $I1 = length $S1
265     $I2 = startIndex
266     if $I2 >= 0 goto L4
267     $I2 = 0
268   L4:
269     new res, 'WmlsString'
270     if $I2 >= $I1 goto L5
271     $I3 = Length
272     if $I3 <= 0 goto L5
273     $S0 = substr $S1, $I2, $I3
274     set res, $S0
275   L5:
276     .return (res)
277   L1:
278     new res, 'WmlsInvalid'
279     .return (res)
280 .end
283 =head2 C<find(string, subString)>
285 =head3 DESCRIPTION
287 Returns the index of the first character in the string that matches the
288 requested subString. If no match is found integer value -1 is returned.
290 Two strings are defined to match when they are identical. Characters with
291 multiple possible representations match only if they have the same
292 representation in both strings. No case folding is performed.
294 =head3 PARAMETERS
296 string = String
298 subString = String
300 =head3 RETURN VALUE
302 Integer or invalid.
304 =head3 EXCEPTIONS
306 If subString is an empty string (C<"">), an invalid value is returned.
308 =cut
310 .sub '_string_find' :anon
311     .param pmc String
312     .param pmc subString
313     .local pmc res
314     $I0 = isa String, 'WmlsInvalid'
315     if $I0 goto L1
316     $I0 = isa subString, 'WmlsInvalid'
317     if $I0 goto L1
318     $S1 = String
319     $S2 = subString
320     $I2 = length $S2
321     if $I2 == 0 goto L1
322     $I0 = index $S1, $S2
323     new res, 'WmlsInteger'
324     set res, $I0
325     .return (res)
326   L1:
327     new res, 'WmlsInvalid'
328     .return (res)
329 .end
332 =head2 C<replace(string, oldSubString, newSubString)>
334 =head3 DESCRIPTION
336 Returns a new string resulting from replacing all occurrences of
337 oldSubString in this string with newSubString.
339 Two strings are defined to match when they are identical. Characters with
340 multiple possible representations match only if they have the same
341 representation in both strings. No case folding is performed.
343 =head3 PARAMETERS
345 string = String
347 oldSubString = String
349 newSubString = String
351 =head3 RETURN VALUE
353 String or invalid.
355 =head3 EXCEPTIONS
357 If oldSubString is an empty string an C<invalid> value is returned.
359 =cut
361 .sub '_string_replace' :anon
362     .param pmc String
363     .param pmc oldSubString
364     .param pmc newSubString
365     .local pmc res
366     $I0 = isa String, 'WmlsInvalid'
367     if $I0 goto L1
368     $I0 = isa oldSubString, 'WmlsInvalid'
369     if $I0 goto L1
370     $I0 = isa newSubString, 'WmlsInvalid'
371     if $I0 goto L1
372     $S1 = String
373     $S2 = oldSubString
374     $I2 = length $S2
375     if $I2 == 0 goto L1
376     $S3 = newSubString
377     $P0 = split $S2, $S1
378     $S0 = join $S3, $P0
379     new res, 'WmlsString'
380     set res, $S0
381     .return (res)
382   L1:
383     new res, 'WmlsInvalid'
384     .return (res)
385 .end
388 =head2 C<elements(string, separator)>
390 =head3 DESCRIPTION
392 Returns the number of elements in the given string separated by the given
393 separator. Empty string ("") is a valid element (thus, this function can never
394 return a value that is less or equal to zero).
396 =head3 PARAMETERS
398 string = String
400 separator = String (the first character of the string used as separator)
402 =head3 RETURN VALUE
404 Integer or invalid.
406 =head3 EXCEPTIONS
408 Returns C<invalid> if the separator is an empty string.
410 =cut
412 .sub '_string_elements' :anon
413     .param pmc str
414     .param pmc separator
415     .local pmc res
416     $I0 = isa str, 'WmlsInvalid'
417     if $I0 goto L1
418     $I0 = isa separator, 'WmlsInvalid'
419     if $I0 goto L1
420     $S1 = str
421     $S2 = separator
422     $I2 = length $S2
423     if $I2 == 0 goto L1
424     $S2 = substr $S2, 0, 1
425     new res, 'WmlsInteger'
426     $I1 = length $S1
427     if $I1 != 0 goto L2
428     set res, 1
429     .return (res)
430   L2:
431     $P0 = split $S2, $S1
432     $I0 = elements $P0
433     set res, $I0
434     .return (res)
435   L1:
436     new res, 'WmlsInvalid'
437     .return (res)
438 .end
441 =head2 C<elementAt(string, index, separator)>
443 =head3 DESCRIPTION
445 Search string for index'th element, elements being separated by separator
446 and return the corresponding element. If the index is less than 0 then the first
447 element is returned. If the index is larger than the number of elements then
448 the last element is returned. If the string is an empty string then an empty
449 string is returned.
451 If the index is of type floating-point, Float.int() is first used to calculate the
452 actual index value.
454 =head3 PARAMETERS
456 string = String
458 index = Number (the index of the element to be returned)
460 separator = String (the first character of the string used as separator)
462 =head3 RETURN VALUE
464 String or invalid.
466 =head3 EXCEPTIONS
468 Returns C<invalid> if the separator is an empty string.
470 =cut
472 .sub '_string_elementAt' :anon
473     .param pmc str
474     .param pmc index_
475     .param pmc separator
476     .local pmc res
477     $I0 = isa str, 'WmlsInvalid'
478     if $I0 goto L1
479     $I0 = isa index_, 'WmlsInteger'
480     if $I0 goto L2
481     $I0 = isa index_, 'WmlsFloat'
482     if $I0 goto L2
483     goto L1
484   L2:
485     $I0 = isa separator, 'WmlsInvalid'
486     if $I0 goto L1
487     $S1 = str
488     $I2 = index_
489     if $I2 >= 0 goto L3
490     $I2 = 0
491   L3:
492     $S3 = separator
493     $I3 = length $S3
494     if $I3 == 0 goto L1
495     $S3 = substr $S3, 0, 1
496     new res, 'WmlsString'
497     $I1 = length $S1
498     if $I1 != 0 goto L4
499     .return (res)
500   L4:
501     $P0 = split $S3, $S1
502     $I0 = elements $P0
503     if $I2 < $I0 goto L5
504     $I2 = $I0 - 1
505   L5:
506     $S0 = $P0[$I2]
507     set res, $S0
508     .return (res)
509   L1:
510     new res, 'WmlsInvalid'
511     .return (res)
512 .end
515 =head2 C<removeAt(string, index, separator)>
517 =head3 DESCRIPTION
519 Returns a new string where the element and the corresponding separator (if
520 existing) with the given index are removed from the given string. If the index
521 is less than 0 then the first element is removed. If the index is larger than the
522 number of elements then the last element is removed. If the string is empty,
523 the function returns a new empty string.
525 If the index is of type floating-point, Float.int() is first used to calculate the
526 actual index value.
528 =head3 PARAMETERS
530 string = String
532 index = Number (the index of the element to be deleted)
534 separator = String (the first character of the string used as separator)
536 =head3 RETURN VALUE
538 String or invalid.
540 =head3 EXCEPTIONS
542 Returns C<invalid> if the separator is an empty string.
544 =cut
546 .sub '_string_removeAt' :anon
547     .param pmc str
548     .param pmc index_
549     .param pmc separator
550     .local pmc res
551     $I0 = isa str, 'WmlsInvalid'
552     if $I0 goto L1
553     $I0 = isa index_, 'WmlsInteger'
554     if $I0 goto L2
555     $I0 = isa index_, 'WmlsFloat'
556     if $I0 goto L2
557     goto L1
558   L2:
559     $I0 = isa separator, 'WmlsInvalid'
560     if $I0 goto L1
561     $S1 = str
562     $I2 = index_
563     if $I2 >= 0 goto L3
564     $I2 = 0
565   L3:
566     $S3 = separator
567     $I3 = length $S3
568     if $I3 == 0 goto L1
569     $S3 = substr $S3, 0, 1
570     new res, 'WmlsString'
571     $I1 = length $S1
572     if $I1 != 0 goto L4
573     .return (res)
574   L4:
575     $P0 = split $S3, $S1
576     $I4 = elements $P0
577     if $I2 < $I4 goto L5
578     $I2 = $I4 - 1
579   L5:
580     dec $I4
581     new $P1, 'Array'
582     set $P1, $I4
583     $I0 = 0
584     $I1 = 0
585   L6:
586     unless $I1 < $I4 goto L7
587     $S0 = $P0[$I0]
588     $P1[$I1] = $S0
589     if $I0 == $I2 goto L8
590     inc $I1
591   L8:
592     inc $I0
593     goto L6
594   L7:
595     $S0 = join $S3, $P1
596     set res, $S0
597     .return (res)
598   L1:
599     new res, 'WmlsInvalid'
600     .return (res)
601 .end
604 =head2 C<replaceAt(string, element, index, separator)>
606 =head3 DESCRIPTION
608 Returns a string with the current element at the specified index replaced with
609 the given element. If the index is less than 0 then the first element is
610 replaced. If the index is larger than the number of elements then the last
611 element is replaced. If the string is empty, the function returns a new string
612 with the given element.
614 If the index is of type floating-point, Float.int() is first used to calculate the
615 actual index value.
617 =head3 PARAMETERS
619 string = String
621 element = String
623 index = Number (the index of the element to be replaced)
625 separator = String (the first character of the string used as separator)
627 =head3 RETURN VALUE
629 String or invalid.
631 =head3 EXCEPTIONS
633 Returns C<invalid> if the separator is an empty string.
635 =cut
637 .sub '_string_replaceAt' :anon
638     .param pmc str
639     .param pmc element
640     .param pmc index_
641     .param pmc separator
642     .local pmc res
643     $I0 = isa str, 'WmlsInvalid'
644     if $I0 goto L1
645     $I0 = isa element, 'WmlsInvalid'
646     if $I0 goto L1
647     $I0 = isa index_, 'WmlsInteger'
648     if $I0 goto L2
649     $I0 = isa index_, 'WmlsFloat'
650     if $I0 goto L2
651     goto L1
652   L2:
653     $I0 = isa separator, 'WmlsInvalid'
654     if $I0 goto L1
655     $S1 = str
656     $S2 = element
657     $I3 = index_
658     if $I3 >= 0 goto L3
659     $I3 = 0
660   L3:
661     $S4 = separator
662     $I4 = length $S4
663     if $I4 == 0 goto L1
664     $S4 = substr $S4, 0, 1
665     new res, 'WmlsString'
666     $I1 = length $S1
667     if $I1 != 0 goto L4
668     set res, $S2
669     .return (res)
670   L4:
671     $P0 = split $S4, $S1
672     $I0 = elements $P0
673     if $I3 < $I0 goto L5
674     $I3 = $I0 - 1
675   L5:
676     $P0[$I3] = $S2
677     $S0 = join $S4, $P0
678     set res, $S0
679     .return (res)
680   L1:
681     new res, 'WmlsInvalid'
682     .return (res)
683 .end
686 =head2 C<insertAt(string, element, index, separator)>
688 =head3 DESCRIPTION
690 Returns a string with the element and the corresponding separator (if
691 needed) inserted at the specified element index of the original string. If the
692 index is less than 0 then 0 is used as the index. If the index is larger than the
693 number of elements then the element is appended at the end of the string. If
694 the string is empty, the function returns a new string with the given element.
696 If the index is of type floating-point, Float.int() is first used to calculate the
697 actual index value.
699 =head3 PARAMETERS
701 string = String (original string)
703 element = String (element to be inserted)
705 index = Number (the index of the element to be added)
707 separator = String (the first character of the string used as separator)
709 =head3 RETURN VALUE
711 String or invalid.
713 =head3 EXCEPTIONS
715 Returns C<invalid> if the separator is an empty string.
717 =cut
719 .sub '_string_insertAt' :anon
720     .param pmc str
721     .param pmc element
722     .param pmc index_
723     .param pmc separator
724     .local pmc res
725     $I0 = isa str, 'WmlsInvalid'
726     if $I0 goto L1
727     $I0 = isa element, 'WmlsInvalid'
728     if $I0 goto L1
729     $I0 = isa index_, 'WmlsInteger'
730     if $I0 goto L2
731     $I0 = isa index_, 'WmlsFloat'
732     if $I0 goto L2
733     goto L1
734   L2:
735     $I0 = isa separator, 'WmlsInvalid'
736     if $I0 goto L1
737     $S1 = str
738     $S2 = element
739     $I3 = index_
740     if $I3 >= 0 goto L3
741     $I3 = 0
742   L3:
743     $S4 = separator
744     $I4 = length $S4
745     if $I4 == 0 goto L1
746     $S4 = substr $S4, 0, 1
747     new res, 'WmlsString'
748     $I1 = length $S1
749     if $I1 != 0 goto L4
750     set res, $S2
751     .return (res)
752   L4:
753     $P0 = split $S4, $S1
754     $I5 = elements $P0
755     if $I3 <= $I5 goto L5
756     $I3 = $I5
757   L5:
758     $I6 = $I5 + 1
759     new $P1, 'Array'
760     set $P1, $I6
761     $I0 = 0
762     $I1 = 0
763   L6:
764     unless $I0 < $I5 goto L7
765     if $I1 != $I3 goto L8
766     inc $I1
767   L8:
768     $S0 = $P0[$I0]
769     $P1[$I1] = $S0
770     inc $I0
771     inc $I1
772     goto L6
773   L7:
774     $P1[$I3] = $S2
775     $S0 = join $S4, $P1
776     set res, $S0
777     .return (res)
778   L1:
779     new res, 'WmlsInvalid'
780     .return (res)
781 .end
784 =head2 C<squeeze(string)>
786 =head3 DESCRIPTION
788 Returns a string where all consecutive series of white spaces within the
789 string are reduced to single inter-word space.
791 =head3 PARAMETERS
793 String = String
795 =head3 RETURN VALUE
797 String or invalid.
799 =cut
801 .include 'cclass.pasm'
803 .sub 'squeeze' :anon
804     .param string s
805     .local string res
806     .local int idx
807     res = ''
808     idx = 0
809     $I1 = length s
810   L1:
811     unless idx < $I1 goto L2
812     $I0 = is_cclass .CCLASS_WHITESPACE, s, idx
813     if $I0 goto L3
814     $S0 = substr s, idx, 1
815     res = concat $S0
816     inc idx
817     goto L1
818   L3:
819     res = concat ' '
820   L4:
821     inc idx
822     $I0 = is_cclass .CCLASS_WHITESPACE, s, idx
823     if $I0 goto L4
824     goto L1
825   L2:
826     .return (res)
827 .end
829 .sub '_string_squeeze' :anon
830     .param pmc str
831     .local pmc res
832     $I0 = isa str, 'WmlsInvalid'
833     if $I0 goto L1
834     new res, 'WmlsString'
835     $S1 = str
836     $S0 = squeeze($S1)
837     set res, $S0
838     .return (res)
839   L1:
840     new res, 'WmlsInvalid'
841     .return (res)
842 .end
845 =head2 C<trim(string)>
847 =head3 DESCRIPTION
849 Returns a string where all trailing and leading white spaces in the given
850 string have been trimmed.
852 =head3 PARAMETERS
854 String = String
856 =head3 RETURN VALUE
858 String or invalid.
860 =cut
862 .sub 'trim' :anon
863     .param string s
864     .local string res
865     $I1 = 0
866   L1:
867     $I0 = is_cclass .CCLASS_WHITESPACE, s, $I1
868     unless $I0 goto L2
869     inc $I1
870     goto L1
871   L2:
872     $I2 = length s
873   L3:
874     dec $I2
875     $I0 = is_cclass .CCLASS_WHITESPACE, s, $I2
876     if $I0 goto L3
877     inc $I2
878     $I2 -= $I1
879     res = substr s, $I1, $I2
880     .return (res)
881 .end
883 .sub '_string_trim' :anon
884     .param pmc str
885     .local pmc res
886     $I0 = isa str, 'WmlsInvalid'
887     if $I0 goto L1
888     new res, 'WmlsString'
889     $S1 = str
890     $S0 = trim($S1)
891     set res, $S0
892     .return (res)
893   L1:
894     new res, 'WmlsInvalid'
895     .return (res)
896 .end
899 =head2 C<compare(string1, string2)>
901 =head3 DESCRIPTION
903 The return value indicates the lexicographic relation of string1 to string2. The
904 relation is based on the relation of the character codes in the native
905 character set. The return value is -1 if string1 is less than string2, 0 if string1
906 is identical to string2 or 1 if string1 is greater than string2.
908 =head3 PARAMETERS
910 String1 = String
912 String2 = String
914 =head3 RETURN VALUE
916 Integer or invalid.
918 =cut
920 .sub '_string_compare' :anon
921     .param pmc string1
922     .param pmc string2
923     .local pmc res
924     $I0 = isa string1, 'WmlsInvalid'
925     if $I0 goto L1
926     $I0 = isa string2, 'WmlsInvalid'
927     if $I0 goto L1
928     $S1 = string1
929     $S2 = string2
930     new res, 'WmlsInteger'
931     if $S1 >= $S2 goto L2
932     set res, -1
933     .return (res)
934   L2:
935     if $S1 > $S2 goto L3
936     set res, 0
937     .return (res)
938   L3:
939     set res, 1
940     .return (res)
941   L1:
942     new res, 'WmlsInvalid'
943     .return (res)
944 .end
947 =head2 C<toString(value)>
949 =head3 DESCRIPTION
951 Returns a string representation of the given value. This function performs
952 exactly the same conversions as supported by the [WMLScript] language
953 (automatic conversion from boolean, integer and floating-point values to
954 strings) except that C<invalid> value returns the string C<"invalid">.
956 =head3 PARAMETERS
958 value = Any
960 =head3 RETURN VALUE
962 String.
964 =cut
966 .sub '_string_toString' :anon
967     .param pmc value
968     $S1 = value
969     .local pmc res
970     new res, 'WmlsString'
971     set res, $S1
972     .return (res)
973 .end
976 =head2 C<format(format, value)>
978 =head3 DESCRIPTION
980 Converts the given value to a string by using the given formatting provided as
981 a format string. The format string can contain only one format specifier,
982 which can be located anywhere inside the string. If more than one is
983 specified, only the first one (leftmost) is used and the remaining specifiers
984 are replaced by an empty string. The format specifier has the following form:
986  % [width] [.precision] type
988 The C<width> argument is a nonnegative decimal integer controlling the
989 minimum number of characters printed. If the number of characters in the
990 output value is less than the specified width, blanks are added to the left until
991 the minimum width is reached. The C<width> argument never causes the
992 value to be truncated. If the number of characters in the output value is
993 greater than the specified width or, if width is not given, all characters of the
994 value are printed (subject to the precision argument).
996 The C<precision> argument specifies a nonnegative decimal integer,
997 preceded by a period (.), which can be used to set the precision of the output
998 value. The interpretation of this value depends on the given C<type>:
1000 =over 4
1002 =item d
1004 Specifies the minimum number of digits to be printed. If the number
1005 of digits in the value is less than precision, the output value is
1006 padded on the left with zeroes. The value is not truncated when the
1007 number of digits exceeds precision. Default precision is 1. If
1008 precision is specified as 0 and the value to be converted is 0, the
1009 result is an empty string.
1011 =item f
1013 Specifies the number of digits after the decimal point. If a decimal
1014 point appears, at least one digit appears before it. The value is
1015 rounded to the appropriate number of digits. Default precision is 6; if
1016 precision is 0 or if the period (.) appears without a number following
1017 it, no decimal point is printed.
1019 =item s
1021 Specifies the maximum number of characters to be printed. By
1022 default, all characters are printed.
1024 =back
1026 Unlike the C<width> argument, the C<precision> argument can cause either
1027 truncation of the output value or rounding of a floating-point value.
1029 The C<type> argument is the only required format argument; it appears after
1030 any optional format fields. The type character determines whether the given
1031 value is interpreted as integer, floating-point or string. If the value argument
1032 is of a different type than is specified by the type argument, it is converted
1033 according to WMLScript standard automatic conversion rules, with the
1034 addition that if value is of type floating-point and type is B<d>, Float.int() is called
1035 to convert the value. The supported C<type> arguments are:
1037 =over 4
1039 =item d
1041 Integer: The output value has the form [-]dddd, where dddd is one
1042 or more decimal digits.
1044 =item f
1046 Floating-point: The output value has the form [-]dddd.dddd, where
1047 dddd is one or more decimal digits. The number of digits before the
1048 decimal point depends on the magnitude of the number and the
1049 number of digits after the decimal point depends on the requested
1050 precision. When the number of digits after the decimal point in the value
1051 is less than the precision, letter 0 should be padded to fill columns
1052 (e.g. the result of String.format("%2.3f", 1.2) will be
1053 "1.200")
1055 =item s
1057 String: Characters are printed up to the end of the string or until the
1058 precision value is reached. When the width is larger than
1059 precision, the width should be ignored.
1061 =back
1063 A literal percent character (%) may be included in the format string by
1064 preceding it with another percent character (%%).
1066 MINIMALIST IMPLEMENTATION
1068 =head3 PARAMETERS
1070 format = String
1072 value = Any
1074 =head3 RETURN VALUE
1076 String or invalid.
1078 =head3 EXCEPTIONS
1080 Illegal format specifier results in an C<invalid> return value.
1082 =cut
1084 .sub '_string_format' :anon
1085     .param pmc format
1086     .param pmc value
1087     .local pmc res
1088     $I0 = isa format, 'WmlsInvalid'
1089     if $I0 goto L1
1090     $S0 = format
1091     new $P0, 'Array'
1092     set $P0, 1
1093     $P0[0] = value
1094   L2:
1095     $S1 = sprintf $S0, $P0
1096     new res, 'WmlsString'
1097     set res, $S1
1098     .return (res)
1099   L1:
1100     new res, 'WmlsInvalid'
1101     .return (res)
1102 .end
1105 =head1 AUTHORS
1107 Francois Perrad.
1109 =cut
1112 # Local Variables:
1113 #   mode: pir
1114 #   fill-column: 100
1115 # End:
1116 # vim: expandtab shiftwidth=4 ft=pir: