Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement .fmt #393

Closed
p6rt opened this issue Nov 6, 2008 · 7 comments
Closed

Implement .fmt #393

p6rt opened this issue Nov 6, 2008 · 7 comments
Labels

Comments

@p6rt
Copy link

p6rt commented Nov 6, 2008

Migrated from rt.perl.org#60382 (status was 'resolved')

Searchable as RT60382$

@p6rt
Copy link
Author

p6rt commented Nov 6, 2008

From @masak

Now also found in S29. :)

<http://perlcabal.org/syn/S29.html#line_1436>

@p6rt
Copy link
Author

p6rt commented Nov 7, 2008

From @masak

Implementation attached.

@p6rt
Copy link
Author

p6rt commented Nov 7, 2008

From @masak

fmt.patch
Index: src/builtins/any-str.pir
===================================================================
--- src/builtins/any-str.pir	(revision 32421)
+++ src/builtins/any-str.pir	(working copy)
@@ -191,6 +191,23 @@ scalar .fmt
     .return ($P0)
 .end
 
+=item fmt
+    
+ our Str multi Any::fmt ( Str $format )
+    
+Returns the invocant formatted by an implicit call to sprintf.
+
+=cut
+
+.sub 'fmt' :method :multi(_)
+    .param string format
+    .local pmc retv
+
+    retv = 'sprintf'(format, self)
+
+    .return(retv)
+.end 
+
 =item index()
 
 =cut
Index: src/classes/Mapping.pir
===================================================================
--- src/classes/Mapping.pir	(revision 32421)
+++ src/classes/Mapping.pir	(working copy)
@@ -137,6 +137,51 @@ mapping .fmt
 .end
 
 
+=item fmt
+    
+ our Str multi Mapping::fmt ( Str $format, $separator = "\n" )
+    
+Returns the invocant mapping formatted by an implicit call to sprintf on
+the key and value of every pair, joined by newlines or an explicitly given
+separator.
+    
+=cut
+
+.sub 'fmt' :method :multi('Hash')
+    .param pmc format
+    .param string sep          :optional
+    .param int has_sep         :opt_flag
+
+    .local pmc pairs
+    .local pmc res
+    .local pmc iter
+    .local pmc retv
+    .local pmc elem
+    .local pmc key
+    .local pmc value
+    .local pmc elemres
+
+    if has_sep goto have_sep
+    sep = "\n"
+  have_sep:
+    pairs = self.'pairs'()
+    res = new 'List'
+    iter = pairs.'iterator'()
+  elem_loop:
+    unless iter goto done
+
+  invoke:
+    elem = shift iter
+    elemres = elem.'fmt'(format)
+    push res, elemres
+    goto elem_loop
+  
+  done:
+    retv = 'join'(sep, res)
+    .return(retv)
+.end
+
+
 .sub 'keys' :method :multi('Hash')
     .local pmc iter
     .local pmc rv
Index: src/classes/Pair.pir
===================================================================
--- src/classes/Pair.pir	(revision 32421)
+++ src/classes/Pair.pir	(working copy)
@@ -75,6 +75,29 @@ pair .fmt
 .end
 
 
+=item fmt
+
+ our Str multi Pair::fmt ( Str $format )
+
+Returns the invocant pair formatted by an implicit call to sprintf on
+the key and value.
+
+=cut
+
+.sub 'fmt' :method
+    .param pmc format
+
+    .local pmc retv
+    .local pmc key
+    .local pmc value
+
+    key = self.'key'()
+    value = self.'value'()
+    retv = 'sprintf'(format, key, value)
+
+    .return(retv)
+.end
+
 =item perl
 
 Returns a Perl code representation of the pair.
Index: src/classes/List.pir
===================================================================
--- src/classes/List.pir	(revision 32421)
+++ src/classes/List.pir	(working copy)
@@ -270,7 +270,45 @@ list .fmt
     .return values.'first'(test)
 .end
 
+=item fmt
 
+ our Str multi List::fmt ( Str $format, $separator = ' ' )
+
+Returns the invocant list formatted by an implicit call to sprintf on each
+of the elements, then joined with spaces or an explicitly given separator.
+    
+=cut
+
+.sub 'fmt' :method :multi('ResizablePMCArray')
+    .param pmc format
+    .param string sep          :optional
+    .param int has_sep         :opt_flag
+
+    .local pmc res
+    .local pmc iter
+    .local pmc retv
+    .local pmc elem
+    .local pmc elemres
+
+    if has_sep goto have_sep
+    sep = ' '
+  have_sep:
+    res = new 'List'
+    iter = self.'iterator'()
+  elem_loop:
+    unless iter goto done
+
+  invoke:
+    elem = shift iter
+    elemres = 'sprintf'(format, elem)
+    push res, elemres
+    goto elem_loop
+
+  done:
+    retv = 'join'(sep, res)
+    .return(retv)
+.end
+
 =item grep(...)
 
 =cut

@p6rt
Copy link
Author

p6rt commented Nov 7, 2008

From @masak

Removed a few trailing spaces, and rewrote the comment in Mapping​::fmt
to reflect more accurately what it does. New patch attached.

@p6rt
Copy link
Author

p6rt commented Nov 7, 2008

From @masak

fmt-small-fixes.patch
Index: src/builtins/any-str.pir
===================================================================
--- src/builtins/any-str.pir	(revision 32421)
+++ src/builtins/any-str.pir	(working copy)
@@ -191,6 +191,23 @@
     .return ($P0)
 .end
 
+=item fmt
+
+ our Str multi Any::fmt ( Str $format )
+
+Returns the invocant formatted by an implicit call to C<sprintf>.
+
+=cut
+
+.sub 'fmt' :method :multi(_)
+    .param string format
+    .local pmc retv
+
+    retv = 'sprintf'(format, self)
+
+    .return(retv)
+.end
+
 =item index()
 
 =cut
Index: src/classes/Mapping.pir
===================================================================
--- src/classes/Mapping.pir	(revision 32421)
+++ src/classes/Mapping.pir	(working copy)
@@ -137,6 +137,50 @@
 .end
 
 
+=item fmt
+
+ our Str multi Mapping::fmt ( Str $format, $separator = "\n" )
+
+Returns the invocant mapping formatted by an implicit call to C<.fmt> on
+every pair, joined by newlines or an explicitly given separator.
+
+=cut
+
+.sub 'fmt' :method :multi('Hash')
+    .param pmc format
+    .param string sep          :optional
+    .param int has_sep         :opt_flag
+
+    .local pmc pairs
+    .local pmc res
+    .local pmc iter
+    .local pmc retv
+    .local pmc elem
+    .local pmc key
+    .local pmc value
+    .local pmc elemres
+
+    if has_sep goto have_sep
+    sep = "\n"
+  have_sep:
+    pairs = self.'pairs'()
+    res = new 'List'
+    iter = pairs.'iterator'()
+  elem_loop:
+    unless iter goto done
+
+  invoke:
+    elem = shift iter
+    elemres = elem.'fmt'(format)
+    push res, elemres
+    goto elem_loop
+
+  done:
+    retv = 'join'(sep, res)
+    .return(retv)
+.end
+
+
 .sub 'keys' :method :multi('Hash')
     .local pmc iter
     .local pmc rv
Index: src/classes/Pair.pir
===================================================================
--- src/classes/Pair.pir	(revision 32421)
+++ src/classes/Pair.pir	(working copy)
@@ -75,6 +75,29 @@
 .end
 
 
+=item fmt
+
+ our Str multi Pair::fmt ( Str $format )
+
+Returns the invocant pair formatted by an implicit call to C<sprintf> on
+the key and value.
+
+=cut
+
+.sub 'fmt' :method
+    .param pmc format
+
+    .local pmc retv
+    .local pmc key
+    .local pmc value
+
+    key = self.'key'()
+    value = self.'value'()
+    retv = 'sprintf'(format, key, value)
+
+    .return(retv)
+.end
+
 =item perl
 
 Returns a Perl code representation of the pair.
Index: src/classes/List.pir
===================================================================
--- src/classes/List.pir	(revision 32421)
+++ src/classes/List.pir	(working copy)
@@ -270,7 +270,45 @@
     .return values.'first'(test)
 .end
 
+=item fmt
 
+ our Str multi List::fmt ( Str $format, $separator = ' ' )
+
+Returns the invocant list formatted by an implicit call to C<sprintf> on each
+of the elements, then joined with spaces or an explicitly given separator.
+
+=cut
+
+.sub 'fmt' :method :multi('ResizablePMCArray')
+    .param pmc format
+    .param string sep          :optional
+    .param int has_sep         :opt_flag
+
+    .local pmc res
+    .local pmc iter
+    .local pmc retv
+    .local pmc elem
+    .local pmc elemres
+
+    if has_sep goto have_sep
+    sep = ' '
+  have_sep:
+    res = new 'List'
+    iter = self.'iterator'()
+  elem_loop:
+    unless iter goto done
+
+  invoke:
+    elem = shift iter
+    elemres = 'sprintf'(format, elem)
+    push res, elemres
+    goto elem_loop
+
+  done:
+    retv = 'join'(sep, res)
+    .return(retv)
+.end
+
 =item grep(...)
 
 =cut

@p6rt
Copy link
Author

p6rt commented Nov 10, 2008

From @pmichaud

Applied in r32478, thanks!

Pm

@p6rt
Copy link
Author

p6rt commented Nov 10, 2008

@pmichaud - Status changed from 'new' to 'resolved'

@p6rt p6rt closed this as completed Nov 10, 2008
@p6rt p6rt added the Todo label Jan 5, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant