unofficial mirror of guile-devel@gnu.org 
 help / color / mirror / Atom feed
* [PATCH] object->string should return obj immediately while obj is a string
@ 2012-07-03 12:41 Nala Ginrut
  2012-07-03 14:05 ` Krister Svanlund
  0 siblings, 1 reply; 4+ messages in thread
From: Nala Ginrut @ 2012-07-03 12:41 UTC (permalink / raw)
  To: guile-devel

[-- Attachment #1: Type: text/plain, Size: 427 bytes --]

hi guys!
I realized that our object->string will do some unnecessary works for
a string object.
--------------code--------------
 (object->string "\n")
==> "\\n"
---------------end--------------

It's illogical! I get several errors when I'm reading xml files. It
generates too many "\\" that I must delete them with redundant
operations.
I believe we should return the object directly when it's a string.

And here's a patch.

[-- Attachment #2: 0001-string-should-return-immediatly-in-object-to-string.patch --]
[-- Type: application/octet-stream, Size: 1221 bytes --]

From d805cd93afe2ccea41a289542160cdd52e48a2b1 Mon Sep 17 00:00:00 2001
From: Nala Ginrut <nalaginrut@gmail.com>
Date: Tue, 3 Jul 2012 20:29:25 +0800
Subject: [PATCH] string should return immediatly in object to string

It's illogical to convert "\n" to "\\n" with object->string.
---
 libguile/strports.c      |    3 +++
 module/ice-9/channel.scm |    2 +-
 2 files changed, 4 insertions(+), 1 deletions(-)

diff --git a/libguile/strports.c b/libguile/strports.c
index 14cc93f..6042405 100644
--- a/libguile/strports.c
+++ b/libguile/strports.c
@@ -395,6 +395,9 @@ SCM_DEFINE (scm_object_to_string, "object->string", 1, 1, 0,
   if (!SCM_UNBNDP (printer))
     SCM_VALIDATE_PROC (2, printer);
 
+  if (scm_is_string (obj))
+    return obj;
+
   port = scm_mkstrport (SCM_INUM0, SCM_BOOL_F,
 			SCM_OPN | SCM_WRTNG, FUNC_NAME);
 
diff --git a/module/ice-9/channel.scm b/module/ice-9/channel.scm
index 9c237f5..a03fe3c 100644
--- a/module/ice-9/channel.scm
+++ b/module/ice-9/channel.scm
@@ -165,6 +165,6 @@
 (define object->string
   (if (defined? 'object->string)
     object->string
-    (lambda (x) (format #f "~S" x))))
+    (lambda (x) (if (string? x) x (format #f "~S" x)))))
 
 ;;; channel.scm ends here
-- 
1.7.7


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH] object->string should return obj immediately while obj is a string
  2012-07-03 12:41 [PATCH] object->string should return obj immediately while obj is a string Nala Ginrut
@ 2012-07-03 14:05 ` Krister Svanlund
  2012-07-03 19:34   ` dsmich
  0 siblings, 1 reply; 4+ messages in thread
From: Krister Svanlund @ 2012-07-03 14:05 UTC (permalink / raw)
  To: Nala Ginrut; +Cc: guile-devel

[-- Attachment #1: Type: text/plain, Size: 706 bytes --]

Shouldn't the function return a string representing the object.rather than
the object itself, even for a string. It should be up to your application
to handle if it's a string imho.
On Jul 3, 2012 2:42 PM, "Nala Ginrut" <nalaginrut@gmail.com> wrote:

> hi guys!
> I realized that our object->string will do some unnecessary works for
> a string object.
> --------------code--------------
>  (object->string "\n")
> ==> "\\n"
> ---------------end--------------
>
> It's illogical! I get several errors when I'm reading xml files. It
> generates too many "\\" that I must delete them with redundant
> operations.
> I believe we should return the object directly when it's a string.
>
> And here's a patch.
>

[-- Attachment #2: Type: text/html, Size: 1049 bytes --]

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] object->string should return obj immediately while obj is a string
  2012-07-03 14:05 ` Krister Svanlund
@ 2012-07-03 19:34   ` dsmich
  2012-07-04  1:58     ` Nala Ginrut
  0 siblings, 1 reply; 4+ messages in thread
From: dsmich @ 2012-07-03 19:34 UTC (permalink / raw)
  To: Nala Ginrut, Krister Svanlund; +Cc: guile-devel


---- Krister Svanlund <adsummodo@gmail.com> wrote: 
> Shouldn't the function return a string representing the object.rather than
> the object itself, even for a string. It should be up to your application
> to handle if it's a string imho.
> On Jul 3, 2012 2:42 PM, "Nala Ginrut" <nalaginrut@gmail.com> wrote:
> 
> > hi guys!
> > I realized that our object->string will do some unnecessary works for
> > a string object.
> > --------------code--------------
> >  (object->string "\n")
> > ==> "\\n"
> > ---------------end--------------
> >
> > It's illogical! I get several errors when I'm reading xml files. It
> > generates too many "\\" that I must delete them with redundant
> > operations.
> > I believe we should return the object directly when it's a string.

Object->string takes an optional "printer" argument.  The default is write,
but you can also use display.

scheme@(guile-user)> (help object->string)
`object->string' is a procedure in the (guile) module.

 -- Scheme Procedure: object->string obj [printer]
     Return a Scheme string obtained by printing OBJ.  Printing
     function can be specified by the optional second argument PRINTER
     (default: `write').

 
scheme@(guile-user)> (object->string "\n")
$1 = "\"\\n\""
scheme@(guile-user)> (object->string "\n" display)
$2 = "\n"

-Dale



^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] object->string should return obj immediately while obj is a string
  2012-07-03 19:34   ` dsmich
@ 2012-07-04  1:58     ` Nala Ginrut
  0 siblings, 0 replies; 4+ messages in thread
From: Nala Ginrut @ 2012-07-04  1:58 UTC (permalink / raw)
  To: dsmich; +Cc: guile-devel, Krister Svanlund

@Krister
I thought it should be a generic requirement. And dsmich's answer
proved it. It should be the duty of this API but the user choose the
output policy.

@dsmich
I confess I didn't read the manual carefully. Thanks for reminding.

On Wed, Jul 4, 2012 at 3:34 AM,  <dsmich@roadrunner.com> wrote:
>
> ---- Krister Svanlund <adsummodo@gmail.com> wrote:
>> Shouldn't the function return a string representing the object.rather than
>> the object itself, even for a string. It should be up to your application
>> to handle if it's a string imho.
>> On Jul 3, 2012 2:42 PM, "Nala Ginrut" <nalaginrut@gmail.com> wrote:
>>
>> > hi guys!
>> > I realized that our object->string will do some unnecessary works for
>> > a string object.
>> > --------------code--------------
>> >  (object->string "\n")
>> > ==> "\\n"
>> > ---------------end--------------
>> >
>> > It's illogical! I get several errors when I'm reading xml files. It
>> > generates too many "\\" that I must delete them with redundant
>> > operations.
>> > I believe we should return the object directly when it's a string.
>
> Object->string takes an optional "printer" argument.  The default is write,
> but you can also use display.
>
> scheme@(guile-user)> (help object->string)
> `object->string' is a procedure in the (guile) module.
>
>  -- Scheme Procedure: object->string obj [printer]
>      Return a Scheme string obtained by printing OBJ.  Printing
>      function can be specified by the optional second argument PRINTER
>      (default: `write').
>
>
> scheme@(guile-user)> (object->string "\n")
> $1 = "\"\\n\""
> scheme@(guile-user)> (object->string "\n" display)
> $2 = "\n"
>
> -Dale



^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2012-07-04  1:58 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2012-07-03 12:41 [PATCH] object->string should return obj immediately while obj is a string Nala Ginrut
2012-07-03 14:05 ` Krister Svanlund
2012-07-03 19:34   ` dsmich
2012-07-04  1:58     ` Nala Ginrut

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).